1
1
package com .pulkit4tech .privy .utilities ;
2
2
3
3
import android .Manifest ;
4
+ import android .app .Activity ;
4
5
import android .content .Context ;
6
+ import android .content .Intent ;
5
7
import android .content .pm .PackageManager ;
6
8
import android .location .Criteria ;
7
9
import android .location .Location ;
8
10
import android .location .LocationManager ;
9
11
import android .location .LocationListener ;
10
12
import android .os .Bundle ;
13
+ import android .provider .Settings ;
14
+ import android .support .design .widget .Snackbar ;
11
15
import android .support .v4 .app .ActivityCompat ;
12
- import android .widget . Toast ;
16
+ import android .view . View ;
13
17
14
18
import com .google .android .gms .maps .model .LatLng ;
15
19
20
+ import com .pulkit4tech .privy .R ;
16
21
import com .pulkit4tech .privy .data .LocationData ;
17
22
18
23
import static android .content .Context .LOCATION_SERVICE ;
@@ -28,12 +33,10 @@ public LocationServices(Context context) {
28
33
29
34
public LocationData getCurrentLocation () {
30
35
LocationManager locationManager = (LocationManager ) mContext .getSystemService (LOCATION_SERVICE );
31
- Criteria criteria = new Criteria ();
32
- criteria .setAccuracy (Criteria .ACCURACY_COARSE );
33
- criteria .setAltitudeRequired (false );
34
- criteria .setBearingRequired (false );
35
- criteria .setCostAllowed (true );
36
- criteria .setPowerRequirement (Criteria .POWER_MEDIUM );
36
+ if (!checkGPSon (locationManager )){
37
+ promptToEnableGPS ();
38
+ }
39
+ Criteria criteria = setCriteria ();
37
40
String bestProvider = locationManager .getBestProvider (criteria , true );
38
41
if (ActivityCompat .checkSelfPermission (mContext , Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
39
42
// ActivityCompat#requestPermissions
@@ -52,6 +55,20 @@ public LocationData getCurrentLocation() {
52
55
return myLocation ;
53
56
}
54
57
58
+ private void promptToEnableGPS () {
59
+ snackMsgWithAction (mContext .getString (R .string .enable_gps_msg ));
60
+ }
61
+
62
+ private Criteria setCriteria () {
63
+ Criteria criteria = new Criteria ();
64
+ criteria .setAccuracy (Criteria .ACCURACY_FINE );
65
+ criteria .setAltitudeRequired (false );
66
+ criteria .setBearingRequired (false );
67
+ criteria .setCostAllowed (true );
68
+ criteria .setPowerRequirement (Criteria .POWER_MEDIUM );
69
+ return criteria ;
70
+ }
71
+
55
72
56
73
@ Override
57
74
public void onLocationChanged (Location location ) {
@@ -70,11 +87,32 @@ public void onStatusChanged(String s, int i, Bundle bundle) {
70
87
71
88
@ Override
72
89
public void onProviderEnabled (String s ) {
73
-
90
+ snackMsg ( mContext . getString ( R . string . gps_enable_request_success ));
74
91
}
75
92
76
93
@ Override
77
94
public void onProviderDisabled (String s ) {
78
- Toast .makeText (mContext , "Provider Disabled" , Toast .LENGTH_SHORT ).show ();
95
+ promptToEnableGPS ();
96
+ }
97
+
98
+ private boolean checkGPSon (LocationManager manager ){
99
+ return manager .isProviderEnabled (LocationManager .GPS_PROVIDER );
100
+ }
101
+
102
+ private void snackMsg (String msg ) {
103
+ Snackbar .make (((Activity ) mContext ).findViewById (R .id .coordinator_layout ), msg , Snackbar .LENGTH_LONG ).show ();
104
+ }
105
+
106
+ private void snackMsgWithAction (String msg ){
107
+ Snackbar snackbar = Snackbar .make (((Activity ) mContext ).findViewById (R .id .coordinator_layout ),msg ,Snackbar .LENGTH_LONG );
108
+ snackbar .setAction (R .string .turn_on , new View .OnClickListener () {
109
+ @ Override
110
+ public void onClick (View view ) {
111
+ // start intent to turn on GPS
112
+ Intent onGPS = new Intent (Settings .ACTION_LOCATION_SOURCE_SETTINGS );
113
+ mContext .startActivity (onGPS );
114
+ }
115
+ });
116
+ snackbar .show ();
79
117
}
80
118
}
0 commit comments