Skip to content

Commit 5245744

Browse files
committed
Refactoring code base according to new formatting rules.
In order to standardize the style of the code, we introduce formatting and checkstyle tools (see utils/README.md for more information). The entire codebase has been refactored to conform to these standards.
1 parent 18eea56 commit 5245744

31 files changed

+2205
-1380
lines changed

.checkstyle

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
4+
<local-check-config name="gist - shareme/948425" location="utils/checkstyle.xml" type="project" description="Found on: https://gist.github.com/shareme/948425">
5+
<additional-data name="protect-config-file" value="true"/>
6+
</local-check-config>
7+
<fileset name="all" enabled="true" check-config-name="gist - shareme/948425" local="true">
8+
<file-match-pattern match-pattern="." include-pattern="true"/>
9+
</fileset>
10+
</fileset-config>

lint.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
<issue id="DefaultLocale" severity="ignore" />
4+
</lint>

src/ch/epfl/unison/AppData.java

+76-55
Original file line numberDiff line numberDiff line change
@@ -9,123 +9,144 @@
99
import android.os.Bundle;
1010
import android.preference.PreferenceManager;
1111
import android.util.Log;
12-
import ch.epfl.unison.api.PreferenceKeys;
12+
1313
import ch.epfl.unison.api.UnisonAPI;
1414

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 {
1621

1722
private static final String TAG = "ch.epfl.unison.AppData";
1823
private static final int LOCATION_INTERVAL = 20 * 60 * 1000; // In ms.
1924

20-
private static AppData instance;
25+
private static AppData sInstance;
2126

22-
private Context context;
23-
private UnisonAPI api;
24-
private SharedPreferences prefs;
27+
private Context mContext;
28+
private UnisonAPI mApi;
29+
private SharedPreferences mPrefs;
2530

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;
3136

3237
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);
3641
}
3742

3843
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);
4147
}
4248
// 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(
4955
LocationManager.NETWORK_PROVIDER);
5056
}
5157
// 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(
5864
LocationManager.GPS_PROVIDER);
5965
}
6066
return this;
6167
}
6268

6369
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);
6773
if (email != null && password != null) {
68-
this.api = new UnisonAPI(email, password);
74+
mApi = new UnisonAPI(email, password);
6975
} else {
70-
this.api = new UnisonAPI();
76+
mApi = new UnisonAPI();
7177
}
7278
}
73-
return this.api;
79+
return mApi;
7480
}
7581

7682
public long getUid() {
77-
return this.prefs.getLong(PreferenceKeys.UID_KEY, -1);
83+
return mPrefs.getLong(Const.PrefKeys.UID, -1);
7884
}
7985

8086
public boolean showHelpDialog() {
81-
return this.prefs.getBoolean(PreferenceKeys.HELPDIALOG_KEY, true);
87+
return mPrefs.getBoolean(Const.PrefKeys.HELPDIALOG, true);
8288
}
8389

8490
public void setShowHelpDialog(boolean value) {
85-
this.prefs.edit().putBoolean(PreferenceKeys.HELPDIALOG_KEY, value).commit();
91+
mPrefs.edit().putBoolean(Const.PrefKeys.HELPDIALOG, value).commit();
8692
}
8793

8894
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;
9199
}
92100

93101
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());
96104
}
97-
return instance.setupLocation();
105+
return sInstance.setupLocation();
98106
}
99107

108+
@Override
100109
public synchronized void onSharedPreferenceChanged(
101110
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;
104115
}
105116
}
106117

118+
/**
119+
* Simple LocationListener that differentiates updates from the
120+
* network provider and those from the GPS provider.
121+
*
122+
* @author lum
123+
*/
107124
public class UnisonLocationListener implements LocationListener {
108125

109-
private String provider;
126+
private String mProvider;
110127

111128
public UnisonLocationListener(String provider) {
112-
this.provider = provider;
129+
mProvider = provider;
113130
}
114131

132+
@Override
115133
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;
120138
} else {
121139
throw new RuntimeException("unsupported location provider");
122-
};
140+
}
123141
Log.i(TAG, String.format("Got location (%s): lat=%f, lon=%f",
124-
provider, location.getLatitude(), location.getLongitude()));
142+
mProvider, location.getLatitude(), location.getLongitude()));
125143
}
126144

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) { }
130151
}
131152
}

