1 package pl
.blip
.divide
.bliper
;
3 import java
.util
.concurrent
.ExecutorService
;
4 import java
.util
.concurrent
.Executors
;
5 import java
.util
.concurrent
.Future
;
7 import android
.app
.Activity
;
8 import android
.app
.ProgressDialog
;
9 import android
.content
.DialogInterface
;
10 import android
.content
.Intent
;
11 import android
.content
.DialogInterface
.OnCancelListener
;
12 import android
.database
.Cursor
;
13 import android
.database
.DataSetObserver
;
14 import android
.graphics
.Typeface
;
15 import android
.net
.Uri
;
16 import android
.os
.Bundle
;
17 import android
.os
.Handler
;
18 import android
.text
.SpannableString
;
19 import android
.text
.style
.StyleSpan
;
20 import android
.view
.LayoutInflater
;
21 import android
.view
.View
;
22 import android
.view
.ViewGroup
;
23 import android
.widget
.EditText
;
24 import android
.widget
.ImageView
;
25 import android
.widget
.ListAdapter
;
26 import android
.widget
.ListView
;
27 import android
.widget
.TextView
;
28 import android
.widget
.Toast
;
30 public class Dashboard
extends Activity
implements OnCancelListener
{
31 private static final String
[] DASHBOARD_PROJECTION
= { "_id", "user/login", "user/avatar/url_50", "body" };
32 private final int DASHBOARD_ID
= 0, DASHBOARD_LOGIN
= 1, DASHBOARD_AVATAR
= 2, DASHBOARD_BODY
= 3;
33 public class DashboardAdapter
implements ListAdapter
{
34 private static final int UPDATE_TYPE
= 0;
37 public boolean areAllItemsEnabled() {
42 public boolean isEnabled(int position
) {
47 public int getCount() {
48 return updates
.getCount();
52 public Object
getItem(int position
) {
53 updates
.moveToPosition(position
);
54 return updates
.getLong(DASHBOARD_ID
);
58 public long getItemId(int position
) {
59 updates
.moveToPosition(position
);
60 return updates
.getLong(DASHBOARD_ID
);
64 public int getItemViewType(int position
) {
69 public View
getView(int position
, View convertView
, ViewGroup parent
) {
70 updates
.moveToPosition(position
);
72 View v
= convertView
== null ?
getLayoutInflater().inflate(R
.layout
.update_list_item
, null) : convertView
;
73 ImageView avatarView
= (ImageView
) v
.findViewById(R
.id
.avatar
);
74 TextView updateView
= (TextView
) v
.findViewById(R
.id
.update
);
76 avatarView
.setImageURI(Uri
.withAppendedPath(Blip
.CONTENT_URI
, updates
.getString(DASHBOARD_AVATAR
)));
78 final String sender
= updates
.getString(DASHBOARD_LOGIN
);
79 final SpannableString updateText
= new SpannableString(sender
+ ": " + updates
.getString(DASHBOARD_BODY
));
80 updateText
.setSpan(new StyleSpan(Typeface
.BOLD
), 0, sender
.length(), 0);
81 updateView
.setText(updateText
);
87 public int getViewTypeCount() {
92 public boolean hasStableIds() {
97 public boolean isEmpty() {
98 return updates
== null || updates
.getCount() == 0;
102 public void registerDataSetObserver(DataSetObserver observer
) {
103 // TODO Auto-generated method stub
108 public void unregisterDataSetObserver(DataSetObserver observer
) {
109 // TODO Auto-generated method stub
115 private static final String
[] MYSELF_PROJECTION
= { "avatar/url_50", "current_status/body" };
116 private static final int MYSELF_AVATAR
= 0, MYSELF_STATUS
= 1;
118 private Cursor myself
;
119 private ProgressDialog progressDialog
= null;
120 private ExecutorService executor
= Executors
.newSingleThreadExecutor();
121 private Handler handler
= new Handler();
122 private Future
<?
> operation
;
123 private boolean initialized
= false;
124 private String login
;
125 private Cursor updates
;
127 /** Called when the activity is first created. */
129 public void onCreate(Bundle icicle
) {
130 super.onCreate(icicle
);
132 // TODO: breaks on switching orientation
134 login
= Blip
.getUsername(this);
136 handler
.post(new Runnable() {
145 operation
= initialization();
148 private Future
<?
> initialization() {
149 showProgress(getString(R
.string
.connecting
));
150 return executor
.submit(new Runnable() {
155 } catch (Exception e
) {
156 // TODO something smarter, maybe.
158 handler
.post(new Runnable() {
169 protected void initialize() {
170 myself
= managedQuery(Uri
.withAppendedPath(Blip
.Users
.CONTENT_URI
, login
), MYSELF_PROJECTION
, null, null, null);
172 if (!myself
.moveToFirst()) {
173 handler
.post(new Runnable() {
182 updates
= managedQuery(Uri
.parse(Blip
.DASHBOARD_URI
.toString() + "?limit=1"),
183 DASHBOARD_PROJECTION
, null, null, null);
185 handler
.post(new Runnable() {
193 protected void couldntConnect() {
194 if (progressDialog
!= null)
195 progressDialog
.dismiss();
196 progressDialog
= null;
197 Toast
.makeText(this, getString(R
.string
.connection_unsuccessful
), Toast
.LENGTH_LONG
).show();
201 protected void showInterface() {
202 LayoutInflater inflater
= getLayoutInflater();
203 View view
= inflater
.inflate(R
.layout
.dashboard
, null);
204 setContentView(view
);
206 setTitle(R
.string
.dashboard
);
208 ImageView avatarView
= (ImageView
) view
.findViewById(R
.id
.avatar
);
209 EditText statusView
= (EditText
) view
.findViewById(R
.id
.status
);
210 ListView updatesView
= (ListView
) view
.findViewById(R
.id
.updates
);
212 avatarView
.setImageURI(Uri
.withAppendedPath(Blip
.CONTENT_URI
, myself
.getString(MYSELF_AVATAR
)));
213 statusView
.setText(myself
.getString(MYSELF_STATUS
));
215 updatesView
.setAdapter(new DashboardAdapter());
217 if (progressDialog
!= null)
218 progressDialog
.dismiss();
219 progressDialog
= null;
222 private void showProgress(String message
) {
223 progressDialog
= ProgressDialog
.show(this, getString(R
.string
.app_name
), message
, true, true, this);
226 private void gotoLoginScreen() {
227 Intent i
= new Intent(this, LoginActivity
.class);
233 public void onCancel(DialogInterface dialog
) {
234 if (progressDialog
!= null)
235 progressDialog
.dismiss();
236 progressDialog
= null;
238 if (operation
!= null)
239 operation
.cancel(true);