Fix audio/video recording with permissions on Android 6

This commit is contained in:
Alexey Kulish 2015-12-04 17:22:13 +03:00
parent 8df1c6ca33
commit c85c4e52b7
5 changed files with 64 additions and 33 deletions

View file

@ -1,9 +1,11 @@
package net.osmand.plus; package net.osmand.plus;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import net.osmand.IProgress; import net.osmand.IProgress;
@ -211,14 +213,15 @@ public abstract class OsmandPlugin {
public void mapActivityScreenOff(MapActivity activity) { public void mapActivityScreenOff(MapActivity activity) {
} }
@TargetApi(Build.VERSION_CODES.M)
public void handleRequestPermissionsResult(int requestCode, String[] permissions, public void handleRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, MapActivity activity) { int[] grantResults) {
} }
public static final void onRequestPermissionsResult(int requestCode, String[] permissions, public static final void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, MapActivity activity) { int[] grantResults) {
for (OsmandPlugin plugin : getAvailablePlugins()) { for (OsmandPlugin plugin : getAvailablePlugins()) {
plugin.handleRequestPermissionsResult(requestCode, permissions, grantResults, activity); plugin.handleRequestPermissionsResult(requestCode, permissions, grantResults);
} }
} }

View file

@ -1057,7 +1057,7 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
OsmandPlugin.onRequestPermissionsResult(requestCode, permissions, grantResults, this); OsmandPlugin.onRequestPermissionsResult(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} }

View file

@ -18,6 +18,9 @@ import net.osmand.plus.audionotes.AudioVideoNotesPlugin.CurrentRecording;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.util.Timer;
import java.util.TimerTask;
import static android.util.TypedValue.COMPLEX_UNIT_DIP; import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class AudioVideoNoteRecordingMenu { public class AudioVideoNoteRecordingMenu {
@ -27,9 +30,9 @@ public class AudioVideoNoteRecordingMenu {
private AudioVideoNotesPlugin plugin; private AudioVideoNotesPlugin plugin;
private long startTime; private long startTime;
private Handler handler; private Handler handler;
private boolean counting;
private boolean portraitMode; private boolean portraitMode;
private boolean largeDevice; private boolean largeDevice;
private Timer recTimer;
public static boolean showViewfinder = true; public static boolean showViewfinder = true;
@ -184,23 +187,30 @@ public class AudioVideoNoteRecordingMenu {
private void startCounter() { private void startCounter() {
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
counting = true;
updateCounter(); if (recTimer != null) {
recTimer.cancel();
}
recTimer = new Timer();
recTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
updateDuration();
}
});
}
}, 0, 1000);
} }
private void stopCounter() { private void stopCounter() {
counting = false; if (recTimer != null) {
} recTimer.cancel();
recTimer = null;
private void updateCounter() {
updateDuration();
if (counting) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
updateCounter();
}
}, 1000);
} }
} }

View file

