mobile/user-location: Do not reload when switching between loc+sensor and loc-only

This commit is contained in:
fkloft 2013-12-23 20:35:19 +01:00
parent 7f82671276
commit 2823450857
6 changed files with 130 additions and 110 deletions

View File

@ -94,13 +94,25 @@ window.plugin.userLocation.onLocationChange = function(lat, lng) {
};
window.plugin.userLocation.onOrientationChange = function(direction) {
$(".container", window.plugin.userLocation.marker._icon)
.removeClass("circle")
.addClass("arrow")
.css({
"transform": "rotate(" + direction + "deg)",
"webkitTransform": "rotate(" + direction + "deg)"
});
var container = $(".container", window.plugin.userLocation.marker._icon);
if(direction === null) {
container
.removeClass("arrow")
.addClass("circle")
.css({
"transform": "",
"webkitTransform": ""
});
} else {
container
.removeClass("circle")
.addClass("arrow")
.css({
"transform": "rotate(" + direction + "deg)",
"webkitTransform": "rotate(" + direction + "deg)"
});
}
}
var setup = window.plugin.userLocation.setup;

View File

@ -83,11 +83,7 @@
<string name="pref_tweaks_cat">Tweaks</string>
<string name="pref_plugins">IITC Plugins</string>
<string name="pref_plugins_title">Available plugins</string>
<string name="pref_user_loc_screen">User location</string>
<string name="pref_user_loc">Display user location</string>
<string name="pref_user_loc_sum">Show users position on map</string>
<string name="pref_user_loc_sensor">Use sensor orientation</string>
<string name="pref_user_loc_sensor_sum">Experimental! Fancier but eats battery packs for breakfast</string>
<string name="pref_user_location">Display user location on map</string>
<string name="pref_user_zoom">Show zoom control</string>
<string name="pref_user_zoom_sum">Shows +/- buttons even on multitouch capable devices.</string>
<string name="pref_fullscreen">Hide in fullscreen mode</string>
@ -128,6 +124,16 @@
<item>8</item>
<item>16</item>
</string-array>
<string-array name="pref_user_location_titles">
<item>Don\'t show user position</item>
<item>Show user position on map</item>
<item>Show user position and orientation on map</item>
</string-array>
<string-array name="pref_user_location_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string name="menu_clear_cookies">Clear Cookies</string>
<string name="menu_search">Search</string>

View File

@ -14,33 +14,26 @@
<PreferenceCategory
android:key="pref_ui_cat"
android:title="@string/pref_ui_cat">
<PreferenceScreen
android:key="pref_user_loc_screen"
android:persistent="false"
android:title="@string/pref_user_loc_screen">
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_user_loc"
android:summary="@string/pref_user_loc_sum"
android:title="@string/pref_user_loc"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_user_loc_sensor"
android:dependency="pref_user_loc"
android:summary="@string/pref_user_loc_sensor_sum"
android:title="@string/pref_user_loc_sensor"/>
</PreferenceScreen>
<ListPreference
android:defaultValue="0"
android:entries="@array/pref_user_location_titles"
android:entryValues="@array/pref_user_location_values"
android:key="pref_user_location_mode"
android:title="@string/pref_user_location"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_user_zoom"
android:summary="@string/pref_user_zoom_sum"
android:title="@string/pref_user_zoom"/>
<MultiSelectListPreference
android:entries="@array/pref_hide_fullscreen"
android:entryValues="@array/pref_hide_fullscreen_vals"
android:key="pref_fullscreen"
android:summary="@string/pref_fullscreen_sum"
android:title="@string/pref_fullscreen"
android:entries="@array/pref_hide_fullscreen"
android:entryValues="@array/pref_hide_fullscreen_vals"/>
android:title="@string/pref_fullscreen"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_force_desktop"
@ -50,7 +43,6 @@
<PreferenceCategory
android:key="pref_mics"
android:title="@string/pref_misc_cat">
<PreferenceScreen
android:fragment="com.cradle.iitc_mobile.fragments.PluginsFragment"
android:key="pref_plugins"
@ -67,15 +59,15 @@
android:summary="@string/pref_force_https_sum"
android:title="@string/pref_force_https"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_external_storage"
android:title="@string/pref_external_storage"
android:summary="@string/pref_external_storage_sum"
android:defaultValue="false" />
android:title="@string/pref_external_storage"/>
<CheckBoxPreference
android:key="pref_press_twice_to_exit"
android:title="@string/pref_press_twice_to_exit"
android:summary="@string/pref_press_twice_to_exit_sum"
android:defaultValue="false" />
android:defaultValue="false"
android:key="pref_press_twice_to_exit"
android:summary="@string/pref_press_twice_to_exit_sum"
android:title="@string/pref_press_twice_to_exit"/>
</PreferenceCategory>
<PreferenceCategory
android:key="pref_developer_options"