src/ch/epfl/unison/Const.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ch.epfl.unison;
2+
3+
/** Constants for the app. */
4+
public final class Const {
5+
6+
/** Preference keys. */
7+
public final class PrefKeys {
8+
public static final String EMAIL = "email";
9+
public static final String PASSWORD = "password";
10+
public static final String UID = "uid";
11+
public static final String LASTUPDATE = "lastupdate";
12+
public static final String NICKNAME = "nickname";
13+
public static final String HELPDIALOG = "helpdialog";
14+
15+
private PrefKeys() { } // Non-instantiable.
16+
}
17+
18+
/** Various other strings, e.g. keys for Intent extras. */
19+
public final class Strings {
20+
public static final String GID = "gid";
21+
public static final String NAME = "name";
22+
public static final String LOGOUT = "logout";
23+
24+
private Strings() { } // Non-instantiable.
25+
}
26+
27+
private Const() { } // Non-instantiable.
28+
}

src/ch/epfl/unison/LibraryHelper.java

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package ch.epfl.unison;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
5-
63
import android.content.ContentValues;
74
import android.content.Context;
85
import android.database.Cursor;
96
import android.database.sqlite.SQLiteDatabase;
107
import android.database.sqlite.SQLiteOpenHelper;
118
import android.provider.BaseColumns;
129

10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
/**
14+
* Helper class for accessing / managing the music library. Note: we are talking
15+
* about the one *that GroupStreamer owns*, not the Android content provider which
16+
* gives access to the music files.
17+
*
18+
* @author lum
19+
*/
1320
public class LibraryHelper {
1421

1522
@SuppressWarnings("unused")
@@ -27,14 +34,14 @@ public class LibraryHelper {
2734
private static final String WHERE_ALL = C_LOCAL_ID + " = ? AND "
2835
+ C_ARTIST + " = ? AND " + C_TITLE + " = ?";
2936

30-
private final OpenHelper dbHelper;
37+
private final OpenHelper mDbHelper;
3138

3239
public LibraryHelper(Context context) {
33-
this.dbHelper = new OpenHelper(context);
40+
mDbHelper = new OpenHelper(context);
3441
}
3542

3643
public Set<MusicItem> getEntries() {
37-
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
44+
SQLiteDatabase db = mDbHelper.getReadableDatabase();
3845
Cursor cur = db.query(TABLE_NAME,
3946
new String[] { C_LOCAL_ID, C_ARTIST, C_TITLE },
4047
null, null, null, null, null);
@@ -53,7 +60,7 @@ public Set<MusicItem> getEntries() {
5360
}
5461

5562
public boolean isEmpty() {
56-
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
63+
SQLiteDatabase db = mDbHelper.getReadableDatabase();
5764
Cursor cur = db.query(TABLE_NAME,
5865
new String[] { C_LOCAL_ID, C_ARTIST, C_TITLE },
5966
null, null, null, null, null);
@@ -68,20 +75,20 @@ public void insert(MusicItem item) {
6875
values.put(C_ARTIST, item.artist);
6976
values.put(C_TITLE, item.title);
7077

71-
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
78+
SQLiteDatabase db = mDbHelper.getWritableDatabase();
7279
db.insert(TABLE_NAME, null, values);
7380
db.close();
7481
}
7582

7683
public void delete(MusicItem item) {
77-
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
84+
SQLiteDatabase db = mDbHelper.getWritableDatabase();
7885
db.delete(TABLE_NAME, WHERE_ALL,
7986
new String[] { String.valueOf(item.localId), item.artist, item.title});
8087
db.close();
8188
}
8289

8390
public boolean exists(MusicItem item) {
84-
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
91+
SQLiteDatabase db = mDbHelper.getReadableDatabase();
8592
Cursor c = db.query(TABLE_NAME,
8693
new String[] { C_LOCAL_ID },
8794
WHERE_ALL,
@@ -93,15 +100,21 @@ public boolean exists(MusicItem item) {
93100
}
94101

95102
public void truncate() {
96-
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
103+
SQLiteDatabase db = mDbHelper.getWritableDatabase();
97104
db.delete(TABLE_NAME, null, null);
98105
db.close();
99106
}
100107

101108
public void close() {
102-
this.dbHelper.close();
109+
mDbHelper.close();
103110
}
104111

112+
/**
113+
* Simple SQLiteOpenHelper implementation that takes care of initializing the
114+
* database on installation and app updates.
115+
*
116+
* @author lum
117+
*/
105118
private static class OpenHelper extends SQLiteOpenHelper {
106119
private static final String DATABASE_SCHEMA = "CREATE TABLE " + TABLE_NAME + " ("
107120
+ C_ID + " int PRIMARY KEY, " + C_LOCAL_ID + " int UNIQUE, "
@@ -119,7 +132,7 @@ public void onCreate(SQLiteDatabase db) {
119132
@Override
120133
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
121134
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
122-
this.onCreate(db);
135+
onCreate(db);
123136
}
124137
}
125138

0 commit comments

Comments
 (0)