My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package org.jtb.csdroid;

import java.io.IOException;
import java.util.List;

import org.jtb.csc.CSCLocation;
import org.jtb.csc.CSCManager;
import org.jtb.csdroid.donate.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TabHost;

public class TabWidgetActivity extends TabActivity {
// static final int SERVICE_START_DIALOG_SHOW_WHAT = 0;
private final int SERVICE_START_DIALOG_DISMISS_WHAT = 1;
public static final int REFRESH_ERROR_SHOW_WHAT = 2;
static final int REFRESH_ERROR_HIDE_WHAT = 3;
static final int FAVE_SHORTCUT_SHOW_WHAT = 4;

private static final int INFO_DIALOG = 0;
private static final int REFRESH_DIALOG = 1;
private static final int SERVICE_START_DIALOG = 2;
private static final int ADDRESS_DIALOG = 3;
private static final int LOCATION_ERROR_DIALOG = 4;
private static final int GEOCODE_ERROR_DIALOG = 5;
private static final int REFRESH_ERROR_DIALOG = 6;

private static final int INFO_MENU = 0;
private static final int ADDRESS_MENU = 1;
private static final int REFRESH_MENU = 2;
private static final int PREFS_MENU = 3;
private static final int MY_LOCATION_MENU = 4;

private static final int PREFS_REQUEST = 0;

static CSCLocation mLocation = new CSCLocation();

private TabHost mTabHost;
private TabWidgetActivity mThis;

private Dialog mInfoDialog = null;
private AlertDialog mRefreshDialog = null;
private ProgressDialog mServiceStartDialog;
private AlertDialog mAddressDialog;
private AlertDialog mGeocodeErrorDialog;
private AlertDialog mLocationErrorDialog;
private AlertDialog mRefreshErrorDialog;

private Prefs mPrefs;
private String mRefreshError = null;

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SERVICE_START_DIALOG_DISMISS_WHAT:
if (mServiceStartDialog.isShowing()) {
mServiceStartDialog.hide();
}
break;
case REFRESH_ERROR_SHOW_WHAT:
mRefreshError = (String) msg.obj;
showDialog(REFRESH_ERROR_DIALOG);
break;
case REFRESH_ERROR_HIDE_WHAT:
if (mRefreshErrorDialog.isShowing()) {
dismissDialog(REFRESH_ERROR_DIALOG);
}
break;
case FAVE_SHORTCUT_SHOW_WHAT:
Intent i = new Intent(TabWidgetActivity.this,
FavesShortcutActivity.class);
startActivity(i);
break;
}
}
};
public static Handler mStaticHandler = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);

mThis = this;
mStaticHandler = mHandler;
mPrefs = new Prefs(this);

mTabHost = getTabHost();

TabHost.TabSpec closestTab = mTabHost.newTabSpec("closest")
.setIndicator(
"Closest",
getResources().getDrawable(
android.R.drawable.ic_menu_compass));
TabHost.TabSpec searchTab = mTabHost.newTabSpec("search").setIndicator(
"Search",
getResources().getDrawable(android.R.drawable.ic_menu_search));
TabHost.TabSpec favesTab = mTabHost.newTabSpec("faves").setIndicator(
"Favorites",
getResources().getDrawable(android.R.drawable.ic_menu_edit));

closestTab.setContent(new Intent(this, ClosestActivity.class));
searchTab.setContent(new Intent(this, SearchActivity.class));
favesTab.setContent(new Intent(this, FavesActivity.class));

mTabHost.addTab(closestTab);
mTabHost.addTab(searchTab);
mTabHost.addTab(favesTab);

String currentTab = mPrefs.getCurrentTab();
mTabHost.setCurrentTabByTag(currentTab);

mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
mPrefs.setCurrentTab(mTabHost);
initActivity();
}
});

}

@Override
public void onResume() {
super.onResume();
startService();
}

private boolean setLocationByAddress(String addr) {
Geocoder gc = new Geocoder(this);
List<Address> addrs;
try {
addrs = gc.getFromLocationName(addr, 1);
if (addrs.size() == 0) {
Log.w(getClass().getSimpleName(), "could not geocode address: "
+ addr);
return false;
}
Address a = addrs.get(0);

TabWidgetActivity.mLocation.setLatitude(a.getLatitude());
TabWidgetActivity.mLocation.setLongitude(a.getLongitude());
return true;
} catch (IOException e) {
Log.w(getClass().getSimpleName(), "could not geocode address: "
+ addr, e);
return false;
}
}

