18
18
19
19
package com .thanksmister .iot .mqtt .alarmpanel .ui .fragments ;
20
20
21
+ import android .Manifest ;
21
22
import android .content .Context ;
23
+ import android .content .DialogInterface ;
24
+ import android .content .Intent ;
22
25
import android .content .SharedPreferences ;
26
+ import android .content .pm .PackageManager ;
23
27
import android .location .Criteria ;
24
28
import android .location .Location ;
25
29
import android .location .LocationListener ;
26
30
import android .location .LocationManager ;
31
+ import android .os .Build ;
27
32
import android .os .Bundle ;
33
+ import android .os .Handler ;
34
+ import android .support .annotation .NonNull ;
35
+ import android .support .v4 .app .ActivityCompat ;
36
+ import android .support .v7 .app .AlertDialog ;
28
37
import android .support .v7 .preference .CheckBoxPreference ;
29
38
import android .support .v7 .preference .EditTextPreference ;
30
39
import android .support .v7 .preference .PreferenceFragmentCompat ;
31
40
import android .text .TextUtils ;
41
+ import android .view .Menu ;
42
+ import android .view .MenuInflater ;
43
+ import android .view .MenuItem ;
32
44
import android .view .View ;
33
45
import android .widget .Toast ;
34
46
44
56
public class WeatherSettingsFragment extends PreferenceFragmentCompat
45
57
implements SharedPreferences .OnSharedPreferenceChangeListener {
46
58
59
+ private static final int REQUEST_PERMISSIONS = 6 ;
47
60
private static final long HOUR_MILLIS = 60 * 60 * 1000 ;
48
61
private static final int METERS_MIN = 500 ;
49
62
@@ -54,22 +67,42 @@ public class WeatherSettingsFragment extends PreferenceFragmentCompat
54
67
private EditTextPreference weatherLongitude ;
55
68
private Configuration configuration ;
56
69
private LocationManager locationManager ;
70
+ private Handler locationHandler ;
57
71
58
72
@ Override
59
73
public void onCreate (Bundle savedInstanceState ) {
60
74
super .onCreate (savedInstanceState );
75
+ setHasOptionsMenu (true );
61
76
addPreferencesFromResource (preferences_weather );
62
77
}
63
78
64
79
@ Override
65
80
public void onCreatePreferences (Bundle savedInstanceState , String rootKey ) {
66
-
81
+ }
82
+
83
+ @ Override
84
+ public void onCreateOptionsMenu (Menu menu , MenuInflater inflater ) {
85
+ inflater .inflate (R .menu .search , menu );
86
+ super .onCreateOptionsMenu (menu , inflater );
87
+ }
88
+
89
+ @ Override
90
+ public boolean onOptionsItemSelected (MenuItem item ) {
91
+ switch (item .getItemId ()) {
92
+ case R .id .action_location :
93
+ checkLocationEnabled ();
94
+ return true ;
95
+ }
96
+ return false ;
67
97
}
68
98
69
99
@ Override
70
100
public void onResume () {
71
101
super .onResume ();
72
102
getPreferenceScreen ().getSharedPreferences ().registerOnSharedPreferenceChangeListener (this );
103
+ if (TextUtils .isEmpty (configuration .getLongitude ()) || TextUtils .isEmpty (configuration .getLatitude ()) ) {
104
+ checkLocationEnabled (); // check that we have location permissions
105
+ }
73
106
}
74
107
75
108
@ Override
@@ -81,6 +114,9 @@ public void onPause() {
81
114
@ Override
82
115
public void onDestroyView () {
83
116
super .onDestroyView ();
117
+ if (locationHandler != null ) {
118
+ locationHandler .removeCallbacks (locationRunnable );
119
+ }
84
120
}
85
121
86
122
@ Override
@@ -128,6 +164,40 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
128
164
weatherLongitude .setEnabled (configuration .showWeatherModule ());
129
165
}
130
166
167
+ public void checkLocationEnabled () {
168
+ if (isAdded () && getActivity () != null ) {
169
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
170
+ if (ActivityCompat .checkSelfPermission (getActivity (), Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED
171
+ && ActivityCompat .checkSelfPermission (getActivity (), Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
172
+ requestPermissions (new String []{Manifest .permission .ACCESS_COARSE_LOCATION }, REQUEST_PERMISSIONS );
173
+ return ;
174
+ }
175
+ }
176
+ setUpLocationMonitoring ();
177
+ }
178
+ }
179
+
180
+ @ Override
181
+ public void onRequestPermissionsResult (int requestCode , @ NonNull String permissions [], @ NonNull int [] grantResults ) {
182
+ switch (requestCode ) {
183
+ case REQUEST_PERMISSIONS : {
184
+ if (grantResults .length > 0 ) {
185
+ boolean permissionsDenied = false ;
186
+ for (int permission : grantResults ) {
187
+ if (permission != PackageManager .PERMISSION_GRANTED ) {
188
+ permissionsDenied = true ;
189
+ break ;
190
+ }
191
+ }
192
+ if (!permissionsDenied ) {
193
+ setUpLocationMonitoring ();
194
+ }
195
+ }
196
+ }
197
+ }
198
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
199
+ }
200
+
131
201
@ Override
132
202
public void onSharedPreferenceChanged (SharedPreferences sharedPreferences , String key ) {
133
203
@@ -179,19 +249,20 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
179
249
180
250
private void setUpLocationMonitoring () {
181
251
252
+ Timber .d ("setUpLocationMonitoring" );
182
253
locationManager = (LocationManager ) getActivity ().getSystemService (Context .LOCATION_SERVICE );
183
-
184
254
Criteria criteria = new Criteria ();
185
255
criteria .setAccuracy (Criteria .ACCURACY_COARSE );
186
256
187
257
try {
188
- LocationListener locationListener = new LocationListener () {
258
+ final LocationListener locationListener = new LocationListener () {
189
259
@ Override
190
260
public void onLocationChanged (Location location ) {
191
261
if (location != null ) {
192
262
String latitude = String .valueOf (location .getLatitude ());
193
263
String longitude = String .valueOf (location .getLongitude ());
194
264
if (LocationUtils .coordinatesValid (latitude , longitude )){
265
+ Timber .d ("setUpLocationMonitoring complete" );
195
266
configuration .setLat (String .valueOf (location .getLatitude ()));
196
267
configuration .setLon (String .valueOf (location .getLongitude ()));
197
268
weatherLatitude .setSummary (String .valueOf (configuration .getLatitude ()));
@@ -217,6 +288,8 @@ public void onProviderEnabled(String provider) {
217
288
@ Override
218
289
public void onProviderDisabled (String provider ) {
219
290
Timber .d ("onProviderDisabled" );
291
+ locationHandler = new Handler ();
292
+ locationHandler .postDelayed (locationRunnable , 500 );
220
293
}
221
294
};
222
295
locationManager .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , HOUR_MILLIS , 0 , locationListener );
@@ -225,4 +298,26 @@ public void onProviderDisabled(String provider) {
225
298
Toast .makeText (getActivity (), R .string .toast_invalid_provider , Toast .LENGTH_SHORT ).show ();
226
299
}
227
300
}
301
+
302
+ private Runnable locationRunnable = new Runnable () {
303
+ @ Override
304
+ public void run () {
305
+ if (isAdded ()) { // Without this in certain cases application will show ANR
306
+ AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
307
+ builder .setMessage (R .string .string_location_services_disabled ).setCancelable (false ).setPositiveButton (android .R .string .ok , new DialogInterface .OnClickListener () {
308
+ public void onClick (DialogInterface dialog , int id ) {
309
+ Intent gpsOptionsIntent = new Intent (android .provider .Settings .ACTION_LOCATION_SOURCE_SETTINGS );
310
+ startActivity (gpsOptionsIntent );
311
+ }
312
+ });
313
+ builder .setNegativeButton (android .R .string .cancel , new DialogInterface .OnClickListener () {
314
+ public void onClick (DialogInterface dialog , int id ) {
315
+ dialog .cancel ();
316
+ }
317
+ });
318
+ AlertDialog alert = builder .create ();
319
+ alert .show ();
320
+ }
321
+ }
322
+ };
228
323
}
0 commit comments