2
* OpenERP, Open Source Management Solution
3
* Copyright (C) 2012-today OpenERP SA (<http://www.openerp.com>)
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
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
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/>
19
package com.openerp.addons.message;
21
import java.util.ArrayList;
22
import java.util.HashMap;
23
import java.util.List;
25
import openerp.OEArguments;
27
import org.json.JSONArray;
28
import org.json.JSONObject;
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;
47
import com.openerp.OETouchListener;
48
import com.openerp.OETouchListener.OnPullListener;
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;
63
public class MailGroup extends BaseFragment implements OnPullListener {
65
public static final String TAG = "com.openerp.addons.message.MailGroup";
70
private OETouchListener mTouchAttacher;
73
Boolean hasSynced = false;
74
GridView mGroupGridView = null;
75
List<Object> mGroupGridListItems = new ArrayList<Object>();
76
OEListAdapter mGroupGridViewAdapter = null;
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",
88
GroupsLoader mGroupsLoader = null;
89
JoinUnfollowGroup mJoinUnFollowGroup = null;
94
MailFollowers mMailFollowerDB = null;
97
public View onCreateView(LayoutInflater inflater, ViewGroup container,
98
Bundle savedInstanceState) {
99
mView = inflater.inflate(R.layout.fragment_message_groups_list,
105
private void init() {
106
scope = new AppScope(getActivity());
107
mMailFollowerDB = new MailFollowers(getActivity());
109
mGroupsLoader = new GroupsLoader();
110
mGroupsLoader.execute();
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) {
118
public View getView(int position, View convertView, ViewGroup parent) {
119
View mView = convertView;
121
mView = getActivity().getLayoutInflater().inflate(
122
getResource(), parent, false);
123
generateView(position, mView);
127
mGroupGridView.setAdapter(mGroupGridViewAdapter);
128
mTouchAttacher = scope.main().getTouchAttacher();
129
mTouchAttacher.setPullableView(mGroupGridView, this);
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"));
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() {
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);
162
btnJoin.setOnClickListener(new OnClickListener() {
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);
173
btnJoin.setVisibility(View.GONE);
174
btnUnJoin.setVisibility(View.VISIBLE);
177
btnJoin.setVisibility(View.VISIBLE);
178
btnUnJoin.setVisibility(View.GONE);
183
public Object databaseHelper(Context context) {
184
return new MailGroupDB(context);
188
public List<DrawerItem> drawerMenus(Context context) {
190
MailGroupDB db = new MailGroupDB(context);
191
mMailFollowerDB = new MailFollowers(context);
193
List<DrawerItem> menu = new ArrayList<DrawerItem>();
195
MailGroup group = new MailGroup();
196
Bundle bundle = new Bundle();
198
if (db.isInstalledOnServer()) {
199
menu.add(new DrawerItem(TAG, "My Groups", true));
202
group.setArguments(bundle);
203
menu.add(new DrawerItem(TAG, "Join Group", 0,
204
R.drawable.ic_action_social_group, group));
207
List<OEDataRow> groups = mMailFollowerDB.select(
208
"res_model = ? AND partner_id = ?",
209
new String[] { db.getModelName(),
210
OEUser.current(context).getPartner_id() + "" });
212
MessageDB messageDB = new MessageDB(context);
213
for (OEDataRow row : groups) {
214
if (mTagColors.length - 1 < index)
216
OEDataRow grp = db.select(row.getInt("res_id"));
218
Message message = new Message();
219
bundle = new Bundle();
220
bundle.putInt("group_id", grp.getInt("id"));
221
message.setArguments(bundle);
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);
238
public void onPullStarted(View arg0) {
239
Log.d(TAG, "MailGroup->OETouchListener->onPullStarted()");
240
scope.main().requestSync(MailGroupProvider.AUTHORITY);
244
class GroupsLoader extends AsyncTask<Void, Void, Void> {
246
public GroupsLoader() {
247
mView.findViewById(R.id.loadingProgress)
248
.setVisibility(View.VISIBLE);
252
protected Void doInBackground(Void... params) {
253
mGroupGridListItems.clear();
254
mGroupGridListItems.addAll(db().select());
257
} catch (Exception e) {
263
protected void onPostExecute(Void result) {
264
mView.findViewById(R.id.loadingProgress).setVisibility(View.GONE);
265
mGroupGridViewAdapter.notifiyDataChange(mGroupGridListItems);
266
mGroupsLoader = null;
271
private void checkStatus() {
272
if (!db().isEmptyTable()) {
273
mView.findViewById(R.id.groupSyncWaiter).setVisibility(View.GONE);
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");
282
scope.main().requestSync(MailGroupProvider.AUTHORITY);
288
public void onResume() {
290
getActivity().registerReceiver(syncFinishReceiver,
291
new IntentFilter(SyncFinishReceiver.SYNC_FINISH));
295
public void onPause() {
297
getActivity().unregisterReceiver(syncFinishReceiver);
300
private SyncFinishReceiver syncFinishReceiver = new SyncFinishReceiver() {
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);
314
public class JoinUnfollowGroup extends AsyncTask<Void, Void, Boolean> {
316
boolean mJoin = false;
318
JSONObject result = new JSONObject();
320
public JoinUnfollowGroup(int group_id, boolean join) {
326
protected Boolean doInBackground(Void... params) {
328
if (mMailFollowerDB == null)
329
mMailFollowerDB = new MailFollowers(getActivity());
330
int partner_id = OEUser.current(getActivity()).getPartner_id();
331
OEHelper oe = db().getOEInstance();
333
mToast = "No Connection";
337
OEArguments arguments = new OEArguments();
338
arguments.add(new JSONArray().put(mGroupId));
339
arguments.add(oe.openERP().updateContext(new JSONObject()));
341
oe.call_kw("action_follow", arguments, null);
342
mToast = "Group joined";
343
oe.syncWithServer(true);
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() });
354
} catch (Exception e) {
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);