[yopme, and] Update frequency settings.

This commit is contained in:
Dmitry Kunin 2013-09-11 18:37:15 +03:00 committed by Alex Zolotarev
parent 1418b6cb50
commit 19c8678df3
10 changed files with 251 additions and 27 deletions

1
.gitignore vendored
View file

@ -173,3 +173,4 @@ tools/twine/.git
syntax: regexp
(.*/)?\#[^/]*\#$
android/YoPme/.gradletasknamecache

View file

@ -55,6 +55,15 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapswithme.yopme.YopmeFrontActivity" />
</activity>
<activity
android:name="com.mapswithme.yopme.YopmePreference"
android:label="@string/menu_settings"
android:parentActivityName="com.mapswithme.yopme.YopmeFrontActivity"
android:screenOrientation="sensorPortrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapswithme.yopme.YopmeFrontActivity" />
</activity>
</application>
</manifest>

View file

@ -1,14 +1,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
<item
android:id="@+id/action_settings"
android:id="@+id/menu_settings"
android:icon="@drawable/ic_menu"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/menu_settings"/>
-->
<item
android:id="@+id/menu_help"
android:icon="@drawable/ic_menu"

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="roboto_light">"sans-serif-light"</string>
<string name="roboto_regular">"sans-serif"</string>
<string name="pref_loc_update">"pref_location_update_interval"</string>
<string name="pref_about">"pref_about"</string>
<string-array name="update_frequency_values">
<item>-1</item>
<item>15</item>
<item>30</item>
<item>60</item>
</string-array>
</resources>

View file

@ -2,6 +2,7 @@
<resources>
<string name="app_name">MapsWithMe E-ink</string>
<string name="title_activity_reference">MapsWithMe E-ink</string>
<string name="menu_settings">Настройки</string>
<string name="menu_help">Справка</string>
<string name="wait_msg">Определение местоположения ...</string>
@ -12,9 +13,14 @@
<string name="toast_your_location">Теперь ваше местоположение отображено на втором экране</string>
<string name="toast_poi">Теперь это место отображено на втором экране</string>
<string name="pick_point_button_name">Показать на E-ink экране</string>
<string name="title_activity_reference">MapsWithMe E-ink</string>
<string name="action_settings">Настройки</string>
<string name="roboto_light">"sans-serif-light"</string>
<string name="roboto_regular">"sans-serif"</string>
<string name="location_update_frequency">Частота обновления</string>
<string name="pref_update_title">Обновление карты</string>
<string-array name="update_frequency">
<item>По нажатию</item>
<item>15 секунд</item>
<item>30 секунд</item>
<item>60 секунд</item>
</string-array>
</resources>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:orderingFromXml="true"
android:title="@string/pref_update_title" >
<ListPreference
android:defaultValue="-1"
android:entries="@array/update_frequency"
android:entryValues="@array/update_frequency_values"
android:key="@string/pref_loc_update"
android:title="@string/location_update_frequency" />
</PreferenceCategory>
</PreferenceScreen>

View file

@ -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);

View file

@ -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;
}
});

View file

@ -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);
}
}
}

View file

@ -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() {}
}