Android 6.0 permissions handling for location and Audio-Video notes plugin

This commit is contained in:
GaidamakUA 2015-12-04 11:26:41 +02:00
parent 233cec7962
commit 8ca050b01b
6 changed files with 196 additions and 84 deletions

View file

@ -25,5 +25,7 @@
<string name="tag_poi_name">name</string> <string name="tag_poi_name">name</string>
<string name="hint_tag">Tag</string> <string name="hint_tag">Tag</string>
<string name="hint_value">Value</string> <string name="hint_value">Value</string>
<string name="no_location_permission">App have no permission to access location data.</string>
<string name="no_camera_permission">App have no permission to access camera.</string>
<string name="no_microphone_permission">App have no permission to access microphone.</string>
</resources> </resources>

View file

@ -1,8 +1,10 @@
package net.osmand.plus; package net.osmand.plus;
import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.GeomagneticField; import android.hardware.GeomagneticField;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
@ -17,8 +19,11 @@ import android.location.LocationManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import net.osmand.GeoidAltitudeCorrection; import net.osmand.GeoidAltitudeCorrection;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
@ -41,6 +46,8 @@ import java.util.List;
public class OsmAndLocationProvider implements SensorEventListener { public class OsmAndLocationProvider implements SensorEventListener {
public static final int REQUEST_LOCATION_PERMISSION = 100;
private static final String SIMULATED_PROVIDER = "OsmAnd"; private static final String SIMULATED_PROVIDER = "OsmAnd";
@ -230,6 +237,7 @@ public class OsmAndLocationProvider implements SensorEventListener {
} }
} }
} }
if (isLocationPermissionAvailable(app)) {
service.addGpsStatusListener(getGpsStatusListener(service)); service.addGpsStatusListener(getGpsStatusListener(service));
try { try {
service.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_TIMEOUT_REQUEST, GPS_DIST_REQUEST, gpsListener); service.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_TIMEOUT_REQUEST, GPS_DIST_REQUEST, gpsListener);
@ -255,6 +263,7 @@ public class OsmAndLocationProvider implements SensorEventListener {
} }
} }
} }
}
public void redownloadAGPS() { public void redownloadAGPS() {
try { try {
@ -332,6 +341,9 @@ public class OsmAndLocationProvider implements SensorEventListener {
} }
public net.osmand.Location getFirstTimeRunDefaultLocation() { public net.osmand.Location getFirstTimeRunDefaultLocation() {
if (!isLocationPermissionAvailable(app)) {
return null;
}
LocationManager service = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE); LocationManager service = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE);
List<String> ps = service.getProviders(true); List<String> ps = service.getProviders(true);
if(ps == null) { if(ps == null) {
@ -914,4 +926,12 @@ public class OsmAndLocationProvider implements SensorEventListener {
return true; return true;
} }
public static boolean isLocationPermissionAvailable(Context context) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, R.string.no_location_permission, Toast.LENGTH_LONG).show();
return false;
}
return true;
}
} }

View file

@ -211,6 +211,17 @@ public abstract class OsmandPlugin {
public void mapActivityScreenOff(MapActivity activity) { public void mapActivityScreenOff(MapActivity activity) {
} }
public void handleRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, MapActivity activity) {
}
public static final void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, MapActivity activity) {
for (OsmandPlugin plugin : getAvailablePlugins()) {
plugin.handleRequestPermissionsResult(requestCode, permissions, grantResults, activity);
}
}
public boolean destinationReached() { public boolean destinationReached() {
return true; return true;
} }
@ -443,5 +454,4 @@ public abstract class OsmandPlugin {
p.addMyPlacesTab(favoritesActivity, mTabs, intent); p.addMyPlacesTab(favoritesActivity, mTabs, intent);
} }
} }
} }

View file

