|
9 | 9 | import android.os.Bundle;
|
10 | 10 | import android.preference.PreferenceManager;
|
11 | 11 | import android.util.Log;
|
12 |
| -import ch.epfl.unison.api.PreferenceKeys; |
| 12 | + |
13 | 13 | import ch.epfl.unison.api.UnisonAPI;
|
14 | 14 |
|
15 |
| -public class AppData implements OnSharedPreferenceChangeListener { |
| 15 | +/** |
| 16 | + * Singleton object containing various utilities for the app. |
| 17 | + * |
| 18 | + * @author lum |
| 19 | + */ |
| 20 | +public final class AppData implements OnSharedPreferenceChangeListener { |
16 | 21 |
|
17 | 22 | private static final String TAG = "ch.epfl.unison.AppData";
|
18 | 23 | private static final int LOCATION_INTERVAL = 20 * 60 * 1000; // In ms.
|
19 | 24 |
|
20 |
| - private static AppData instance; |
| 25 | + private static AppData sInstance; |
21 | 26 |
|
22 |
| - private Context context; |
23 |
| - private UnisonAPI api; |
24 |
| - private SharedPreferences prefs; |
| 27 | + private Context mContext; |
| 28 | + private UnisonAPI mApi; |
| 29 | + private SharedPreferences mPrefs; |
25 | 30 |
|
26 |
| - private LocationManager locationMgr; |
27 |
| - private UnisonLocationListener gpsListener; |
28 |
| - private Location gpsLocation; |
29 |
| - private UnisonLocationListener networkListener; |
30 |
| - private Location networkLocation; |
| 31 | + private LocationManager mLocationMgr; |
| 32 | + private UnisonLocationListener mGpsListener; |
| 33 | + private Location mGpsLocation; |
| 34 | + private UnisonLocationListener mNetworkListener; |
| 35 | + private Location mNetworkLocation; |
31 | 36 |
|
32 | 37 | private AppData(Context context) {
|
33 |
| - this.context = context; |
34 |
| - this.prefs = PreferenceManager.getDefaultSharedPreferences(context); |
35 |
| - this.prefs.registerOnSharedPreferenceChangeListener(this); |
| 38 | + mContext = context; |
| 39 | + mPrefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 40 | + mPrefs.registerOnSharedPreferenceChangeListener(this); |
36 | 41 | }
|
37 | 42 |
|
38 | 43 | private AppData setupLocation() {
|
39 |
| - if (this.locationMgr == null) { |
40 |
| - this.locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); |
| 44 | + if (mLocationMgr == null) { |
| 45 | + mLocationMgr = (LocationManager) mContext.getSystemService( |
| 46 | + Context.LOCATION_SERVICE); |
41 | 47 | }
|
42 | 48 | // try to set up the network location listener.
|
43 |
| - if (this.networkListener == null |
44 |
| - && this.locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { |
45 |
| - this.networkListener = new UnisonLocationListener(LocationManager.NETWORK_PROVIDER); |
46 |
| - this.locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, |
47 |
| - LOCATION_INTERVAL, 1f, this.networkListener); |
48 |
| - this.networkLocation = this.locationMgr.getLastKnownLocation( |
| 49 | + if (mNetworkListener == null |
| 50 | + && mLocationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { |
| 51 | + mNetworkListener = new UnisonLocationListener(LocationManager.NETWORK_PROVIDER); |
| 52 | + mLocationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, |
| 53 | + LOCATION_INTERVAL, 1f, mNetworkListener); |
| 54 | + mNetworkLocation = mLocationMgr.getLastKnownLocation( |
49 | 55 | LocationManager.NETWORK_PROVIDER);
|
50 | 56 | }
|
51 | 57 | // try to set up the GPS location listener.
|
52 |
| - if (this.gpsListener == null |
53 |
| - && this.locationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER)) { |
54 |
| - this.gpsListener = new UnisonLocationListener(LocationManager.GPS_PROVIDER); |
55 |
| - this.locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, |
56 |
| - LOCATION_INTERVAL, 1f, this.gpsListener); |
57 |
| - this.gpsLocation = this.locationMgr.getLastKnownLocation( |
| 58 | + if (mGpsListener == null |
| 59 | + && mLocationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER)) { |
| 60 | + mGpsListener = new UnisonLocationListener(LocationManager.GPS_PROVIDER); |
| 61 | + mLocationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, |
| 62 | + LOCATION_INTERVAL, 1f, mGpsListener); |
| 63 | + mGpsLocation = mLocationMgr.getLastKnownLocation( |
58 | 64 | LocationManager.GPS_PROVIDER);
|
59 | 65 | }
|
60 | 66 | return this;
|
61 | 67 | }
|
62 | 68 |
|
63 | 69 | public UnisonAPI getAPI() {
|
64 |
| - if (this.api == null) { |
65 |
| - String email = this.prefs.getString(PreferenceKeys.EMAIL_KEY, null); |
66 |
| - String password = this.prefs.getString(PreferenceKeys.PASSWORD_KEY, null); |
| 70 | + if (mApi == null) { |
| 71 | + String email = mPrefs.getString(Const.PrefKeys.EMAIL, null); |
| 72 | + String password = mPrefs.getString(Const.PrefKeys.PASSWORD, null); |
67 | 73 | if (email != null && password != null) {
|
68 |
| - this.api = new UnisonAPI(email, password); |
| 74 | + mApi = new UnisonAPI(email, password); |
69 | 75 | } else {
|
70 |
| - this.api = new UnisonAPI(); |
| 76 | + mApi = new UnisonAPI(); |
71 | 77 | }
|
72 | 78 | }
|
73 |
| - return this.api; |
| 79 | + return mApi; |
74 | 80 | }
|
75 | 81 |
|
76 | 82 | public long getUid() {
|
77 |
| - return this.prefs.getLong(PreferenceKeys.UID_KEY, -1); |
| 83 | + return mPrefs.getLong(Const.PrefKeys.UID, -1); |
78 | 84 | }
|
79 | 85 |
|
80 | 86 | public boolean showHelpDialog() {
|
81 |
| - return this.prefs.getBoolean(PreferenceKeys.HELPDIALOG_KEY, true); |
| 87 | + return mPrefs.getBoolean(Const.PrefKeys.HELPDIALOG, true); |
82 | 88 | }
|
83 | 89 |
|
84 | 90 | public void setShowHelpDialog(boolean value) {
|
85 |
| - this.prefs.edit().putBoolean(PreferenceKeys.HELPDIALOG_KEY, value).commit(); |
| 91 | + mPrefs.edit().putBoolean(Const.PrefKeys.HELPDIALOG, value).commit(); |
86 | 92 | }
|
87 | 93 |
|
88 | 94 | public Location getLocation() {
|
89 |
| - // Prefer GPS locations over network locations. |
90 |
| - return this.gpsLocation != null ? this.gpsLocation : this.networkLocation; |
| 95 | + if (mGpsLocation != null) { |
| 96 | + return mGpsLocation; // Prefer GPS locations over network locations. |
| 97 | + } |
| 98 | + return mNetworkLocation; |
91 | 99 | }
|
92 | 100 |
|
93 | 101 | public static synchronized AppData getInstance(Context c) {
|
94 |
| - if (instance == null) { |
95 |
| - instance = new AppData(c.getApplicationContext()); |
| 102 | + if (sInstance == null) { |
| 103 | + sInstance = new AppData(c.getApplicationContext()); |
96 | 104 | }
|
97 |
| - return instance.setupLocation(); |
| 105 | + return sInstance.setupLocation(); |
98 | 106 | }
|
99 | 107 |
|
| 108 | + @Override |
100 | 109 | public synchronized void onSharedPreferenceChanged(
|
101 | 110 | SharedPreferences sharedPreferences, String key) {
|
102 |
| - if (key.equals(PreferenceKeys.EMAIL_KEY) || key.equals(PreferenceKeys.PASSWORD_KEY) || key.equals(PreferenceKeys.UID_KEY)) { |
103 |
| - this.api = null; |
| 111 | + if (key.equals(Const.PrefKeys.EMAIL) |
| 112 | + || key.equals(Const.PrefKeys.PASSWORD) |
| 113 | + || key.equals(Const.PrefKeys.UID)) { |
| 114 | + mApi = null; |
104 | 115 | }
|
105 | 116 | }
|
106 | 117 |
|
| 118 | + /** |
| 119 | + * Simple LocationListener that differentiates updates from the |
| 120 | + * network provider and those from the GPS provider. |
| 121 | + * |
| 122 | + * @author lum |
| 123 | + */ |
107 | 124 | public class UnisonLocationListener implements LocationListener {
|
108 | 125 |
|
109 |
| - private String provider; |
| 126 | + private String mProvider; |
110 | 127 |
|
111 | 128 | public UnisonLocationListener(String provider) {
|
112 |
| - this.provider = provider; |
| 129 | + mProvider = provider; |
113 | 130 | }
|
114 | 131 |
|
| 132 | + @Override |
115 | 133 | public void onLocationChanged(Location location) {
|
116 |
| - if (LocationManager.GPS_PROVIDER.equals(this.provider)) { |
117 |
| - AppData.this.gpsLocation = location; |
118 |
| - } else if (LocationManager.NETWORK_PROVIDER.equals(this.provider)) { |
119 |
| - AppData.this.networkLocation = location; |
| 134 | + if (LocationManager.GPS_PROVIDER.equals(mProvider)) { |
| 135 | + AppData.this.mGpsLocation = location; |
| 136 | + } else if (LocationManager.NETWORK_PROVIDER.equals(mProvider)) { |
| 137 | + AppData.this.mNetworkLocation = location; |
120 | 138 | } else {
|
121 | 139 | throw new RuntimeException("unsupported location provider");
|
122 |
| - }; |
| 140 | + } |
123 | 141 | Log.i(TAG, String.format("Got location (%s): lat=%f, lon=%f",
|
124 |
| - provider, location.getLatitude(), location.getLongitude())); |
| 142 | + mProvider, location.getLatitude(), location.getLongitude())); |
125 | 143 | }
|
126 | 144 |
|
127 |
| - public void onProviderDisabled(String provider) {} |
128 |
| - public void onProviderEnabled(String provider) {} |
129 |
| - public void onStatusChanged(String provider, int status, Bundle extras) {} |
| 145 | + @Override |
| 146 | + public void onProviderDisabled(String provider) { } |
| 147 | + @Override |
| 148 | + public void onProviderEnabled(String provider) { } |
| 149 | + @Override |
| 150 | + public void onStatusChanged(String provider, int status, Bundle extras) { } |
130 | 151 | }
|
131 | 152 | }
|
0 commit comments