-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into features/1640-fdroid-is-crazy
- Loading branch information
Showing
35 changed files
with
1,072 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBDataStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...id/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBStumblerBundleReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
android/src/main/java/org/mozilla/mozstumbler/client/leaderboard/LBUploadTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.