Skip to content

Commit c3ad1f2

Browse files
committed
first step for runtime location request
1 parent 58440ae commit c3ad1f2

File tree

2 files changed

+63
-49
lines changed

2 files changed

+63
-49
lines changed

WordPressUtils/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
}
2222
compile 'com.mcxiaoke.volley:library:1.0.18'
2323
compile 'com.android.support:support-v13:23.0.0'
24+
compile 'com.android.support:design:23.0.0'
2425
}
2526

2627
android {
Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
11
//This Handy-Dandy class acquired and tweaked from http://stackoverflow.com/a/3145655/309558
22
package org.wordpress.android.util.helpers;
33

4-
import java.util.Timer;
5-
import java.util.TimerTask;
6-
4+
import android.Manifest.permission_group;
5+
import android.app.Activity;
6+
import android.app.AlertDialog;
77
import android.content.Context;
8+
import android.content.DialogInterface;
9+
import android.content.pm.PackageManager;
810
import android.location.Location;
911
import android.location.LocationListener;
1012
import android.location.LocationManager;
1113
import android.os.Bundle;
14+
import android.support.v4.app.ActivityCompat;
15+
16+
import java.util.Timer;
17+
import java.util.TimerTask;
1218

1319
public class LocationHelper {
14-
Timer timer1;
15-
LocationManager lm;
16-
LocationResult locationResult;
17-
boolean gps_enabled = false;
18-
boolean network_enabled = false;
19-
20-
public boolean getLocation(Context context, LocationResult result) {
21-
locationResult = result;
22-
if (lm == null)
23-
lm = (LocationManager) context
24-
.getSystemService(Context.LOCATION_SERVICE);
20+
Timer mTimer;
21+
LocationManager mLocationManager;
22+
LocationResult mLocationResult;
23+
boolean mGpsEnabled = false;
24+
boolean mNetworkEnabled = false;
25+
26+
public boolean getLocation(Activity activity, LocationResult result) {
27+
28+
29+
mLocationResult = result;
30+
if (mLocationManager == null) {
31+
mLocationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
32+
}
2533

2634
// exceptions will be thrown if provider is not permitted.
2735
try {
28-
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
36+
mGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
2937
} catch (Exception ex) {
3038
}
3139
try {
32-
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
40+
mNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
3341
} catch (Exception ex) {
3442
}
3543

3644
// don't start listeners if no provider is enabled
37-
if (!gps_enabled && !network_enabled)
45+
if (!mGpsEnabled && !mNetworkEnabled) {
3846
return false;
47+
}
3948

40-
if (gps_enabled)
41-
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
49+
if (mGpsEnabled) {
50+
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
51+
}
4252

43-
if (network_enabled)
44-
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
53+
if (mNetworkEnabled) {
54+
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
55+
}
4556

46-
timer1 = new Timer();
47-
timer1.schedule(new GetLastLocation(), 30000);
57+
mTimer = new Timer();
58+
mTimer.schedule(new GetLastLocation(), 30000);
4859
return true;
4960
}
5061

5162
LocationListener locationListenerGps = new LocationListener() {
5263
public void onLocationChanged(Location location) {
53-
timer1.cancel();
54-
locationResult.gotLocation(location);
55-
lm.removeUpdates(this);
56-
lm.removeUpdates(locationListenerNetwork);
64+
mTimer.cancel();
65+
mLocationResult.gotLocation(location);
66+
mLocationManager.removeUpdates(this);
67+
mLocationManager.removeUpdates(locationListenerNetwork);
5768
}
5869

5970
public void onProviderDisabled(String provider) {
@@ -68,10 +79,10 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
6879

6980
LocationListener locationListenerNetwork = new LocationListener() {
7081
public void onLocationChanged(Location location) {
71-
timer1.cancel();
72-
locationResult.gotLocation(location);
73-
lm.removeUpdates(this);
74-
lm.removeUpdates(locationListenerGps);
82+
mTimer.cancel();
83+
mLocationResult.gotLocation(location);
84+
mLocationManager.removeUpdates(this);
85+
mLocationManager.removeUpdates(locationListenerGps);
7586
}
7687

7788
public void onProviderDisabled(String provider) {
@@ -87,34 +98,36 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
8798
class GetLastLocation extends TimerTask {
8899
@Override
89100
public void run() {
90-
lm.removeUpdates(locationListenerGps);
91-
lm.removeUpdates(locationListenerNetwork);
101+
mLocationManager.removeUpdates(locationListenerGps);
102+
mLocationManager.removeUpdates(locationListenerNetwork);
92103

93104
Location net_loc = null, gps_loc = null;
94-
if (gps_enabled)
95-
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
96-
if (network_enabled)
97-
net_loc = lm
98-
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
105+
if (mGpsEnabled) {
106+
gps_loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
107+
}
108+
if (mNetworkEnabled) {
109+
net_loc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
110+
}
99111

100112
// if there are both values use the latest one
101113
if (gps_loc != null && net_loc != null) {
102-
if (gps_loc.getTime() > net_loc.getTime())
103-
locationResult.gotLocation(gps_loc);
104-
else
105-
locationResult.gotLocation(net_loc);
114+
if (gps_loc.getTime() > net_loc.getTime()) {
115+
mLocationResult.gotLocation(gps_loc);
116+
} else {
117+
mLocationResult.gotLocation(net_loc);
118+
}
106119
return;
107120
}
108121

109122
if (gps_loc != null) {
110-
locationResult.gotLocation(gps_loc);
123+
mLocationResult.gotLocation(gps_loc);
111124
return;
112125
}
113126
if (net_loc != null) {
114-
locationResult.gotLocation(net_loc);
127+
mLocationResult.gotLocation(net_loc);
115128
return;
116129
}
117-
locationResult.gotLocation(null);
130+
mLocationResult.gotLocation(null);
118131
}
119132
}
120133

@@ -123,10 +136,10 @@ public static abstract class LocationResult {
123136
}
124137

125138
public void cancelTimer() {
126-
if (timer1 != null) {
127-
timer1.cancel();
128-
lm.removeUpdates(locationListenerGps);
129-
lm.removeUpdates(locationListenerNetwork);
139+
if (mTimer != null) {
140+
mTimer.cancel();
141+
mLocationManager.removeUpdates(locationListenerGps);
142+
mLocationManager.removeUpdates(locationListenerNetwork);
130143
}
131144
}
132145
}

0 commit comments

Comments
 (0)