aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/LocationActivity.java
blob: 04bd03584cac55eb9857034062db55871ca61b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 android.support.v7.app.AppCompatActivity;

import de.pixart.messenger.Config;

public abstract class LocationActivity extends AppCompatActivity 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) {
            }
        }
        if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)
                && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Config.LOCATION_FIX_TIME_DELTA, Config.LOCATION_FIX_SPACE_DELTA, this);
        }
        if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)
                && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            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.
        if (locationManager.getAllProviders().contains(LocationManager.PASSIVE_PROVIDER)) {
            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();
    }
}