Add check for camera permission

This commit is contained in:
Vitaliy 2020-01-22 17:12:24 +02:00
parent ee1eb4382b
commit fd8d4be6ee
4 changed files with 162 additions and 17 deletions

View file

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_padding_half"
android:layout_marginRight="@dimen/content_padding_half"
android:background="?attr/list_background_color">
<LinearLayout
android:id="@+id/selectable_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/bottom_sheet_list_item_height"
android:orientation="horizontal">
<ImageView
android:id="@android:id/icon"
android:layout_width="@dimen/standard_icon_size"
android:layout_height="@dimen/standard_icon_size"
android:layout_marginLeft="@dimen/content_padding_half"
android:layout_marginTop="@dimen/content_padding_small"
android:layout_marginRight="@dimen/content_padding_half"
tools:src="@drawable/ic_action_lock" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="@dimen/dialog_content_margin"
android:paddingLeft="@dimen/dialog_content_margin">
<net.osmand.plus.widgets.TextViewEx
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/content_padding_small"
android:textAppearance="@style/TextAppearance.ListItemTitle"
osmand:typeface="@string/font_roboto_medium"
tools:text="@string/permission_is_required" />
<net.osmand.plus.widgets.TextViewEx
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:letterSpacing="@dimen/description_letter_spacing"
android:maxLines="1"
android:paddingTop="@dimen/content_padding_small"
android:paddingBottom="@dimen/content_padding_small"
android:textColor="?attr/active_color_basic"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:text="@string/give_permission" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

View file

@ -11,6 +11,7 @@
Thx - Hardy Thx - Hardy
--> -->
<string name="permission_is_required">Permission is required to use this option.</string>
<string name="logcat_buffer_descr">Check and share detailed logs of the application</string> <string name="logcat_buffer_descr">Check and share detailed logs of the application</string>
<string name="file_does_not_contain_routing_rules">No routing rules in \'%1$s\'. Please choose another file.</string> <string name="file_does_not_contain_routing_rules">No routing rules in \'%1$s\'. Please choose another file.</string>
<string name="not_support_file_type_with_ext">Select a supported %1$s extension file instead.</string> <string name="not_support_file_type_with_ext">Select a supported %1$s extension file instead.</string>

View file

@ -8,6 +8,13 @@
android:layout="@layout/preference_category_with_descr" android:layout="@layout/preference_category_with_descr"
android:title="@string/photo_notes" /> android:title="@string/photo_notes" />
<Preference
android:icon="@drawable/ic_action_lock"
android:key="camera_permission"
android:layout="@layout/preference_permission"
android:summary="@string/give_permission"
android:title="@string/permission_is_required" />
<net.osmand.plus.settings.preferences.SwitchPreferenceEx <net.osmand.plus.settings.preferences.SwitchPreferenceEx
android:key="av_external_cam" android:key="av_external_cam"
android:layout="@layout/preference_with_descr_dialog_and_switch" android:layout="@layout/preference_with_descr_dialog_and_switch"

View file

