1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.controller.social;
20
21 import org.exoplatform.R;
22 import org.exoplatform.model.SocialSpaceInfo;
23 import org.exoplatform.singleton.SocialDetailHelper;
24 import org.exoplatform.singleton.SocialServiceHelper;
25 import org.exoplatform.social.client.api.SocialClientLibException;
26 import org.exoplatform.social.client.api.model.RestActivity;
27 import org.exoplatform.social.client.api.model.RestComment;
28 import org.exoplatform.social.client.api.service.ActivityService;
29 import org.exoplatform.ui.social.AllUpdatesFragment;
30 import org.exoplatform.ui.social.ComposeMessageActivity;
31 import org.exoplatform.ui.social.MyConnectionsFragment;
32 import org.exoplatform.ui.social.MySpacesFragment;
33 import org.exoplatform.ui.social.MyStatusFragment;
34 import org.exoplatform.ui.social.SocialDetailActivity;
35 import org.exoplatform.ui.social.SocialTabsActivity;
36 import org.exoplatform.utils.ExoConnectionUtils;
37 import org.exoplatform.utils.ExoConstants;
38 import org.exoplatform.utils.Log;
39 import org.exoplatform.utils.PhotoUtils;
40 import org.exoplatform.widget.ConnectionErrorDialog;
41 import org.exoplatform.widget.PostWaitingDialog;
42 import org.exoplatform.widget.WarningDialog;
43
44 import android.app.Activity;
45 import android.content.res.Resources;
46 import android.os.AsyncTask;
47 import android.view.Gravity;
48 import android.widget.Toast;
49
50 public class ComposeMessageController {
51
52 private PostWaitingDialog _progressDialog;
53
54 private int composeType;
55
56 private ComposeMessageActivity mComposeActivity;
57
58 private PostStatusTask mPostTask;
59
60 private CommentTask mCommetnTask;
61
62 private String sdcard_temp_dir = null;
63
64 private String inputTextWarning;
65
66 private String okString;
67
68 private String titleString;
69
70 private String contentString;
71
72
73
74
75 private SocialSpaceInfo postDestination;
76
77 public ComposeMessageController(ComposeMessageActivity activity, int type, PostWaitingDialog dialog) {
78 mComposeActivity = activity;
79 composeType = type;
80 postDestination = null;
81 _progressDialog = dialog;
82 changeLanguage();
83
84 }
85
86
87
88
89
90
91 public void setPostDestination(SocialSpaceInfo destination) {
92 this.postDestination = destination;
93 }
94
95
96
97
98
99
100 public SocialSpaceInfo getPostDestination() {
101 return this.postDestination;
102 }
103
104
105
106
107 public void initCamera() {
108 sdcard_temp_dir = PhotoUtils.startImageCapture(mComposeActivity);
109 }
110
111 public void onSendMessage(String composeMessage, String sdcard, int position) {
112 if ((composeMessage != null) && (composeMessage.length() > 0)) {
113 if (ExoConnectionUtils.isNetworkAvailableExt(mComposeActivity)) {
114 if (composeType == ExoConstants.COMPOSE_POST_TYPE) {
115 onPostTask(composeMessage, sdcard);
116 } else {
117 onCommentTask(composeMessage, position);
118 }
119 } else {
120 new ConnectionErrorDialog(mComposeActivity).show();
121 }
122 } else {
123 Toast toast = Toast.makeText(mComposeActivity, inputTextWarning, Toast.LENGTH_LONG);
124 toast.setGravity(Gravity.BOTTOM, 0, 0);
125 toast.show();
126 }
127 }
128
129 private void onPostTask(String composeMessage, String sdcard) {
130 if (mPostTask == null || mPostTask.getStatus() == PostStatusTask.Status.FINISHED) {
131 mPostTask = (PostStatusTask) new PostStatusTask(mComposeActivity, sdcard, composeMessage, this, _progressDialog).execute();
132 }
133 }
134
135 public void onCancelPostTask() {
136 if (mPostTask != null && mPostTask.getStatus() == PostStatusTask.Status.RUNNING) {
137 mPostTask.cancel(true);
138 mPostTask = null;
139 }
140 }
141
142 private void onCommentTask(String composeMessage, int position) {
143 if (mCommetnTask == null || mCommetnTask.getStatus() == CommentTask.Status.FINISHED) {
144 mCommetnTask = (CommentTask) new CommentTask(position).execute(composeMessage);
145 }
146 }
147
148 public String getSdCardTempDir() {
149 return sdcard_temp_dir;
150 }
151
152 private void changeLanguage() {
153 Resources resource = mComposeActivity.getResources();
154 inputTextWarning = resource.getString(R.string.InputTextWarning);
155 okString = resource.getString(R.string.OK);
156 titleString = resource.getString(R.string.Warning);
157 contentString = resource.getString(R.string.ErrorOnComment);
158 }
159
160 private class CommentTask extends AsyncTask<String, Void, Boolean> {
161
162 private int currentPosition;
163
164 public CommentTask(int position) {
165 currentPosition = position;
166 }
167
168 @Override
169 protected Boolean doInBackground(String... params) {
170 try {
171 String composeMessage = params[0];
172 RestComment comment = new RestComment();
173 comment.setText(composeMessage);
174 String selectedId = SocialDetailHelper.getInstance().getActivityId();
175 ActivityService<RestActivity> activityService = SocialServiceHelper.getInstance().activityService;
176 RestActivity restActivity = (RestActivity) activityService.get(selectedId);
177
178 activityService.createComment(restActivity, comment);
179 return true;
180 } catch (SocialClientLibException e) {
181 if (Log.LOGD)
182 Log.d(getClass().getSimpleName(), e.getMessage(), Log.getStackTraceString(e));
183 return false;
184 }
185
186 }
187
188 @Override
189 protected void onPostExecute(Boolean result) {
190 if (result) {
191 ((Activity) mComposeActivity).finish();
192 if (SocialDetailActivity.socialDetailActivity != null) {
193 SocialDetailActivity.socialDetailActivity.onLoad();
194 }
195
196 if (SocialTabsActivity.instance != null) {
197 int tabId = SocialTabsActivity.instance.mPager.getCurrentItem();
198 switch (tabId) {
199 case SocialTabsActivity.ALL_UPDATES:
200 AllUpdatesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
201 break;
202 case SocialTabsActivity.MY_CONNECTIONS:
203 MyConnectionsFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
204 break;
205 case SocialTabsActivity.MY_SPACES:
206 MySpacesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
207 break;
208 case SocialTabsActivity.MY_STATUS:
209 MyStatusFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
210 break;
211 }
212 }
213 } else {
214 WarningDialog dialog = new WarningDialog(mComposeActivity, titleString, contentString, okString);
215 dialog.show();
216 }
217 }
218
219 }
220 }