Skip to content

Commit

Permalink
Merge branch 'dev' into features/1640-fdroid-is-crazy
Browse files Browse the repository at this point in the history
  • Loading branch information
crankycoder committed May 21, 2015
2 parents 7650456 + ec8bff5 commit 04e8cf4
Show file tree
Hide file tree
Showing 35 changed files with 1,072 additions and 550 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.mozstumbler.service.stumblerthread.datahandling;
package org.mozilla.mozstumbler.client;

import android.content.Context;
import android.os.Environment;

import org.mozilla.mozstumbler.service.Prefs;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageManager;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.StorageIsEmptyTracker;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.base.SerializedJSONRows;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

import java.io.File;
Expand Down Expand Up @@ -43,17 +46,16 @@ public static synchronized DataStorageManager createGlobalInstance(Context conte

/* Pass filename returned from dataToSend() */
@Override
public synchronized boolean delete(String filename) {
if (filename.equals(MEMORY_BUFFER_NAME)) {
mCurrentReportsSendBuffer = null;
return true;
public synchronized boolean delete(SerializedJSONRows data) {
if (data.storageState == SerializedJSONRows.StorageState.IN_MEMORY_ONLY) {
return super.delete(data);
}

final File file = new File(mReportsDir, filename);
final File file = new File(mStorageDir, data.filename);
boolean ok = true;

if (Prefs.getInstanceWithoutContext().isSaveStumbleLogs()) {
File newFile = new File(sdcardArchivePath() + File.separator + filename);
File newFile = new File(sdcardArchivePath() + File.separator + data.filename);
ok = copyAndDelete(file, newFile);

if (!ok) {
Expand All @@ -63,7 +65,7 @@ public synchronized boolean delete(String filename) {
ok = file.delete();
}

mFileList.update(mReportsDir);
mFileList.update();
return ok;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.mozilla.mozstumbler.service.AppGlobals;
import org.mozilla.mozstumbler.service.stumblerthread.Reporter;
import org.mozilla.mozstumbler.service.stumblerthread.StumblerService;
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.ServiceLocator;
Expand Down
24 changes: 14 additions & 10 deletions android/src/main/java/org/mozilla/mozstumbler/client/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,34 @@
import org.acra.sender.HttpSender;
import org.mozilla.mozstumbler.BuildConfig;
import org.mozilla.mozstumbler.R;
import org.mozilla.mozstumbler.client.leaderboard.LBStumblerBundleReceiver;
import org.mozilla.mozstumbler.client.subactivities.DeveloperActivity;
import org.mozilla.mozstumbler.client.subactivities.LogActivity;
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.HttpUtil;
import org.mozilla.mozstumbler.service.core.http.IHttpUtil;
import org.mozilla.mozstumbler.service.core.http.ILocationService;
import org.mozilla.mozstumbler.service.core.http.MLSLocationService;
import org.mozilla.mozstumbler.service.core.logging.MockAcraLog;
import org.mozilla.mozstumbler.service.stumblerthread.Reporter;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageConstants;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageManager;;
import org.mozilla.mozstumbler.service.stumblerthread.motiondetection.MotionSensor;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.ISimulatorService;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.ScanManager;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.SimulatorService;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.WifiScanner;
import org.mozilla.mozstumbler.service.stumblerthread.scanners.cellscanner.CellScanner;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploadParam;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploader;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploaderMLS;
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.SystemClock;
import org.mozilla.mozstumbler.svclocator.services.log.ILogger;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;
import org.mozilla.osmdroid.tileprovider.constants.TileFilePath;
Expand Down Expand Up @@ -82,6 +89,7 @@ public class MainApp extends Application

// These track the state of the currently running service
private ScanManager.ScannerState scannerState = ScannerState.STOPPED;
private LBStumblerBundleReceiver mLeaderboard;


boolean isStumblerStopped() {
Expand Down Expand Up @@ -130,14 +138,10 @@ public static ServiceConfig defaultServiceConfig() {

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"));
result.put(ISystemClock.class,
ServiceConfig.load("org.mozilla.mozstumbler.svclocator.services.SystemClock"));
result.put(ILocationService.class,
ServiceConfig.load("org.mozilla.mozstumbler.service.core.http.MLS"));
result.put(ISimulatorService.class,
ServiceConfig.load("org.mozilla.mozstumbler.service.stumblerthread.scanners.SimulatorService"));
result.put(IHttpUtil.class, ServiceConfig.load(HttpUtil.class.getName()));
result.put(ISystemClock.class, ServiceConfig.load(SystemClock.class.getName()));
result.put(ILocationService.class, ServiceConfig.load(MLSLocationService.class.getName()));
result.put(ISimulatorService.class, ServiceConfig.load(SimulatorService.class.getName()));

if (BuildConfig.BUILD_TYPE.equals("unittest")) {
result.put(ILogger.class, ServiceConfig.load("org.mozilla.mozstumbler.svclocator.services.log.UnittestLogger"));
Expand Down Expand Up @@ -269,7 +273,7 @@ public void onServiceDisconnected(ComponentName className) {
Intent intent = new Intent(this, ClientStumblerService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);


mLeaderboard = new LBStumblerBundleReceiver(this);
}

private void checkSimulationPermission() {
Expand Down Expand Up @@ -318,7 +322,7 @@ public void stopScanning() {
mMainActivity.get().stop();
}

AsyncUploader uploader = new AsyncUploader();
AsyncUploader uploader = new AsyncUploaderMLS((DataStorageManager)DataStorageManager.getInstance(), this);
AsyncUploadParam param = new AsyncUploadParam(
ClientPrefs.getInstance(this).getUseWifiOnly(),
new NetworkInfo(this).isWifiAvailable(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.mozstumbler.client.leaderboard;

import android.content.Context;
import android.location.Location;

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.base.JSONRowsStorageManager;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageConstants;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

import java.util.HashMap;

class LBDataStorage extends JSONRowsStorageManager {

private static final String LOG_TAG = LoggerUtil.makeLogTag(LBDataStorage.class);
public static final String KEY_GRID = "grid";
public static final String KEY_CELL = "cellcount";
public static final String KEY_WIFI = "wificount";

private HashMap<String, JSONObject> mGridToRow = new HashMap<String, JSONObject>();
// mGridToRow applies only to the current in memory buffer, when this buffer changes it no longer applies
// the following is used to track this
private Object mCurrentObjForGridToRow = new Object();

LBDataStorage(Context c) {
super(c, null, DataStorageConstants.DEFAULT_MAX_BYTES_STORED_ON_DISK,
DataStorageConstants.DEFAULT_MAX_WEEKS_DATA_ON_DISK, "/leaderboard");
}

private void incrementJSON(JSONObject json, String key, int value) throws JSONException {
json.put(key, (Integer) json.get(key) + value);
}

public void insert(Location location, int cellCount, int wifiCount) {
String grid = locationToGrid(location);

JSONObject prev = findRowWithMatchingGrid(grid);
try {
if (prev != null) {
incrementJSON(prev, KEY_CELL, cellCount);
incrementJSON(prev, KEY_WIFI, wifiCount);
return;
}

JSONObject json = new JSONObject();
json.put(KEY_GRID, grid);
json.put(KEY_CELL, cellCount);
json.put(KEY_WIFI, wifiCount);
insertRow(json);

if (mCurrentObjForGridToRow != mInMemoryActiveJSONRows) {
mCurrentObjForGridToRow = mInMemoryActiveJSONRows;
mGridToRow = new HashMap<String, JSONObject>();
}
mGridToRow.put(grid, json);
} catch (JSONException e) {
Log.e(LOG_TAG, e.toString());
}
}

private JSONObject findRowWithMatchingGrid(String grid) {
if (!mGridToRow.containsKey(grid)) {
return null;
}
return mGridToRow.get(grid);
}

private String locationToGrid(Location location) {
//Code from here, http://wiki.openstreetmap.org/wiki/Mercator,spherical world mercator (not elliptical)
final double earthRadius = 6378137.000;

// get northing, easting of lower left of grid cell
double northing = earthRadius * Math.log(Math.tan(Math.PI / 4.0 +
Math.toRadians(location.getLatitude()) / 2.0));

double easting = Math.toRadians(location.getLongitude()) * earthRadius;

// round down to grid lower left, 500m increments
northing = Math.floor(northing / 1000.0 * 2) / 2.0 * 1000;
easting = Math.floor(easting / 1000.0 * 2) / 2.0 * 1000;

// bump up by small amount towards center of cell (to avoid being on the edge)
northing += 100;
easting += 100;

String grid = String.format("%.4f,%.4f", easting, northing);
// 4th decimal is ~10 meters
return grid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.mozstumbler.client.leaderboard;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import org.mozilla.mozstumbler.client.ClientPrefs;
import org.mozilla.mozstumbler.service.stumblerthread.Reporter;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.StumblerBundle;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploadParam;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploaderMLS;

public class LBStumblerBundleReceiver extends BroadcastReceiver {
class UploadingOccurringReceiver extends BroadcastReceiver {
LBUploadTask mUploadTask;

@Override
public void onReceive(Context context, Intent intent) {
// start this upload
if (mUploadTask != null &&
mUploadTask.isUploading.get()) {
return;
}

mUploadTask = new LBUploadTask(mStorage);
boolean ignoredValue = false;
ClientPrefs prefs = ClientPrefs.getInstance(context);
AsyncUploadParam params = new AsyncUploadParam(ignoredValue, ignoredValue,
prefs.getEmail(), prefs.getNickname());
mUploadTask.execute(params);
}
}

final LBDataStorage mStorage;
final UploadingOccurringReceiver mUploadTrigger = new UploadingOccurringReceiver();

public LBStumblerBundleReceiver(Context c) {
mStorage = new LBDataStorage(c);

LocalBroadcastManager.getInstance(c).registerReceiver(this,
new IntentFilter(Reporter.ACTION_NEW_BUNDLE));

LocalBroadcastManager.getInstance(c).registerReceiver(mUploadTrigger,
new IntentFilter(AsyncUploaderMLS.ACTION_MLS_UPLOAD_COMPLETED));

}

@Override
public void onReceive(Context context, Intent intent) {
final StumblerBundle bundle = intent.getParcelableExtra(Reporter.NEW_BUNDLE_ARG_BUNDLE);
mStorage.insert(bundle.getGpsPosition(),
bundle.getUnmodifiableCellData().size(),
bundle.getUnmodifiableWifiData().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.mozstumbler.client.leaderboard;

import org.mozilla.mozstumbler.service.core.http.IHttpUtil;
import org.mozilla.mozstumbler.service.core.http.IResponse;
import org.mozilla.mozstumbler.service.core.http.ISubmitService;
import org.mozilla.mozstumbler.service.core.http.MLSLocationService;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.base.JSONRowsStorageManager;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploadParam;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploader;
import org.mozilla.mozstumbler.svclocator.ServiceLocator;

import java.util.HashMap;
import java.util.Map;

class LBUploadTask extends AsyncUploader {
class LBSubmitter implements ISubmitService {
private static final String SUBMIT_URL = "https://fill me in please";
final IHttpUtil httpDelegate = (IHttpUtil) ServiceLocator.getInstance().getService(IHttpUtil.class);

@Override
public IResponse submit(byte[] data, Map<String, String> headers, boolean precompressed) {
return httpDelegate.post(SUBMIT_URL, data, headers, precompressed);
}
}

public LBUploadTask(JSONRowsStorageManager storageManager) {
super(storageManager);
}

@Override
protected ISubmitService getSubmitter() {
return new LBSubmitter();
}

@Override
protected HashMap<String, String> getHeaders(AsyncUploadParam param) {
HashMap<String,String> headers = new HashMap<String, String>();
headers.put(MLSLocationService.EMAIL_HEADER, param.emailAddress);
headers.put(MLSLocationService.NICKNAME_HEADER, param.nickname);
return headers;
}

@Override
protected boolean checkCanUpload(AsyncUploadParam param) {
// This task is triggered only on a successful upload of MLS data, so we assume
// this can also upload without further checks
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
import org.mozilla.mozstumbler.client.util.NotificationUtil;
import org.mozilla.mozstumbler.service.Prefs;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageConstants;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageManager;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.IDataStorageManager;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.PersistedStats;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploadParam;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploader;
import org.mozilla.mozstumbler.service.uploadthread.AsyncUploaderMLS;
import org.mozilla.mozstumbler.service.utils.NetworkInfo;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;

Expand Down Expand Up @@ -112,7 +115,8 @@ public void onClick(View v) {
return;
}

AsyncUploader uploader = new AsyncUploader();
Context c = mView.getContext();
AsyncUploader uploader = new AsyncUploaderMLS((DataStorageManager)DataStorageManager.getInstance(), c);
AsyncUploadParam param = new AsyncUploadParam(false /* useWifiOnly */,
new NetworkInfo(mView.getContext()).isWifiAvailable(),
Prefs.getInstance(mView.getContext()).getNickname(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import java.io.File;

import static org.mozilla.mozstumbler.service.stumblerthread.datahandling.ClientDataStorageManager.sdcardArchivePath;
import static org.mozilla.mozstumbler.client.ClientDataStorageManager.sdcardArchivePath;

public class DeveloperActivity extends ActionBarActivity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void sendReport(View button) {
EditText bugDescription = (EditText) findViewById(R.id.bug_report_description);
EditText bugReproduction = (EditText) findViewById(R.id.bug_report_reproduction);

if (verifyInput(bugName, bugDescription, bugReproduction)) {
if (verifyFieldNotEmpty(senderEmail, bugName, bugDescription, bugReproduction)) {

// Set custom data fields
ACRA.getErrorReporter().putCustomData(SENDER_NAME, senderName.getText().toString());
Expand All @@ -78,8 +78,7 @@ public void sendReport(View button) {
}
}

// Making sure input isn't just blank spaces for required fields
private boolean verifyInput(EditText... args) {
private boolean verifyFieldNotEmpty(EditText... args) {
boolean allValid = true;
for (EditText field : args) {
String input = field.getText().toString();
Expand Down
Loading

0 comments on commit 04e8cf4

Please sign in to comment.