@ -1,6 +1,7 @@
package net.osmand.plus.audionotes; package net.osmand.plus.audionotes;
import android.Manifest; import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -162,8 +163,10 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
private Timer playerTimer; private Timer playerTimer;
private final static char SPLIT_DESC = ' '; private final static char SPLIT_DESC = ' ';
private double tempLat;
private double tempLon; private double actionLat;
private double actionLon;
private int runAction = -1;
public enum AVActionType { public enum AVActionType {
REC_AUDIO, REC_AUDIO,
@ -774,10 +777,14 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
this.mapActivity = activity; this.mapActivity = activity;
((AudioManager) activity.getSystemService(Context.AUDIO_SERVICE)).registerMediaButtonEventReceiver( ((AudioManager) activity.getSystemService(Context.AUDIO_SERVICE)).registerMediaButtonEventReceiver(
new ComponentName(activity, MediaRemoteControlReceiver.class)); new ComponentName(activity, MediaRemoteControlReceiver.class));
if (runAction != -1) {
takeAction(activity, actionLat, actionLon, runAction);
runAction = -1;
}
} }
@Override @Override
public void mapActivityDestroy(MapActivity activity) { public void mapActivityPause(MapActivity activity) {
if (isRecording()) { if (isRecording()) {
if (currentRecording.getType() == AVActionType.REC_PHOTO) { if (currentRecording.getType() == AVActionType.REC_PHOTO) {
recordingMenu.hide(); recordingMenu.hide();
@ -819,8 +826,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
recordVideoCamera(lat, lon, mapActivity); recordVideoCamera(lat, lon, mapActivity);
} }
} else { } else {
tempLat = lat; actionLat = lat;
tempLon = lon; actionLon = lon;
ActivityCompat.requestPermissions(mapActivity, ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
CAMERA_FOR_VIDEO_REQUEST_CODE); CAMERA_FOR_VIDEO_REQUEST_CODE);
@ -1001,8 +1008,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
+ e.getMessage(), Toast.LENGTH_LONG).show(); + e.getMessage(), Toast.LENGTH_LONG).show();
} }
} else { } else {
tempLat = lat; actionLat = lat;
tempLon = lon; actionLon = lon;
ActivityCompat.requestPermissions(mapActivity, ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.RECORD_AUDIO}, new String[]{Manifest.permission.RECORD_AUDIO},
AUDIO_REQUEST_CODE); AUDIO_REQUEST_CODE);
@ -1017,8 +1024,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
== PackageManager.PERMISSION_GRANTED) { == PackageManager.PERMISSION_GRANTED) {
takePhotoInternalOrExternal(lat, lon, mapActivity); takePhotoInternalOrExternal(lat, lon, mapActivity);
} else { } else {
tempLat = lat; actionLat = lat;
tempLon = lon; actionLon = lon;
ActivityCompat.requestPermissions(mapActivity, ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.CAMERA}, new String[]{Manifest.permission.CAMERA},
CAMERA_FOR_PHOTO_REQUEST_CODE); CAMERA_FOR_PHOTO_REQUEST_CODE);
@ -1029,6 +1036,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
private void takePhotoInternalOrExternal(double lat, double lon, MapActivity mapActivity) { private void takePhotoInternalOrExternal(double lat, double lon, MapActivity mapActivity) {
openCamera(); openCamera();
if (cam != null) { if (cam != null) {
initRecMenu(AVActionType.REC_PHOTO);
takePhotoWithCamera(lat, lon, mapActivity); takePhotoWithCamera(lat, lon, mapActivity);
} else { } else {
takePhotoExternal(lat, lon, mapActivity); takePhotoExternal(lat, lon, mapActivity);
@ -1586,29 +1594,36 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
return false; return false;
} }
@TargetApi(Build.VERSION_CODES.M)
@Override @Override
public void handleRequestPermissionsResult(int requestCode, String[] permissions, public void handleRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, MapActivity activity) { int[] grantResults) {
runAction = -1;
if (requestCode == CAMERA_FOR_VIDEO_REQUEST_CODE) { if (requestCode == CAMERA_FOR_VIDEO_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED if (grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) { && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recordVideoCamera(tempLat, tempLon, activity); runAction = AV_DEFAULT_ACTION_VIDEO;
} else { } else {
app.showToastMessage(R.string.no_camera_permission); app.showToastMessage(R.string.no_camera_permission);
} }
} else if (requestCode == CAMERA_FOR_PHOTO_REQUEST_CODE) { } else if (requestCode == CAMERA_FOR_PHOTO_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePhotoInternalOrExternal(tempLat, tempLon, activity); runAction = AV_DEFAULT_ACTION_TAKEPICTURE;
} else { } else {
app.showToastMessage(R.string.no_camera_permission); app.showToastMessage(R.string.no_camera_permission);
} }
} else if (requestCode == AUDIO_REQUEST_CODE) { } else if (requestCode == AUDIO_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
recordAudio(tempLat, tempLon, activity); runAction = AV_DEFAULT_ACTION_AUDIO;
} else { } else {
app.showToastMessage(R.string.no_microphone_permission); app.showToastMessage(R.string.no_microphone_permission);
} }
} }
MapActivity mapActivity = getMapActivity();
if (mapActivity != null && !mapActivity.isDestroyed()) {
takeAction(mapActivity, actionLat, actionLon, runAction);
runAction = -1;
}
} }
public class AudioVideoPhotoHandler implements PictureCallback { public class AudioVideoPhotoHandler implements PictureCallback {

View file

@ -941,7 +941,10 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
public void dismissMenu() { public void dismissMenu() {
FragmentActivity activity = getActivity(); FragmentActivity activity = getActivity();
if (activity != null) { if (activity != null) {
try {
activity.getSupportFragmentManager().popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); activity.getSupportFragmentManager().popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} catch (Exception e) {
}
} }
} }