From 184f5a72293276c4547b11044122be757576b5d1 Mon Sep 17 00:00:00 2001 From: Alexey Illarionov Date: Sun, 5 Jan 2014 04:39:31 +0400 Subject: [PATCH] Send List to reporter in place of JSON --- src/org/mozilla/mozstumbler/Reporter.java | 30 +++++++++---------- .../mozstumbler/cellscanner/CellScanner.java | 14 ++++----- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/org/mozilla/mozstumbler/Reporter.java b/src/org/mozilla/mozstumbler/Reporter.java index 9c31f3988..54e0cc1ac 100644 --- a/src/org/mozilla/mozstumbler/Reporter.java +++ b/src/org/mozilla/mozstumbler/Reporter.java @@ -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; @@ -50,13 +51,12 @@ final class Reporter extends BroadcastReceiver { private List mWifiData; private long mWifiDataTime; - private String mCellData; + private List 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) { @@ -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(); @@ -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); @@ -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(); } } @@ -261,7 +260,7 @@ public void run() { }).start(); } - void reportLocation(Location gpsPosition, List wifiInfo, String radioType, String cellInfo) { + void reportLocation(Location gpsPosition, List wifiInfo, String radioType, List cellInfo) { Log.d(LOGTAG, "reportLocation called"); JSONObject locInfo = null; JSONArray cellJSON = null; @@ -275,8 +274,9 @@ void reportLocation(Location gpsPosition, List 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); } diff --git a/src/org/mozilla/mozstumbler/cellscanner/CellScanner.java b/src/org/mozilla/mozstumbler/cellscanner/CellScanner.java index f5db69cc2..0ef058fd9 100644 --- a/src/org/mozilla/mozstumbler/cellscanner/CellScanner.java +++ b/src/org/mozilla/mozstumbler/cellscanner/CellScanner.java @@ -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 @@ -53,19 +57,15 @@ public void start() { public void run() { Log.d(LOGTAG, "Cell Scanning Timer fired"); final long curTime = System.currentTimeMillis(); - List cells = mImpl.getCellInfo(); + ArrayList cells = new ArrayList(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);