Working on Status layout
[mustard2:mustard2.git] / src / org / mustard2 / android / activity / Status.java
1 package org.mustard2.android.activity;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5
6 import org.mustard2.android.Account;
7 import org.mustard2.android.MessagingController;
8 import org.mustard2.android.MessagingListener;
9 import org.mustard2.android.MustardApp;
10 import org.mustard2.android.R;
11 import org.mustard2.android.view.RemoteImageView;
12 import org.mustard2.db.AttachmentTable;
13 import org.mustard2.db.StatusTable;
14 import org.mustard2.geonames.GeoName;
15 import org.mustard2.manager.AccountManager;
16 import org.mustard2.manager.DbManager;
17 import org.mustard2.microblog.Attachment;
18 import org.mustard2.util.DateUtils;
19
20 import android.app.Activity;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.util.Log;
28 import android.view.Gravity;
29 import android.view.LayoutInflater;
30 import android.view.MotionEvent;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.View.OnClickListener;
34 import android.webkit.WebView;
35 import android.widget.Button;
36 import android.widget.ImageView;
37 import android.widget.PopupWindow;
38 import android.widget.TextView;
39 import android.widget.RelativeLayout.LayoutParams;
40
41 public class Status extends Activity {
42
43         private static final String EXTRA_ACCOUNT = "account";
44         private static final String EXTRA_STATUS_ID = "status_id";
45
46         private DbManager mDbHelper;
47         private StatusHandler mHandler = new StatusHandler();
48
49         private PopupWindow mPopupWindow;
50         
51         private MessagingController mMessagingController ;
52         private Account mAccount;
53
54         private long mRowId;
55
56         private ArrayList<String> mUsers = new ArrayList<String>();
57 //      private ArrayList<String> mGroups = new ArrayList<String>();
58 //      private ArrayList<String> mTags = new ArrayList<String>();
59 //      private ArrayList<String> mUrls = new ArrayList<String>();
60
61         @Override
62         public void onCreate(Bundle savedInstanceState) {
63                 super.onCreate(savedInstanceState);
64
65                 setContentView(R.layout.status);
66
67                 mDbHelper = new DbManager(this);
68                 mDbHelper.open();
69
70                 Bundle extras = getIntent().getExtras();
71
72                 if (extras.containsKey(EXTRA_ACCOUNT)) {
73                         long account_id = extras.getLong(EXTRA_ACCOUNT);
74                         AccountManager am = ((MustardApp) getApplication())
75                                         .getAccountManager();
76                         mAccount = am.getAccount(account_id);
77                 }
78
79                 if (extras.containsKey(EXTRA_STATUS_ID)) {
80                         mRowId = extras.getLong(EXTRA_STATUS_ID);
81                 } else {
82                         // TODO Throw an exception ???
83                 }
84
85                 Cursor cursor = mDbHelper.getStatus(mRowId);
86                 startManagingCursor(cursor);
87                 cursor.moveToNext();
88                 TextView vScreenName = (TextView) findViewById(R.id.status_screen_name);
89                 String screenName = cursor.getString(cursor
90                                 .getColumnIndexOrThrow(StatusTable.KEY_SCREEN_NAME));
91                 vScreenName.setText(screenName);
92                 TextView vAuthor = (TextView) findViewById(R.id.status_author);
93                 String author = cursor.getString(cursor
94                                 .getColumnIndexOrThrow(StatusTable.KEY_NAME));
95                 vAuthor.setText(author);
96
97                 RemoteImageView vAvatar = (RemoteImageView) findViewById(R.id.avatar);
98                 String profileUrl = cursor.getString(cursor
99                                 .getColumnIndexOrThrow(StatusTable.KEY_USER_IMAGE_BIG));
100                 if (profileUrl != null && !"".equals(profileUrl)) {
101                         vAvatar.setRemoteURI(profileUrl);
102                         vAvatar.loadImage();
103                 }
104
105                 TextView vContent = (TextView) findViewById(R.id.status_content);
106                 String content = cursor.getString(cursor
107                                 .getColumnIndexOrThrow(StatusTable.KEY_STATUS));
108                 doParseContent(content);
109                 vContent.setText(content);
110
111                 TextView vDatetime = (TextView) findViewById(R.id.status_date);
112                 Date d = new Date();
113                 d.setTime(cursor.getLong(cursor
114                                 .getColumnIndexOrThrow(StatusTable.KEY_INSERT_AT)));
115                 vDatetime.setText(DateUtils.getRelativeDate(d));
116
117                 TextView vSource = (TextView) findViewById(R.id.status_source);
118                 String source = cursor.getString(cursor
119                                 .getColumnIndexOrThrow(StatusTable.KEY_SOURCE));
120                 if (source != null && !"".equals(source)) {
121                         source = getString(R.string.using, source.trim());
122                 }
123                 vSource.setText(source);
124
125                 String lon = cursor.getString(cursor
126                                 .getColumnIndexOrThrow(StatusTable.KEY_LON));
127                 String lat = cursor.getString(cursor
128                                 .getColumnIndexOrThrow(StatusTable.KEY_LAT));
129                 if (lon != null && lat != null) {
130                         if (!lon.equals("0") && !lat.equals("0")) {
131                                 String geodesc=cursor.getString(cursor.getColumnIndexOrThrow(StatusTable.KEY_GEO_DESCRIPTION));
132                                 if(geodesc==null) {
133                                         if (MustardApp.DEBUG)
134                                                 Log.d(MustardApp.APPLICATION_NAME, "Lon: " + lon
135                                                                 + " | lat: " + lat);
136                                         getLocation(lon, lat);
137                                 } else {
138                                         showGeoDescription(geodesc);
139                                 }
140                         }
141                 }
142
143                 int attachment = cursor.getInt(cursor
144                                 .getColumnIndexOrThrow(StatusTable.KEY_ATTACHMENT));
145                 if (attachment == 1) {
146                         ArrayList<Attachment> al = getAttachments();
147                         for (Attachment attachment2 : al) {
148                                 if (attachment2.getMimeType().startsWith("image")) {
149                                         RemoteImageView vAttachment = (RemoteImageView) findViewById(R.id.attachmentimg);
150                                         vAttachment.setRemoteURI(attachment2.getUrl());
151                                         vAttachment.loadImage();
152                                 } else if (attachment2.getMimeType().startsWith("text/html")) {
153                                         WebView html = (WebView) findViewById(R.id.attachmenttxt);
154                                         html.loadUrl(attachment2.getUrl());
155                                         html.setVisibility(View.VISIBLE);
156                                 }
157                                 break;
158                         }
159                 }
160
161 //              Button bReply = (Button) findViewById(R.id.reply);
162 //
163 //              bReply.setOnClickListener(new View.OnClickListener() {
164 //                      public void onClick(View view) {
165 //                              doReply();
166 //                      }
167 //              });
168 //
169 //              Button bRepeat = (Button) findViewById(R.id.repeat);
170 //              bRepeat.setOnClickListener(new View.OnClickListener() {
171 //                      public void onClick(View view) {
172 //                              doRepeat();
173 //                      }
174 //              });
175 //
176 //              Button bForward = (Button) findViewById(R.id.forward);
177 //              bForward.setOnClickListener(new View.OnClickListener() {
178 //
179 //                      public void onClick(View view) {
180 //                              doForward();
181 //                      }
182 //              });
183 //              
184
185                 ImageView ivRepeat = (ImageView) findViewById(R.id.reply);
186                 ivRepeat.setOnClickListener(new View.OnClickListener() {
187                         public void onClick(View view) {
188                                 doReply();
189                         }
190                 });
191                 
192                 boolean favorite = cursor.getInt(cursor
193                                 .getColumnIndexOrThrow(StatusTable.KEY_FAVORITE)) == 1;
194                 ImageView imageView = (ImageView)findViewById(R.id.favor);
195                 imageView.setImageResource(favorite ? android.R.drawable.star_on : android.R.drawable.star_off);
196                 imageView.setOnClickListener(new OnClickListener() {
197                         public void onClick(View view) {
198                                 doFavor();
199                         }
200                 });
201                 
202                 ImageView actionView = (ImageView)findViewById(R.id.activity);
203                 actionView.setOnClickListener(new OnClickListener() {
204                         public void onClick(View view) {
205                                 doShowPopupActions();
206                         }
207                 });
208         }
209
210         private void doShowPopupActions() {
211                 if(mPopupWindow == null) {
212                         LayoutInflater inflater = getLayoutInflater();
213                         View mView= inflater.inflate(R.layout.status_action_popup,(ViewGroup)findViewById(R.id.lnparentpopup));
214                         mPopupWindow = new PopupWindow(mView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, false);
215                         mPopupWindow.setAnimationStyle(-1);
216                         mPopupWindow.setOutsideTouchable(true);
217                         mPopupWindow.setFocusable(false);
218                         mPopupWindow.setTouchable(true);
219                         mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
220                                 public boolean onTouch(View v, MotionEvent event) {
221                                         Log.i(MustardApp.APPLICATION_NAME, "setTouchInterceptor.onTouch");
222                                         mPopupWindow.dismiss();
223                                         return false;
224                                 }
225                         });
226                 }
227                 if(mPopupWindow.isShowing()) {
228                         mPopupWindow.dismiss();
229 //                      ((ImageView)findViewById(R.id.activity)).setImageResource(android.R.drawable.btn_plus);
230                 } else {
231                         mPopupWindow.showAsDropDown(findViewById(R.id.activity));
232 //                      ((ImageView)findViewById(R.id.activity)).setImageResource(android.R.drawable.btn_minus);
233                 }
234         }
235
236     private void doParseContent(String content) {
237
238         }
239
240         private void showFavorStatus() {
241                 Cursor cursor = mDbHelper.getStatus(mRowId);
242                 startManagingCursor(cursor);
243                 cursor.moveToNext();
244                 boolean favorite = cursor.getInt(cursor
245                                 .getColumnIndexOrThrow(StatusTable.KEY_FAVORITE)) == 1;
246                 ImageView imageView = (ImageView)findViewById(R.id.favor);
247                 imageView.setImageResource(favorite ? android.R.drawable.star_on : android.R.drawable.star_off);                
248         }
249         
250         private void doReply() {
251                 // TODO must check how many @-users are in text
252                 // and if > 1 ask reply / reply all
253                 Compose.actionReply(this, mRowId, mUsers.size() > 1);
254         }
255
256         private void doForward() {
257                 Compose.actionForward(this, mRowId);
258         }
259
260         private void doRepeat() {
261                 prepareController();
262                 new Thread() {
263                         public void run() {
264                                 mMessagingController.repeatStatus(mAccount, getApplicationContext(), mRowId, mListener);
265                         }
266                 }.start();
267         }
268
269         private void doFavor() {
270                 prepareController();
271                 new Thread() {
272                         public void run() {
273                                 mMessagingController.favoriteStatus(mAccount, getApplicationContext(), mRowId, mListener);
274                         }
275                 }.start();
276         }
277         
278         private void prepareController() {
279                 if(mMessagingController==null) {
280                         mMessagingController = MessagingController
281                         .getInstance(getApplication());
282                         mMessagingController.addListener(mListener);
283                 }
284         }
285         
286         private ArrayList<Attachment> getAttachments() {
287                 if (MustardApp.DEBUG)
288                         Log.d(MustardApp.APPLICATION_NAME, "Looking for attachments..");
289                 ArrayList<Attachment> al = new ArrayList<Attachment>();
290                 Cursor c = mDbHelper.fetchAttachment(mRowId);
291                 while (c.moveToNext()) {
292                         String mimeType = c.getString(c
293                                         .getColumnIndex(AttachmentTable.KEY_MIMETYPE));
294                         String url = c.getString(c.getColumnIndex(AttachmentTable.KEY_URL));
295                         Attachment a = new Attachment();
296                         a.setMimeType(mimeType);
297                         a.setUrl(url);
298                         al.add(a);
299                 }
300                 if (MustardApp.DEBUG)
301                         Log.d(MustardApp.APPLICATION_NAME, "No attachment..");
302                 c.close();
303                 return al;
304         }
305
306         @Override
307         public void onDestroy() {
308
309                 super.onDestroy();
310                 
311                 if (mPopupWindow != null) {
312                         mPopupWindow.dismiss();
313                 }
314                 if (MustardApp.DEBUG)
315                         Log.i(MustardApp.APPLICATION_NAME, "onDestroy()");
316
317                 if(mMessagingController!=null) {
318                         // Unregister listerner
319                         MessagingController.getInstance(getApplication()).removeListener(
320                                         mListener);
321                 }
322                 if (mDbHelper != null) {
323                         try {
324                                 mDbHelper.close();
325                         } catch (Exception e) {
326                                 if (MustardApp.DEBUG)
327                                         e.printStackTrace();
328                         } finally {
329
330                         }
331                 }
332         }
333
334         public static void actionHandleStatus(Context context, long status_id,
335                         long account_id) {
336                 Intent intent = new Intent(context, Status.class);
337                 if (account_id > -1)
338                         intent.putExtra(EXTRA_ACCOUNT, account_id);
339                 intent.putExtra(EXTRA_STATUS_ID, status_id);
340                 context.startActivity(intent);
341         }
342
343         private void getLocation(final String lon, final String lat) {
344                 prepareController();
345                 new Thread() {
346                         public void run() {
347                                 mMessagingController.checkGeolocation(getApplication(), lon, lat, mListener);
348                         }
349                 }.start();
350         }
351
352         private void showGeoDescription(String location) {
353                 TextView vLocation = (TextView) findViewById(R.id.status_location);
354                 vLocation.setText(location);
355                 vLocation.setVisibility(View.VISIBLE);
356         }
357
358         private MessagingListener mListener = new MessagingListener() {
359
360                 @Override
361                 public void checkGeolocationFinished(Context context, Account account,
362                                 GeoName geoname) {
363                         mHandler.dataGeoAvailable(geoname);
364                 }
365
366                 public void favoriteStatusCompleted() {
367                         mHandler.favorChanged();
368                 }
369
370         };
371
372         class StatusHandler extends Handler {
373
374                 private static final int MSG_GEO_AVAILABLE = 2;
375                 private static final int MSG_FAVOR_CHANGED = 3;
376
377                 private static final String K_GEO_DESC = "geoinfo";
378
379                 @Override
380                 public void handleMessage(Message msg) {
381                         switch (msg.what) {
382                         case MSG_GEO_AVAILABLE:
383                                 String loc = msg.getData().getString(K_GEO_DESC);
384                                 showGeoDescription(loc);
385                                 break;
386
387                         case MSG_FAVOR_CHANGED:
388                                 showFavorStatus();
389                                 break;
390                                 
391                         default:
392                                 break;
393                         }
394                 }
395
396                 public void dataGeoAvailable(GeoName gn) {
397                         Message msg = new Message();
398                         msg.what = MSG_GEO_AVAILABLE;
399                         Bundle data = new Bundle();
400                         String loc = getString(R.string.geo_from, gn.getName() + ","
401                                         + gn.getAdminName1() + "," + gn.getCountryName());
402                         
403                         StatusTable st = new StatusTable(mDbHelper.getDb(), getApplicationContext());
404                         boolean r = st.updateGeoDescription(loc, gn.getLat(), gn.getLng());
405                         Log.i(MustardApp.APPLICATION_NAME, "Updated location: " + r);
406                         data.putString(K_GEO_DESC, loc);
407                         msg.setData(data);
408                         sendMessage(msg);
409                 }
410
411                 public void favorChanged() {
412                         sendEmptyMessage(MSG_FAVOR_CHANGED);
413                 }
414                 
415         }
416
417 }