diff --git a/.gitignore b/.gitignore index 9b6e377..a127fe3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ /res/raw/build.txt /.settings /local.properties -/.nb-gradle/ \ No newline at end of file +/.nb-gradle/ +/private/ diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f14dbb2..bd85b2f 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5,11 +5,12 @@ android:versionName="0.2.8"> + @@ -22,11 +23,12 @@ android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> + - + + - \ No newline at end of file + diff --git a/build.gradle b/build.gradle index 3c5aba7..114cddd 100644 --- a/build.gradle +++ b/build.gradle @@ -28,17 +28,19 @@ apply plugin: 'com.android.application' repositories { mavenCentral() + jcenter() } dependencies { compile group: 'org.microg', name: 'unifiednlp-api', version: '1.5.4' compile 'ch.acra:acra:4.5.0' compile 'com.android.support:support-v4:25.3.1' + compile 'com.obsez.android.lib.filechooser:filechooser:1.1.5' } android { compileSdkVersion 21 - buildToolsVersion '25.0.3' + buildToolsVersion "25.0.3" lintOptions.abortOnError false sourceSets { @@ -64,4 +66,4 @@ task writeVersionInfo { if (file('user.gradle').exists()) { apply from: 'user.gradle' -} \ No newline at end of file +} diff --git a/res/layout/dialogpreference_catalogs_group.xml b/res/layout/dialogpreference_catalogs_group.xml index 49433db..4f4d4e8 100644 --- a/res/layout/dialogpreference_catalogs_group.xml +++ b/res/layout/dialogpreference_catalogs_group.xml @@ -4,7 +4,7 @@ android:layout_width="wrap_content" android:layout_height="?android:attr/listPreferredItemHeight" android:layout_marginLeft="8dp" - android:gravity="start" + android:gravity="end" android:paddingLeft="32dp" android:paddingTop="8dp" android:textSize="14sp" diff --git a/res/values-ja/log_file_lasting.xml b/res/values-ja/log_file_lasting.xml new file mode 100644 index 0000000..e4e3c09 --- /dev/null +++ b/res/values-ja/log_file_lasting.xml @@ -0,0 +1,19 @@ + + + + @string/log_file_12_label + @string/log_file_24_label + @string/log_file_48_label + @string/log_file_72_label + @string/log_file_168_label + @string/log_file_720_label + + + 12 + 24 + 48 + 72 + 168 + 720 + + \ No newline at end of file diff --git a/res/values-ja/operation_modes.xml b/res/values-ja/operation_modes.xml new file mode 100644 index 0000000..38d9713 --- /dev/null +++ b/res/values-ja/operation_modes.xml @@ -0,0 +1,11 @@ + + + + Online mode + Offline mode + + + online + offline + + \ No newline at end of file diff --git a/res/values-ja/sources.xml b/res/values-ja/sources.xml new file mode 100644 index 0000000..20fe91a --- /dev/null +++ b/res/values-ja/sources.xml @@ -0,0 +1,13 @@ + + + + Combined (Wifis and Cells) + Wifis only + Cells only + + + combined + wifis + cells + + \ No newline at end of file diff --git a/res/values-ja/strings.xml b/res/values-ja/strings.xml index 045548c..74770c9 100644 --- a/res/values-ja/strings.xml +++ b/res/values-ja/strings.xml @@ -38,4 +38,17 @@ WiFi カタログをダウンロード 現在のサイズ (国に応じて 10–400MB) サーバーの応答を解析中にエラー + When location stucks when phone sleeps you can try to wake up phone when cell is looked for + Wake up strategy + No wake up + Wake up CPU only + Wake up with display + After specified number of time log file will recreated + Rotation of the log file + 12 hours + a day + two days + three days + a week + a month diff --git a/res/values-ja/wake_up_strategy.xml b/res/values-ja/wake_up_strategy.xml new file mode 100644 index 0000000..a9a314a --- /dev/null +++ b/res/values-ja/wake_up_strategy.xml @@ -0,0 +1,13 @@ + + + + @string/nowakeup_label + @string/wakeuppartial_label + @string/wakeupfull_label + + + nowakeup + wakeuppartial + wakeupfull + + \ No newline at end of file diff --git a/res/values/log_file_lasting.xml b/res/values/log_file_lasting.xml new file mode 100644 index 0000000..e4e3c09 --- /dev/null +++ b/res/values/log_file_lasting.xml @@ -0,0 +1,19 @@ + + + + @string/log_file_12_label + @string/log_file_24_label + @string/log_file_48_label + @string/log_file_72_label + @string/log_file_168_label + @string/log_file_720_label + + + 12 + 24 + 48 + 72 + 168 + 720 + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 7f9fe53..2921427 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -39,4 +39,18 @@ Download Wifi Catalog Current size (10–400MB depending on country) Error parsing server reply + When location stucks when phone sleeps you can try to wake up phone when cell is looked for + Wake up strategy + No wake up + Wake up CPU only + Wake up with display + Select directory for logging + After specified number of time log file will recreated + Rotation of the log file + 12 hours + a day + two days + three days + a week + a month diff --git a/res/values/wake_up_strategy.xml b/res/values/wake_up_strategy.xml new file mode 100644 index 0000000..a9a314a --- /dev/null +++ b/res/values/wake_up_strategy.xml @@ -0,0 +1,13 @@ + + + + @string/nowakeup_label + @string/wakeuppartial_label + @string/wakeupfull_label + + + nowakeup + wakeuppartial + wakeupfull + + \ No newline at end of file diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index ca8839b..848f56d 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -8,6 +8,13 @@ android:key="mode" android:summary="@string/operation_mode_summary" android:title="@string/operation_mode_title"/> + + + + 0)?resultIds[0]:"") + ":" + resultIds.length); + if (resultIds.length == 0) { return null; } else if (resultIds.length == 1) { @@ -507,7 +505,8 @@ private boolean hasWifiTables() { @Override protected void onPostExecute(Location result) { if (result == null) { - Log.w(TAG, "Location was null"); + Log.d(TAG, "Broadcasting location" + result); + mListener.onLocationReceived(result); return; } diff --git a/src/org/openbmap/unifiedNlp/services/RadiocellsOrgNlpService.java b/src/org/openbmap/unifiedNlp/services/RadiocellsOrgNlpService.java index 9b02388..9f740d1 100644 --- a/src/org/openbmap/unifiedNlp/services/RadiocellsOrgNlpService.java +++ b/src/org/openbmap/unifiedNlp/services/RadiocellsOrgNlpService.java @@ -18,6 +18,8 @@ License, or (at your option) any later version. package org.openbmap.unifiedNlp.services; import android.annotation.SuppressLint; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -27,7 +29,8 @@ License, or (at your option) any later version. import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.WifiLock; import android.os.Build; -import android.os.Handler; +import android.os.PowerManager; +import android.os.SystemClock; import android.preference.PreferenceManager; import android.telephony.CellInfo; import android.telephony.CellInfoCdma; @@ -36,7 +39,6 @@ License, or (at your option) any later version. import android.telephony.CellInfoWcdma; import android.telephony.CellLocation; import android.telephony.NeighboringCellInfo; -import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.telephony.cdma.CdmaCellLocation; import android.telephony.gsm.GsmCellLocation; @@ -56,6 +58,9 @@ License, or (at your option) any later version. import java.util.HashMap; import java.util.List; import java.util.Map; +import org.openbmap.unifiedNlp.utils.LogToFile; + +import static org.openbmap.unifiedNlp.utils.LogToFile.appendLog; @SuppressLint("NewApi") public class RadiocellsOrgNlpService extends LocationBackendService implements ILocationCallback { @@ -85,8 +90,10 @@ public class RadiocellsOrgNlpService extends LocationBackendService implements I * Phone state listeners to receive cell updates */ private TelephonyManager mTelephonyManager; - private PhoneStateListener mPhoneListener; - + private PowerManager powerManager; + private PowerManager.WakeLock wakeLock; + private AlarmManager alarmManager; + WifiLock mWifiLock; /** @@ -100,8 +107,6 @@ public class RadiocellsOrgNlpService extends LocationBackendService implements I private ILocationProvider mGeocoder; - private boolean running; - /** * Time of last geolocation request (millis) */ @@ -112,7 +117,7 @@ public class RadiocellsOrgNlpService extends LocationBackendService implements I /** * Receives location updates as well as wifi scan result updates */ - private BroadcastReceiver mReceiver = new BroadcastReceiver() { + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(final Context context, final Intent intent) { @@ -160,6 +165,11 @@ public static Map TECHNOLOGY_MAP() { @Override protected void onOpen() { Log.i(TAG, "Opening " + TAG); + + LogToFile.logFilePathname = PreferenceManager.getDefaultSharedPreferences(this).getString(Preferences.KEY_DEBUG_FILE,""); + LogToFile.logToFileEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Preferences.KEY_DEBUG_TO_FILE, false); + LogToFile.logFileHoursOfLasting = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(this).getString(Preferences.KEY_DEBUG_FILE_LASTING_HOURS, "24")); + wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); mWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "SCAN_LOCK"); @@ -168,19 +178,19 @@ protected void onOpen() { } registerReceiver(mReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); - mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); - + mTelephonyManager = ((TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE)); + powerManager = ((PowerManager) this.getSystemService(Context.POWER_SERVICE)); + alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); + Log.d(TAG, "[Config] Debug Mode: " + PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Preferences.KEY_DEBUG_MESSAGES, Preferences.VAL_DEBUG_MESSAGES)); mDebug = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Preferences.KEY_DEBUG_MESSAGES, Preferences.VAL_DEBUG_MESSAGES); Log.d(TAG, "[Config] Operation Mode: " + PreferenceManager.getDefaultSharedPreferences(this).getString(Preferences.KEY_OPERATION_MODE, Preferences.VAL_OPERATION_MODE)); mOnlineMode = PreferenceManager.getDefaultSharedPreferences(this).getString(Preferences.KEY_OPERATION_MODE, Preferences.VAL_OPERATION_MODE).equals("online"); - running = true; } - + @Override protected void onClose() { - running = false; if (mWifiLock != null) { if (mWifiLock.isHeld()) { mWifiLock.release(); @@ -188,32 +198,54 @@ protected void onClose() { } } - unregisterReceiver(mReceiver); + unregisterReceiver(mReceiver); wifiManager = null; } - Handler timerHandler = new Handler(); - Runnable timerRunnable = new Runnable() { - - @Override - public void run() { - if (nextScanningAllowedFrom == null) { - return; - } - nextScanningAllowedFrom = null; - getLocationFromWifisAndCells(new ArrayList()); + @Override + public int onStartCommand(Intent intent, int flags, final int startId) { + int ret = super.onStartCommand(intent, flags, startId); + + if (intent == null) { + return ret; } - }; + + if (null != intent.getAction()) switch (intent.getAction()) { + case "org.openbmap.unifiedNlp.LOCATION_UPDATE_CELLS_ONLY": + appendLog(TAG, "LOCATION_UPDATE_CELLS_ONLY:nextScanningAllowedFrom:" + nextScanningAllowedFrom); + if (nextScanningAllowedFrom == null) { + return ret; + } + nextScanningAllowedFrom = null; + getLocationFromWifisAndCells(null); + break; + + default: + break; + } + return ret; + } + + private PendingIntent getIntentToGetCellsOnly() { + Intent intent = new Intent(getBaseContext(), RadiocellsOrgNlpService.class); + intent.setAction("org.openbmap.unifiedNlp.LOCATION_UPDATE_CELLS_ONLY"); + return PendingIntent.getService(getBaseContext(), + 0, + intent, + PendingIntent.FLAG_CANCEL_CURRENT); + } @Override protected Location update() { + appendLog(TAG, "call update()"); Calendar now = Calendar.getInstance(); - + if ((nextScanningAllowedFrom != null) && (nextScanningAllowedFrom.after(now))) { Log.v(TAG, "Another scan is taking place"); + appendLog(TAG, "update():Another scan is taking place"); return null; } - + appendLog(TAG, "update():mGeocoder:" + mGeocoder); if (mGeocoder == null) { if (mOnlineMode) { Log.i(TAG, "Using online geocoder"); @@ -223,15 +255,21 @@ protected Location update() { mGeocoder = new OfflineProvider(this, this); } } + appendLog(TAG, "update():isWifiSupported():" + isWifiSupported() + ":isWifisSourceSelected():" + isWifisSourceSelected() + ":" + isCellsSourceSelected()); if (isWifiSupported() && isWifisSourceSelected()) { //Log.i(TAG, "Scanning wifis"); + appendLog(TAG, "update():nextScanningAllowedFrom:" + nextScanningAllowedFrom); if(nextScanningAllowedFrom == null) { scanning = wifiManager.startScan(); nextScanningAllowedFrom = Calendar.getInstance(); - nextScanningAllowedFrom.add(Calendar.MINUTE, 5); - timerHandler.postDelayed(timerRunnable, 20000); + nextScanningAllowedFrom.add(Calendar.MINUTE, 1); } - + final PendingIntent intentToCancel = getIntentToGetCellsOnly(); + wakeUp(); + alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, + SystemClock.elapsedRealtime() + 8000, + intentToCancel); + appendLog(TAG, "update():alarm set"); /** * Processes scan results and sends query to geocoding service */ @@ -242,7 +280,9 @@ protected Location update() { public void onWifiResultsAvailable() { //Log.d(TAG, "Wifi results are available now."); nextScanningAllowedFrom = null; - timerHandler.removeCallbacks(timerRunnable); + //timerHandler.removeCallbacks(timerRunnable); + intentToCancel.cancel(); + alarmManager.cancel(intentToCancel); if (scanning) { // TODO pass wifi signal strength to geocoder //Log.i(TAG, "Wifi scan results arrived.."); @@ -263,6 +303,7 @@ public void onWifiResultsAvailable() { getLocationFromWifisAndCells(null); } else { Log.e(TAG, "Neigther cells nor wifis as source selected? Com'on.."); + appendLog(TAG, "update():Neigther cells nor wifis as source selected? Com'on.."); } return null; @@ -272,7 +313,7 @@ private void getLocationFromWifisAndCells(List scans) { final long passed = System.currentTimeMillis() - lastFix; final boolean ok_online = (mOnlineMode && (passed > ONLINE_REFRESH_INTERVAL) || lastFix == 0); final boolean ok_offline = (!mOnlineMode && (passed > OFFLINE_REFRESH_INTERVAL) || lastFix == 0); - + appendLog(TAG, "getLocationFromWifisAndCells():ok_online:" + ok_online + ":ok_offline:" + ok_offline); if (ok_online || ok_offline) { Log.d(TAG, "Scanning wifis & cells"); lastFix = System.currentTimeMillis(); @@ -282,6 +323,7 @@ private void getLocationFromWifisAndCells(List scans) { if (isCellsSourceSelected()) { cells = getCells(); } + appendLog(TAG, "getLocationFromWifisAndCells():mGeocoder:" + mGeocoder); if (mGeocoder != null) { mGeocoder.getLocation(scans, cells); } else { @@ -310,6 +352,45 @@ private boolean isCellsSourceSelected() { return (source.equals(Preferences.SOURCE_CELLS)) || (source.equals(Preferences.SOURCE_COMBINED)); } + private void wakeUp() { + appendLog(TAG, "powerManager:" + powerManager); + + String wakeUpStrategy = PreferenceManager.getDefaultSharedPreferences(this).getString(Preferences.KEY_WAKE_UP_STRATEGY, "nowakeup"); + + appendLog(TAG, "wakeLock:wakeUpStrategy:" + wakeUpStrategy); + + if ("nowakeup".equals(wakeUpStrategy)) { + return; + } + + int powerLockID; + + if ("wakeupfull".equals(wakeUpStrategy)) { + powerLockID = PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; + } else { + powerLockID = PowerManager.PARTIAL_WAKE_LOCK; + } + + appendLog(TAG, "wakeLock:powerLockID:" + powerLockID); + + boolean isInUse; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { + isInUse = powerManager.isInteractive(); + } else { + isInUse = powerManager.isScreenOn(); + } + + if (!isInUse) { + wakeLock = powerManager.newWakeLock(powerLockID, TAG); + appendLog(TAG, "wakeLock:" + wakeLock + ":" + wakeLock.isHeld()); + if (!wakeLock.isHeld()) { + wakeLock.acquire(); + } + appendLog(TAG, "wakeLock acquired"); + } + } + /** * Returns cells phone is currently connected to * @return list of cells @@ -317,6 +398,8 @@ private boolean isCellsSourceSelected() { private List getCells() { List cells = new ArrayList<>(); + wakeUp(); + String operator = mTelephonyManager.getNetworkOperator(); String mnc; int mcc; @@ -330,9 +413,11 @@ private List getCells() { mcc = 0; mnc = ""; } - + CellLocation cellLocation = mTelephonyManager.getCellLocation(); + appendLog(TAG, "getCells():cellLocation:" + cellLocation); + if (cellLocation != null) { if (cellLocation instanceof GsmCellLocation) { Cell cell = new Cell(); @@ -341,23 +426,29 @@ private List getCells() { cell.mcc = mcc; cell.mnc = mnc; cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); + appendLog(TAG, String.format("GsmCellLocation for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((GsmCellLocation) cellLocation).getPsc())); Log.d(TAG, String.format("GsmCellLocation for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((GsmCellLocation) cellLocation).getPsc())); cells.add(cell); } else if (cellLocation instanceof CdmaCellLocation) { - Log.w(TAG, "CdmaCellLocation: Using CDMA cells for NLP is not yet implemented"); - } else - Log.wtf(TAG, "Got a CellLocation of an unknown class"); + appendLog(TAG, "getCells():cellLocation - CdmaCellLocation: Using CDMA cells for NLP is not yet implemented"); + Log.w(TAG, "CdmaCellLocation: Using CDMA cells for NLP is not yet implemented"); + } else { + appendLog(TAG, "getCells():cellLocation - Got a CellLocation of an unknown class"); + Log.wtf(TAG, "Got a CellLocation of an unknown class"); + } } else { Log.d(TAG, "getCellLocation returned null"); } List neighboringCells = mTelephonyManager.getNeighboringCellInfo(); + appendLog(TAG, "getCells():neighboringCells:" + neighboringCells); if (neighboringCells != null) { + appendLog(TAG, "getCells():neighboringCells.size:" + neighboringCells.size()); Log.d(TAG, "getNeighboringCellInfo found " + neighboringCells.size() + " cells"); } else { Log.d(TAG, "getNeighboringCellInfo returned null"); } - + if (neighboringCells != null) { for (NeighboringCellInfo c : neighboringCells) { Cell cell = new Cell(); @@ -366,6 +457,7 @@ private List getCells() { cell.mcc = mcc; cell.mnc = mnc; cell.technology = TECHNOLOGY_MAP().get(c.getNetworkType()); + appendLog(TAG, String.format("getCells():NeighboringCellInfo for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, c.getPsc())); Log.d(TAG, String.format("NeighboringCellInfo for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, c.getPsc())); cells.add(cell); } @@ -373,56 +465,79 @@ private List getCells() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { List cellsRawList = mTelephonyManager.getAllCellInfo(); + appendLog(TAG, "getCells():getAllCellInfo:cellsRawList:" + cellsRawList); if (cellsRawList != null) { - Log.d(TAG, "getAllCellInfo found " + cellsRawList.size() + " cells"); + appendLog(TAG, "getCells():cellsRawList.size:" + cellsRawList.size()); + Log.d(TAG, "getAllCellInfo found " + cellsRawList.size() + " cells"); } else { - Log.d(TAG, "getAllCellInfo returned null"); + Log.d(TAG, "getAllCellInfo returned null"); } - if (cellsRawList != null) { - for (CellInfo c : cellsRawList) { - Cell cell = new Cell(); - if (c instanceof CellInfoGsm) { - //Log.v(TAG, "GSM cell found"); - cell.cellId = ((CellInfoGsm) c).getCellIdentity().getCid(); - cell.area = ((CellInfoGsm) c).getCellIdentity().getLac(); - cell.mcc = ((CellInfoGsm)c).getCellIdentity().getMcc(); - cell.mnc = String.valueOf(((CellInfoGsm)c).getCellIdentity().getMnc()); - cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); - Log.d(TAG, String.format("CellInfoGsm for %d|%s|%d|%d|%s", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology)); - } else if (c instanceof CellInfoCdma) { - /* - object.put("cellId", ((CellInfoCdma)s).getCellIdentity().getBasestationId()); - object.put("locationAreaCode", ((CellInfoCdma)s).getCellIdentity().getLac()); - object.put("mobileCountryCode", ((CellInfoCdma)s).getCellIdentity().get()); - object.put("mobileNetworkCode", ((CellInfoCdma)s).getCellIdentity().getMnc());*/ - Log.wtf(TAG, "Using of CDMA cells for NLP not yet implemented"); - } else if (c instanceof CellInfoLte) { - //Log.v(TAG, "LTE cell found"); - cell.cellId = ((CellInfoLte) c).getCellIdentity().getCi(); - cell.area = ((CellInfoLte) c).getCellIdentity().getTac(); - cell.mcc = ((CellInfoLte)c).getCellIdentity().getMcc(); - cell.mnc = String.valueOf(((CellInfoLte)c).getCellIdentity().getMnc()); - cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); - Log.d(TAG, String.format("CellInfoLte for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((CellInfoLte)c).getCellIdentity().getPci())); - } else if (c instanceof CellInfoWcdma) { - //Log.v(TAG, "CellInfoWcdma cell found"); - cell.cellId = ((CellInfoWcdma) c).getCellIdentity().getCid(); - cell.area = ((CellInfoWcdma) c).getCellIdentity().getLac(); - cell.mcc = ((CellInfoWcdma)c).getCellIdentity().getMcc(); - cell.mnc = String.valueOf(((CellInfoWcdma)c).getCellIdentity().getMnc()); - cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); - Log.d(TAG, String.format("CellInfoWcdma for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((CellInfoWcdma) c).getCellIdentity().getPsc())); - } - cells.add(cell); - } + if ((cellsRawList != null) && !cellsRawList.isEmpty()) { + processCellInfoList(cellsRawList, cells); } } else { Log.d(TAG, "getAllCellInfo is not available (requires API 17)"); } + + if (wakeLock != null) { + try { + appendLog(TAG, "getCells():releasing wakeLock"); + wakeLock.release(); + } catch (Throwable th) { + // ignoring this exception, probably wakeLock was already released + } + } + + appendLog(TAG, "getCells():return cells.size: " + cells.size()); return cells; } - + + private void processCellInfoList(List cellInfoList, List cells) { + for (CellInfo c : cellInfoList) { + Cell cell = new Cell(); + if (c instanceof CellInfoGsm) { + //Log.v(TAG, "GSM cell found"); + cell.cellId = ((CellInfoGsm) c).getCellIdentity().getCid(); + cell.area = ((CellInfoGsm) c).getCellIdentity().getLac(); + cell.mcc = ((CellInfoGsm)c).getCellIdentity().getMcc(); + cell.mnc = String.valueOf(((CellInfoGsm)c).getCellIdentity().getMnc()); + cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); + appendLog(TAG, String.format("CellInfoGsm for %d|%s|%d|%d|%s", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology)); + Log.d(TAG, String.format("CellInfoGsm for %d|%s|%d|%d|%s", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology)); + } else if (c instanceof CellInfoCdma) { + /* + object.put("cellId", ((CellInfoCdma)s).getCellIdentity().getBasestationId()); + object.put("locationAreaCode", ((CellInfoCdma)s).getCellIdentity().getLac()); + object.put("mobileCountryCode", ((CellInfoCdma)s).getCellIdentity().get()); + object.put("mobileNetworkCode", ((CellInfoCdma)s).getCellIdentity().getMnc());*/ + appendLog(TAG, ":Using of CDMA cells for NLP not yet implemented"); + Log.wtf(TAG, "Using of CDMA cells for NLP not yet implemented"); + } else if (c instanceof CellInfoLte) { + //Log.v(TAG, "LTE cell found"); + cell.cellId = ((CellInfoLte) c).getCellIdentity().getCi(); + cell.area = ((CellInfoLte) c).getCellIdentity().getTac(); + cell.mcc = ((CellInfoLte)c).getCellIdentity().getMcc(); + cell.mnc = String.valueOf(((CellInfoLte)c).getCellIdentity().getMnc()); + cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); + appendLog(TAG, String.format("CellInfoLte for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((CellInfoLte)c).getCellIdentity().getPci())); + Log.d(TAG, String.format("CellInfoLte for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((CellInfoLte)c).getCellIdentity().getPci())); + } else if (c instanceof CellInfoWcdma) { + //Log.v(TAG, "CellInfoWcdma cell found"); + cell.cellId = ((CellInfoWcdma) c).getCellIdentity().getCid(); + cell.area = ((CellInfoWcdma) c).getCellIdentity().getLac(); + cell.mcc = ((CellInfoWcdma)c).getCellIdentity().getMcc(); + cell.mnc = String.valueOf(((CellInfoWcdma)c).getCellIdentity().getMnc()); + cell.technology = TECHNOLOGY_MAP().get(mTelephonyManager.getNetworkType()); + appendLog(TAG, String.format("CellInfoWcdma for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((CellInfoWcdma) c).getCellIdentity().getPsc())); + Log.d(TAG, String.format("CellInfoWcdma for %d|%s|%d|%d|%s|%d", cell.mcc, cell.mnc, cell.area, cell.cellId, cell.technology, ((CellInfoWcdma) c).getCellIdentity().getPsc())); + } else { + appendLog(TAG, "CellInfo of unexpected type: " + c); + } + cells.add(cell); + } + } + /** * Checks whether we can scan wifis * @return true if wifi is enabled or background scanning allowed @@ -441,6 +556,8 @@ public boolean isWifiSupported() { public Location onLocationReceived(Location location) { if (location == null) { Log.i(TAG, "Location was null, ignoring"); + appendLog(TAG, " Location is null"); + report(null); return null; } @@ -451,10 +568,10 @@ public Location onLocationReceived(Location location) { Log.d(TAG, "[UnifiedNlp Results]: Est. Speed " + Math.round(location.distanceTo(last) / (location.getTime() - last.getTime() / 1000 / 60)) + " km/h"); } } - + report(location); last = location; return location; } -} \ No newline at end of file +} diff --git a/src/org/openbmap/unifiedNlp/utils/LogToFile.java b/src/org/openbmap/unifiedNlp/utils/LogToFile.java new file mode 100644 index 0000000..fe796e8 --- /dev/null +++ b/src/org/openbmap/unifiedNlp/utils/LogToFile.java @@ -0,0 +1,107 @@ +package org.openbmap.unifiedNlp.utils; + +import android.util.Log; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class LogToFile { + + private static final String TAG = LogToFile.class.getName(); + + private static final String TIME_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; + + private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(TIME_DATE_PATTERN); + + public static String logFilePathname; + public static boolean logToFileEnabled; + public static int logFileHoursOfLasting; + private static Calendar logFileAtTheEndOfLive; + + public static void appendLog(String tag, String text) { + appendLog(tag, text, null); + } + + public static void appendLog(String tag, String text, Throwable throwable) { + + if (!logToFileEnabled || (logFilePathname == null)) { + return; + } + + File logFile = new File(logFilePathname); + + Date now = new Date(); + try { + if (logFile.exists()) { + if (logFileAtTheEndOfLive == null) { + boolean succeeded = initFileLogging(logFile); + if (!succeeded) { + createNewLogFile(logFile, now); + } + } else if(Calendar.getInstance().after(logFileAtTheEndOfLive)) { + logFile.delete(); + createNewLogFile(logFile, now); + } + } else { + createNewLogFile(logFile, now); + } + BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); + buf.append(DATE_FORMATTER.format(now)); + buf.append(" "); + buf.append(tag); + buf.append(" - "); + buf.append(text); + if (throwable != null) { + buf.append(" - "); + buf.append(throwable.getMessage()); + for (StackTraceElement ste: throwable.getStackTrace()) { + buf.newLine(); + buf.append(ste.toString()); + } + } + buf.newLine(); + buf.close(); + } catch (IOException e) { + Log.e(TAG, e.getMessage()); + } + } + + private static boolean initFileLogging(File logFile) { + char[] logFileDateCreatedBytes = new char[TIME_DATE_PATTERN.length()]; + Date logFileDateCreated; + FileReader logFileReader = null; + try { + logFileReader = new FileReader(logFile); + logFileReader.read(logFileDateCreatedBytes); + logFileDateCreated = DATE_FORMATTER.parse(new String(logFileDateCreatedBytes)); + } catch (Exception e) { + return false; + } finally { + if (logFileReader != null) { + try { + logFileReader.close(); + } catch (IOException ex) { + + } + } + } + initEndOfLive(logFileDateCreated); + return true; + } + + private static void initEndOfLive(Date logFileDateCreated) { + logFileAtTheEndOfLive = Calendar.getInstance(); + logFileAtTheEndOfLive.setTime(logFileDateCreated); + logFileAtTheEndOfLive.add(Calendar.HOUR_OF_DAY, logFileHoursOfLasting); + } + + private static void createNewLogFile(File logFile, Date dateOfCreation) throws IOException { + logFile.createNewFile(); + initEndOfLive(dateOfCreation); + } +}