Skip to content

Commit

Permalink
Improved recent camera runtime permission code, included videos
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Mar 10, 2024
1 parent dbfc00d commit 0e66b3f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions omniNotes/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.CAMERA"/>

<uses-feature
android:name="android.hardware.camera"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ protected void onResume() {
LogDelegate.d(Prefs.getAll().toString());
}

protected void showToast(int resourceId, int duration) {
showToast(getResources().getString(resourceId), duration);
}

protected void showToast(CharSequence text, int duration) {
if (Prefs.getBoolean("settings_enable_info", true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static android.content.Context.CLIPBOARD_SERVICE;
import static android.content.Context.LAYOUT_INFLATER_SERVICE;
import static android.content.pm.PackageManager.FEATURE_CAMERA;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.widget.Toast.LENGTH_SHORT;
import static androidx.core.view.ViewCompat.animate;
import static it.feio.android.omninotes.BaseActivity.TRANSITION_HORIZONTAL;
import static it.feio.android.omninotes.BaseActivity.TRANSITION_VERTICAL;
Expand Down Expand Up @@ -73,7 +75,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand Down Expand Up @@ -112,10 +113,10 @@
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.content.ContextCompat;
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission;
import androidx.core.util.Pair;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentTransaction;
Expand All @@ -124,7 +125,6 @@
import com.neopixl.pixlui.components.edittext.EditText;
import com.pixplicity.easyprefs.library.Prefs;
import com.pushbullet.android.extension.MessagingExtension;
import com.tbruyelle.rxpermissions.RxPermissions;

import de.greenrobot.event.EventBus;
import de.keyboardsurfer.android.widget.crouton.Style;
Expand All @@ -135,7 +135,6 @@
import it.feio.android.checklistview.models.ChecklistManager;
import it.feio.android.omninotes.async.AttachmentTask;
import it.feio.android.omninotes.async.bus.NotesUpdatedEvent;
import it.feio.android.omninotes.async.bus.NotificationsGrantedEvent;
import it.feio.android.omninotes.async.bus.PushbulletReplyEvent;
import it.feio.android.omninotes.async.bus.SwitchFragmentEvent;
import it.feio.android.omninotes.async.notes.NoteProcessorDelete;
Expand All @@ -145,6 +144,7 @@
import it.feio.android.omninotes.exceptions.checked.ContentSecurityException;
import it.feio.android.omninotes.exceptions.checked.UnhandledIntentException;
import it.feio.android.omninotes.helpers.AttachmentsHelper;
import it.feio.android.omninotes.helpers.BuildHelper;
import it.feio.android.omninotes.helpers.IntentHelper;
import it.feio.android.omninotes.helpers.LogDelegate;
import it.feio.android.omninotes.helpers.PermissionsHelper;
Expand Down Expand Up @@ -192,7 +192,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -516,12 +515,12 @@ private void handleIntents() {

// Sub-action is to take a photo
if (IntentChecker.checkAction(i, ACTION_WIDGET_TAKE_PHOTO)) {
checkAndRequestPermissions();
requestCameraPermission(TAKE_PHOTO);
}
}

if (IntentChecker.checkAction(i, ACTION_FAB_TAKE_PHOTO)) {
checkAndRequestPermissions();
requestCameraPermission(TAKE_PHOTO);
}

// Handles third party apps requests of sharing
Expand Down Expand Up @@ -830,7 +829,7 @@ private void performAttachmentAction(int attachmentPosition, int i) {
Attachment attachment = mAttachmentAdapter.getItem(attachmentPosition);
Uri shareableAttachmentUri = mainActivity.getShareableAttachmentUri(attachment);
if (shareableAttachmentUri == null) {
Toast.makeText(getActivity(), R.string.error_saving_attachments, Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), R.string.error_saving_attachments, LENGTH_SHORT).show();
break;
}
shareIntent.setType(StorageHelper.getMimeType(getAppContext(), attachment.getUri()));
Expand Down Expand Up @@ -1043,7 +1042,7 @@ private boolean goHome() {
// performs a normal onBackPressed instead of returning back to ListActivity
if (!afterSavedReturnsToList) {
if (!TextUtils.isEmpty(exitMessage)) {
mainActivity.showToast(exitMessage, Toast.LENGTH_SHORT);
mainActivity.showToast(exitMessage, LENGTH_SHORT);
}
mainActivity.finish();

Expand Down Expand Up @@ -1338,30 +1337,45 @@ private void takePhoto() {

}

private void checkAndRequestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(mainActivity, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);

private void requestCameraPermission(int requestCode) {
if (BuildHelper.isAboveOrEqual(Build.VERSION_CODES.M)) {
if (ContextCompat.checkSelfPermission(mainActivity, permission.CAMERA) != PERMISSION_GRANTED) {
if (requestCode == TAKE_PHOTO) {
requestCameraPermissionLauncher.launch(permission.CAMERA);
} else if (requestCode == TAKE_VIDEO) {
requestCameraVideoPermissionLauncher.launch(permission.CAMERA);
}
} else {
// Permission already granted
takePhoto();
if (requestCode == TAKE_PHOTO) {
takePhoto();
} else if (requestCode == TAKE_VIDEO) {
takeVideo();
}
}
} else {
// Runtime permissions not needed before Marshmallow
takePhoto();
if (requestCode == TAKE_PHOTO) {
takePhoto();
} else if (requestCode == TAKE_VIDEO) {
takeVideo();
}
}
}
private final ActivityResultLauncher<String> requestPermissionLauncher =

private final ActivityResultLauncher<String> requestCameraPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
// Permission granted
if (Boolean.TRUE.equals(isGranted)) {
takePhoto();
} else {
// Permission denied
Toast.makeText(mainActivity,"Permission denied",Toast.LENGTH_SHORT).show();
mainActivity.showToast(R.string.permission_not_granted, LENGTH_SHORT);
}
});

private final ActivityResultLauncher<String> requestCameraVideoPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (Boolean.TRUE.equals(isGranted)) {
takeVideo();
} else {
mainActivity.showToast(R.string.permission_not_granted, LENGTH_SHORT);
}
});

Expand Down Expand Up @@ -2331,11 +2345,9 @@ private class AttachmentOnClickListener implements OnClickListener {

@Override
public void onClick(View v) {

switch (v.getId()) {
// Photo from camera
case R.id.camera:
takePhoto();
requestCameraPermission(TAKE_PHOTO);
break;
case R.id.recording:
if (!isRecording) {
Expand All @@ -2351,7 +2363,7 @@ public void onClick(View v) {
}
break;
case R.id.video:
takeVideo();
requestCameraPermission(TAKE_VIDEO);
break;
case R.id.files:
startGetContentAction();
Expand Down
1 change: 1 addition & 0 deletions omniNotes/src/main/res/raw/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
changeDate="Mar 8, 2024"
versionName="6.3.2">
<changelogtext>[b]Happy Women Day!![/b]</changelogtext>
<changelogtext>[i]Improved![/i] Ask for camera permission at runtime (thanks to [a href='https://github.com/pratistha-05']Pratistha Sinha[/a] and [a href='https://github.com/akashs056']Akash Subramanian[/a])</changelogtext>
<changelogtext>[i]Improved![/i] When trying to pin a note without notifications permissions enabled a message will be shown</changelogtext>
<changelogtext>[i]Improved![/i] Updated Gradle build tools to 8.3.0</changelogtext>
<changelogtext>[u]Fix[/u] Protected notes are no more searchable nor their tags (from content) will appear anywhere content's secrecy (thanks to [a href='https://github.com/XYIheng']Yiheng Xiong[/a])</changelogtext>
Expand Down

0 comments on commit 0e66b3f

Please sign in to comment.