@ -1,15 +1,24 @@
package net.osmand.plus.audionotes; package net.osmand.plus.audionotes;
import android.Manifest;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.hardware.Camera; import android.hardware.Camera;
import android.media.CamcorderProfile; import android.media.CamcorderProfile;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.os.Build; import android.os.Build;
import android.os.StatFs; import android.os.StatFs;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.SpannableString; import android.text.SpannableString;
import android.view.View;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -17,6 +26,7 @@ import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmAndAppCustomization; import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.helpers.FontCache; import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet; import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener; import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener;
@ -45,8 +55,11 @@ import static net.osmand.plus.audionotes.AudioVideoNotesPlugin.cameraPictureSize
public class MultimediaNotesFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener { public class MultimediaNotesFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener {
public static final int CAMERA_FOR_PHOTO_PARAMS_REQUEST_CODE = 104;
private static final Log log = PlatformUtil.getLog(MultimediaNotesFragment.class); private static final Log log = PlatformUtil.getLog(MultimediaNotesFragment.class);
private static final String CAMERA_PERMISSION = "camera_permission";
private static final String COPY_PLUGIN_SETTINGS = "copy_plugin_settings"; private static final String COPY_PLUGIN_SETTINGS = "copy_plugin_settings";
private static final String RESET_TO_DEFAULT = "reset_to_default"; private static final String RESET_TO_DEFAULT = "reset_to_default";
private static final String OPEN_NOTES = "open_notes"; private static final String OPEN_NOTES = "open_notes";
@ -55,15 +68,7 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
protected void setupPreferences() { protected void setupPreferences() {
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class); AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
if (plugin != null) { if (plugin != null) {
setupCameraPhotoPrefs(plugin);
Camera cam = openCamera();
if (cam != null) {
setupExternalPhotoCamPref(plugin);
setupCameraPictureSizePref(cam, plugin);
setupCameraFocusTypePref(cam, plugin);
setupPhotoPlaySoundPref(plugin);
cam.release();
}
setupAudioFormatPref(plugin); setupAudioFormatPref(plugin);
setupAudioBitratePref(plugin); setupAudioBitratePref(plugin);
@ -83,13 +88,44 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
} }
} }
private void setupExternalPhotoCamPref(AudioVideoNotesPlugin plugin) { private void setupCameraPhotoPrefs(AudioVideoNotesPlugin plugin) {
Camera cam = openCamera();
setupCameraPermissionPref(cam);
setupExternalPhotoCamPref(cam, plugin);
setupCameraPictureSizePref(cam, plugin);
setupCameraFocusTypePref(cam, plugin);
setupPhotoPlaySoundPref(cam, plugin);
if (cam != null) {
cam.release();
}
}
private void setupCameraPermissionPref(Camera cam) {
Context ctx = getContext();
if (ctx == null) {
return;
}
Preference cameraPermission = findPreference(CAMERA_PERMISSION);
boolean permissionGranted = ActivityCompat.checkSelfPermission(ctx, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
cameraPermission.setVisible(cam == null && !permissionGranted);
}
private void setupExternalPhotoCamPref(Camera cam, AudioVideoNotesPlugin plugin) {
SwitchPreferenceEx externalPhotoCam = (SwitchPreferenceEx) findPreference(plugin.AV_EXTERNAL_PHOTO_CAM.getId()); SwitchPreferenceEx externalPhotoCam = (SwitchPreferenceEx) findPreference(plugin.AV_EXTERNAL_PHOTO_CAM.getId());
externalPhotoCam.setDescription(getString(R.string.av_use_external_camera_descr)); externalPhotoCam.setDescription(getString(R.string.av_use_external_camera_descr));
externalPhotoCam.setIcon(getActiveIcon(R.drawable.ic_action_photo_dark)); externalPhotoCam.setIcon(getActiveIcon(R.drawable.ic_action_photo_dark));
externalPhotoCam.setEnabled(cam != null);
} }
private void setupCameraPictureSizePref(Camera cam, AudioVideoNotesPlugin plugin) { private void setupCameraPictureSizePref(Camera cam, AudioVideoNotesPlugin plugin) {
ListPreferenceEx cameraPictureSize = (ListPreferenceEx) findPreference(plugin.AV_CAMERA_PICTURE_SIZE.getId());
cameraPictureSize.setDescription(R.string.av_camera_pic_size_descr);
cameraPictureSize.setIcon(getActiveIcon(R.drawable.ic_action_picture_size));
if (cam == null) {
cameraPictureSize.setEnabled(false);
return;
}
Camera.Parameters parameters = cam.getParameters(); Camera.Parameters parameters = cam.getParameters();
// Photo picture size // Photo picture size
@ -145,18 +181,24 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
String[] entries = itemsPicSizes.toArray(new String[0]); String[] entries = itemsPicSizes.toArray(new String[0]);
Integer[] entryValues = picSizesValues.toArray(new Integer[0]); Integer[] entryValues = picSizesValues.toArray(new Integer[0]);
ListPreferenceEx cameraPictureSize = (ListPreferenceEx) findPreference(plugin.AV_CAMERA_PICTURE_SIZE.getId());
if (entries.length > 0) { if (entries.length > 0) {
cameraPictureSize.setEntries(entries); cameraPictureSize.setEntries(entries);
cameraPictureSize.setEntryValues(entryValues); cameraPictureSize.setEntryValues(entryValues);
cameraPictureSize.setDescription(R.string.av_camera_pic_size_descr);
cameraPictureSize.setIcon(getActiveIcon(R.drawable.ic_action_picture_size));
} else { } else {
cameraPictureSize.setVisible(false); cameraPictureSize.setVisible(false);
} }
} }
private void setupCameraFocusTypePref(Camera cam, AudioVideoNotesPlugin plugin) { private void setupCameraFocusTypePref(Camera cam, AudioVideoNotesPlugin plugin) {
ListPreferenceEx cameraFocusType = (ListPreferenceEx) findPreference(plugin.AV_CAMERA_FOCUS_TYPE.getId());
cameraFocusType.setDescription(R.string.av_camera_focus_descr);
cameraFocusType.setIcon(getActiveIcon(R.drawable.ic_action_camera_focus));
if (cam == null) {
cameraFocusType.setEnabled(false);
return;
}
Camera.Parameters parameters = cam.getParameters(); Camera.Parameters parameters = cam.getParameters();
// focus mode settings // focus mode settings
@ -190,21 +232,19 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
String[] entries = items.toArray(new String[0]); String[] entries = items.toArray(new String[0]);
Integer[] entryValues = itemsValues.toArray(new Integer[0]); Integer[] entryValues = itemsValues.toArray(new Integer[0]);
ListPreferenceEx cameraFocusType = (ListPreferenceEx) findPreference(plugin.AV_CAMERA_FOCUS_TYPE.getId());
if (entries.length > 0) { if (entries.length > 0) {
cameraFocusType.setEntries(entries); cameraFocusType.setEntries(entries);
cameraFocusType.setEntryValues(entryValues); cameraFocusType.setEntryValues(entryValues);
cameraFocusType.setDescription(R.string.av_camera_focus_descr);
cameraFocusType.setIcon(getActiveIcon(R.drawable.ic_action_camera_focus));
} else { } else {
cameraFocusType.setVisible(false); cameraFocusType.setVisible(false);
} }
} }
private void setupPhotoPlaySoundPref(AudioVideoNotesPlugin plugin) { private void setupPhotoPlaySoundPref(Camera cam, AudioVideoNotesPlugin plugin) {
SwitchPreferenceEx photoPlaySound = (SwitchPreferenceEx) findPreference(plugin.AV_PHOTO_PLAY_SOUND.getId()); SwitchPreferenceEx photoPlaySound = (SwitchPreferenceEx) findPreference(plugin.AV_PHOTO_PLAY_SOUND.getId());
photoPlaySound.setDescription(getString(R.string.av_photo_play_sound_descr)); photoPlaySound.setDescription(getString(R.string.av_photo_play_sound_descr));
photoPlaySound.setIcon(getContentIcon(R.drawable.ic_action_music_off)); photoPlaySound.setIcon(getContentIcon(R.drawable.ic_action_music_off));
photoPlaySound.setEnabled(cam != null);
} }
private void setupAudioFormatPref(AudioVideoNotesPlugin plugin) { private void setupAudioFormatPref(AudioVideoNotesPlugin plugin) {
@ -380,6 +420,8 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
if (fragmentManager != null) { if (fragmentManager != null) {
ResetProfilePrefsBottomSheet.showInstance(fragmentManager, prefId, this, false, getSelectedAppMode()); ResetProfilePrefsBottomSheet.showInstance(fragmentManager, prefId, this, false, getSelectedAppMode());
} }
} else if (CAMERA_PERMISSION.equals(prefId)) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, CAMERA_FOR_PHOTO_PARAMS_REQUEST_CODE);
} }
return super.onPreferenceClick(preference); return super.onPreferenceClick(preference);
} }
@ -393,6 +435,41 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
} }
} }
@Override
protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) {
super.onBindPreferenceViewHolder(preference, holder);
if (CAMERA_PERMISSION.equals(preference.getKey())) {
View selectableView = holder.itemView.findViewById(R.id.selectable_list_item);
if (selectableView != null) {
int color = AndroidUtils.getColorFromAttr(app, R.attr.activity_background_color);
int selectedColor = UiUtilities.getColorWithAlpha(getActiveProfileColor(), 0.3f);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color);
Drawable selectable = getPaintedIcon(R.drawable.ripple_rectangle_rounded, selectedColor);
Drawable[] layers = {bgDrawable, selectable};
AndroidUtils.setBackground(selectableView, new LayerDrawable(layers));
} else {
Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color);
AndroidUtils.setBackground(selectableView, bgDrawable);
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_FOR_PHOTO_PARAMS_REQUEST_CODE && grantResults.length > 0
&& permissions.length > 0 && Manifest.permission.CAMERA.equals(permissions[0])) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
updateAllSettings();
} else {
app.showToastMessage(R.string.no_camera_permission);
}
}
}
@Override @Override
public void copyAppModePrefs(ApplicationMode appMode) { public void copyAppModePrefs(ApplicationMode appMode) {
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class); AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);