From 19c8678df31eaef7d6b6950198d48240df469034 Mon Sep 17 00:00:00 2001 From: Dmitry Kunin Date: Wed, 11 Sep 2013 18:37:15 +0300 Subject: [PATCH] [yopme, and] Update frequency settings. --- .gitignore | 1 + android/YoPme/AndroidManifest.xml | 9 +++ android/YoPme/res/menu/yopme_main.xml | 5 +- android/YoPme/res/values/internal_strings.xml | 16 ++++ android/YoPme/res/values/strings.xml | 14 +++- android/YoPme/res/xml/prefs.xml | 15 ++++ .../mapswithme/yopme/BackscreenActivity.java | 80 ++++++++++++++----- .../mapswithme/yopme/YopmeFrontActivity.java | 5 ++ .../com/mapswithme/yopme/YopmePreference.java | 72 +++++++++++++++++ .../src/com/mapswithme/yopme/util/Utils.java | 61 ++++++++++++++ 10 files changed, 251 insertions(+), 27 deletions(-) create mode 100644 android/YoPme/res/values/internal_strings.xml create mode 100644 android/YoPme/res/xml/prefs.xml create mode 100644 android/YoPme/src/com/mapswithme/yopme/YopmePreference.java diff --git a/.gitignore b/.gitignore index 8d7fe5ab10..b901810b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -173,3 +173,4 @@ tools/twine/.git syntax: regexp (.*/)?\#[^/]*\#$ +android/YoPme/.gradletasknamecache \ No newline at end of file diff --git a/android/YoPme/AndroidManifest.xml b/android/YoPme/AndroidManifest.xml index 8a891a4a1d..182d39221a 100644 --- a/android/YoPme/AndroidManifest.xml +++ b/android/YoPme/AndroidManifest.xml @@ -55,6 +55,15 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapswithme.yopme.YopmeFrontActivity" /> + + + \ No newline at end of file diff --git a/android/YoPme/res/menu/yopme_main.xml b/android/YoPme/res/menu/yopme_main.xml index 4e8c249ae7..848a43728e 100644 --- a/android/YoPme/res/menu/yopme_main.xml +++ b/android/YoPme/res/menu/yopme_main.xml @@ -1,14 +1,11 @@ - - + + + "sans-serif-light" + "sans-serif" + "pref_location_update_interval" + "pref_about" + + + -1 + 15 + 30 + 60 + + + \ No newline at end of file diff --git a/android/YoPme/res/values/strings.xml b/android/YoPme/res/values/strings.xml index 4e0d803ab4..5b18335337 100644 --- a/android/YoPme/res/values/strings.xml +++ b/android/YoPme/res/values/strings.xml @@ -2,6 +2,7 @@ MapsWithMe E-ink + MapsWithMe E-ink Настройки Справка Определение местоположения ... @@ -12,9 +13,14 @@ Теперь ваше местоположение отображено на втором экране Теперь это место отображено на втором экране Показать на E-ink экране - MapsWithMe E-ink - Настройки - "sans-serif-light" - "sans-serif" + Частота обновления + Обновление карты + + + По нажатию + 15 секунд + 30 секунд + 60 секунд + \ No newline at end of file diff --git a/android/YoPme/res/xml/prefs.xml b/android/YoPme/res/xml/prefs.xml new file mode 100644 index 0000000000..e37e04c24d --- /dev/null +++ b/android/YoPme/res/xml/prefs.xml @@ -0,0 +1,15 @@ + + + + + + + + \ No newline at end of file diff --git a/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java b/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java index 78c869095d..decb401f29 100644 --- a/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java +++ b/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java @@ -1,19 +1,20 @@ package com.mapswithme.yopme; +import java.io.File; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; -import java.io.File; + import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager.NameNotFoundException; -import android.content.res.Resources; import android.graphics.Bitmap; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.os.Environment; +import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.ImageView; @@ -23,9 +24,10 @@ import com.mapswithme.maps.api.MWMPoint; import com.mapswithme.yopme.map.MapData; import com.mapswithme.yopme.map.MapDataProvider; import com.mapswithme.yopme.map.MapRenderer; +import com.mapswithme.yopme.util.Utils; import com.yotadevices.sdk.BSActivity; -import com.yotadevices.sdk.BSMotionEvent; import com.yotadevices.sdk.BSDrawer.Waveform; +import com.yotadevices.sdk.BSMotionEvent; import com.yotadevices.sdk.Constants.Gestures; public class BackscreenActivity extends BSActivity @@ -62,6 +64,7 @@ public class BackscreenActivity extends BSActivity protected View mPoiInfo; protected MapDataProvider mMapDataProvider; + private LocationManager mLocationManager; @Override protected void onBSCreate() @@ -74,9 +77,9 @@ public class BackscreenActivity extends BSActivity // Create folders if they don't exist new File(extStoragePath).mkdirs(); new File(extTmpPath).mkdirs(); - + nativeInitPlatform(getApkPath(), extStoragePath, extTmpPath, "", true); - + /// !!! Create MapRenderer ONLY AFTER platform init !!! //final Resources res = getResources(); //mMapDataProvider = new MapRenderer((int) res.getDimension(R.dimen.yota_width), @@ -84,6 +87,15 @@ public class BackscreenActivity extends BSActivity mMapDataProvider = MapRenderer.GetRenderer(); setUpView(); + + mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); + } + + @Override + protected void onBSPause() + { + super.onBSPause(); + mLocationManager.removeUpdates(getLocationPendingIntent(this)); } @Override @@ -136,6 +148,8 @@ public class BackscreenActivity extends BSActivity zoomIn(); else if (action == Gestures.GESTURES_BS_RL) zoomOut(); + else + return; // do not react on other events updateData(); invalidate(); @@ -156,7 +170,7 @@ public class BackscreenActivity extends BSActivity updateData(); hideWaitMessage(); - requestLocationUpdate(); + requestLocationUpdate(); } } @@ -197,24 +211,52 @@ public class BackscreenActivity extends BSActivity private void requestLocationUpdate() { - final LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); + final String updateIntervalStr = PreferenceManager.getDefaultSharedPreferences(this) + .getString(getString(R.string.pref_loc_update), "-1"); + final long updateInterval = Long.parseLong(updateIntervalStr); - if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) - lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, BackscreenActivity.getLocationPendingIntent(this)); - else if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) - lm.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, BackscreenActivity.getLocationPendingIntent(this)); - else - throw new IllegalStateException("No providers found."); + final String[] providers = { + LocationManager.GPS_PROVIDER, + LocationManager.NETWORK_PROVIDER, + LocationManager.PASSIVE_PROVIDER, + }; - if (mMode == Mode.LOCATION) + // before requesting updates try to get last known in the first try + if (mLocation == null) + { + for (final String provider : providers) + if (mLocationManager.isProviderEnabled(provider)) + { + final Location lastLocation = mLocationManager.getLastKnownLocation(provider); + if (lastLocation != null) + onLocationUpdate(lastLocation); + } + } + + // then listen to updates + for (final String provider : providers) + { + if (mLocationManager.isProviderEnabled(provider)) + { + if (updateInterval == -1) + mLocationManager.requestSingleUpdate(provider, getLocationPendingIntent(this)); + else + mLocationManager.requestLocationUpdates(provider, updateInterval*1000, 0, getLocationPendingIntent(this)); + } + } + + if (mMode == Mode.LOCATION && mLocation == null) showWaitMessage(getString(R.string.wait_msg)); } private void onLocationUpdate(Location location) { - hideWaitMessage(); - mLocation = location; + if (Utils.isFirstOneBetterLocation(location, mLocation)) + mLocation = location; + else + return; + hideWaitMessage(); updateData(); invalidate(); } @@ -322,7 +364,7 @@ public class BackscreenActivity extends BSActivity final PendingIntent pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); return pi; } - + public String getApkPath() { try @@ -358,11 +400,11 @@ public class BackscreenActivity extends BSActivity return storagePath.concat(String.format("/Android/obb/%s/", getPackageName())); } - static + static { System.loadLibrary("yopme"); } - + private native void nativeInitPlatform(String apkPath, String storagePath, String tmpPath, String obbGooglePath, boolean isPro); diff --git a/android/YoPme/src/com/mapswithme/yopme/YopmeFrontActivity.java b/android/YoPme/src/com/mapswithme/yopme/YopmeFrontActivity.java index 9f6c61df5b..3bc68f0875 100644 --- a/android/YoPme/src/com/mapswithme/yopme/YopmeFrontActivity.java +++ b/android/YoPme/src/com/mapswithme/yopme/YopmeFrontActivity.java @@ -138,6 +138,11 @@ public class YopmeFrontActivity extends Activity startActivity(new Intent(getApplicationContext(), ReferenceActivity.class)); return true; } + else if (item.getItemId() == R.id.menu_settings) + { + startActivity(new Intent(getApplicationContext(), YopmePreference.class)); + return true; + } return false; } }); diff --git a/android/YoPme/src/com/mapswithme/yopme/YopmePreference.java b/android/YoPme/src/com/mapswithme/yopme/YopmePreference.java new file mode 100644 index 0000000000..0fa8a59aec --- /dev/null +++ b/android/YoPme/src/com/mapswithme/yopme/YopmePreference.java @@ -0,0 +1,72 @@ +package com.mapswithme.yopme; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceManager; + +public class YopmePreference extends PreferenceActivity + implements OnSharedPreferenceChangeListener +{ + + private ListPreference mLocationUpdatePref; + + @SuppressWarnings("deprecation") + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.prefs); + + mLocationUpdatePref = (ListPreference) findPreference(getString(R.string.pref_loc_update)); + + final String prefValue = PreferenceManager + .getDefaultSharedPreferences(this) + .getString(getString(R.string.pref_loc_update), "-1"); + final String summary = getResources() + .getStringArray(R.array.update_frequency)[mLocationUpdatePref.findIndexOfValue(prefValue)]; + + mLocationUpdatePref.setSummary(summary); + } + + @SuppressWarnings("deprecation") + @Override + protected void onResume() + { + super.onResume(); + + getPreferenceScreen() + .getSharedPreferences() + .registerOnSharedPreferenceChangeListener(this); + } + + + @SuppressWarnings("deprecation") + @Override + protected void onPause() { + super.onPause(); + + getPreferenceScreen() + .getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) + { + if (key.equals(getString(R.string.pref_loc_update))) + { + final String prefValue = PreferenceManager + .getDefaultSharedPreferences(this) + .getString(getString(R.string.pref_loc_update), "Ololo"); + final String summary = getResources() + .getStringArray(R.array.update_frequency)[mLocationUpdatePref.findIndexOfValue(prefValue)]; + + mLocationUpdatePref.setSummary(summary); + } + } + + +} diff --git a/android/YoPme/src/com/mapswithme/yopme/util/Utils.java b/android/YoPme/src/com/mapswithme/yopme/util/Utils.java index 36ebd1c0da..0486de1500 100644 --- a/android/YoPme/src/com/mapswithme/yopme/util/Utils.java +++ b/android/YoPme/src/com/mapswithme/yopme/util/Utils.java @@ -7,6 +7,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.text.MessageFormat; +import android.location.Location; import android.util.Log; public class Utils @@ -45,5 +46,65 @@ public class Utils } } + private static final int TWO_MINUTES = 1000 * 60 * 2; + /** Determines whether one Location reading is better than the current Location fix + * @param firstLoc The new Location that you want to evaluate + * @param secondLoc The current Location fix, to which you want to compare the new one + */ + public static boolean isFirstOneBetterLocation(Location firstLoc, Location secondLoc) + { + if (secondLoc == null) + { + // A new location is always better than no location + return true; + } + + // Check whether the new location fix is newer or older + final long timeDelta = firstLoc.getTime() - secondLoc.getTime(); + final boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; + final boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; + final boolean isNewer = timeDelta > 0; + + // If it's been more than two minutes since the current location, use the new location + // because the user has likely moved + if (isSignificantlyNewer) + { + return true; + // If the new location is more than two minutes older, it must be worse + } + else if (isSignificantlyOlder) + return false; + + // Check whether the new location fix is more or less accurate + final int accuracyDelta = (int) (firstLoc.getAccuracy() - secondLoc.getAccuracy()); + // Relative diff, not absolute + final boolean almostAsAccurate = Math.abs(accuracyDelta) <= 0.1*secondLoc.getAccuracy(); + + final boolean isMoreAccurate = accuracyDelta < 0; + final boolean isSignificantlyLessAccurate = accuracyDelta > 200; + + // Check if the old and new location are from the same provider + final boolean isFromSameProvider = isSameProvider(firstLoc.getProvider(), + secondLoc.getProvider()); + + // Determine location quality using a combination of timeliness and accuracy + if (isMoreAccurate) + return true; + else if (isNewer && almostAsAccurate) + return true; + else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) + return true; + return false; + } + + /** Checks whether two providers are the same */ + public static boolean isSameProvider(String provider1, String provider2) + { + if (provider1 == null) + return provider2 == null; + else + return provider1.equals(provider2); + } + private Utils() {} }