-
Notifications
You must be signed in to change notification settings - Fork 1
/
TuanTrip.java
363 lines (302 loc) · 11 KB
/
TuanTrip.java
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
package com.tudaidai.tuantrip;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Location;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import android.util.Log;
import com.tudaidai.tuantrip.app.TuanTripApp;
import com.tudaidai.tuantrip.app.TuanTripHttpApi;
import com.tudaidai.tuantrip.location.BestLocationListener;
import com.tudaidai.tuantrip.location.NetworkLocationListener;
import com.tudaidai.tuantrip.types.City;
import com.tudaidai.tuantrip.types.Group;
import com.tudaidai.tuantrip.types.Product;
import com.tudaidai.tuantrip.types.ProductShort;
import com.tudaidai.tuantrip.types.User;
import com.tudaidai.tuantrip.util.BaseDiskCache;
import com.tudaidai.tuantrip.util.JavaLoggingHandler;
import com.tudaidai.tuantrip.util.MemDiskCache;
import com.tudaidai.tuantrip.util.NullDiskCache;
import com.tudaidai.tuantrip.util.RemoteResourceManager;
public class TuanTrip extends Application {
private static final String TAG = "TuanTrip";
private static final boolean DEBUG = TuanTripSettings.DEBUG;
private BestLocationListener mBestLocationListener = new BestLocationListener();
private BestLocationListener networkLocationListener = new NetworkLocationListener();
static {
Logger.getLogger("com.tudaidai.tuantrip").addHandler(
new JavaLoggingHandler());
Logger.getLogger("com.tudaidai.tuantrip").setLevel(Level.ALL);
}
public static final String PACKAGE_NAME = "com.tudaidai.tuantrip";
public static final String INTENT_ACTION_LOGGED_OUT = "com.tudaidai.tuantrip.intent.action.LOGGED_OUT";
public static final String INTENT_ACTION_LOGGED_IN = "com.tudaidai.tuantrip.intent.action.LOGGED_IN";
public static final String EXTRA_LOGIN_USER = "com.aibang.abtuan.EXTRA_LOGIN_USER";
private int mVersion;
private SharedPreferences mPrefs;
private RemoteResourceManager mRemoteResourceManager;
private RemoteResourceManager mFileRemoteResourceManager;
private TuanTripApp tuanTripApp;
private boolean mIsFirstRun;
private String city;
private Product product = new Product();
private User mUser;
private Group<City> mCities;
private Group<ProductShort> mProductList;
@Override
public void onCreate() {
Log.i(TAG, "Using Debug Server:\t" + TuanTripSettings.USE_DEBUG_SERVER);
Log.i(TAG, "Using Dumpcatcher:\t" + TuanTripSettings.USE_DUMPCATCHER);
Log.i(TAG, "Using Debug Log:\t" + DEBUG);
mVersion = getVersionString(this);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
new MediaCardStateBroadcastReceiver().register();
loadResourceManagers();
TuanTripHttpApi tripHttpApi = new TuanTripHttpApi("mi.tuantrip.com",
PACKAGE_NAME + ":" + mVersion);
tuanTripApp = new TuanTripApp(tripHttpApi);
super.onCreate();
}
public int getVersion() {
return mVersion;
}
private static int getVersionString(Context context) {
// Get a version string for the app.
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(PACKAGE_NAME, 0);
return pi.versionCode;
} catch (NameNotFoundException e) {
if (DEBUG)
Log.d(TAG, "Could not retrieve package info", e);
throw new RuntimeException(e);
}
}
private void loadResourceManagers() {
try {
if (DEBUG)
Log.d(TAG,
"Attempting to load RemoteResourceManager(new MemDiskCache())");
mRemoteResourceManager = new RemoteResourceManager(
new MemDiskCache());
} catch (Exception e) {
if (DEBUG)
Log.d(TAG,
"Falling back to NullDiskCache for RemoteResourceManager(new MemDiskCache())");
mRemoteResourceManager = new RemoteResourceManager(
new NullDiskCache());
}
try {
if (DEBUG)
Log.d(TAG, "Attempting to load RemoteResourceManager(cache)");
mFileRemoteResourceManager = new RemoteResourceManager(
new BaseDiskCache("tuantrip", "cache"));
} catch (Exception e) {
if (DEBUG)
Log.d(TAG,
"Falling back to NullDiskCache for RemoteResourceManager(cache)");
mFileRemoteResourceManager = new RemoteResourceManager(
new MemDiskCache());
}
}
public TuanTripApp getTuanTripApp() {
return tuanTripApp;
}
public Group<City> getCitys() {
return mCities;
}
public String getStoreCity() {
city = this.mPrefs.getString("city", "sh");
return city;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public RemoteResourceManager getRemoteResourceManager() {
return this.mRemoteResourceManager;
}
public RemoteResourceManager getFileRemoteResourceManager() {
return this.mFileRemoteResourceManager;
}
public String getPwd() {
return this.mPrefs.getString("pwd", "");
}
public User getUser() {
return this.mUser;
}
public void setUser(User user) {
this.mUser = user;
if (user != null) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putInt("uid", user.getUid());
localEditor1.putString("uname", user.getEmail());
localEditor1.putString("pwd", user.getPwd());
localEditor1.commit();
}
}
public void setUserOnly(User user) {
this.mUser = user;
}
public String getUserId() {
return this.mPrefs.getString("uid", "");
}
public String getUserName() {
return this.mPrefs.getString("uname", "");
}
public void setProductList(Group<ProductShort> mProductList) {
this.mProductList = mProductList;
}
public Group<ProductShort> getProductList() {
return this.mProductList;
}
public void loginOut() {
this.mUser = null;
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putString("uname", "");
localEditor1.putString("pwd", "");
localEditor1.commit();
}
public void storeLoginUser(String paramString) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putString(EXTRA_LOGIN_USER, paramString);
localEditor1.commit();
}
public String getLoginUser() {
return this.mPrefs.getString(EXTRA_LOGIN_USER, "");
}
public void setCities(Group<City> paramGroup) {
this.mCities = paramGroup;
}
public void setCity(String paramString) {
this.city = paramString;
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putString("city", paramString);
localEditor1.commit();
}
public void setShowPic(boolean isShowPic) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putBoolean("showPic", isShowPic);
localEditor1.commit();
}
public boolean getShowPic() {
return this.mPrefs.getBoolean("showPic", true);
}
public void setOpenGps(boolean isopen) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putBoolean("openGps", isopen);
localEditor1.commit();
}
public boolean getOpenGps() {
return this.mPrefs.getBoolean("openGps", true);
}
public void setCnCity(String paramString) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putString("cncity", paramString);
localEditor1.commit();
}
public String getStoreCnCity() {
return this.mPrefs.getString("cncity", "上海");
}
//
public void setPoi(String paramString) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putString("Poi", paramString);
localEditor1.commit();
}
public String getPoi() {
return this.mPrefs.getString("Poi", "0-0");
}
public void setTuanLocation(String paramString) {
SharedPreferences.Editor localEditor1 = this.mPrefs.edit();
localEditor1.putString("location", paramString);
localEditor1.commit();
}
public String getTuanLocation() {
return this.mPrefs.getString("location", null);
}
public BestLocationListener requestLocationUpdates(Observer observer) {
mBestLocationListener.addObserver(observer);
mBestLocationListener.register(
(LocationManager) getSystemService(LOCATION_SERVICE), true);
return mBestLocationListener;
}
public void removeLocationUpdates() {
mBestLocationListener
.unregister((LocationManager) getSystemService(LOCATION_SERVICE));
}
public boolean isProviderEnabled() {
return mBestLocationListener.isProviderEnabled();
}
public void removeLocationUpdates(Observer observer) {
mBestLocationListener.deleteObserver(observer);
this.removeLocationUpdates();
}
public Location getLastKnownLocation() {
return mBestLocationListener.getLastKnownLocation();
}
// /////////////////network////////
public BestLocationListener requestLocationUpdates1() {
networkLocationListener.register(
(LocationManager) getSystemService(LOCATION_SERVICE), true);
return networkLocationListener;
}
public void removeLocationUpdates1() {
networkLocationListener
.unregister((LocationManager) getSystemService(LOCATION_SERVICE));
}
public boolean isProviderEnabled1() {
return networkLocationListener.isProviderEnabled();
}
public Location getLastKnownLocation1() {
return networkLocationListener.getLastKnownLocation();
}
// /////////////////
private class MediaCardStateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG)
Log.d(TAG, "Media state changed, reloading resource managers:"
+ intent.getAction());
if (Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())) {
getFileRemoteResourceManager().shutdown();
mFileRemoteResourceManager = new RemoteResourceManager(
new MemDiskCache());
} else if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {
getFileRemoteResourceManager().shutdown();
mFileRemoteResourceManager = new RemoteResourceManager(
new BaseDiskCache("tuantrip", "cache"));
}
}
public void register() {
// Register our media card broadcast receiver so we can
// enable/disable the cache as
// appropriate.
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
// intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
// intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
// intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
// intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
// intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
// intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
// intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addDataScheme("file");
registerReceiver(this, intentFilter);
}
}
}