Skip to content

Commit

Permalink
Send List<CellInfo> to reporter in place of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov committed Jan 5, 2014
1 parent 7f04c20 commit 184f5a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/org/mozilla/mozstumbler/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import android.content.IntentFilter;
import android.location.Location;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.util.Log;

import org.mozilla.mozstumbler.cellscanner.CellInfo;
import org.mozilla.mozstumbler.cellscanner.CellScanner;
import org.mozilla.mozstumbler.preferences.Prefs;

import org.json.JSONArray;
Expand Down Expand Up @@ -50,13 +51,12 @@ final class Reporter extends BroadcastReceiver {
private List<ScanResult> mWifiData;
private long mWifiDataTime;

private String mCellData;
private List<CellInfo> mCellData;
private long mCellDataTime;

private boolean mIsGpsPositionKnown = false;
private final Location mGpsPosition = new Location("");

private String mRadioType;
private long mReportsSent;

Reporter(Context context, Prefs prefs) {
Expand Down Expand Up @@ -86,8 +86,7 @@ final class Reporter extends BroadcastReceiver {

private void resetData() {
mWifiData = Collections.emptyList();
mCellData = "";
mRadioType = "";
mCellData = Collections.emptyList();
mWifiDataTime = 0;
mCellDataTime = 0;
mGpsPosition.reset();
Expand Down Expand Up @@ -122,21 +121,20 @@ public void onReceive(Context context, Intent intent) {
}

if (mWifiDataTime - time > REPORTER_WINDOW) {
mWifiData = Collections.emptyList();;
mWifiData = Collections.emptyList();
mWifiDataTime = 0;
}

if (mCellDataTime - time > REPORTER_WINDOW) {
mCellData = "";
mCellData = Collections.emptyList();
mCellDataTime = 0;
}

if (WifiScanner.WIFI_SCANNER_EXTRA_SUBJECT.equals(subject)) {
mWifiData = intent.getParcelableArrayListExtra(WifiScanner.WIFI_SCANNER_ARG_SCAN_RESULTS);
mWifiDataTime = time;
} else if (subject.equals("CellScanner")) {
mCellData = data;
mRadioType = intent.getStringExtra("radioType");
} else if (CellScanner.CELL_SCANNER_EXTRA_SUBJECT.equals(subject)) {
mCellData = intent.getParcelableArrayListExtra(CellScanner.CELL_SCANNER_ARG_CELLS);
mCellDataTime = time;
} else if (GPSScanner.GPS_SCANNER_EXTRA_SUBJECT.equals(subject)) {
Location l = intent.getParcelableExtra(GPSScanner.GPS_SCANNER_ARG_LOCATION);
Expand All @@ -152,8 +150,9 @@ public void onReceive(Context context, Intent intent) {
return; // Intent not aimed at the Reporter (it is possibly for UI instead)
}

if (mIsGpsPositionKnown && (!mWifiData.isEmpty() || mCellData.length() > 0)) {
reportLocation(mGpsPosition, mWifiData, mRadioType, mCellData);
if (mIsGpsPositionKnown && (!mWifiData.isEmpty() || !mCellData.isEmpty())) {
String radioType = mCellData.isEmpty() ? null : mCellData.get(0).getRadio();
reportLocation(mGpsPosition, mWifiData, radioType, mCellData);
resetData();
}
}
Expand Down Expand Up @@ -261,7 +260,7 @@ public void run() {
}).start();
}

void reportLocation(Location gpsPosition, List<ScanResult> wifiInfo, String radioType, String cellInfo) {
void reportLocation(Location gpsPosition, List<ScanResult> wifiInfo, String radioType, List<CellInfo> cellInfo) {
Log.d(LOGTAG, "reportLocation called");
JSONObject locInfo = null;
JSONArray cellJSON = null;
Expand All @@ -275,8 +274,9 @@ void reportLocation(Location gpsPosition, List<ScanResult> wifiInfo, String radi
if (gpsPosition.hasAccuracy()) locInfo.put("accuracy", (int)gpsPosition.getAccuracy());
if (gpsPosition.hasAltitude()) locInfo.put("altitude", (int)gpsPosition.getAltitude());

if (cellInfo.length()>0) {
cellJSON=new JSONArray(cellInfo);
if (!cellInfo.isEmpty()) {
cellJSON=new JSONArray();
for (CellInfo cell: cellInfo) cellJSON.put(cell.toJSONObject());
locInfo.put("cell", cellJSON);
locInfo.put("radio", radioType);
}
Expand Down
14 changes: 7 additions & 7 deletions src/org/mozilla/mozstumbler/cellscanner/CellScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import org.json.JSONArray;
import org.mozilla.mozstumbler.ScannerService;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class CellScanner {
public static final String CELL_SCANNER_EXTRA_SUBJECT = "CellScanner";
public static final String CELL_SCANNER_ARG_CELLS = "org.mozilla.mozstumbler.cellscanner.CellScanner.cells";

private static final String LOGTAG = CellScanner.class.getName();
private static final long CELL_MIN_UPDATE_TIME = 1000; // milliseconds

Expand Down Expand Up @@ -53,19 +57,15 @@ public void start() {
public void run() {
Log.d(LOGTAG, "Cell Scanning Timer fired");
final long curTime = System.currentTimeMillis();
List<CellInfo> cells = mImpl.getCellInfo();
ArrayList<CellInfo> cells = new ArrayList<CellInfo>(mImpl.getCellInfo());
if (cells.isEmpty()) {
return;
}

JSONArray cellsJson = new JSONArray();
for (CellInfo cell : cells) cellsJson.put(cell.toJSONObject());

Intent intent = new Intent(ScannerService.MESSAGE_TOPIC);
intent.putExtra(Intent.EXTRA_SUBJECT, "CellScanner");
intent.putExtra(Intent.EXTRA_SUBJECT, CELL_SCANNER_EXTRA_SUBJECT);
intent.putParcelableArrayListExtra(CELL_SCANNER_ARG_CELLS, cells);
intent.putExtra("time", curTime);
intent.putExtra("data", cellsJson.toString());
intent.putExtra("radioType", cells.get(0).getRadio());
mContext.sendBroadcast(intent);
}
}, 0, CELL_MIN_UPDATE_TIME);
Expand Down

0 comments on commit 184f5a7

Please sign in to comment.