3
3
import android .Manifest ;
4
4
import android .content .Context ;
5
5
import android .content .Intent ;
6
+ import android .content .SharedPreferences ;
6
7
import android .content .pm .PackageManager ;
7
8
import android .location .LocationManager ;
8
9
import android .net .Uri ;
9
10
import android .os .AsyncTask ;
10
11
import android .os .Build ;
11
12
import android .os .Bundle ;
13
+ import android .preference .PreferenceManager ;
12
14
import android .support .annotation .NonNull ;
13
15
import android .support .v4 .app .ActivityCompat ;
14
16
import android .support .v4 .app .Fragment ;
@@ -42,28 +44,46 @@ public class NearbyActivity extends NavigationBaseActivity {
42
44
43
45
@ BindView (R .id .progressBar )
44
46
ProgressBar progressBar ;
45
- private boolean isMapViewActive = false ;
46
47
private static final int LOCATION_REQUEST = 1 ;
48
+ private static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed" ;
47
49
48
50
private LocationServiceManager locationManager ;
49
51
private LatLng curLatLang ;
50
52
private Bundle bundle ;
51
53
private NearbyAsyncTask nearbyAsyncTask ;
54
+ private SharedPreferences sharedPreferences ;
55
+ private NearbyActivityMode viewMode ;
52
56
53
57
@ Override
54
58
protected void onCreate (Bundle savedInstanceState ) {
55
59
super .onCreate (savedInstanceState );
60
+ sharedPreferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
56
61
setContentView (R .layout .activity_nearby );
57
62
ButterKnife .bind (this );
58
63
checkLocationPermission ();
59
64
bundle = new Bundle ();
60
65
initDrawer ();
66
+ initViewState ();
67
+ }
68
+
69
+ private void initViewState () {
70
+ if (sharedPreferences .getBoolean (MAP_LAST_USED_PREFERENCE , false )) {
71
+ viewMode = NearbyActivityMode .MAP ;
72
+ } else {
73
+ viewMode = NearbyActivityMode .LIST ;
74
+ }
61
75
}
62
76
63
77
@ Override
64
78
public boolean onCreateOptionsMenu (Menu menu ) {
65
79
MenuInflater inflater = getMenuInflater ();
66
80
inflater .inflate (R .menu .menu_nearby , menu );
81
+
82
+ if (viewMode .isMap ()) {
83
+ MenuItem item = menu .findItem (R .id .action_toggle_view );
84
+ item .setIcon (viewMode .getIcon ());
85
+ }
86
+
67
87
return super .onCreateOptionsMenu (menu );
68
88
}
69
89
@@ -74,13 +94,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
74
94
case R .id .action_refresh :
75
95
refreshView ();
76
96
return true ;
77
- case R .id .action_map :
78
- showMapView ();
79
- if (isMapViewActive ) {
80
- item .setIcon (R .drawable .ic_list_white_24dp );
81
- } else {
82
- item .setIcon (R .drawable .ic_map_white_24dp );
83
- }
97
+ case R .id .action_toggle_view :
98
+ viewMode = viewMode .toggle ();
99
+ item .setIcon (viewMode .getIcon ());
100
+ toggleView ();
84
101
return true ;
85
102
default :
86
103
return super .onOptionsItemSelected (item );
@@ -158,15 +175,30 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
158
175
if (progressBar != null ) {
159
176
progressBar .setVisibility (View .GONE );
160
177
}
161
- FragmentTransaction fragmentTransaction = getSupportFragmentManager ().beginTransaction ();
162
- Fragment noPermissionsFragment = new NoPermissionsFragment ();
163
- fragmentTransaction .replace (R .id .container , noPermissionsFragment );
164
- fragmentTransaction .commit ();
178
+
179
+ showLocationPermissionDeniedErrorDialog ();
165
180
}
166
181
}
167
182
}
168
183
}
169
184
185
+ private void showLocationPermissionDeniedErrorDialog () {
186
+ new AlertDialog .Builder (this )
187
+ .setMessage (R .string .nearby_needs_permissions )
188
+ .setCancelable (false )
189
+ .setPositiveButton (R .string .give_permission , (dialog , which ) -> {
190
+ //will ask for the location permission again
191
+ checkLocationPermission ();
192
+ })
193
+ .setNegativeButton (R .string .cancel , (dialog , which ) -> {
194
+ //dismiss dialog and finish activity
195
+ dialog .cancel ();
196
+ finish ();
197
+ })
198
+ .create ()
199
+ .show ();
200
+ }
201
+
170
202
private void checkGps () {
171
203
LocationManager manager = (LocationManager ) getSystemService (LOCATION_SERVICE );
172
204
if (!manager .isProviderEnabled (LocationManager .GPS_PROVIDER )) {
@@ -198,20 +230,16 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
198
230
}
199
231
}
200
232
201
- private void showMapView () {
233
+ private void toggleView () {
202
234
if (nearbyAsyncTask != null ) {
203
- if (!isMapViewActive ) {
204
- isMapViewActive = true ;
205
- if (nearbyAsyncTask .getStatus () == AsyncTask .Status .FINISHED ) {
235
+ if (nearbyAsyncTask .getStatus () == AsyncTask .Status .FINISHED ) {
236
+ if (viewMode .isMap ()) {
206
237
setMapFragment ();
207
- }
208
-
209
- } else {
210
- isMapViewActive = false ;
211
- if (nearbyAsyncTask .getStatus () == AsyncTask .Status .FINISHED ) {
238
+ } else {
212
239
setListFragment ();
213
240
}
214
241
}
242
+ sharedPreferences .edit ().putBoolean (MAP_LAST_USED_PREFERENCE , viewMode .isMap ()).apply ();
215
243
}
216
244
}
217
245
@@ -287,7 +315,7 @@ protected void onPostExecute(List<Place> placeList) {
287
315
bundle .putString ("CurLatLng" , gsonCurLatLng );
288
316
289
317
// Begin the transaction
290
- if (isMapViewActive ) {
318
+ if (viewMode . isMap () ) {
291
319
setMapFragment ();
292
320
} else {
293
321
setListFragment ();
0 commit comments