View File

@ -88,8 +88,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
mIitcWebView.updateFullscreenStatus();
mUserLocation = new IITC_UserLocation(this);
mUserLocation.setLocationEnabled(mSharedPrefs.getBoolean("pref_user_loc", false));
mUserLocation.setSensorEnabled(mSharedPrefs.getBoolean("pref_user_loc_sensor", true));
mUserLocation.setLocationMode(Integer.parseInt(mSharedPrefs.getString("pref_user_location_mode", "0")));
// pass ActionBar to helper because we deprecated getActionBar
mNavigationHelper = new IITC_NavigationHelper(this, super.getActionBar());
@ -111,10 +110,11 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
if (key.equals("pref_force_desktop")) {
mDesktopMode = sharedPreferences.getBoolean("pref_force_desktop", false);
mNavigationHelper.onPrefChanged();
} else if (key.equals("pref_user_loc")) {
mUserLocation.setLocationEnabled(sharedPreferences.getBoolean("pref_user_loc", false));
} else if (key.equals("pref_user_loc_sensor")) {
mUserLocation.setSensorEnabled(sharedPreferences.getBoolean("pref_user_loc_sensor", true));
} else if (key.equals("pref_user_location_mode")) {
int mode = Integer.parseInt(mSharedPrefs.getString("pref_user_location_mode", "0"));
if (mUserLocation.setLocationMode(mode))
mReloadNeeded = true;
return;
} else if (key.equals("pref_fullscreen")) {
mIitcWebView.updateFullscreenStatus();
mNavigationHelper.onPrefChanged();
@ -247,43 +247,41 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
protected void onStart() {
super.onStart();
// enough idle...let's do some work
Log.d("iitcm", "resuming...reset idleTimer");
mUserLocation.onStart();
if (mReloadNeeded) {
Log.d("iitcm", "preference had changed...reload needed");
reloadIITC();
} else {
// iitc is not fully booted...timer will be reset by the script itself
if (findViewById(R.id.imageLoading).getVisibility() == View.GONE) {
// enough idle...let's do some work
Log.d("iitcm", "resuming...reset idleTimer");
mIitcWebView.loadUrl("javascript: window.idleReset();");
}
}
mUserLocation.onStart();
}
@Override
protected void onResume() {
super.onResume();
mIitcWebView.resumeTimers();
mIitcWebView.onResume();
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
mIitcWebView.pauseTimers();
mIitcWebView.onPause();
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
Log.d("iitcm", "stopping iitcm");
mIitcWebView.loadUrl("javascript: window.idleSet();");
mUserLocation.onStop();
super.onStop();
}
@Override

View File

@ -12,15 +12,15 @@ import android.os.Bundle;
import android.view.Surface;
public class IITC_UserLocation implements LocationListener, SensorEventListener {
private static final double SENSOR_DELAY_USER = 100*1e6;
private boolean mLocationEnabled = false;
private boolean mSensorEnabled = true;
private static final double SENSOR_DELAY_USER = 100 * 1e6; // 100 milliseconds
private int mMode = 0;
private boolean mRunning = false;
private boolean mLocationRegistered = false;
private boolean mOrientationRegistered = false;
private long mLastUpdate = 0;
private IITC_Mobile mIitc;
private Location mLastLocation = null;
private LocationManager mLocationManager;
private boolean mRegistered = false;
private boolean mRunning = false;
private Sensor mSensorAccelerometer, mSensorMagnetometer;
private SensorManager mSensorManager = null;
float[] mValuesGravity = null, mValuesGeomagnetic = null;
@ -35,51 +35,44 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
mSensorMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
private void registerListeners() {
if (mRegistered) return;
mRegistered = true;
private void updateListeners() {
boolean useLocation = mRunning && mMode != 0;
boolean useOrientation = mRunning && mMode == 2;
try {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) {
// if the given provider doesn't exist
e.printStackTrace();
if (useLocation && !mLocationRegistered) {
try {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) {
// if the given provider doesn't exist
e.printStackTrace();
}
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) {
// if the given provider doesn't exist
e.printStackTrace();
}
mLocationRegistered = true;
}
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) {
// if the given provider doesn't exist
e.printStackTrace();
if (!useLocation && mLocationRegistered) {
mLocationManager.removeUpdates(this);
mLocationRegistered = false;
}
if (mSensorAccelerometer != null && mSensorMagnetometer != null && mSensorEnabled) {
if (useOrientation && !mOrientationRegistered && mSensorAccelerometer != null && mSensorMagnetometer != null) {
mSensorManager.registerListener(this, mSensorAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mSensorMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
mOrientationRegistered = true;
}
}
private void unregisterListeners() {
if (!mRegistered) return;
mRegistered = false;
mLocationManager.removeUpdates(this);
if (mSensorAccelerometer != null && mSensorMagnetometer != null && mSensorEnabled) {
if (!useOrientation && mOrientationRegistered && mSensorAccelerometer != null && mSensorMagnetometer != null) {
mSensorManager.unregisterListener(this, mSensorAccelerometer);
mSensorManager.unregisterListener(this, mSensorMagnetometer);
mOrientationRegistered = false;
}
}
private void updateListeners() {
if (mRunning && mLocationEnabled)
registerListeners();
else
unregisterListeners();
}
public boolean hasCurrentLocation() {
if (!mRegistered) return false;
if (!mLocationRegistered) return false;
return mLastLocation != null;
}
@ -98,6 +91,12 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
public void onStart() {
mRunning = true;
updateListeners();
// in case we just switched from loc+sensor to loc-only, let javascript know
if (mMode == 1) {
mIitc.getWebView().loadJS("if(window.plugin && window.plugin.userLocation)"
+ "window.plugin.userLocation.onOrientationChange(null);");
}
}
public void onStop() {
@ -105,18 +104,19 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
updateListeners();
}
public void setLocationEnabled(boolean enabled) {
if (enabled == mLocationEnabled) return;
/**
* set the location mode to use. Available modes:
* 0: don't show user's position
* 1: show user's position
* 2: show user's position and orientation
*
* @return whether a reload is needed to reflect the changes made to the preferences
*/
public boolean setLocationMode(int mode) {
boolean needsReload = (mode == 0 && mMode != 0) || (mode != 0 && mMode == 0);
mMode = mode;
mLocationEnabled = enabled;
updateListeners();
}
public void setSensorEnabled(boolean enabled) {
if (enabled == mSensorEnabled) return;
mSensorEnabled = enabled;
updateListeners();
return needsReload;
}
// ------------------------------------------------------------------------
@ -140,12 +140,10 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
@ -165,15 +163,15 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
@Override
public void onSensorChanged(SensorEvent event) {
// save some battery 10 updates per second should be enough
if ((event.timestamp - mLastUpdate) < SENSOR_DELAY_USER) return;
mLastUpdate = event.timestamp;
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mValuesGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mValuesGeomagnetic = event.values;
// save some battery, 10 updates per second should be enough
if ((event.timestamp - mLastUpdate) < SENSOR_DELAY_USER) return;
mLastUpdate = event.timestamp;
// do not touch the javascript while iitc boots
if (mIitc.isLoading()) return;

View File

@ -6,6 +6,7 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
@ -54,6 +55,20 @@ public class MainSettings extends PreferenceFragment {
// first init of summary
String pref_iitc_source_sum = getString(R.string.pref_select_iitc_sum) + " " + pref_iitc_source.getText();
pref_iitc_source.setSummary(pref_iitc_source_sum);
final ListPreference pref_user_location_mode = (ListPreference) findPreference("pref_user_location_mode");
pref_user_location_mode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int mode = Integer.parseInt((String) newValue);
preference.setSummary(getResources().getStringArray(R.array.pref_user_location_titles)[mode]);
return true;
}
});
String value = getPreferenceManager().getSharedPreferences().getString("pref_user_location_mode", "0");
int mode = Integer.parseInt(value);
pref_user_location_mode.setSummary(getResources().getStringArray(R.array.pref_user_location_titles)[mode]);
}
// we want a home button + HomeAsUpEnabled in nested preferences
@ -63,8 +78,7 @@ public class MainSettings extends PreferenceFragment {
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference.getTitle().toString().equals(getString(R.string.pref_advanced_options))
|| preference.getTitle().toString().equals(getString(R.string.pref_about_title))
|| preference.getTitle().toString().equals(getString(R.string.pref_user_loc_screen))) {
|| preference.getTitle().toString().equals(getString(R.string.pref_about_title))) {
initializeActionBar((PreferenceScreen) preference);
}
return super.onPreferenceTreeClick(preferenceScreen, preference);