From 9148c0b6cf98b6a2eb328b5acfea0f98b682b4cd Mon Sep 17 00:00:00 2001 From: Felix Knecht Date: Fri, 28 Nov 2014 14:49:22 +0800 Subject: [PATCH 01/10] Added support for offline search Change-Id: I6371df8e092905e2585dfd30ffa93cdf491e4f6f --- AndroidManifest.xml | 17 ++- README.md | 7 +- gen_openwifimap_db.sh | 45 ++++++ res/values/strings.xml | 7 + res/xml/settings.xml | 20 +++ .../backend/openwlanmap/BackendService.java | 102 +++++++++++++- .../backend/openwlanmap/Configuration.java | 44 ++++++ .../openwlanmap/local/PrefsFragment.java | 53 +++++++ .../backend/openwlanmap/local/Settings.java | 16 +++ .../openwlanmap/local/WifiLocationFile.java | 130 ++++++++++++++++++ .../openwlanmap/local/WifiReceiver.java | 70 ++++++++++ 11 files changed, 502 insertions(+), 9 deletions(-) create mode 100755 gen_openwifimap_db.sh create mode 100644 res/xml/settings.xml create mode 100644 src/org/microg/nlp/backend/openwlanmap/Configuration.java create mode 100644 src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java create mode 100644 src/org/microg/nlp/backend/openwlanmap/local/Settings.java create mode 100644 src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java create mode 100644 src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ac24a0b..350952d 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,11 +1,15 @@ + package="org.microg.nlp.backend.openwlanmap" + android:versionCode="2" + android:versionName="0.0.2" + > + + - + + + + + + diff --git a/README.md b/README.md index 06c5abd..88d2d38 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,12 @@ OpenWlanMapNlpBackend ===================== [UnifiedNlp](https://github.com/microg/android_packages_apps_UnifiedNlp) backend that uses [OpenWlanMap](http://www.openwlanmap.org/) to resolve user location. -Location calculation is done online and therefor requires internet connection. +Location calculation is done either online or offline. This can be switched in the settings. +Online calculation of course requires internet connection. +Offline calculation won't use any data, but look up the wifi access points in a database on your sd-card only. +To generate the database a shell script (gen_openwifimap_db.sh) is included. + +To contribute to the OpenWlanMap database you can use the available Android app or upload your "wardriving" data manually [here](https://openwlanmap.org/upload.php?lang=). Don't forget to enable the "Publish own data" in the Android apps settings! Building -------- diff --git a/gen_openwifimap_db.sh b/gen_openwifimap_db.sh new file mode 100755 index 0000000..11cc947 --- /dev/null +++ b/gen_openwifimap_db.sh @@ -0,0 +1,45 @@ +#! /bin/bash +# +# Quick and dirty script to build and install a new +# wifi APs location database on a phone for microg/nogapps +# OpenWlanMapNlpBackend. +# + + +# +# Get latest wifi AP locations from OpenWLANMap.org +# +echo 'Getting wifi AP locations from OpenWLANMap.org' +if [ -e db.tar.bz2 ] ; then + rm db.tar.bz2 +fi +if [ -e db.csv ] ; then + mv -f db.csv db.csv.bak +fi +wget "http://openwlanmap.org/db.tar.bz2" +tar --strip-components=1 -xjf db.tar.bz2 db/db.csv + + +echo 'Building database file' +if [ -e openwifimap.db ] ; then + mv -f openwifimap.db openwifimap.db.bak +fi + +### TODO: Filter all entries with lat or long = 0 +### Those are the _nomap entries + +sqlite3 openwifimap.db < OpenWlanMapNlpBackend + Network + Allow network activity + Local + Database location + Assumed accuracy for database in meters + + The supplied value for assumed accuracy is not a float \ No newline at end of file diff --git a/res/xml/settings.xml b/res/xml/settings.xml new file mode 100644 index 0000000..1902a0a --- /dev/null +++ b/res/xml/settings.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/org/microg/nlp/backend/openwlanmap/BackendService.java b/src/org/microg/nlp/backend/openwlanmap/BackendService.java index be1f77a..b700a49 100644 --- a/src/org/microg/nlp/backend/openwlanmap/BackendService.java +++ b/src/org/microg/nlp/backend/openwlanmap/BackendService.java @@ -1,40 +1,92 @@ package org.microg.nlp.backend.openwlanmap; +import java.util.ArrayList; +import java.util.List; + +import org.microg.nlp.api.LocationBackendService; +import org.microg.nlp.api.LocationHelper; +import org.microg.nlp.backend.openwlanmap.local.WifiLocationFile; +import org.microg.nlp.backend.openwlanmap.local.WifiReceiver; +import org.microg.nlp.backend.openwlanmap.local.WifiReceiver.WifiReceivedCallback; + import android.content.Context; +import android.content.IntentFilter; +import android.content.SharedPreferences; import android.location.Location; +import android.net.wifi.WifiManager; +import android.preference.PreferenceManager; import android.util.Log; + import com.vwp.libwlocate.WLocate; -import org.microg.nlp.api.LocationBackendService; -import org.microg.nlp.api.LocationHelper; public class BackendService extends LocationBackendService { private static final String TAG = BackendService.class.getName(); private WLocate wLocate; + private WifiLocationFile wifiLocationFile; + private WifiReceiver wifiReceiver; @Override protected void onOpen() { - if (wLocate == null) { - wLocate = new MyWLocate(this); + Log.d(TAG, "onOpen"); + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + Configuration.fillFromPrefs(sharedPrefs); + sharedPrefs.registerOnSharedPreferenceChangeListener(Configuration.listener); + + if (Configuration.networkAllowed) { + if (wLocate == null) { + wLocate = new MyWLocate(this); + } else { + wLocate.doResume(); + } } else { - wLocate.doResume(); + openDatabase(); + if (wifiReceiver == null) { + wifiReceiver = new WifiReceiver(this, new WifiDBResolver()); + } + registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } } @Override protected void onClose() { + Log.d(TAG, "onClose"); + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + sharedPrefs.unregisterOnSharedPreferenceChangeListener(Configuration.listener); + if (wLocate != null) { wLocate.doPause(); } + if (wifiReceiver != null) { + unregisterReceiver(wifiReceiver); + } + } @Override protected Location update() { + Log.d(TAG, "update"); if (wLocate != null) { + Log.d(TAG, "Requesting location from net"); wLocate.wloc_request_position(WLocate.FLAG_NO_GPS_ACCESS); + return null; + } + + if (wifiReceiver != null) { + Log.d(TAG, "Requesting location from db"); + wifiReceiver.startScan(); } + return null; } + private void openDatabase() { + if (wifiLocationFile == null) { + wifiLocationFile = new WifiLocationFile(); + } + } + private class MyWLocate extends WLocate { public MyWLocate(Context ctx) throws IllegalArgumentException { @@ -53,4 +105,42 @@ protected void wloc_return_position(int ret, double lat, double lon, float radiu } } } -} + + private class WifiDBResolver implements WifiReceivedCallback { + + @Override + public void process(List foundBssids) { + + if (foundBssids == null || foundBssids.isEmpty()) { + return; + } + if (wifiLocationFile != null) { + + List locations = new ArrayList(foundBssids.size()); + + for (String bssid : foundBssids) { + Location result = wifiLocationFile.query(bssid); + if (result != null) { + locations.add(result); + } + } + + if (locations.isEmpty()) { + return; + } + + //TODO fix LocationHelper:average to not calculate with null values + //TODO sort out wifis obviously in the wrong spot + Location avgLoc = LocationHelper.average("owm", locations); + + if (avgLoc == null) { + Log.e(TAG, "Averaging locations did not work."); + return; + } + + Log.d(TAG, "Reporting location: " + avgLoc.toString()); + report(avgLoc); + } + } + } +} \ No newline at end of file diff --git a/src/org/microg/nlp/backend/openwlanmap/Configuration.java b/src/org/microg/nlp/backend/openwlanmap/Configuration.java new file mode 100644 index 0000000..3e6e134 --- /dev/null +++ b/src/org/microg/nlp/backend/openwlanmap/Configuration.java @@ -0,0 +1,44 @@ +package org.microg.nlp.backend.openwlanmap; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.os.Environment; +import android.util.Log; + +public class Configuration { + private static String TAG = Configuration.class.getName(); + + public static boolean networkAllowed; + + public static String dbLocation = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.nogapps/openwifimap.db"; + + public static float assumedAccuracy; + + public static ConfigChangedListener listener = new ConfigChangedListener(); + + + public static void fillFromPrefs(SharedPreferences sharedPrefs) { + + networkAllowed = sharedPrefs.getBoolean("networkAllowed", false); + Log.d(TAG, "Network allowed: " + networkAllowed); + + dbLocation = sharedPrefs.getString("databaseLocation", Environment.getExternalStorageDirectory().getAbsolutePath() + + "/.nogapps/openwifimap.db"); + + try { + assumedAccuracy = Float.parseFloat(sharedPrefs.getString("assumedAccuracy", "50")); + } catch (NumberFormatException e) { + assumedAccuracy = 50; + } + } + + private static class ConfigChangedListener implements OnSharedPreferenceChangeListener { + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, + String key) { + fillFromPrefs(sharedPreferences); + } + } + +} diff --git a/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java b/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java new file mode 100644 index 0000000..b272c13 --- /dev/null +++ b/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java @@ -0,0 +1,53 @@ +package org.microg.nlp.backend.openwlanmap.local; + +import org.microg.nlp.backend.openwlanmap.R; + +import android.os.Bundle; +import android.os.Environment; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; +import android.preference.Preference; +import android.preference.PreferenceCategory; +import android.preference.PreferenceFragment; + +public class PrefsFragment extends PreferenceFragment { + + public PrefsFragment() { + super(); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + + CheckBoxPreference allowNetwork = (CheckBoxPreference) this.findPreference("networkAllowed"); + allowNetwork.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + + return switchLocalGroup((Boolean) newValue); + } + }); + //get initial state right + switchLocalGroup(allowNetwork.isChecked()); + + + EditTextPreference dbLocPreference = (EditTextPreference) this.findPreference("databaseLocation"); + if (dbLocPreference != null) { + //defaultValue doesn't work very well from code so we fill the pref this way + if (dbLocPreference.getText() == null || dbLocPreference.getText().isEmpty()) { + dbLocPreference.setText(Environment.getExternalStorageDirectory().getAbsolutePath() + "/.nogapps/openwifimap.db"); + } + } + } + + private boolean switchLocalGroup(boolean networkAllowed) { + + PreferenceCategory localCategory = (PreferenceCategory) PrefsFragment.this.findPreference("category_local"); + localCategory.setEnabled(!networkAllowed); + + return true; + } +} diff --git a/src/org/microg/nlp/backend/openwlanmap/local/Settings.java b/src/org/microg/nlp/backend/openwlanmap/local/Settings.java new file mode 100644 index 0000000..5600b8c --- /dev/null +++ b/src/org/microg/nlp/backend/openwlanmap/local/Settings.java @@ -0,0 +1,16 @@ +package org.microg.nlp.backend.openwlanmap.local; + +import android.app.Activity; +import android.os.Bundle; + +public class Settings extends Activity { + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + getFragmentManager().beginTransaction().replace(android.R.id.content, + new PrefsFragment()).commit(); + } +} diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java new file mode 100644 index 0000000..4d18436 --- /dev/null +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java @@ -0,0 +1,130 @@ +package org.microg.nlp.backend.openwlanmap.local; + +import java.io.File; + +import org.microg.nlp.backend.openwlanmap.Configuration; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.location.Location; +import android.util.Log; +import android.util.LruCache; + +public class WifiLocationFile { + private static final String TABLE_APS = "APs"; + private static final String COL_BSSSID = "bssid"; + private static final String COL_LATITUDE = "latitude"; + private static final String COL_LONGITUDE = "longitude"; + private static File file; + private SQLiteDatabase database; + + protected String TAG = WifiLocationFile.class.getName(); + + + public WifiLocationFile() { + openDatabase(); + } + + /** + * DB negative query cache (not found in db). + */ + private final LruCache queryResultNegativeCache = + new LruCache(1000); + /** + * DB positive query cache (found in the db). + */ + private final LruCache queryResultCache = + new LruCache(1000); + + + private void openDatabase() { + if (database == null) { + file = new File(Configuration.dbLocation); + if (file.exists() && file.canRead()) { + database = SQLiteDatabase.openDatabase(file.getAbsolutePath(), + null, + SQLiteDatabase.NO_LOCALIZED_COLLATORS); + } else { + Log.e(TAG, "Could not open database at " + Configuration.dbLocation); + database = null; + } + } + } + + public void close() { + if (database != null) { + database.close(); + database = null; + } + } + + public boolean exists() { + return file.exists() && file.canRead(); + } + + public String getPath() { + return file.getAbsolutePath(); + } + + public synchronized Location query(final String bssid) { + + String normalizedBssid = bssid.replace(":", ""); + + Log.d(TAG, "Searching for BSSID '" + normalizedBssid + "'"); + + Boolean negative = queryResultNegativeCache.get(normalizedBssid); + if (negative != null && negative.booleanValue()) return null; + + Location cached = queryResultCache.get(normalizedBssid); + if (cached != null) return cached; + + //openDatabase(); + if (database == null) { + Log.d(TAG, "Unable to open wifi database file."); + return null; + } + + Location result = null; + + Cursor cursor = + database.query(TABLE_APS, + new String[]{COL_LATITUDE, + COL_LONGITUDE}, + COL_BSSSID + "=?", + new String[]{normalizedBssid}, + null, + null, + null); + if (cursor != null) { + Log.d(TAG,"Database contains " + cursor.getCount() + " entries"); + try { + if (cursor.getCount() > 0) { + cursor.moveToNext(); + + result = new Location("owm"); + result.setLatitude(cursor.getDouble(cursor.getColumnIndexOrThrow(COL_LATITUDE))); + result.setLongitude(cursor.getDouble(cursor.getColumnIndexOrThrow(COL_LONGITUDE))); + result.setAccuracy(Configuration.assumedAccuracy); + + if (result.getLatitude() == 0 || result.getLongitude() == 0) { + Log.d(TAG, "BSSID '" + bssid + "' returns 0 values for lat or long. Skipped."); + queryResultNegativeCache.put(normalizedBssid, true); + return null; + } + + queryResultCache.put(normalizedBssid, result); + Log.d(TAG,"Wifi info found for: " + normalizedBssid); + + return result; + } + } finally { + cursor.close(); + } + + + } + Log.d(TAG,"No Wifi info found for: " + normalizedBssid); + queryResultNegativeCache.put(normalizedBssid, true); + return null; + } +} diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java new file mode 100644 index 0000000..5472e6f --- /dev/null +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java @@ -0,0 +1,70 @@ +package org.microg.nlp.backend.openwlanmap.local; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.wifi.ScanResult; +import android.net.wifi.WifiManager; +import android.util.Log; + +public class WifiReceiver extends BroadcastReceiver { + + private boolean scanStarted = false; + private WifiManager wifi; + private String TAG = WifiReceiver.class.getName(); + private WifiReceivedCallback callback; + + public WifiReceiver(Context ctx, WifiReceivedCallback aCallback) { + wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE); + callback = aCallback; + } + + public void onReceive(Context c, Intent intent) { + if (!isScanStarted()) + return; + setScanStarted(false); + List configs = wifi.getScanResults(); + + Log.d(TAG, "Got " + configs.size() + " wifi access points"); + + if (configs.size() > 0) { + + List foundBssids = new ArrayList(configs.size()); + + for (ScanResult config : configs) { + // some strange devices use a dot instead of : + foundBssids.add(config.BSSID.toUpperCase(Locale.US).replace(".",":")); + } + + callback.process(foundBssids); + } + + } + + public boolean isScanStarted() { + return scanStarted; + } + + public void setScanStarted(boolean scanStarted) { + this.scanStarted = scanStarted; + } + + + public interface WifiReceivedCallback { + + void process(List foundBssids); + + } + + public void startScan() { + setScanStarted(true); + if (!wifi.isWifiEnabled() && !wifi.isScanAlwaysAvailable()) { + Log.d(TAG, "Wifi is disabled and we can't scan either. Not doing anything."); + } + wifi.startScan(); + } +} \ No newline at end of file From 05b7110f83f12d7ec113f145694a2945c78fd5fc Mon Sep 17 00:00:00 2001 From: Felix Knecht Date: Fri, 28 Nov 2014 19:19:07 +0800 Subject: [PATCH 02/10] Added changelog Change-Id: I3d77c5b36bd256448216a7546e8a38f7684fc519 --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 88d2d38..18c7993 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,13 @@ Used libraries - [libwlocate](http://sourceforge.net/projects/libwlocate/) (included) +Changes +------- + +0.0.2 - Felix Knecht added offline support + +0.0.1 - Initial version by @mar-v-in with online support + License ------- libwlocate is GPLv3, so is OpenWlanMapNlpBackend. From 13d9dc9989f0ee50bfc46b857ea8fd4e3cc524e1 Mon Sep 17 00:00:00 2001 From: Tod Fitch Date: Sat, 6 Dec 2014 11:34:54 -0800 Subject: [PATCH 03/10] Add filtering by lat,lon to gen script Signed-off-by: Tod Fitch --- gen_openwifimap_db.sh | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/gen_openwifimap_db.sh b/gen_openwifimap_db.sh index 11cc947..2432356 100755 --- a/gen_openwifimap_db.sh +++ b/gen_openwifimap_db.sh @@ -5,10 +5,38 @@ # OpenWlanMapNlpBackend. # +MAX_LAT="90" +MIN_LAT="-90" +MAX_LON="180" +MIN_LON="-180" + +function usage { + echo "Calling Sequence:" + echo "${0} [options]" + echo " Options:" + echo " -nDD -(North) Maximum latitude" + echo " -sDD -(South) Minimum latitude" + echo " -eDD -(East) Maximum longitude" + echo " -nDD -(West) Minimum latitude" + exit +} + +#Process the arguments +while getopts n:s:e:w: opt +do + case "$opt" in + n) MAX_LAT=$OPTARG;; + s) MIN_LAT=$OPTARG;; + e) MAX_LON=$OPTARG;; + w) MIN_LON=$OPTARG;; + \?) usage;; + esac +done # # Get latest wifi AP locations from OpenWLANMap.org # + echo 'Getting wifi AP locations from OpenWLANMap.org' if [ -e db.tar.bz2 ] ; then rm db.tar.bz2 @@ -19,7 +47,6 @@ fi wget "http://openwlanmap.org/db.tar.bz2" tar --strip-components=1 -xjf db.tar.bz2 db/db.csv - echo 'Building database file' if [ -e openwifimap.db ] ; then mv -f openwifimap.db openwifimap.db.bak @@ -33,13 +60,17 @@ CREATE TABLE APs(bssid STRING, latitude REAL, longitude REAL); .mode csv .separator "\t" .import db.csv APs +DELETE FROM APs WHERE latitude>${MAX_LAT}; +DELETE FROM APs WHERE latitude<${MIN_LAT}; +DELETE FROM APs WHERE longitude>${MAX_LON}; +DELETE FROM APs WHERE longitude<${MIN_LON}; CREATE INDEX _idx1 ON APs (bssid); VACUUM; .quit ! # -# Push the new database to the phone. +# Push the new database to the phone. # echo 'Pushing database to phone' adb push openwifimap.db /sdcard/.nogapps/openwifimap.db From 7c5400eabf58580fc3a146347c9d0a1dc989dea7 Mon Sep 17 00:00:00 2001 From: Tod Fitch Date: Tue, 9 Dec 2014 08:27:39 -0800 Subject: [PATCH 04/10] Moke accesible only through NLP settings. Signed-off-by: Tod Fitch --- AndroidManifest.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 350952d..bc3419e 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionCode="2" android:versionName="0.0.2" > - + @@ -31,7 +31,6 @@ android:label="OpenWlanMap Backend"> - From feb6a7ed3e23c65413625a10f27a1c333108de1c Mon Sep 17 00:00:00 2001 From: Tod Fitch Date: Tue, 9 Dec 2014 08:28:31 -0800 Subject: [PATCH 05/10] Kinder, gentler transition to new database file. Signed-off-by: Tod Fitch --- gen_openwifimap_db.sh | 3 +- .../openwlanmap/local/WifiLocationFile.java | 33 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/gen_openwifimap_db.sh b/gen_openwifimap_db.sh index 2432356..b5e89e9 100755 --- a/gen_openwifimap_db.sh +++ b/gen_openwifimap_db.sh @@ -73,4 +73,5 @@ VACUUM; # Push the new database to the phone. # echo 'Pushing database to phone' -adb push openwifimap.db /sdcard/.nogapps/openwifimap.db +adb push openwifimap.db /sdcard/.nogapps/openwifimap.db.x +adb shell mv /sdcard/.nogapps/openwifimap.db.x /sdcard/.nogapps/openwifimap.db.new diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java index 4d18436..ce86cbf 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java @@ -24,16 +24,16 @@ public class WifiLocationFile { public WifiLocationFile() { openDatabase(); } - + /** * DB negative query cache (not found in db). */ - private final LruCache queryResultNegativeCache = + private LruCache queryResultNegativeCache = new LruCache(1000); /** * DB positive query cache (found in the db). */ - private final LruCache queryResultCache = + private LruCache queryResultCache = new LruCache(1000); @@ -66,12 +66,27 @@ public String getPath() { return file.getAbsolutePath(); } + private void checkForNewDb() { + File newDbFile = new File(Configuration.dbLocation + ".new"); + if (newDbFile.exists() && newDbFile.canRead()) { + Log.d(TAG, "New database file detected."); + this.close(); + queryResultCache = new LruCache(1000); + queryResultNegativeCache = new LruCache(1000); + file.renameTo(new File(Configuration.dbLocation + ".bak")); + newDbFile.renameTo(new File(Configuration.dbLocation)); + openDatabase(); + } + } + public synchronized Location query(final String bssid) { - + + checkForNewDb(); + String normalizedBssid = bssid.replace(":", ""); - + Log.d(TAG, "Searching for BSSID '" + normalizedBssid + "'"); - + Boolean negative = queryResultNegativeCache.get(normalizedBssid); if (negative != null && negative.booleanValue()) return null; @@ -111,17 +126,17 @@ public synchronized Location query(final String bssid) { queryResultNegativeCache.put(normalizedBssid, true); return null; } - + queryResultCache.put(normalizedBssid, result); Log.d(TAG,"Wifi info found for: " + normalizedBssid); - + return result; } } finally { cursor.close(); } - + } Log.d(TAG,"No Wifi info found for: " + normalizedBssid); queryResultNegativeCache.put(normalizedBssid, true); From c76e1f98077b9a45d0be3bd68624ae49adbcb77b Mon Sep 17 00:00:00 2001 From: Tod Fitch Date: Tue, 9 Dec 2014 08:55:55 -0800 Subject: [PATCH 06/10] Formatting only: Use tabs consistently. Signed-off-by: Tod Fitch --- .../openwlanmap/local/PrefsFragment.java | 50 ++-- .../backend/openwlanmap/local/Settings.java | 12 +- .../openwlanmap/local/WifiLocationFile.java | 262 +++++++++--------- 3 files changed, 162 insertions(+), 162 deletions(-) diff --git a/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java b/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java index b272c13..d734c01 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java @@ -15,39 +15,39 @@ public class PrefsFragment extends PreferenceFragment { public PrefsFragment() { super(); } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.settings); - - CheckBoxPreference allowNetwork = (CheckBoxPreference) this.findPreference("networkAllowed"); - allowNetwork.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + + CheckBoxPreference allowNetwork = (CheckBoxPreference) this.findPreference("networkAllowed"); + allowNetwork.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - + return switchLocalGroup((Boolean) newValue); } }); - //get initial state right - switchLocalGroup(allowNetwork.isChecked()); - - - EditTextPreference dbLocPreference = (EditTextPreference) this.findPreference("databaseLocation"); - if (dbLocPreference != null) { - //defaultValue doesn't work very well from code so we fill the pref this way - if (dbLocPreference.getText() == null || dbLocPreference.getText().isEmpty()) { - dbLocPreference.setText(Environment.getExternalStorageDirectory().getAbsolutePath() + "/.nogapps/openwifimap.db"); - } - } - } - + //get initial state right + switchLocalGroup(allowNetwork.isChecked()); + + + EditTextPreference dbLocPreference = (EditTextPreference) this.findPreference("databaseLocation"); + if (dbLocPreference != null) { + //defaultValue doesn't work very well from code so we fill the pref this way + if (dbLocPreference.getText() == null || dbLocPreference.getText().isEmpty()) { + dbLocPreference.setText(Environment.getExternalStorageDirectory().getAbsolutePath() + "/.nogapps/openwifimap.db"); + } + } + } + private boolean switchLocalGroup(boolean networkAllowed) { - + PreferenceCategory localCategory = (PreferenceCategory) PrefsFragment.this.findPreference("category_local"); localCategory.setEnabled(!networkAllowed); - + return true; } } diff --git a/src/org/microg/nlp/backend/openwlanmap/local/Settings.java b/src/org/microg/nlp/backend/openwlanmap/local/Settings.java index 5600b8c..16d77e7 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/Settings.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/Settings.java @@ -6,11 +6,11 @@ public class Settings extends Activity { - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - getFragmentManager().beginTransaction().replace(android.R.id.content, - new PrefsFragment()).commit(); - } + getFragmentManager().beginTransaction().replace(android.R.id.content, + new PrefsFragment()).commit(); + } } diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java index ce86cbf..fdb69d8 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java @@ -11,135 +11,135 @@ import android.util.LruCache; public class WifiLocationFile { - private static final String TABLE_APS = "APs"; - private static final String COL_BSSSID = "bssid"; - private static final String COL_LATITUDE = "latitude"; - private static final String COL_LONGITUDE = "longitude"; - private static File file; - private SQLiteDatabase database; - - protected String TAG = WifiLocationFile.class.getName(); - - - public WifiLocationFile() { - openDatabase(); - } - - /** - * DB negative query cache (not found in db). - */ - private LruCache queryResultNegativeCache = - new LruCache(1000); - /** - * DB positive query cache (found in the db). - */ - private LruCache queryResultCache = - new LruCache(1000); - - - private void openDatabase() { - if (database == null) { - file = new File(Configuration.dbLocation); - if (file.exists() && file.canRead()) { - database = SQLiteDatabase.openDatabase(file.getAbsolutePath(), - null, - SQLiteDatabase.NO_LOCALIZED_COLLATORS); - } else { - Log.e(TAG, "Could not open database at " + Configuration.dbLocation); - database = null; - } - } - } - - public void close() { - if (database != null) { - database.close(); - database = null; - } - } - - public boolean exists() { - return file.exists() && file.canRead(); - } - - public String getPath() { - return file.getAbsolutePath(); - } - - private void checkForNewDb() { - File newDbFile = new File(Configuration.dbLocation + ".new"); - if (newDbFile.exists() && newDbFile.canRead()) { - Log.d(TAG, "New database file detected."); - this.close(); - queryResultCache = new LruCache(1000); - queryResultNegativeCache = new LruCache(1000); - file.renameTo(new File(Configuration.dbLocation + ".bak")); - newDbFile.renameTo(new File(Configuration.dbLocation)); - openDatabase(); - } - } - - public synchronized Location query(final String bssid) { - - checkForNewDb(); - - String normalizedBssid = bssid.replace(":", ""); - - Log.d(TAG, "Searching for BSSID '" + normalizedBssid + "'"); - - Boolean negative = queryResultNegativeCache.get(normalizedBssid); - if (negative != null && negative.booleanValue()) return null; - - Location cached = queryResultCache.get(normalizedBssid); - if (cached != null) return cached; - - //openDatabase(); - if (database == null) { - Log.d(TAG, "Unable to open wifi database file."); - return null; - } - - Location result = null; - - Cursor cursor = - database.query(TABLE_APS, - new String[]{COL_LATITUDE, - COL_LONGITUDE}, - COL_BSSSID + "=?", - new String[]{normalizedBssid}, - null, - null, - null); - if (cursor != null) { - Log.d(TAG,"Database contains " + cursor.getCount() + " entries"); - try { - if (cursor.getCount() > 0) { - cursor.moveToNext(); - - result = new Location("owm"); - result.setLatitude(cursor.getDouble(cursor.getColumnIndexOrThrow(COL_LATITUDE))); - result.setLongitude(cursor.getDouble(cursor.getColumnIndexOrThrow(COL_LONGITUDE))); - result.setAccuracy(Configuration.assumedAccuracy); - - if (result.getLatitude() == 0 || result.getLongitude() == 0) { - Log.d(TAG, "BSSID '" + bssid + "' returns 0 values for lat or long. Skipped."); - queryResultNegativeCache.put(normalizedBssid, true); - return null; - } - - queryResultCache.put(normalizedBssid, result); - Log.d(TAG,"Wifi info found for: " + normalizedBssid); - - return result; - } - } finally { - cursor.close(); - } - - - } - Log.d(TAG,"No Wifi info found for: " + normalizedBssid); - queryResultNegativeCache.put(normalizedBssid, true); - return null; - } + private static final String TABLE_APS = "APs"; + private static final String COL_BSSSID = "bssid"; + private static final String COL_LATITUDE = "latitude"; + private static final String COL_LONGITUDE = "longitude"; + private static File file; + private SQLiteDatabase database; + + protected String TAG = WifiLocationFile.class.getName(); + + + public WifiLocationFile() { + openDatabase(); + } + + /** + * DB negative query cache (not found in db). + */ + private LruCache queryResultNegativeCache = + new LruCache(1000); + /** + * DB positive query cache (found in the db). + */ + private LruCache queryResultCache = + new LruCache(1000); + + + private void openDatabase() { + if (database == null) { + file = new File(Configuration.dbLocation); + if (file.exists() && file.canRead()) { + database = SQLiteDatabase.openDatabase(file.getAbsolutePath(), + null, + SQLiteDatabase.NO_LOCALIZED_COLLATORS); + } else { + Log.e(TAG, "Could not open database at " + Configuration.dbLocation); + database = null; + } + } + } + + public void close() { + if (database != null) { + database.close(); + database = null; + } + } + + public boolean exists() { + return file.exists() && file.canRead(); + } + + public String getPath() { + return file.getAbsolutePath(); + } + + private void checkForNewDb() { + File newDbFile = new File(Configuration.dbLocation + ".new"); + if (newDbFile.exists() && newDbFile.canRead()) { + Log.d(TAG, "New database file detected."); + this.close(); + queryResultCache = new LruCache(1000); + queryResultNegativeCache = new LruCache(1000); + file.renameTo(new File(Configuration.dbLocation + ".bak")); + newDbFile.renameTo(new File(Configuration.dbLocation)); + openDatabase(); + } + } + + public synchronized Location query(final String bssid) { + + checkForNewDb(); + + String normalizedBssid = bssid.replace(":", ""); + + Log.d(TAG, "Searching for BSSID '" + normalizedBssid + "'"); + + Boolean negative = queryResultNegativeCache.get(normalizedBssid); + if (negative != null && negative.booleanValue()) return null; + + Location cached = queryResultCache.get(normalizedBssid); + if (cached != null) return cached; + + //openDatabase(); + if (database == null) { + Log.d(TAG, "Unable to open wifi database file."); + return null; + } + + Location result = null; + + Cursor cursor = + database.query(TABLE_APS, + new String[]{COL_LATITUDE, + COL_LONGITUDE}, + COL_BSSSID + "=?", + new String[]{normalizedBssid}, + null, + null, + null); + if (cursor != null) { + Log.d(TAG,"Database contains " + cursor.getCount() + " entries"); + try { + if (cursor.getCount() > 0) { + cursor.moveToNext(); + + result = new Location("owm"); + result.setLatitude(cursor.getDouble(cursor.getColumnIndexOrThrow(COL_LATITUDE))); + result.setLongitude(cursor.getDouble(cursor.getColumnIndexOrThrow(COL_LONGITUDE))); + result.setAccuracy(Configuration.assumedAccuracy); + + if (result.getLatitude() == 0 || result.getLongitude() == 0) { + Log.d(TAG, "BSSID '" + bssid + "' returns 0 values for lat or long. Skipped."); + queryResultNegativeCache.put(normalizedBssid, true); + return null; + } + + queryResultCache.put(normalizedBssid, result); + Log.d(TAG,"Wifi info found for: " + normalizedBssid); + + return result; + } + } finally { + cursor.close(); + } + + + } + Log.d(TAG,"No Wifi info found for: " + normalizedBssid); + queryResultNegativeCache.put(normalizedBssid, true); + return null; + } } From fab20dcc2c238f4fbcb1b604c50895fe2f2f0d9f Mon Sep 17 00:00:00 2001 From: Tod Fitch Date: Tue, 9 Dec 2014 13:18:07 -0800 Subject: [PATCH 07/10] Move settings out of local processing and make select work. Signed-off-by: Tod Fitch --- AndroidManifest.xml | 16 ++--- .../local => }/PrefsFragment.java | 2 +- .../{openwlanmap/local => }/Settings.java | 2 +- .../backend/openwlanmap/BackendService.java | 68 ++++++++++++------- 4 files changed, 52 insertions(+), 36 deletions(-) rename src/org/microg/nlp/backend/{openwlanmap/local => }/PrefsFragment.java (97%) rename src/org/microg/nlp/backend/{openwlanmap/local => }/Settings.java (86%) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index bc3419e..5d49eb6 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -24,14 +24,14 @@ + android:name="org.microg.nlp.BACKEND_SETTINGS_ACTIVITY" + android:value="org.microg.nlp.backend.openwlanmap.Settings" /> - - - - - + + + + + diff --git a/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java b/src/org/microg/nlp/backend/PrefsFragment.java similarity index 97% rename from src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java rename to src/org/microg/nlp/backend/PrefsFragment.java index d734c01..7d8c8fe 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/PrefsFragment.java +++ b/src/org/microg/nlp/backend/PrefsFragment.java @@ -1,4 +1,4 @@ -package org.microg.nlp.backend.openwlanmap.local; +package org.microg.nlp.backend.openwlanmap; import org.microg.nlp.backend.openwlanmap.R; diff --git a/src/org/microg/nlp/backend/openwlanmap/local/Settings.java b/src/org/microg/nlp/backend/Settings.java similarity index 86% rename from src/org/microg/nlp/backend/openwlanmap/local/Settings.java rename to src/org/microg/nlp/backend/Settings.java index 16d77e7..e303a54 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/Settings.java +++ b/src/org/microg/nlp/backend/Settings.java @@ -1,4 +1,4 @@ -package org.microg.nlp.backend.openwlanmap.local; +package org.microg.nlp.backend.openwlanmap; import android.app.Activity; import android.os.Bundle; diff --git a/src/org/microg/nlp/backend/openwlanmap/BackendService.java b/src/org/microg/nlp/backend/openwlanmap/BackendService.java index b700a49..21787b0 100644 --- a/src/org/microg/nlp/backend/openwlanmap/BackendService.java +++ b/src/org/microg/nlp/backend/openwlanmap/BackendService.java @@ -24,16 +24,33 @@ public class BackendService extends LocationBackendService { private WLocate wLocate; private WifiLocationFile wifiLocationFile; private WifiReceiver wifiReceiver; + private boolean networkAllowed; @Override protected void onOpen() { Log.d(TAG, "onOpen"); - + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); Configuration.fillFromPrefs(sharedPrefs); sharedPrefs.registerOnSharedPreferenceChangeListener(Configuration.listener); - - if (Configuration.networkAllowed) { + + setOperatingMode(); + } + + @Override + protected void onClose() { + Log.d(TAG, "onClose"); + + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + sharedPrefs.unregisterOnSharedPreferenceChangeListener(Configuration.listener); + + cleanupOperatingMode(); + + } + + private void setOperatingMode() { + this.networkAllowed = Configuration.networkAllowed; + if (this.networkAllowed) { if (wLocate == null) { wLocate = new MyWLocate(this); } else { @@ -44,40 +61,39 @@ protected void onOpen() { if (wifiReceiver == null) { wifiReceiver = new WifiReceiver(this, new WifiDBResolver()); } - registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); + registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } - } + } - @Override - protected void onClose() { - Log.d(TAG, "onClose"); - - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - sharedPrefs.unregisterOnSharedPreferenceChangeListener(Configuration.listener); - + private void cleanupOperatingMode() { if (wLocate != null) { wLocate.doPause(); } if (wifiReceiver != null) { unregisterReceiver(wifiReceiver); } - - } + } @Override protected Location update() { Log.d(TAG, "update"); + + if (this.networkAllowed != Configuration.networkAllowed) { + Log.d(TAG, "Network allowed changed"); + cleanupOperatingMode(); + setOperatingMode(); + } if (wLocate != null) { Log.d(TAG, "Requesting location from net"); wLocate.wloc_request_position(WLocate.FLAG_NO_GPS_ACCESS); return null; } - + if (wifiReceiver != null) { Log.d(TAG, "Requesting location from db"); wifiReceiver.startScan(); } - + return null; } @@ -86,7 +102,7 @@ private void openDatabase() { wifiLocationFile = new WifiLocationFile(); } } - + private class MyWLocate extends WLocate { public MyWLocate(Context ctx) throws IllegalArgumentException { @@ -105,42 +121,42 @@ protected void wloc_return_position(int ret, double lat, double lon, float radiu } } } - + private class WifiDBResolver implements WifiReceivedCallback { @Override public void process(List foundBssids) { - + if (foundBssids == null || foundBssids.isEmpty()) { return; } if (wifiLocationFile != null) { - + List locations = new ArrayList(foundBssids.size()); - + for (String bssid : foundBssids) { Location result = wifiLocationFile.query(bssid); if (result != null) { locations.add(result); } } - + if (locations.isEmpty()) { return; } - + //TODO fix LocationHelper:average to not calculate with null values //TODO sort out wifis obviously in the wrong spot Location avgLoc = LocationHelper.average("owm", locations); - + if (avgLoc == null) { Log.e(TAG, "Averaging locations did not work."); return; } - + Log.d(TAG, "Reporting location: " + avgLoc.toString()); report(avgLoc); } } } -} \ No newline at end of file +} From a8cb7249401f141a9df712137a36c5aa93c1ab41 Mon Sep 17 00:00:00 2001 From: Tod Fitch Date: Wed, 10 Dec 2014 10:40:48 -0800 Subject: [PATCH 08/10] Ignore APs with "_nomap" suffix in name. Should help with situation where a moving AP is detected. Signed-off-by: Tod Fitch --- .../openwlanmap/local/WifiReceiver.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java index 5472e6f..cdd26bb 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java @@ -16,6 +16,7 @@ public class WifiReceiver extends BroadcastReceiver { private boolean scanStarted = false; private WifiManager wifi; private String TAG = WifiReceiver.class.getName(); + private boolean DEBUG = true; private WifiReceivedCallback callback; public WifiReceiver(Context ctx, WifiReceivedCallback aCallback) { @@ -29,17 +30,23 @@ public void onReceive(Context c, Intent intent) { setScanStarted(false); List configs = wifi.getScanResults(); - Log.d(TAG, "Got " + configs.size() + " wifi access points"); + if (DEBUG) Log.d(TAG, "Got " + configs.size() + " wifi access points"); if (configs.size() > 0) { - + List foundBssids = new ArrayList(configs.size()); - + for (ScanResult config : configs) { // some strange devices use a dot instead of : - foundBssids.add(config.BSSID.toUpperCase(Locale.US).replace(".",":")); + final String canonicalBSSID = config.BSSID.toUpperCase(Locale.US).replace(".",":"); + // ignore APs that have _nomap suffix on SSID + if (config.SSID.endsWith("_nomap")) { + if (DEBUG) Log.d(TAG, "Ignoring AP '" + config.SSID + "' BSSID: " + canonicalBSSID); + } else { + foundBssids.add(canonicalBSSID); + } } - + callback.process(foundBssids); } @@ -52,12 +59,12 @@ public boolean isScanStarted() { public void setScanStarted(boolean scanStarted) { this.scanStarted = scanStarted; } - - + + public interface WifiReceivedCallback { void process(List foundBssids); - + } public void startScan() { @@ -67,4 +74,4 @@ public void startScan() { } wifi.startScan(); } -} \ No newline at end of file +} From 5aebe039c49afd22f828c01272130092470b0984 Mon Sep 17 00:00:00 2001 From: Felix Knecht Date: Wed, 31 Dec 2014 16:02:51 +0100 Subject: [PATCH 09/10] Changed location of files to match declared package --- src/org/microg/nlp/backend/{ => openwlanmap}/PrefsFragment.java | 0 src/org/microg/nlp/backend/{ => openwlanmap}/Settings.java | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/org/microg/nlp/backend/{ => openwlanmap}/PrefsFragment.java (100%) rename src/org/microg/nlp/backend/{ => openwlanmap}/Settings.java (100%) diff --git a/src/org/microg/nlp/backend/PrefsFragment.java b/src/org/microg/nlp/backend/openwlanmap/PrefsFragment.java similarity index 100% rename from src/org/microg/nlp/backend/PrefsFragment.java rename to src/org/microg/nlp/backend/openwlanmap/PrefsFragment.java diff --git a/src/org/microg/nlp/backend/Settings.java b/src/org/microg/nlp/backend/openwlanmap/Settings.java similarity index 100% rename from src/org/microg/nlp/backend/Settings.java rename to src/org/microg/nlp/backend/openwlanmap/Settings.java From 7cd47dad7a83101a0e80f2a81b4897a1f8d28a5e Mon Sep 17 00:00:00 2001 From: Felix Knecht Date: Wed, 31 Dec 2014 16:54:54 +0100 Subject: [PATCH 10/10] Added setting to control debug logging --- res/values/strings.xml | 3 ++- res/xml/settings.xml | 6 ++++++ .../nlp/backend/openwlanmap/BackendService.java | 14 +++++++------- .../nlp/backend/openwlanmap/Configuration.java | 6 +++++- .../openwlanmap/local/WifiLocationFile.java | 16 ++++++++-------- .../backend/openwlanmap/local/WifiReceiver.java | 9 +++++---- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 215f0e5..2ae4736 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6,6 +6,7 @@ Local Database location Assumed accuracy for database in meters - + Debug + Enable debug log (contains your location) The supplied value for assumed accuracy is not a float \ No newline at end of file diff --git a/res/xml/settings.xml b/res/xml/settings.xml index 1902a0a..82c68cc 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -17,4 +17,10 @@ android:key="assumedAccuracy" android:title="@string/title_assumedAccuracy_preference" /> + + + + \ No newline at end of file diff --git a/src/org/microg/nlp/backend/openwlanmap/BackendService.java b/src/org/microg/nlp/backend/openwlanmap/BackendService.java index 21787b0..afd3277 100644 --- a/src/org/microg/nlp/backend/openwlanmap/BackendService.java +++ b/src/org/microg/nlp/backend/openwlanmap/BackendService.java @@ -39,7 +39,7 @@ protected void onOpen() { @Override protected void onClose() { - Log.d(TAG, "onClose"); + if (Configuration.debugEnabled) Log.d(TAG, "onClose"); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); sharedPrefs.unregisterOnSharedPreferenceChangeListener(Configuration.listener); @@ -76,21 +76,21 @@ private void cleanupOperatingMode() { @Override protected Location update() { - Log.d(TAG, "update"); + if (Configuration.debugEnabled) Log.d(TAG, "update"); if (this.networkAllowed != Configuration.networkAllowed) { - Log.d(TAG, "Network allowed changed"); + if (Configuration.debugEnabled) Log.d(TAG, "Network allowed changed"); cleanupOperatingMode(); setOperatingMode(); } if (wLocate != null) { - Log.d(TAG, "Requesting location from net"); + if (Configuration.debugEnabled) Log.d(TAG, "Requesting location from net"); wLocate.wloc_request_position(WLocate.FLAG_NO_GPS_ACCESS); return null; } if (wifiReceiver != null) { - Log.d(TAG, "Requesting location from db"); + if (Configuration.debugEnabled) Log.d(TAG, "Requesting location from db"); wifiReceiver.startScan(); } @@ -111,7 +111,7 @@ public MyWLocate(Context ctx) throws IllegalArgumentException { @Override protected void wloc_return_position(int ret, double lat, double lon, float radius, short ccode, float cog) { - Log.d(TAG, String.format("wloc_return_position ret=%d lat=%f lon=%f radius=%f ccode=%d cog=%f", ret, lat, lon, radius, ccode, cog)); + if (Configuration.debugEnabled) Log.d(TAG, String.format("wloc_return_position ret=%d lat=%f lon=%f radius=%f ccode=%d cog=%f", ret, lat, lon, radius, ccode, cog)); if (ret == WLOC_OK) { Location location = LocationHelper.create("libwlocate", lat, lon, radius); if (cog != -1) { @@ -154,7 +154,7 @@ public void process(List foundBssids) { return; } - Log.d(TAG, "Reporting location: " + avgLoc.toString()); + if (Configuration.debugEnabled) Log.d(TAG, "Reporting location: " + avgLoc.toString()); report(avgLoc); } } diff --git a/src/org/microg/nlp/backend/openwlanmap/Configuration.java b/src/org/microg/nlp/backend/openwlanmap/Configuration.java index 3e6e134..ab499ee 100644 --- a/src/org/microg/nlp/backend/openwlanmap/Configuration.java +++ b/src/org/microg/nlp/backend/openwlanmap/Configuration.java @@ -15,12 +15,16 @@ public class Configuration { public static float assumedAccuracy; public static ConfigChangedListener listener = new ConfigChangedListener(); + + public static boolean debugEnabled; public static void fillFromPrefs(SharedPreferences sharedPrefs) { + debugEnabled = sharedPrefs.getBoolean("debugEnabled", false); + networkAllowed = sharedPrefs.getBoolean("networkAllowed", false); - Log.d(TAG, "Network allowed: " + networkAllowed); + if (debugEnabled) Log.d(TAG, "Network allowed: " + networkAllowed); dbLocation = sharedPrefs.getString("databaseLocation", Environment.getExternalStorageDirectory().getAbsolutePath() + "/.nogapps/openwifimap.db"); diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java index fdb69d8..dea7bdc 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiLocationFile.java @@ -69,7 +69,7 @@ public String getPath() { private void checkForNewDb() { File newDbFile = new File(Configuration.dbLocation + ".new"); if (newDbFile.exists() && newDbFile.canRead()) { - Log.d(TAG, "New database file detected."); + if (Configuration.debugEnabled) Log.d(TAG, "New database file detected."); this.close(); queryResultCache = new LruCache(1000); queryResultNegativeCache = new LruCache(1000); @@ -85,7 +85,7 @@ public synchronized Location query(final String bssid) { String normalizedBssid = bssid.replace(":", ""); - Log.d(TAG, "Searching for BSSID '" + normalizedBssid + "'"); + if (Configuration.debugEnabled) Log.d(TAG, "Searching for BSSID '" + normalizedBssid + "'"); Boolean negative = queryResultNegativeCache.get(normalizedBssid); if (negative != null && negative.booleanValue()) return null; @@ -93,9 +93,8 @@ public synchronized Location query(final String bssid) { Location cached = queryResultCache.get(normalizedBssid); if (cached != null) return cached; - //openDatabase(); if (database == null) { - Log.d(TAG, "Unable to open wifi database file."); + if (Configuration.debugEnabled) Log.d(TAG, "Unable to open wifi database file."); return null; } @@ -111,7 +110,7 @@ public synchronized Location query(final String bssid) { null, null); if (cursor != null) { - Log.d(TAG,"Database contains " + cursor.getCount() + " entries"); + if (Configuration.debugEnabled) Log.d(TAG,"Database contains " + cursor.getCount() + " entries"); try { if (cursor.getCount() > 0) { cursor.moveToNext(); @@ -122,13 +121,14 @@ public synchronized Location query(final String bssid) { result.setAccuracy(Configuration.assumedAccuracy); if (result.getLatitude() == 0 || result.getLongitude() == 0) { - Log.d(TAG, "BSSID '" + bssid + "' returns 0 values for lat or long. Skipped."); + //this is the case for bssids where OWM detected a _nomap or other moving AP + if (Configuration.debugEnabled) Log.d(TAG, "BSSID '" + bssid + "' returns 0 values for lat or long. Skipped."); queryResultNegativeCache.put(normalizedBssid, true); return null; } queryResultCache.put(normalizedBssid, result); - Log.d(TAG,"Wifi info found for: " + normalizedBssid); + if (Configuration.debugEnabled) Log.d(TAG,"Wifi info found for: " + normalizedBssid); return result; } @@ -138,7 +138,7 @@ public synchronized Location query(final String bssid) { } - Log.d(TAG,"No Wifi info found for: " + normalizedBssid); + if (Configuration.debugEnabled) Log.d(TAG,"No Wifi info found for: " + normalizedBssid); queryResultNegativeCache.put(normalizedBssid, true); return null; } diff --git a/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java b/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java index cdd26bb..169ae35 100644 --- a/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java +++ b/src/org/microg/nlp/backend/openwlanmap/local/WifiReceiver.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.Locale; +import org.microg.nlp.backend.openwlanmap.Configuration; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -16,7 +18,6 @@ public class WifiReceiver extends BroadcastReceiver { private boolean scanStarted = false; private WifiManager wifi; private String TAG = WifiReceiver.class.getName(); - private boolean DEBUG = true; private WifiReceivedCallback callback; public WifiReceiver(Context ctx, WifiReceivedCallback aCallback) { @@ -30,7 +31,7 @@ public void onReceive(Context c, Intent intent) { setScanStarted(false); List configs = wifi.getScanResults(); - if (DEBUG) Log.d(TAG, "Got " + configs.size() + " wifi access points"); + if (Configuration.debugEnabled) Log.d(TAG, "Got " + configs.size() + " wifi access points"); if (configs.size() > 0) { @@ -41,7 +42,7 @@ public void onReceive(Context c, Intent intent) { final String canonicalBSSID = config.BSSID.toUpperCase(Locale.US).replace(".",":"); // ignore APs that have _nomap suffix on SSID if (config.SSID.endsWith("_nomap")) { - if (DEBUG) Log.d(TAG, "Ignoring AP '" + config.SSID + "' BSSID: " + canonicalBSSID); + if (Configuration.debugEnabled) Log.d(TAG, "Ignoring AP '" + config.SSID + "' BSSID: " + canonicalBSSID); } else { foundBssids.add(canonicalBSSID); } @@ -70,7 +71,7 @@ public interface WifiReceivedCallback { public void startScan() { setScanStarted(true); if (!wifi.isWifiEnabled() && !wifi.isScanAlwaysAvailable()) { - Log.d(TAG, "Wifi is disabled and we can't scan either. Not doing anything."); + Log.i(TAG, "Wifi is disabled and we can't scan either. Not doing anything."); } wifi.startScan(); }