private boolean setLocation() {
String address = mPrefs.getAddress();
if (address != null && address.length() > 0) {
if (setLocationByAddress(address)) {
return true;
}
}
if (!setLocationByProvider()) {
showDialog(LOCATION_ERROR_DIALOG);
return false;
}
return true;
}

private boolean setLocationByProvider() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location l = null;

for (String providerName : lm.getProviders(true)) {
l = lm.getLastKnownLocation(providerName);
if (l != null) {
TabWidgetActivity.mLocation.setLatitude(l.getLatitude());
TabWidgetActivity.mLocation.setLongitude(l.getLongitude());
return true;
}
}

Log.w(getClass().getSimpleName(), "no last location");
return false;
}

private void initActivity() {
String tabId = mPrefs.getCurrentTab();
setLocation();

if (tabId.equals("closest")) {
Handler h = ClosestActivity.mStaticHandler;
if (h != null) {
h.sendMessage(ClosestActivity.mStaticHandler
.obtainMessage(ClosestActivity.INIT_WHAT));
}
} else if (tabId.equals("search")) {
Handler h = SearchActivity.mStaticHandler;
if (h != null) {
h.sendMessage(SearchActivity.mStaticHandler
.obtainMessage(SearchActivity.INIT_WHAT));
}
} else if (tabId.equals("faves")) {
Handler h = FavesActivity.mStaticHandler;
if (h != null) {
h.sendMessage(FavesActivity.mStaticHandler
.obtainMessage(FavesActivity.INIT_WHAT));
}
}
}

private void updateActivity() {
String tabId = mPrefs.getCurrentTab();
setLocation();

if (tabId.equals("closest")) {
Handler h = ClosestActivity.mStaticHandler;
if (h != null) {
h.sendMessage(ClosestActivity.mStaticHandler
.obtainMessage(ClosestActivity.UPDATE_WHAT));
}
} else if (tabId.equals("search")) {
Handler h = SearchActivity.mStaticHandler;
if (h != null) {
h.sendMessage(SearchActivity.mStaticHandler
.obtainMessage(SearchActivity.UPDATE_WHAT));
}
} else if (tabId.equals("faves")) {
Handler h = FavesActivity.mStaticHandler;
if (h != null) {
h.sendMessage(FavesActivity.mStaticHandler
.obtainMessage(FavesActivity.UPDATE_WHAT));
}
}
}

private void resetActivities() {
Handler h = ClosestActivity.mStaticHandler;
if (h != null) {
h.sendMessage(ClosestActivity.mStaticHandler
.obtainMessage(ClosestActivity.RESET_WHAT));
}
h = SearchActivity.mStaticHandler;
if (h != null) {
h.sendMessage(SearchActivity.mStaticHandler
.obtainMessage(SearchActivity.RESET_WHAT));
}
h = FavesActivity.mStaticHandler;
if (h != null) {
h.sendMessage(FavesActivity.mStaticHandler
.obtainMessage(FavesActivity.RESET_WHAT));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, INFO_MENU, 0, R.string.info_menu).setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(0, MY_LOCATION_MENU, 1, R.string.my_location_menu).setIcon(
android.R.drawable.ic_menu_compass);
menu.add(0, ADDRESS_MENU, 2, R.string.address_menu).setIcon(
android.R.drawable.ic_menu_send);
menu.add(0, REFRESH_MENU, 3, R.string.refresh_menu).setIcon(
android.R.drawable.ic_menu_rotate);
menu.add(0, PREFS_MENU, 4, R.string.prefs_menu).setIcon(
android.R.drawable.ic_menu_preferences);
return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case INFO_MENU:
showDialog(INFO_DIALOG);
return true;
case REFRESH_MENU:
showDialog(REFRESH_DIALOG);
return true;
case ADDRESS_MENU:
showDialog(ADDRESS_DIALOG);
return true;
case MY_LOCATION_MENU:
if (setLocationByProvider()) {
mPrefs.setAddress("");
resetActivities();
updateActivity();
mTabHost.setCurrentTabByTag("closest");
} else {
showDialog(LOCATION_ERROR_DIALOG);
}
return true;
case PREFS_MENU:
Intent prefsActivity = new Intent(getBaseContext(),
PrefsActivity.class);
startActivityForResult(prefsActivity, PREFS_REQUEST);
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PREFS_REQUEST:
/*
* if (resultCode == PrefsActivity.CHANGED_RESULT) {
* resetActivities(); updateActivity(); }
*/
resetActivities();
updateActivity();

break;
}
}

