Add permission check for location

This commit is contained in:
misaochan 2016-08-22 18:25:16 +12:00
parent 043c970999
commit 1530ddc969
2 changed files with 25 additions and 5 deletions

View file

@ -151,9 +151,20 @@ public class ContributionsListFragment extends Fragment {
} }
return true; return true;
case R.id.menu_nearby: case R.id.menu_nearby:
//TODO: Check for permissions if API 23+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent nearbyIntent = new Intent(getActivity(), NearbyActivity.class); if (ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
startActivity(nearbyIntent); //See http://stackoverflow.com/questions/33169455/onrequestpermissionsresult-not-being-called-in-dialog-fragment
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 2);
return false;
} else {
Intent nearbyIntent = new Intent(getActivity(), NearbyActivity.class);
startActivity(nearbyIntent);
}
}
else {
Intent nearbyIntent = new Intent(getActivity(), NearbyActivity.class);
startActivity(nearbyIntent);
}
case R.id.menu_refresh: case R.id.menu_refresh:
((SourceRefresher)getActivity()).refreshSource(); ((SourceRefresher)getActivity()).refreshSource();
return true; return true;
@ -172,6 +183,14 @@ public class ContributionsListFragment extends Fragment {
controller.startGalleryPick(); controller.startGalleryPick();
} }
} }
// 2 = Location allowed when 'nearby places' selected
case 2: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("ContributionsList", "Location permission granted");
Intent nearbyIntent = new Intent(getActivity(), NearbyActivity.class);
startActivity(nearbyIntent);
}
}
} }
} }

View file

@ -23,7 +23,7 @@ public class NearbyActivity extends AppCompatActivity {
private double currentLatitude, currentLongitude; private double currentLatitude, currentLongitude;
private String gpsCoords; private String gpsCoords;
private static final String TAG = "NearbyActivty"; private static final String TAG = "NearbyActivity";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -48,7 +48,7 @@ public class NearbyActivity extends AppCompatActivity {
* Registers a LocationManager to listen for current location * Registers a LocationManager to listen for current location
*/ */
protected void registerLocationManager() { protected void registerLocationManager() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria(); criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true); provider = locationManager.getBestProvider(criteria, true);
myLocationListener = new MyLocationListener(); myLocationListener = new MyLocationListener();
@ -83,6 +83,7 @@ public class NearbyActivity extends AppCompatActivity {
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude(); currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude(); currentLongitude = location.getLongitude();
Log.d(TAG, "Latitude: " + String.valueOf(currentLatitude) + " Longitude: " + String.valueOf(currentLongitude));
} }
@Override @Override