Show nick in bold face.
[bliper.git] / src / pl / blip / divide / bliper / Dashboard.java
blobd3ad88ea24d6fcbf5b794d165730236cbc756c0d
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;
36 @Override
37 public boolean areAllItemsEnabled() {
38 return true;
41 @Override
42 public boolean isEnabled(int position) {
43 return true;
46 @Override
47 public int getCount() {
48 return updates.getCount();
51 @Override
52 public Object getItem(int position) {
53 updates.moveToPosition(position);
54 return updates.getLong(DASHBOARD_ID);
57 @Override
58 public long getItemId(int position) {
59 updates.moveToPosition(position);
60 return updates.getLong(DASHBOARD_ID);
63 @Override
64 public int getItemViewType(int position) {
65 return UPDATE_TYPE;
68 @Override
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);
83 return v;
86 @Override
87 public int getViewTypeCount() {
88 return 1;
91 @Override
92 public boolean hasStableIds() {
93 return true;
96 @Override
97 public boolean isEmpty() {
98 return updates == null || updates.getCount() == 0;
101 @Override
102 public void registerDataSetObserver(DataSetObserver observer) {
103 // TODO Auto-generated method stub
107 @Override
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. */
128 @Override
129 public void onCreate(Bundle icicle) {
130 super.onCreate(icicle);
132 // TODO: breaks on switching orientation
134 login = Blip.getUsername(this);
135 if (login == null) {
136 handler.post(new Runnable() {
137 @Override
138 public void run() {
139 gotoLoginScreen();
142 return;
145 operation = initialization();
148 private Future<?> initialization() {
149 showProgress(getString(R.string.connecting));
150 return executor.submit(new Runnable() {
151 @Override
152 public void run() {
153 try {
154 initialize();
155 } catch (Exception e) {
156 // TODO something smarter, maybe.
157 e.printStackTrace();
158 handler.post(new Runnable() {
159 @Override
160 public void run() {
161 couldntConnect();
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() {
174 @Override
175 public void run() {
176 couldntConnect();
179 return;
182 updates = managedQuery(Uri.parse(Blip.DASHBOARD_URI.toString() + "?limit=1"),
183 DASHBOARD_PROJECTION, null, null, null);
184 initialized = true;
185 handler.post(new Runnable() {
186 @Override
187 public void run() {
188 showInterface();
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();
198 finish();
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);
228 startActivity(i);
229 finish();
232 @Override
233 public void onCancel(DialogInterface dialog) {
234 if (progressDialog != null)
235 progressDialog.dismiss();
236 progressDialog = null;
238 if (operation != null)
239 operation.cancel(true);
240 operation = null;
242 if (!initialized)
243 finish();