public void startService() {
showDialog(SERVICE_START_DIALOG);
new Thread(new Runnable() {
public void run() {
Looper.prepare();
try {
CSCManager.getInstance(mThis);
} finally {
mHandler.sendMessage(Message.obtain(mHandler,
SERVICE_START_DIALOG_DISMISS_WHAT));
}

String action = getIntent().getAction();
String id = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
id = extras.getString("org.jtb.csdroid.site.id");
}

// request to create shortcut?
if (action != null
&& action
.equals("android.intent.action.CREATE_SHORTCUT")) {
mHandler.sendEmptyMessage(FAVE_SHORTCUT_SHOW_WHAT);
// request to open details?
} else if (id != null) {
Intent i = new Intent(TabWidgetActivity.this,
DetailActivity.class);
i.putExtra("org.jtb.csdroid.site.id", id);
startActivity(i);
finish();
// request to open application
} else {
resetActivities();
initActivity();
}
}
}).start();
}

protected Dialog onCreateDialog(int id) {
switch (id) {
case REFRESH_DIALOG: {
if (mRefreshDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Charts update automatically. Manual updates can add extra load to servers.\n\nAre you sure?");
builder.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dismissDialog(REFRESH_DIALOG);
CSCManager.getInstance(mThis).clearCache();
startService();
}
});
builder.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dismissDialog(REFRESH_DIALOG);
}
});
mRefreshDialog = builder.create();
}
return mRefreshDialog;
}
case INFO_DIALOG: {
AlertDialog.Builder builder = new InfoDialog.Builder(this);
mInfoDialog = builder.create();
return mInfoDialog;
}
case SERVICE_START_DIALOG: {
if (mServiceStartDialog == null) {
mServiceStartDialog = new ProgressDialog(this);
mServiceStartDialog
.setMessage("Preparing charts, please wait.");
mServiceStartDialog.setIndeterminate(true);
mServiceStartDialog.setCancelable(false);
}
return mServiceStartDialog;
}
case ADDRESS_DIALOG: {
LayoutInflater factory = LayoutInflater.from(this);
final View zipView = factory.inflate(R.layout.address_dialog, null);
final EditText zipEdit = (EditText) zipView
.findViewById(R.id.address);
mAddressDialog = new AlertDialog.Builder(this)
.setTitle("Enter Address / Zip Code")
.setView(zipView)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String address = zipEdit.getText()
.toString();
dismissDialog(ADDRESS_DIALOG);
if (setLocationByAddress(address)) {
mPrefs.setAddress(address);
resetActivities();
updateActivity();
mTabHost.setCurrentTabByTag("closest");
} else {
showDialog(GEOCODE_ERROR_DIALOG);
}
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dismissDialog(ADDRESS_DIALOG);
}
}).create();
return mAddressDialog;
}
case LOCATION_ERROR_DIALOG: {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Warning");
builder.setMessage("Could not determine your location. Enable network or GPS location services, or use Menu>Go to Address.");
builder.setNeutralButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismissDialog(LOCATION_ERROR_DIALOG);
}
});
mLocationErrorDialog = builder.create();
return mLocationErrorDialog;
}
case GEOCODE_ERROR_DIALOG: {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Could not find location for that address / zip.");
builder.setNeutralButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismissDialog(GEOCODE_ERROR_DIALOG);
}
});
mGeocodeErrorDialog = builder.create();
return mGeocodeErrorDialog;
}
case REFRESH_ERROR_DIALOG: {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error preparing charts");
builder.setMessage(mRefreshError);
builder.setNeutralButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismissDialog(REFRESH_ERROR_DIALOG);
}
});
builder.setIcon(android.R.drawable.ic_dialog_alert);
mRefreshErrorDialog = builder.create();
return mRefreshErrorDialog;
}
}
return null;
}

}

Change log

r37 by jeffrey.blattman on Apr 7, 2012   Diff
fix for older SDK (String.isEmpty())
Go to: 
Project members, sign in to write a code review

Older revisions

r36 by jeffrey.blattman on Apr 1, 2012   Diff
shortcut generation through dedicated
activity with dialog theme
r35 by jeffrey.blattman on Feb 14, 2011   Diff
testing with different user agents

r32 by jeffrey.blattman on Jan 23, 2011   Diff
favorite shortcuts
All revisions of this file

File info

Size: 14579 bytes, 491 lines
Powered by Google Project Hosting