Skip to content

Commit

Permalink
Code cleanup and Android linting
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Jun 12, 2017
1 parent 34b5c4f commit 29bc561
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
@@ -1,5 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.northwestern.cbits.anthracite">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
Expand Down
15 changes: 8 additions & 7 deletions src/edu/northwestern/cbits/anthracite/LogContentProvider.java
Expand Up @@ -10,6 +10,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.support.annotation.NonNull;

public class LogContentProvider extends ContentProvider {
private UriMatcher _matcher = new UriMatcher(UriMatcher.NO_MATCH);
Expand Down Expand Up @@ -101,7 +102,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion,
return true;
}

public int delete(Uri uri, String selection, String[] selectionArgs) {
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
switch (this._matcher.match(uri)) {
case LogContentProvider.APP_EVENTS:
return this._db.delete(LogContentProvider.APP_EVENTS_TABLE,
Expand All @@ -115,12 +116,12 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
}

@Override
public String getType(Uri uri) {
public String getType(@NonNull Uri uri) {
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
public Uri insert(@NonNull Uri uri, ContentValues values) {
long id = 0;

switch (this._matcher.match(uri)) {
Expand All @@ -137,8 +138,8 @@ public Uri insert(Uri uri, ContentValues values) {
return Uri.withAppendedPath(uri, "" + id);
}

public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
switch (this._matcher.match(uri)) {
case LogContentProvider.APP_EVENTS:
return this._db
Expand All @@ -153,8 +154,8 @@ public Cursor query(Uri uri, String[] projection, String selection,
return null;
}

public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
public int update(@NonNull Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
switch (this._matcher.match(uri)) {
case LogContentProvider.APP_EVENTS:
return this._db.update(LogContentProvider.APP_EVENTS_TABLE, values,
Expand Down
48 changes: 25 additions & 23 deletions src/edu/northwestern/cbits/anthracite/Logger.java
Expand Up @@ -7,7 +7,7 @@
import java.net.UnknownHostException;
import java.security.cert.CertificateException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;import java.util.Date;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -203,31 +203,33 @@ public boolean log(String event, Map<String, Object> payload)
{
if (prefs.getBoolean(Logger.LOGGER_LOCATION_ENABLED, Logger.LOGGER_LOCATION_ENABLED_DEFAULT))
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (ContextCompat.checkSelfPermission(this._context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this._context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M )
{
LocationManager lm = (LocationManager) this._context.getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(this._context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this._context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager lm = (LocationManager) this._context.getSystemService(Context.LOCATION_SERVICE);

Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Location backupLocation = null;
Location backupLocation = null;

if (lastLocation != null && now - lastLocation.getTime() > (1000 * 60 * 60)) {
backupLocation = lastLocation;
if (lastLocation != null && now - lastLocation.getTime() > (1000 * 60 * 60)) {
backupLocation = lastLocation;

lastLocation = null;
}
lastLocation = null;
}

if (lastLocation == null)
lastLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastLocation == null)
lastLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (lastLocation == null)
lastLocation = backupLocation;
if (lastLocation == null)
lastLocation = backupLocation;

if (lastLocation != null) {
payload.put(Logger.LATITUDE, lastLocation.getLatitude());
payload.put(Logger.LONGITUDE, lastLocation.getLongitude());
payload.put(Logger.ALTITUDE, lastLocation.getAltitude());
payload.put(Logger.TIME_DRIFT, now - lastLocation.getTime());
if (lastLocation != null) {
payload.put(Logger.LATITUDE, lastLocation.getLatitude());
payload.put(Logger.LONGITUDE, lastLocation.getLongitude());
payload.put(Logger.ALTITUDE, lastLocation.getAltitude());
payload.put(Logger.TIME_DRIFT, now - lastLocation.getTime());
}
}
}
}
Expand Down Expand Up @@ -482,7 +484,7 @@ public Response intercept(Chain chain) throws IOException {
String payload = c.getString(c.getColumnIndex(LogContentProvider.APP_EVENT_PAYLOAD));

RequestBody formBody = new FormBody.Builder()
.add(Logger.JSON, payload.toString())
.add(Logger.JSON, payload)
.build();

Request request = new Request.Builder()
Expand Down Expand Up @@ -679,7 +681,7 @@ private void setBoolean(String key, boolean value)

Editor e = prefs.edit();
e.putBoolean(key, value);
e.commit();
e.apply();
}

public void setEnabled(boolean enabled)
Expand Down Expand Up @@ -747,7 +749,7 @@ public void setUploadUri(Uri uri)

Editor e = prefs.edit();
e.putString(Logger.LOGGER_URI, uri.toString());
e.commit();
e.apply();
}

public void setUserAgent(String userAgent)
Expand All @@ -756,7 +758,7 @@ public void setUserAgent(String userAgent)

Editor e = prefs.edit();
e.putString(Logger.LOGGER_USER_AGENT, userAgent);
e.commit();
e.apply();
}

public Uri getUploadUri()
Expand All @@ -775,7 +777,7 @@ public void setUploadInterval(long interval)

Editor e = prefs.edit();
e.putLong(Logger.INTERVAL, interval);
e.commit();
e.apply();
}

public boolean postJsonContent(JSONObject content, Uri destination)
Expand Down
3 changes: 1 addition & 2 deletions src/edu/northwestern/cbits/anthracite/WiFiHelper.java 100644 → 100755
Expand Up @@ -15,8 +15,7 @@ public static boolean wifiAvailable(Context context) {
if (now - WiFiHelper._lastWifiCheck > 10000) {
WiFiHelper._lastWifiCheck = now;

WifiManager wifi = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

if (wifi.isWifiEnabled()) {
WiFiHelper._wifiAvailable = true;
Expand Down

0 comments on commit 29bc561

Please sign in to comment.