View Javadoc
1   /*
2    * Copyright (C) 2003-2015 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.shareextension;
20  
21  import org.exoplatform.R;
22  import org.exoplatform.model.ExoAccount;
23  import org.exoplatform.singleton.ServerSettingHelper;
24  import org.exoplatform.utils.Log;
25  
26  import android.content.Context;
27  import android.graphics.Bitmap;
28  import android.os.Bundle;
29  import android.support.v4.app.Fragment;
30  import android.text.Editable;
31  import android.text.TextWatcher;
32  import android.view.LayoutInflater;
33  import android.view.MotionEvent;
34  import android.view.View;
35  import android.view.ViewGroup;
36  import android.view.inputmethod.InputMethodManager;
37  import android.widget.EditText;
38  import android.widget.ImageView;
39  import android.widget.ScrollView;
40  import android.widget.TextView;
41  
42  /**
43   * Created by The eXo Platform SAS
44   * 
45   * @author Philippe Aristote paristote@exoplatform.com
46   * @since Jun 3, 2015
47   */
48  public class ComposeFragment extends Fragment {
49  
50    private static ComposeFragment instance;
51  
52    public static final String     COMPOSE_FRAGMENT = "compose_fragment";
53  
54    private EditText               etPostMessage;
55  
56    private TextView               tvAccount, tvSpace, tvMoreAttachments;
57  
58    private ImageView              imgThumb;
59    
60    private Bitmap                 bmThumb;
61    
62    private int                    nbAttachments;
63  
64    private ScrollView             scroller;
65  
66    private TextWatcher            postValidator;
67  
68    public ComposeFragment() {
69      postValidator = new TextWatcher() {
70        @Override
71        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
72          // Enables the post button if the composer text field contains a message
73          enableDisableMainButton();
74        }
75  
76        @Override
77        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
78        }
79  
80        @Override
81        public void afterTextChanged(Editable e) {
82          // Update the value of post message after edit
83          getShareActivity().setPostMessage(e.toString());
84        }
85      };
86    }
87  
88    public static ComposeFragment getFragment() {
89      if (instance == null) {
90        instance = new ComposeFragment();
91      }
92      return instance;
93    }
94  
95    private void init() {
96      etPostMessage.setText(getShareActivity().getPostInfo().postMessage);
97      // Show a > icon on the account selector if 2 or more accounts exist
98      boolean manyAccounts = ServerSettingHelper.getInstance().twoOrMoreAccountsExist(getActivity());
99      if (manyAccounts) {
100       tvAccount.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_chevron_right_grey, 0);
101     } else {
102       tvAccount.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
103     }
104   }
105 
106   public void setTouchListener() {
107     // Open the soft keyboard and give focus to the edit text field when the
108     // scroll view is tapped
109     scroller.setOnTouchListener(new View.OnTouchListener() {
110       @Override
111       public boolean onTouch(View v, MotionEvent event) {
112         if (event.getAction() == MotionEvent.ACTION_DOWN) {
113           etPostMessage.requestFocus();
114           InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
115           mgr.showSoftInput(etPostMessage, InputMethodManager.SHOW_IMPLICIT);
116         }
117         return v.performClick();
118       }
119     });
120   }
121 
122   public void setThumbnailImage(Bitmap bm) {
123 	bmThumb = bm;
124     if (bm != null) {
125       imgThumb.setImageBitmap(bm);
126     }
127   }
128   
129   public void setNumberOfAttachments(int nb) {
130 	nbAttachments = nb;
131 	if (nb > 1) {
132 	    tvMoreAttachments.setText("+ " + (nb-1));
133 	    tvMoreAttachments.setVisibility(View.VISIBLE);
134 	  }
135   }
136 
137   @Override
138   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
139     Log.d("TEST", "onCreateView Compose");
140     View layout = inflater.inflate(R.layout.share_extension_compose_fragment, container, false);
141     etPostMessage = (EditText) layout.findViewById(R.id.share_post_message);
142     etPostMessage.addTextChangedListener(postValidator);
143     tvAccount = (TextView) layout.findViewById(R.id.share_account);
144     tvAccount.setOnClickListener(new View.OnClickListener() {
145       @Override
146       public void onClick(View v) {
147         getShareActivity().onSelectAccount();
148       }
149     });
150     tvSpace = (TextView) layout.findViewById(R.id.share_space);
151     tvSpace.setOnClickListener(new View.OnClickListener() {
152       @Override
153       public void onClick(View v) {
154         getShareActivity().onSelectSpace();
155       }
156     });
157     tvMoreAttachments = (TextView) layout.findViewById(R.id.share_attachment_more);
158     imgThumb = (ImageView) layout.findViewById(R.id.share_attachment_thumbnail);
159     scroller = (ScrollView) layout.findViewById(R.id.share_scroll_wrapper);
160     init();
161     return layout;
162   }
163 
164   @Override
165   public void onResume() {
166     setTouchListener();
167     getShareActivity().toggleMainButtonType(ShareActivity.BUTTON_TYPE_SHARE);
168     if (!getShareActivity().isProgressVisible())
169       getShareActivity().toggleProgressVisible(false);
170     ExoAccount selectedAccount = getShareActivity().getPostInfo().ownerAccount;
171     if (selectedAccount != null)
172       tvAccount.setText(selectedAccount.accountName + " (" + selectedAccount.username + ")");
173     setThumbnailImage(bmThumb);
174     setNumberOfAttachments(nbAttachments);
175     super.onResume();
176   }
177 
178   /*
179    * GETTERS & SETTERS
180    */
181 
182   public EditText getEditText() {
183     return etPostMessage;
184   }
185 
186   public ShareActivity getShareActivity() {
187     if (getActivity() instanceof ShareActivity) {
188       return (ShareActivity) getActivity();
189     } else {
190       throw new UnsupportedOperationException("This fragment is only valid in the activity org.exoplatform.shareextension.ShareActivity");
191     }
192   }
193 
194   public String getPostMessage() {
195     return etPostMessage != null ? etPostMessage.getText().toString() : "";
196   }
197 
198   public void setSpaceSelectorLabel(String label) {
199     tvSpace.setText(label);
200   }
201 
202   private void enableDisableMainButton() {
203     if (isAdded()) {
204       boolean postEmpty = "".equals(etPostMessage.getText().toString());
205       getShareActivity().enableDisableMainButton(!postEmpty);
206     }
207   }
208 
209 }