Fix audio/video recording with permissions on Android 6
This commit is contained in:
parent
8df1c6ca33
commit
c85c4e52b7
5 changed files with 64 additions and 33 deletions
|
@ -1,9 +1,11 @@
|
|||
package net.osmand.plus;
|
||||
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.os.Build;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
import net.osmand.IProgress;
|
||||
|
@ -211,14 +213,15 @@ public abstract class OsmandPlugin {
|
|||
public void mapActivityScreenOff(MapActivity activity) {
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
public void handleRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
int[] grantResults, MapActivity activity) {
|
||||
int[] grantResults) {
|
||||
}
|
||||
|
||||
public static final void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
int[] grantResults, MapActivity activity) {
|
||||
int[] grantResults) {
|
||||
for (OsmandPlugin plugin : getAvailablePlugins()) {
|
||||
plugin.handleRequestPermissionsResult(requestCode, permissions, grantResults, activity);
|
||||
plugin.handleRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1057,7 +1057,7 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
|
|||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,9 @@ import net.osmand.plus.audionotes.AudioVideoNotesPlugin.CurrentRecording;
|
|||
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
|
||||
|
||||
public class AudioVideoNoteRecordingMenu {
|
||||
|
@ -27,9 +30,9 @@ public class AudioVideoNoteRecordingMenu {
|
|||
private AudioVideoNotesPlugin plugin;
|
||||
private long startTime;
|
||||
private Handler handler;
|
||||
private boolean counting;
|
||||
private boolean portraitMode;
|
||||
private boolean largeDevice;
|
||||
private Timer recTimer;
|
||||
|
||||
public static boolean showViewfinder = true;
|
||||
|
||||
|
@ -184,23 +187,30 @@ public class AudioVideoNoteRecordingMenu {
|
|||
|
||||
private void startCounter() {
|
||||
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() {
|
||||
counting = false;
|
||||
}
|
||||
|
||||
private void updateCounter() {
|
||||
updateDuration();
|
||||
if (counting) {
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateCounter();
|
||||
}
|
||||
}, 1000);
|
||||
if (recTimer != null) {
|
||||
recTimer.cancel();
|
||||
recTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package net.osmand.plus.audionotes;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
|
@ -162,8 +163,10 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
private Timer playerTimer;
|
||||
|
||||
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 {
|
||||
REC_AUDIO,
|
||||
|
@ -774,10 +777,14 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
this.mapActivity = activity;
|
||||
((AudioManager) activity.getSystemService(Context.AUDIO_SERVICE)).registerMediaButtonEventReceiver(
|
||||
new ComponentName(activity, MediaRemoteControlReceiver.class));
|
||||
if (runAction != -1) {
|
||||
takeAction(activity, actionLat, actionLon, runAction);
|
||||
runAction = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mapActivityDestroy(MapActivity activity) {
|
||||
public void mapActivityPause(MapActivity activity) {
|
||||
if (isRecording()) {
|
||||
if (currentRecording.getType() == AVActionType.REC_PHOTO) {
|
||||
recordingMenu.hide();
|
||||
|
@ -819,8 +826,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
recordVideoCamera(lat, lon, mapActivity);
|
||||
}
|
||||
} else {
|
||||
tempLat = lat;
|
||||
tempLon = lon;
|
||||
actionLat = lat;
|
||||
actionLon = lon;
|
||||
ActivityCompat.requestPermissions(mapActivity,
|
||||
new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
|
||||
CAMERA_FOR_VIDEO_REQUEST_CODE);
|
||||
|
@ -1001,8 +1008,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
+ e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} else {
|
||||
tempLat = lat;
|
||||
tempLon = lon;
|
||||
actionLat = lat;
|
||||
actionLon = lon;
|
||||
ActivityCompat.requestPermissions(mapActivity,
|
||||
new String[]{Manifest.permission.RECORD_AUDIO},
|
||||
AUDIO_REQUEST_CODE);
|
||||
|
@ -1017,8 +1024,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
== PackageManager.PERMISSION_GRANTED) {
|
||||
takePhotoInternalOrExternal(lat, lon, mapActivity);
|
||||
} else {
|
||||
tempLat = lat;
|
||||
tempLon = lon;
|
||||
actionLat = lat;
|
||||
actionLon = lon;
|
||||
ActivityCompat.requestPermissions(mapActivity,
|
||||
new String[]{Manifest.permission.CAMERA},
|
||||
CAMERA_FOR_PHOTO_REQUEST_CODE);
|
||||
|
@ -1029,6 +1036,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
private void takePhotoInternalOrExternal(double lat, double lon, MapActivity mapActivity) {
|
||||
openCamera();
|
||||
if (cam != null) {
|
||||
initRecMenu(AVActionType.REC_PHOTO);
|
||||
takePhotoWithCamera(lat, lon, mapActivity);
|
||||
} else {
|
||||
takePhotoExternal(lat, lon, mapActivity);
|
||||
|
@ -1586,29 +1594,36 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
|
|||
return false;
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
@Override
|
||||
public void handleRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
int[] grantResults, MapActivity activity) {
|
||||
int[] grantResults) {
|
||||
runAction = -1;
|
||||
if (requestCode == CAMERA_FOR_VIDEO_REQUEST_CODE) {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED
|
||||
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
|
||||
recordVideoCamera(tempLat, tempLon, activity);
|
||||
runAction = AV_DEFAULT_ACTION_VIDEO;
|
||||
} 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);
|
||||
runAction = AV_DEFAULT_ACTION_TAKEPICTURE;
|
||||
} else {
|
||||
app.showToastMessage(R.string.no_camera_permission);
|
||||
}
|
||||
} else if (requestCode == AUDIO_REQUEST_CODE) {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
recordAudio(tempLat, tempLon, activity);
|
||||
runAction = AV_DEFAULT_ACTION_AUDIO;
|
||||
} else {
|
||||
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 {
|
||||
|
|
|
@ -941,7 +941,10 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
|
|||
public void dismissMenu() {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.getSupportFragmentManager().popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||
try {
|
||||
activity.getSupportFragmentManager().popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue