~openerp-dev/openerp-mobile/openerp-mobile-10

« back to all changes in this revision

Viewing changes to src/com/openerp/addons/message/MailGroup.java

  • Committer: Dharmang Soni (OpenERP)
  • Date: 2014-04-03 07:28:06 UTC
  • Revision ID: dpr@tinyerp.com-20140403072806-qphqroz65ch61pqt
[REMOVE] removed social client module from base framework

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * OpenERP, Open Source Management Solution
3
 
 * Copyright (C) 2012-today OpenERP SA (<http://www.openerp.com>)
4
 
 * 
5
 
 * This program is free software: you can redistribute it and/or modify
6
 
 * it under the terms of the GNU Affero General Public License as
7
 
 * published by the Free Software Foundation, either version 3 of the
8
 
 * License, or (at your option) any later version
9
 
 * 
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU Affero General Public License for more details
14
 
 * 
15
 
 * You should have received a copy of the GNU Affero General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
17
 
 * 
18
 
 */
19
 
package com.openerp.addons.message;
20
 
 
21
 
import java.util.ArrayList;
22
 
import java.util.HashMap;
23
 
import java.util.List;
24
 
 
25
 
import openerp.OEArguments;
26
 
 
27
 
import org.json.JSONArray;
28
 
import org.json.JSONObject;
29
 
 
30
 
import android.content.Context;
31
 
import android.content.Intent;
32
 
import android.content.IntentFilter;
33
 
import android.graphics.Color;
34
 
import android.os.AsyncTask;
35
 
import android.os.Bundle;
36
 
import android.util.Log;
37
 
import android.view.LayoutInflater;
38
 
import android.view.View;
39
 
import android.view.View.OnClickListener;
40
 
import android.view.ViewGroup;
41
 
import android.widget.Button;
42
 
import android.widget.GridView;
43
 
import android.widget.ImageView;
44
 
import android.widget.TextView;
45
 
import android.widget.Toast;
46
 
 
47
 
import com.openerp.OETouchListener;
48
 
import com.openerp.OETouchListener.OnPullListener;
49
 
import com.openerp.R;
50
 
import com.openerp.addons.message.providers.groups.MailGroupProvider;
51
 
import com.openerp.base.mail.MailFollowers;
52
 
import com.openerp.orm.OEDataRow;
53
 
import com.openerp.orm.OEHelper;
54
 
import com.openerp.receivers.SyncFinishReceiver;
55
 
import com.openerp.support.AppScope;
56
 
import com.openerp.support.BaseFragment;
57
 
import com.openerp.support.OEUser;
58
 
import com.openerp.support.listview.OEListAdapter;
59
 
import com.openerp.util.Base64Helper;
60
 
import com.openerp.util.drawer.DrawerItem;
61
 
import com.openerp.util.drawer.DrawerListener;
62
 
 
63
 
public class MailGroup extends BaseFragment implements OnPullListener {
64
 
 
65
 
        public static final String TAG = "com.openerp.addons.message.MailGroup";
66
 
 
67
 
        /**
68
 
         * OETouchListener
69
 
         */
70
 
        private OETouchListener mTouchAttacher;
71
 
 
72
 
        View mView = null;
73
 
        Boolean hasSynced = false;
74
 
        GridView mGroupGridView = null;
75
 
        List<Object> mGroupGridListItems = new ArrayList<Object>();
76
 
        OEListAdapter mGroupGridViewAdapter = null;
77
 
 
78
 
        /**
79
 
         * Tag Colors
80
 
         */
81
 
        public static HashMap<String, Object> mMenuGroups = new HashMap<String, Object>();
82
 
        String mTagColors[] = new String[] { "#218559", "#192823", "#FF8800",
83
 
                        "#CC0000", "#59A2BE", "#808080", "#9933CC", "#0099CC", "#669900",
84
 
                        "#EBB035" };
85
 
        /**
86
 
         * Loaders
87
 
         */
88
 
        GroupsLoader mGroupsLoader = null;
89
 
        JoinUnfollowGroup mJoinUnFollowGroup = null;
90
 
 
91
 
        /**
92
 
         * Database Objects
93
 
         */
94
 
        MailFollowers mMailFollowerDB = null;
95
 
 
96
 
        @Override
97
 
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
98
 
                        Bundle savedInstanceState) {
99
 
                mView = inflater.inflate(R.layout.fragment_message_groups_list,
100
 
                                container, false);
101
 
                init();
102
 
                return mView;
103
 
        }
104
 
 
105
 
        private void init() {
106
 
                scope = new AppScope(getActivity());
107
 
                mMailFollowerDB = new MailFollowers(getActivity());
108
 
                initControls();
109
 
                mGroupsLoader = new GroupsLoader();
110
 
                mGroupsLoader.execute();
111
 
        }
112
 
 
113
 
        private void initControls() {
114
 
                mGroupGridView = (GridView) mView.findViewById(R.id.listGroups);
115
 
                mGroupGridViewAdapter = new OEListAdapter(getActivity(),
116
 
                                R.layout.fragment_message_groups_list_item, mGroupGridListItems) {
117
 
                        @Override
118
 
                        public View getView(int position, View convertView, ViewGroup parent) {
119
 
                                View mView = convertView;
120
 
                                if (mView == null)
121
 
                                        mView = getActivity().getLayoutInflater().inflate(
122
 
                                                        getResource(), parent, false);
123
 
                                generateView(position, mView);
124
 
                                return mView;
125
 
                        }
126
 
                };
127
 
                mGroupGridView.setAdapter(mGroupGridViewAdapter);
128
 
                mTouchAttacher = scope.main().getTouchAttacher();
129
 
                mTouchAttacher.setPullableView(mGroupGridView, this);
130
 
        }
131
 
 
132
 
        private void generateView(int position, View mView) {
133
 
                OEDataRow row = (OEDataRow) mGroupGridListItems.get(position);
134
 
                TextView txvName = (TextView) mView.findViewById(R.id.txvGroupName);
135
 
                TextView txvDesc = (TextView) mView
136
 
                                .findViewById(R.id.txvGroupDescription);
137
 
                ImageView imgGroupPic = (ImageView) mView
138
 
                                .findViewById(R.id.imgGroupPic);
139
 
                imgGroupPic.setImageBitmap(Base64Helper.getBitmapImage(getActivity(),
140
 
                                row.getString("image_medium")));
141
 
                txvName.setText(row.getString("name"));
142
 
                txvDesc.setText(row.getString("description"));
143
 
 
144
 
                final int group_id = row.getInt("id");
145
 
                final Button btnJoin = (Button) mView.findViewById(R.id.btnJoinGroup);
146
 
                final Button btnUnJoin = (Button) mView
147
 
                                .findViewById(R.id.btnUnJoinGroup);
148
 
                int total = mMailFollowerDB.count(
149
 
                                "res_model = ? AND res_id = ? AND partner_id = ?",
150
 
                                new String[] { db().getModelName(), group_id + "",
151
 
                                                OEUser.current(getActivity()).getPartner_id() + "" });
152
 
                btnUnJoin.setOnClickListener(new OnClickListener() {
153
 
 
154
 
                        @Override
155
 
                        public void onClick(View arg0) {
156
 
                                mJoinUnFollowGroup = new JoinUnfollowGroup(group_id, false);
157
 
                                mJoinUnFollowGroup.execute();
158
 
                                btnJoin.setVisibility(View.VISIBLE);
159
 
                                btnUnJoin.setVisibility(View.GONE);
160
 
                        }
161
 
                });
162
 
                btnJoin.setOnClickListener(new OnClickListener() {
163
 
 
164
 
                        @Override
165
 
                        public void onClick(View v) {
166
 
                                mJoinUnFollowGroup = new JoinUnfollowGroup(group_id, true);
167
 
                                mJoinUnFollowGroup.execute();
168
 
                                btnJoin.setVisibility(View.GONE);
169
 
                                btnUnJoin.setVisibility(View.VISIBLE);
170
 
                        }
171
 
                });
172
 
                if (total > 0) {
173
 
                        btnJoin.setVisibility(View.GONE);
174
 
                        btnUnJoin.setVisibility(View.VISIBLE);
175
 
 
176
 
                } else {
177
 
                        btnJoin.setVisibility(View.VISIBLE);
178
 
                        btnUnJoin.setVisibility(View.GONE);
179
 
                }
180
 
        }
181
 
 
182
 
        @Override
183
 
        public Object databaseHelper(Context context) {
184
 
                return new MailGroupDB(context);
185
 
        }
186
 
 
187
 
        @Override
188
 
        public List<DrawerItem> drawerMenus(Context context) {
189
 
 
190
 
                MailGroupDB db = new MailGroupDB(context);
191
 
                mMailFollowerDB = new MailFollowers(context);
192
 
 
193
 
                List<DrawerItem> menu = new ArrayList<DrawerItem>();
194
 
 
195
 
                MailGroup group = new MailGroup();
196
 
                Bundle bundle = new Bundle();
197
 
 
198
 
                if (db.isInstalledOnServer()) {
199
 
                        menu.add(new DrawerItem(TAG, "My Groups", true));
200
 
 
201
 
                        // Join Group
202
 
                        group.setArguments(bundle);
203
 
                        menu.add(new DrawerItem(TAG, "Join Group", 0,
204
 
                                        R.drawable.ic_action_social_group, group));
205
 
 
206
 
                        // Dynamic Groups
207
 
                        List<OEDataRow> groups = mMailFollowerDB.select(
208
 
                                        "res_model = ? AND partner_id = ?",
209
 
                                        new String[] { db.getModelName(),
210
 
                                                        OEUser.current(context).getPartner_id() + "" });
211
 
                        int index = 0;
212
 
                        MessageDB messageDB = new MessageDB(context);
213
 
                        for (OEDataRow row : groups) {
214
 
                                if (mTagColors.length - 1 < index)
215
 
                                        index = 0;
216
 
                                OEDataRow grp = db.select(row.getInt("res_id"));
217
 
 
218
 
                                Message message = new Message();
219
 
                                bundle = new Bundle();
220
 
                                bundle.putInt("group_id", grp.getInt("id"));
221
 
                                message.setArguments(bundle);
222
 
 
223
 
                                int count = messageDB.count(
224
 
                                                "to_read = ? AND model = ? AND res_id = ?",
225
 
                                                new String[] { "true", db().getModelName(),
226
 
                                                                row.getString("id") });
227
 
                                menu.add(new DrawerItem(TAG, grp.getString("name"), count,
228
 
                                                mTagColors[index], message));
229
 
                                grp.put("tag_color", Color.parseColor(mTagColors[index]));
230
 
                                mMenuGroups.put("group_" + grp.getInt("id"), grp);
231
 
                                index++;
232
 
                        }
233
 
                }
234
 
                return menu;
235
 
        }
236
 
 
237
 
        @Override
238
 
        public void onPullStarted(View arg0) {
239
 
                Log.d(TAG, "MailGroup->OETouchListener->onPullStarted()");
240
 
                scope.main().requestSync(MailGroupProvider.AUTHORITY);
241
 
 
242
 
        }
243
 
 
244
 
        class GroupsLoader extends AsyncTask<Void, Void, Void> {
245
 
 
246
 
                public GroupsLoader() {
247
 
                        mView.findViewById(R.id.loadingProgress)
248
 
                                        .setVisibility(View.VISIBLE);
249
 
                }
250
 
 
251
 
                @Override
252
 
                protected Void doInBackground(Void... params) {
253
 
                        mGroupGridListItems.clear();
254
 
                        mGroupGridListItems.addAll(db().select());
255
 
                        try {
256
 
                                Thread.sleep(1000);
257
 
                        } catch (Exception e) {
258
 
                        }
259
 
                        return null;
260
 
                }
261
 
 
262
 
                @Override
263
 
                protected void onPostExecute(Void result) {
264
 
                        mView.findViewById(R.id.loadingProgress).setVisibility(View.GONE);
265
 
                        mGroupGridViewAdapter.notifiyDataChange(mGroupGridListItems);
266
 
                        mGroupsLoader = null;
267
 
                        checkStatus();
268
 
                }
269
 
        }
270
 
 
271
 
        private void checkStatus() {
272
 
                if (!db().isEmptyTable()) {
273
 
                        mView.findViewById(R.id.groupSyncWaiter).setVisibility(View.GONE);
274
 
                } else {
275
 
                        mView.findViewById(R.id.groupSyncWaiter)
276
 
                                        .setVisibility(View.VISIBLE);
277
 
                        TextView txvSyncDetail = (TextView) mView
278
 
                                        .findViewById(R.id.txvMessageHeaderSubtitle);
279
 
                        txvSyncDetail.setText("Your groups will appear shortly");
280
 
                        if (!hasSynced) {
281
 
                                hasSynced = true;
282
 
                                scope.main().requestSync(MailGroupProvider.AUTHORITY);
283
 
                        }
284
 
                }
285
 
        }
286
 
 
287
 
        @Override
288
 
        public void onResume() {
289
 
                super.onResume();
290
 
                getActivity().registerReceiver(syncFinishReceiver,
291
 
                                new IntentFilter(SyncFinishReceiver.SYNC_FINISH));
292
 
        }
293
 
 
294
 
        @Override
295
 
        public void onPause() {
296
 
                super.onPause();
297
 
                getActivity().unregisterReceiver(syncFinishReceiver);
298
 
        }
299
 
 
300
 
        private SyncFinishReceiver syncFinishReceiver = new SyncFinishReceiver() {
301
 
 
302
 
                @Override
303
 
                public void onReceive(Context context, Intent intent) {
304
 
                        mTouchAttacher.setPullComplete();
305
 
                        mGroupsLoader = new GroupsLoader();
306
 
                        mGroupsLoader.execute();
307
 
                        DrawerListener drawer = (DrawerListener) getActivity();
308
 
                        drawer.refreshDrawer(TAG);
309
 
                        drawer.refreshDrawer(Message.TAG);
310
 
                }
311
 
 
312
 
        };
313
 
 
314
 
        public class JoinUnfollowGroup extends AsyncTask<Void, Void, Boolean> {
315
 
                int mGroupId = 0;
316
 
                boolean mJoin = false;
317
 
                String mToast = "";
318
 
                JSONObject result = new JSONObject();
319
 
 
320
 
                public JoinUnfollowGroup(int group_id, boolean join) {
321
 
                        mGroupId = group_id;
322
 
                        mJoin = join;
323
 
                }
324
 
 
325
 
                @Override
326
 
                protected Boolean doInBackground(Void... params) {
327
 
                        try {
328
 
                                if (mMailFollowerDB == null)
329
 
                                        mMailFollowerDB = new MailFollowers(getActivity());
330
 
                                int partner_id = OEUser.current(getActivity()).getPartner_id();
331
 
                                OEHelper oe = db().getOEInstance();
332
 
                                if (oe == null) {
333
 
                                        mToast = "No Connection";
334
 
                                        return false;
335
 
                                }
336
 
 
337
 
                                OEArguments arguments = new OEArguments();
338
 
                                arguments.add(new JSONArray().put(mGroupId));
339
 
                                arguments.add(oe.openERP().updateContext(new JSONObject()));
340
 
                                if (mJoin) {
341
 
                                        oe.call_kw("action_follow", arguments, null);
342
 
                                        mToast = "Group joined";
343
 
                                        oe.syncWithServer(true);
344
 
                                } else {
345
 
                                        oe.call_kw("action_unfollow", arguments, null);
346
 
                                        mToast = "Unfollowed from group";
347
 
                                        mMailFollowerDB.delete(
348
 
                                                        "res_id = ? AND partner_id = ? AND res_model = ? ",
349
 
                                                        new String[] { mGroupId + "", partner_id + "",
350
 
                                                                        db().getModelName() });
351
 
 
352
 
                                }
353
 
                                return true;
354
 
                        } catch (Exception e) {
355
 
                                e.printStackTrace();
356
 
                        }
357
 
                        return false;
358
 
                }
359
 
 
360
 
                @Override
361
 
                protected void onPostExecute(Boolean result) {
362
 
                        Toast.makeText(getActivity(), mToast, Toast.LENGTH_LONG).show();
363
 
                        DrawerListener drawer = (DrawerListener) getActivity();
364
 
                        drawer.refreshDrawer(TAG);
365
 
                        drawer.refreshDrawer(Message.TAG);
366
 
                }
367
 
        }
368
 
}