11//This Handy-Dandy class acquired and tweaked from http://stackoverflow.com/a/3145655/309558
22package org .wordpress .android .util .helpers ;
33
4- import java .util .Timer ;
5- import java .util .TimerTask ;
6-
4+ import android .app .Activity ;
75import android .content .Context ;
86import android .location .Location ;
97import android .location .LocationListener ;
108import android .location .LocationManager ;
119import android .os .Bundle ;
1210
11+ import java .util .Timer ;
12+ import java .util .TimerTask ;
13+
1314public 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 );
15+ Timer mTimer ;
16+ LocationManager mLocationManager ;
17+ LocationResult mLocationResult ;
18+ boolean mGpsEnabled = false ;
19+ boolean mNetworkEnabled = false ;
20+
21+ public boolean getLocation (Activity activity , LocationResult result ) {
22+
23+
24+ mLocationResult = result ;
25+ if (mLocationManager == null ) {
26+ mLocationManager = (LocationManager ) activity .getSystemService (Context .LOCATION_SERVICE );
27+ }
2528
2629 // exceptions will be thrown if provider is not permitted.
2730 try {
28- gps_enabled = lm .isProviderEnabled (LocationManager .GPS_PROVIDER );
31+ mGpsEnabled = mLocationManager .isProviderEnabled (LocationManager .GPS_PROVIDER );
2932 } catch (Exception ex ) {
3033 }
3134 try {
32- network_enabled = lm .isProviderEnabled (LocationManager .NETWORK_PROVIDER );
35+ mNetworkEnabled = mLocationManager .isProviderEnabled (LocationManager .NETWORK_PROVIDER );
3336 } catch (Exception ex ) {
3437 }
3538
3639 // don't start listeners if no provider is enabled
37- if (!gps_enabled && !network_enabled )
40+ if (!mGpsEnabled && !mNetworkEnabled ) {
3841 return false ;
42+ }
3943
40- if (gps_enabled )
41- lm .requestLocationUpdates (LocationManager .GPS_PROVIDER , 0 , 0 , locationListenerGps );
44+ if (mGpsEnabled ) {
45+ mLocationManager .requestLocationUpdates (LocationManager .GPS_PROVIDER , 0 , 0 , locationListenerGps );
46+ }
4247
43- if (network_enabled )
44- lm .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , 0 , 0 , locationListenerNetwork );
48+ if (mNetworkEnabled ) {
49+ mLocationManager .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , 0 , 0 , locationListenerNetwork );
50+ }
4551
46- timer1 = new Timer ();
47- timer1 .schedule (new GetLastLocation (), 30000 );
52+ mTimer = new Timer ();
53+ mTimer .schedule (new GetLastLocation (), 30000 );
4854 return true ;
4955 }
5056
5157 LocationListener locationListenerGps = new LocationListener () {
5258 public void onLocationChanged (Location location ) {
53- timer1 .cancel ();
54- locationResult .gotLocation (location );
55- lm .removeUpdates (this );
56- lm .removeUpdates (locationListenerNetwork );
59+ mTimer .cancel ();
60+ mLocationResult .gotLocation (location );
61+ mLocationManager .removeUpdates (this );
62+ mLocationManager .removeUpdates (locationListenerNetwork );
5763 }
5864
5965 public void onProviderDisabled (String provider ) {
@@ -68,10 +74,10 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
6874
6975 LocationListener locationListenerNetwork = new LocationListener () {
7076 public void onLocationChanged (Location location ) {
71- timer1 .cancel ();
72- locationResult .gotLocation (location );
73- lm .removeUpdates (this );
74- lm .removeUpdates (locationListenerGps );
77+ mTimer .cancel ();
78+ mLocationResult .gotLocation (location );
79+ mLocationManager .removeUpdates (this );
80+ mLocationManager .removeUpdates (locationListenerGps );
7581 }
7682
7783 public void onProviderDisabled (String provider ) {
@@ -87,34 +93,36 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
8793 class GetLastLocation extends TimerTask {
8894 @ Override
8995 public void run () {
90- lm .removeUpdates (locationListenerGps );
91- lm .removeUpdates (locationListenerNetwork );
96+ mLocationManager .removeUpdates (locationListenerGps );
97+ mLocationManager .removeUpdates (locationListenerNetwork );
9298
9399 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 );
100+ if (mGpsEnabled ) {
101+ gps_loc = mLocationManager .getLastKnownLocation (LocationManager .GPS_PROVIDER );
102+ }
103+ if (mNetworkEnabled ) {
104+ net_loc = mLocationManager .getLastKnownLocation (LocationManager .NETWORK_PROVIDER );
105+ }
99106
100107 // if there are both values use the latest one
101108 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 );
109+ if (gps_loc .getTime () > net_loc .getTime ()) {
110+ mLocationResult .gotLocation (gps_loc );
111+ } else {
112+ mLocationResult .gotLocation (net_loc );
113+ }
106114 return ;
107115 }
108116
109117 if (gps_loc != null ) {
110- locationResult .gotLocation (gps_loc );
118+ mLocationResult .gotLocation (gps_loc );
111119 return ;
112120 }
113121 if (net_loc != null ) {
114- locationResult .gotLocation (net_loc );
122+ mLocationResult .gotLocation (net_loc );
115123 return ;
116124 }
117- locationResult .gotLocation (null );
125+ mLocationResult .gotLocation (null );
118126 }
119127 }
120128
@@ -123,10 +131,10 @@ public static abstract class LocationResult {
123131 }
124132
125133 public void cancelTimer () {
126- if (timer1 != null ) {
127- timer1 .cancel ();
128- lm .removeUpdates (locationListenerGps );
129- lm .removeUpdates (locationListenerNetwork );
134+ if (mTimer != null ) {
135+ mTimer .cancel ();
136+ mLocationManager .removeUpdates (locationListenerGps );
137+ mLocationManager .removeUpdates (locationListenerNetwork );
130138 }
131139 }
132140}
0 commit comments