Skip to content

Commit

Permalink
Reorganize the codebase into a reusable library.
Browse files Browse the repository at this point in the history
Added tests and code to support lazily bound Proxy objects for all
services.  This is required to support static assignment of member
attributes like log instances.

Pulled out all the logic for the logging into a debug logger and a
production logger as we can no longer depend on BuildConfig to switch
loggers at runtime.  The logger must be set explicitly now on
application startup.

Minor tweaks to the ServiceLocator to iron out some edge case
initialisation race conditions.

Updated all log instances to use the new logger service.
Rationalized the makeLogTag calls to always require a class.
  • Loading branch information
crankycoder committed Jan 27, 2015
1 parent 5430833 commit 170f986
Show file tree
Hide file tree
Showing 104 changed files with 1,141 additions and 536 deletions.
5 changes: 2 additions & 3 deletions android/build.gradle
Expand Up @@ -34,7 +34,6 @@ repositories {
}

android {

compileSdkVersion 19
buildToolsVersion "19.1.0"

Expand Down Expand Up @@ -208,8 +207,6 @@ dependencies {
androidTestCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.robolectric:robolectric:2.4'

// This needs to be tweaked so that we don't include this on real
// releases, but I'm tired and can't muster the gradle foo.
androidTestCompile "org.mockito:mockito-core:1.+"
testCompile "org.mockito:mockito-core:1.+"

Expand Down Expand Up @@ -240,6 +237,8 @@ dependencies {
// osmdroid stuff
compile "org.apache.httpcomponents:httpmime:4.0.1"
compile "org.apache.james:apache-mime4j:0.6"

compile project(':libraries:stumbler')
}


Expand Down
Expand Up @@ -2,19 +2,19 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import org.acra.ACRA;
import org.mozilla.mozstumbler.BuildConfig;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.Prefs;
import org.mozilla.mozstumbler.service.core.logging.Log;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.core.logging.MockAcraLog;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;
import org.mozilla.osmdroid.api.IGeoPoint;
import org.mozilla.osmdroid.util.GeoPoint;

public class ClientPrefs extends Prefs {
private static final String LOG_TAG = AppGlobals.makeLogTag(ClientPrefs.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(ClientPrefs.class);
private static final String LAT_PREF = "lat";
private static final String LON_PREF = "lon";
private static final String IS_FIRST_RUN = "is_first_run";
Expand Down Expand Up @@ -126,10 +126,10 @@ public void setOptionEnabledToShowMLSOnMap(boolean isEnabled) {
public void setCrashReportingEnabled(boolean isOn) {
setBoolPref(CRASH_REPORTING, isOn);
if (isOn) {
Log.d(LOG_TAG, "Enabled crash reporting");
ClientLog.d(LOG_TAG, "Enabled crash reporting");
ACRA.setLog(MockAcraLog.getOriginalLog());
} else {
Log.d(LOG_TAG, "Disabled crash reporting");
ClientLog.d(LOG_TAG, "Disabled crash reporting");
ACRA.setLog(new MockAcraLog());
}
}
Expand Down
Expand Up @@ -16,13 +16,14 @@
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.ClientDataStorageManager;
import org.mozilla.mozstumbler.service.utils.BatteryCheckReceiver;
import org.mozilla.mozstumbler.service.utils.BatteryCheckReceiver.BatteryCheckCallback;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

// Used as a bound service (with foreground priority) in Mozilla Stumbler, a.k.a. active scanning mode.
// -- In accordance with Android service docs -and experimental findings- this puts the service as low
// as possible on the Android process kill list.
// -- Binding functions are commented in this class as being unused in the stand-alone service mode.
public class ClientStumblerService extends StumblerService {
private static final String LOG_TAG = AppGlobals.makeLogTag(StumblerService.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(StumblerService.class);
private final IBinder mBinder = new StumblerBinder();
private BatteryCheckReceiver mBatteryChecker;

Expand Down
34 changes: 31 additions & 3 deletions android/src/main/java/org/mozilla/mozstumbler/client/MainApp.java
Expand Up @@ -32,6 +32,7 @@
import org.mozilla.mozstumbler.client.util.NotificationUtil;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.Prefs;
import org.mozilla.mozstumbler.service.core.http.IHttpUtil;
import org.mozilla.mozstumbler.service.core.logging.MockAcraLog;
import org.mozilla.mozstumbler.service.stumblerthread.Reporter;
import org.mozilla.mozstumbler.service.stumblerthread.motiondetection.MotionSensor;
Expand All @@ -43,6 +44,9 @@
import org.mozilla.mozstumbler.service.utils.NetworkInfo;
import org.mozilla.mozstumbler.svclocator.ServiceConfig;
import org.mozilla.mozstumbler.svclocator.ServiceLocator;
import org.mozilla.mozstumbler.svclocator.services.ISystemClock;
import org.mozilla.mozstumbler.svclocator.services.log.ILogger;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;
import org.mozilla.osmdroid.tileprovider.constants.TileFilePath;

import java.io.File;
Expand All @@ -59,7 +63,7 @@
public class MainApp extends Application
implements AsyncUploader.AsyncUploaderListener {
public static final AtomicBoolean isUploading = new AtomicBoolean();
private final String LOG_TAG = AppGlobals.makeLogTag(MainApp.class.getSimpleName());
private final String LOG_TAG = LoggerUtil.makeLogTag(MainApp.class);
private ClientStumblerService mStumblerService;
private ServiceConnection mConnection;
private ServiceBroadcastReceiver mReceiver;
Expand Down Expand Up @@ -109,15 +113,39 @@ private File getCacheDir(Context c) {
return new File(dir, "osmdroid");
}

public static ServiceConfig defaultServiceConfig() {
/*
This will configure the service map with all services required for runtime.
Note that the logger checks the buildconfig type to determine whether or not to use the DebugLogger
or the ProductionLogger.
*/

ServiceConfig result = new ServiceConfig();
// All classes here must have an argument free constructor.
result.put(IHttpUtil.class,
ServiceConfig.load("org.mozilla.mozstumbler.service.core.http.HttpUtil"));
// All classes here must have an argument free constructor.
result.put(ISystemClock.class,
ServiceConfig.load("org.mozilla.mozstumbler.svclocator.services.SystemClock"));

if (BuildConfig.BUILD_TYPE.equals("unittest")) {
result.put(ILogger.class, ServiceConfig.load("org.mozilla.mozstumbler.svclocator.services.log.DebugLogger"));
} else {
result.put(ILogger.class, ServiceConfig.load("org.mozilla.mozstumbler.svclocator.services.log.ProductionLogger"));
}

return result;
}

@Override
public void onCreate() {
super.onCreate();
// The following line triggers the initialization of ACRA
ACRA.init(this);

// Bootstrap the service locator with the default list of services
ServiceLocator rootLocator = ServiceLocator.newRoot(ServiceConfig.defaultServiceConfig());
ServiceLocator.setRootInstance(rootLocator);
ServiceLocator.newRoot(defaultServiceConfig());

// This must be called after init to get a copy of the
// original ACRA.log instance so that we can toggle the logger on
Expand Down
Expand Up @@ -18,10 +18,10 @@
import org.json.JSONObject;
import org.mozilla.mozstumbler.client.mapview.MapFragment;
import org.mozilla.mozstumbler.client.mapview.ObservationPoint;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.core.logging.Log;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.stumblerthread.Reporter;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.StumblerBundle;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;
import org.mozilla.osmdroid.util.GeoPoint;

import java.lang.ref.WeakReference;
Expand All @@ -30,7 +30,7 @@

public class ObservedLocationsReceiver extends BroadcastReceiver {

private static final String LOG_TAG = AppGlobals.makeLogTag(ObservedLocationsReceiver.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(ObservedLocationsReceiver.class);
private WeakReference<MapFragment> mMapActivity = new WeakReference<MapFragment>(null);
private final LinkedList<ObservationPoint> mCollectionPoints = new LinkedList<ObservationPoint>();
private final LinkedList<ObservationPoint> mQueuedForMLS = new LinkedList<ObservationPoint>();
Expand Down Expand Up @@ -145,7 +145,7 @@ public synchronized void onReceive(Context context, Intent intent) {
}
}
} catch (JSONException e) {
Log.w(LOG_TAG, "Failed to convert bundle to JSON: " + e);
ClientLog.w(LOG_TAG, "Failed to convert bundle to JSON: " + e);
}

while (mCollectionPoints.size() > MAX_SIZE_OF_POINT_LISTS) {
Expand Down
Expand Up @@ -15,11 +15,11 @@
import android.util.Log;

import org.mozilla.mozstumbler.R;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.core.http.IHttpUtil;
import org.mozilla.mozstumbler.service.core.http.IResponse;
import org.mozilla.mozstumbler.service.utils.NetworkInfo;
import org.mozilla.mozstumbler.svclocator.ServiceLocator;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

import java.io.File;
import java.io.IOException;
Expand All @@ -29,7 +29,7 @@
import java.util.Map;

public class Updater {
private static final String LOG_TAG = AppGlobals.makeLogTag(Updater.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(Updater.class);
private static final String LATEST_URL = "https://github.com/mozilla/MozStumbler/releases/latest";
private static final String APK_URL_FORMAT = "https://github.com/mozilla/MozStumbler/releases/download/v%s/MozStumbler-v%s.apk";

Expand Down
Expand Up @@ -13,14 +13,14 @@
import android.telephony.CellLocation;
import android.util.Log;

import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

/*
* Determine whether the cell location is updated when the screen is off
* https://code.google.com/p/android/issues/detail?id=10931
*/
public class ScreenMonitor {
private static final String LOG_TAG = AppGlobals.makeLogTag(ScreenMonitor.class.getName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(ScreenMonitor.class);

private static final String PREFS_FILE = ScreenMonitor.class.getSimpleName();
private static final String LOCATION_UPDATES_COUNT_PREF = "location_updates_count";
Expand Down
Expand Up @@ -10,22 +10,21 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.core.http.HttpUtil;
import org.mozilla.mozstumbler.service.core.http.IHttpUtil;
import org.mozilla.mozstumbler.service.core.http.ILocationService;
import org.mozilla.mozstumbler.service.core.http.IResponse;
import org.mozilla.mozstumbler.service.core.http.MLS;
import org.mozilla.mozstumbler.service.utils.LocationAdapter;
import org.mozilla.mozstumbler.svclocator.ServiceLocator;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

import java.util.concurrent.atomic.AtomicInteger;

/*
This class provides MLS locations by calling HTTP methods against the MLS.
*/
public class MLSLocationGetter extends AsyncTask<String, Void, Location> {
private static final String LOG_TAG = AppGlobals.makeLogTag(MLSLocationGetter.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(MLSLocationGetter.class);
private static final String RESPONSE_OK_TEXT = "ok";
private ILocationService mls;
private MLSLocationGetterCallback mCallback;
Expand Down
Expand Up @@ -39,9 +39,10 @@
import org.mozilla.mozstumbler.client.navdrawer.MetricsView;
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.core.http.IHttpUtil;
import org.mozilla.mozstumbler.service.core.logging.Log;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.GPSScanner;
import org.mozilla.mozstumbler.svclocator.ServiceLocator;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;
import org.mozilla.osmdroid.ResourceProxy;
import org.mozilla.osmdroid.api.IGeoPoint;
import org.mozilla.osmdroid.events.DelayedMapListener;
Expand All @@ -68,7 +69,7 @@ public class MapFragment extends android.support.v4.app.Fragment

public static enum NoMapAvailableMessage { eHideNoMapMessage, eNoMapDueToNoAccessibleStorage, eNoMapDueToNoInternet }

private static final String LOG_TAG = AppGlobals.makeLogTag(MapFragment.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(MapFragment.class);

private static final String COVERAGE_REDIRECT_URL = "https://location.services.mozilla.com/map.json";
private static final String ZOOM_KEY = "zoom";
Expand Down Expand Up @@ -241,7 +242,7 @@ public void run() {
// These need a fully constructed map, which on first load seems to take a while.
// Post with no delay does not work for me, adding an arbitrary
// delay of 300 ms should be plenty.
Log.d(LOG_TAG, "postDelayed ZOOM " + zoom);
ClientLog.d(LOG_TAG, "postDelayed ZOOM " + zoom);
setCenterAndZoom(loc, zoom);
}
}, 300);
Expand All @@ -251,7 +252,7 @@ public void run() {
private void initializeVisibleCounts() {
Configuration c = getResources().getConfiguration();
if (c.fontScale > 1) {
Log.d(LOG_TAG, "Large text is enabled: " + c.fontScale);
ClientLog.d(LOG_TAG, "Large text is enabled: " + c.fontScale);
mRootView.findViewById(R.id.text_satellites_sep).setVisibility(View.GONE);
mRootView.findViewById(R.id.text_satellites_avail).setVisibility(View.GONE);
} else {
Expand Down Expand Up @@ -334,7 +335,7 @@ public void run() {
} catch (Exception ex) {
// this will catch java.net.UnknownHostException when offline
if (AppGlobals.isDebug) {
Log.d(LOG_TAG, ex.toString());
ClientLog.d(LOG_TAG, ex.toString());
}
}
// always init coverage tiles
Expand All @@ -351,7 +352,7 @@ public void run() {
}

private void initCoverageTiles(String coverageUrl) {
Log.i(LOG_TAG, "initCoverageTiles: " + coverageUrl);
ClientLog.i(LOG_TAG, "initCoverageTiles: " + coverageUrl);
mCoverageTilesOverlayLowZoom = new CoverageOverlay(CoverageOverlay.LowResType.LOWER_ZOOM,
mRootView.getContext(), coverageUrl, mMap);
mCoverageTilesOverlayHighZoom = new CoverageOverlay(CoverageOverlay.LowResType.HIGHER_ZOOM,
Expand Down Expand Up @@ -653,7 +654,7 @@ public boolean onTouchEvent(final MotionEvent event, final MapView mapView) {
@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "onResume");
ClientLog.d(LOG_TAG, "onResume");

doOnResume();
}
Expand Down Expand Up @@ -699,7 +700,7 @@ public void onSaveInstanceState(Bundle bundle) {
@Override
public void onPause() {
super.onPause();
Log.d(LOG_TAG, "onPause");
ClientLog.d(LOG_TAG, "onPause");
saveStateToPrefs();

if (mMapLocationListener != null) {
Expand All @@ -722,7 +723,7 @@ private void removeLayer(Overlay layer) {
@Override
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy");
ClientLog.d(LOG_TAG, "onDestroy");

removeLayer(mLowResMapOverlayHighZoom);
removeLayer(mLowResMapOverlayLowZoom);
Expand Down
Expand Up @@ -14,15 +14,16 @@
import android.widget.Toast;

import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.core.logging.Log;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.GPSScanner;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

import java.lang.ref.WeakReference;

class MapLocationListener {
private final WeakReference<MapFragment> mMapActivity;
private LocationManager mLocationManager;
private static final String LOG_TAG = AppGlobals.makeLogTag(MapLocationListener.class.getSimpleName());
private static final String LOG_TAG = LoggerUtil.makeLogTag(MapLocationListener.class);

interface ReceivedLocationCallback {
void receivedLocation();
Expand Down Expand Up @@ -107,7 +108,7 @@ public void onGpsStatusChanged(int event) {
lastLoc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (lastLoc != null && mMapActivity.get() != null) {
Log.d(LOG_TAG, "set last known location");
ClientLog.d(LOG_TAG, "set last known location");
mMapActivity.get().setUserPositionAt(lastLoc);
}

Expand Down
Expand Up @@ -11,7 +11,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.mozstumbler.client.ClientPrefs;
import org.mozilla.mozstumbler.service.core.logging.Log;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageContract;
import org.mozilla.mozstumbler.service.utils.NetworkInfo;
import org.mozilla.osmdroid.util.GeoPoint;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void setMLSCoordinate(Coordinate c) {

public void errorMLSResponse(boolean stopRequesting) {
if (stopRequesting) {
Log.i(ObservationPoint.class.getSimpleName(), "Error:" + mMLSQuery.toString());
ClientLog.i(ObservationPoint.class.getSimpleName(), "Error:" + mMLSQuery.toString());
mMLSQuery = null;
}
mIsMLSLocationQueryRunning = false;
Expand Down

0 comments on commit 170f986

Please sign in to comment.