@ -16,6 +16,7 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.NotificationCompat; import android.support.v7.app.NotificationCompat;
@ -95,7 +96,8 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class MapActivity extends AccessibleActivity implements DownloadEvents { public class MapActivity extends AccessibleActivity implements DownloadEvents,
ActivityCompat.OnRequestPermissionsResultCallback {
private static final int SHOW_POSITION_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 1; private static final int SHOW_POSITION_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 1;
private static final int LONG_KEYPRESS_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 2; private static final int LONG_KEYPRESS_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 2;
private static final int LONG_KEYPRESS_DELAY = 500; private static final int LONG_KEYPRESS_DELAY = 500;
@ -687,7 +689,6 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents {
if (atlasMapRendererView != null) { if (atlasMapRendererView != null) {
atlasMapRendererView.handleOnDestroy(); atlasMapRendererView.handleOnDestroy();
} }
} }
private void cancelNotification() { private void cancelNotification() {
@ -1054,6 +1055,12 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents {
} }
} }
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
OsmandPlugin.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private class ScreenOffReceiver extends BroadcastReceiver { private class ScreenOffReceiver extends BroadcastReceiver {
@Override @Override

View file

@ -1,5 +1,6 @@
package net.osmand.plus.audionotes; package net.osmand.plus.audionotes;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.content.ComponentName; import android.content.ComponentName;
@ -7,6 +8,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options; import android.graphics.BitmapFactory.Options;
@ -23,6 +25,7 @@ import android.media.SoundPool;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.view.Display; import android.view.Display;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Surface; import android.view.Surface;
@ -93,6 +96,9 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
public static final String MPEG4_EXTENSION = "mp4"; public static final String MPEG4_EXTENSION = "mp4";
public static final String IMG_EXTENSION = "jpg"; public static final String IMG_EXTENSION = "jpg";
private static final Log log = PlatformUtil.getLog(AudioVideoNotesPlugin.class); private static final Log log = PlatformUtil.getLog(AudioVideoNotesPlugin.class);
public static final int CAMERA_FOR_VIDEO_REQUEST_CODE = 101;
public static final int CAMERA_FOR_PHOTO_REQUEST_CODE = 102;
public static final int AUDIO_REQUEST_CODE = 103;
private static Method mRegisterMediaButtonEventReceiver; private static Method mRegisterMediaButtonEventReceiver;
private static Method mUnregisterMediaButtonEventReceiver; private static Method mUnregisterMediaButtonEventReceiver;
private OsmandApplication app; private OsmandApplication app;
@ -140,6 +146,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
private File lastTakingPhoto; private File lastTakingPhoto;
private final static char SPLIT_DESC = ' '; private final static char SPLIT_DESC = ' ';
private double tempLat;
private double tempLon;
public static class Recording { public static class Recording {
public Recording(File f) { public Recording(File f) {
@ -463,6 +471,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
public String getHelpFileName() { public String getHelpFileName() {
return "feature_articles/audio-video-notes-plugin.html"; return "feature_articles/audio-video-notes-plugin.html";
} }
@Override @Override
public boolean init(final OsmandApplication app, Activity activity) { public boolean init(final OsmandApplication app, Activity activity) {
initializeRemoteControlRegistrationMethods(); initializeRemoteControlRegistrationMethods();
@ -733,7 +742,18 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
if (AV_EXTERNAL_RECORDER.get()) { if (AV_EXTERNAL_RECORDER.get()) {
captureVideoExternal(lat, lon, mapActivity); captureVideoExternal(lat, lon, mapActivity);
} else { } else {
if (ActivityCompat.checkSelfPermission(mapActivity, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mapActivity, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED) {
recordVideoCamera(lat, lon, mapActivity); recordVideoCamera(lat, lon, mapActivity);
} else {
tempLat = lat;
tempLon = lon;
ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
CAMERA_FOR_VIDEO_REQUEST_CODE);
}
} }
} }
@ -830,6 +850,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
} }
public void recordAudio(double lat, double lon, final MapActivity mapActivity) { public void recordAudio(double lat, double lon, final MapActivity mapActivity) {
if (ActivityCompat.checkSelfPermission(mapActivity, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED) {
MediaRecorder mr = new MediaRecorder(); MediaRecorder mr = new MediaRecorder();
final File f = getBaseFileName(lat, lon, app, THREEGP_EXTENSION); final File f = getBaseFileName(lat, lon, app, THREEGP_EXTENSION);
mr.setAudioSource(MediaRecorder.AudioSource.MIC); mr.setAudioSource(MediaRecorder.AudioSource.MIC);
@ -842,19 +864,38 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
log.error("Error starting audio recorder ", e); log.error("Error starting audio recorder ", e);
AccessibleToast.makeText(app, app.getString(R.string.recording_error) + " : " + e.getMessage(), Toast.LENGTH_LONG).show(); AccessibleToast.makeText(app, app.getString(R.string.recording_error) + " : " + e.getMessage(), Toast.LENGTH_LONG).show();
} }
} else {
tempLat = lat;
tempLon = lon;
ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.RECORD_AUDIO},
AUDIO_REQUEST_CODE);
}
} }
public void takePhoto(final double lat, final double lon, final MapActivity mapActivity) { public void takePhoto(final double lat, final double lon, final MapActivity mapActivity) {
if (AV_EXTERNAL_PHOTO_CAM.get()) { if (AV_EXTERNAL_PHOTO_CAM.get()) {
takeIntentPhoto(lat, lon, mapActivity); takePhotoExternal(lat, lon, mapActivity);
} else { } else {
if (ActivityCompat.checkSelfPermission(mapActivity, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
takePhotoInternalOrExternal(lat, lon, mapActivity);
} else {
tempLat = lat;
tempLon = lon;
ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.CAMERA},
CAMERA_FOR_PHOTO_REQUEST_CODE);
}
}
}
private void takePhotoInternalOrExternal(double lat, double lon, MapActivity mapActivity) {
final Camera cam = openCamera(); final Camera cam = openCamera();
if (cam != null) { if (cam != null) {
takePhotoWithCamera(lat, lon, mapActivity, cam); takePhotoWithCamera(lat, lon, mapActivity, cam);
} else { } else {
takeIntentPhoto(lat, lon, mapActivity); takePhotoExternal(lat, lon, mapActivity);
}
} }
} }
@ -1012,7 +1053,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
} }
} }
private void takeIntentPhoto(double lat, double lon, final MapActivity mapActivity) { private void takePhotoExternal(double lat, double lon, final MapActivity mapActivity) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final File f = getBaseFileName(lat, lon, app, IMG_EXTENSION); final File f = getBaseFileName(lat, lon, app, IMG_EXTENSION);
lastTakingPhoto = f; lastTakingPhoto = f;
@ -1327,6 +1368,31 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
return false; return false;
} }
@Override
public void handleRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, MapActivity activity) {
if (requestCode == CAMERA_FOR_VIDEO_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recordVideoCamera(tempLat, tempLon, activity);
} else {
app.showToastMessage(R.string.no_camera_permission);
}
} else if (requestCode == CAMERA_FOR_PHOTO_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePhotoInternalOrExternal(tempLat, tempLon, activity);
} else {
app.showToastMessage(R.string.no_camera_permission);
}
} else if (requestCode == AUDIO_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
recordAudio(tempLat, tempLon, activity);
} else {
app.showToastMessage(R.string.no_microphone_permission);
}
}
}
public class AudioVideoPhotoHandler implements PictureCallback { public class AudioVideoPhotoHandler implements PictureCallback {
private File pictureFile; private File pictureFile;
private Dialog dlg; private Dialog dlg;

View file

@ -1,5 +1,6 @@
package net.osmand.plus.views; package net.osmand.plus.views;
import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -11,6 +12,7 @@ import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@ -28,6 +30,7 @@ import net.osmand.core.android.MapRendererContext;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
import net.osmand.plus.ApplicationMode; import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
@ -406,7 +409,13 @@ public class MapControlsLayer extends OsmandMapLayer {
backToLocation.setOnClickListener(new View.OnClickListener() { backToLocation.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if(OsmAndLocationProvider.isLocationPermissionAvailable(mapActivity)) {
mapActivity.getMapViewTrackingUtilities().backToLocationImpl(); mapActivity.getMapViewTrackingUtilities().backToLocationImpl();
} else {
ActivityCompat.requestPermissions(mapActivity,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION);
}
} }
}); });
controls.add(createHudButton(mapActivity.findViewById(R.id.map_app_mode_shadow), 0).setBg( controls.add(createHudButton(mapActivity.findViewById(R.id.map_app_mode_shadow), 0).setBg(
@ -444,7 +453,7 @@ public class MapControlsLayer extends OsmandMapLayer {
zoomText = (TextView) mapActivity.findViewById(R.id.map_app_mode_text); zoomText = (TextView) mapActivity.findViewById(R.id.map_app_mode_text);
View routePlanButton = mapActivity.findViewById(R.id.map_route_info_button); View routePlanButton = mapActivity.findViewById(R.id.map_route_info_button);
routePlanningBtn = createHudButton((ImageView) routePlanButton, R.drawable.map_directions).setBg( routePlanningBtn = createHudButton(routePlanButton, R.drawable.map_directions).setBg(
R.drawable.btn_round, R.drawable.btn_round_night); R.drawable.btn_round, R.drawable.btn_round_night);
controls.add(routePlanningBtn); controls.add(routePlanningBtn);
routePlanButton.setOnClickListener(new View.OnClickListener() { routePlanButton.setOnClickListener(new View.OnClickListener() {
@ -1021,6 +1030,4 @@ public class MapControlsLayer extends OsmandMapLayer {
} }
} }
} }
} }