aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/LocationActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/pixart/messenger/ui/LocationActivity.java')
-rw-r--r--src/main/java/de/pixart/messenger/ui/LocationActivity.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/LocationActivity.java b/src/main/java/de/pixart/messenger/ui/LocationActivity.java
new file mode 100644
index 000000000..74e2b45f9
--- /dev/null
+++ b/src/main/java/de/pixart/messenger/ui/LocationActivity.java
@@ -0,0 +1,60 @@
+package de.pixart.messenger.ui;
+
+import android.app.Activity;
+import android.content.Context;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+
+import de.pixart.messenger.Config;
+
+public abstract class LocationActivity extends Activity implements LocationListener {
+ private LocationManager locationManager;
+
+ @Override
+ protected void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ this.locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
+ }
+
+ protected abstract void gotoLoc() throws UnsupportedOperationException;
+
+ protected abstract void setmLastLocation(final Location location);
+
+ protected void requestLocationUpdates() {
+ final Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+ if (lastKnownLocation != null) {
+ setmLastLocation(lastKnownLocation);
+ try {
+ gotoLoc();
+ } catch (final UnsupportedOperationException ignored) {
+ }
+ }
+
+ locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Config.LOCATION_FIX_TIME_DELTA, Config.LOCATION_FIX_SPACE_DELTA, this);
+ locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Config.LOCATION_FIX_TIME_DELTA, Config.LOCATION_FIX_SPACE_DELTA, this);
+
+ // If something else is also querying for location more frequently than we are, the battery is already being
+ // drained. Go ahead and use the existing locations as often as we can get them.
+ locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, this);
+ }
+
+ protected void pauseLocationUpdates() {
+ locationManager.removeUpdates(this);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ pauseLocationUpdates();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ this.setmLastLocation(null);
+
+ requestLocationUpdates();
+ }
+} \ No newline at end of file