Skip to content

Commit

Permalink
feat: separate request permission
Browse files Browse the repository at this point in the history
  • Loading branch information
wn-na committed Aug 31, 2024
1 parent 67b66bf commit c1e38e3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class CaptureProtectionModule extends ReactContextBaseJavaModule implemen
private final DisplayManager displayManager;
private final DisplayManager.DisplayListener displayListener;

private final String requestPermission = Build.VERSION.SDK_INT >= 33 // Build.VERSION_CODES.TIRAMISU
? "android.permission.READ_MEDIA_IMAGES" // Manifest.permission.READ_MEDIA_IMAGES
: Manifest.permission.READ_EXTERNAL_STORAGE;

private List<Integer> screens = new ArrayList<>();
private ContentObserver contentObserver = null;

Expand Down Expand Up @@ -202,9 +206,8 @@ public void onHostDestroy() {
}
}

private boolean requestStoragePermission() {
private boolean checkStoragePermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.d(NAME, "Permission is granted for under sdk version 23");
return true;
}

Expand All @@ -213,23 +216,32 @@ private boolean requestStoragePermission() {
}

try {
String requestPermission = Build.VERSION.SDK_INT >= 33 // Build.VERSION_CODES.TIRAMISU
? "android.permission.READ_MEDIA_IMAGES" // Manifest.permission.READ_MEDIA_IMAGES
: Manifest.permission.READ_EXTERNAL_STORAGE;

if (getReactCurrentActivity() == null) {
return false;
}

if (ContextCompat.checkSelfPermission(getReactCurrentActivity(),
requestPermission) == PackageManager.PERMISSION_GRANTED) {
return (ContextCompat.checkSelfPermission(getReactCurrentActivity(),
requestPermission) == PackageManager.PERMISSION_GRANTED);
} catch (Exception e) {
Log.e(NAME, "checkStoragePermission has raise Exception: " + e.getLocalizedMessage());
return false;
}
}

private boolean requestStoragePermission() {
try {
boolean isGranted = checkStoragePermission();
if (getReactCurrentActivity() == null) {
return false;
}
if (isGranted) {
Log.d(NAME, "Permission is granted");
return true;
} else {
Log.d(NAME, "Permission is revoked");
ActivityCompat.requestPermissions(getReactCurrentActivity(), new String[] { requestPermission }, 1);
return false;
}

Log.d(NAME, "Permission is revoked");
ActivityCompat.requestPermissions(getReactCurrentActivity(), new String[] { requestPermission }, 1);
return false;
} catch (Exception e) {
Log.e(NAME, "requestStoragePermission has raise Exception: " + e.getLocalizedMessage());
return false;
Expand All @@ -238,8 +250,7 @@ private boolean requestStoragePermission() {

private void addListener() {
if (getScreenCaptureCallback() == null) {
if (contentObserver == null) {
requestStoragePermission();
if (contentObserver == null && checkStoragePermission()) {
contentObserver = new ContentObserver(Utils.MainHandler.INSTANCE) {
@Override
public void onChange(boolean selfChange, Uri uri) {
Expand Down Expand Up @@ -424,4 +435,11 @@ public void requestPermission(Promise promise) {
promise.resolve(isPermission);
return;
}

@ReactMethod
public void checkPermission(Promise promise) {
boolean isPermission = checkStoragePermission();
promise.resolve(isPermission);
return;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-capture-protection",
"version": "1.9.11",
"version": "1.9.12",
"description": "It’s a library for React Native to control simple capture events(i.e. Screenshot or Screen record)",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
28 changes: 19 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import {
Image,
NativeEventEmitter,
Expand All @@ -9,13 +16,6 @@ import {
CaptureEventStatus,
CaptureProtectionModuleStatus,
} from './type';
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from 'react';

const LINKING_ERROR =
`The package 'react-native-capture-protection' doesn't seem to be linked. Make sure: \n\n` +
Expand Down Expand Up @@ -116,6 +116,7 @@ const allowScreenRecord = async (removeListener = false): Promise<void> => {
*/
const preventScreenRecord = async (isImmediate = false): Promise<void> => {
if (Platform.OS === 'android') {
await requestPermission();
return await CaptureProtectionModule?.preventScreenshot?.();
}
if (Platform.OS === 'ios') {
Expand Down Expand Up @@ -146,6 +147,7 @@ const allowScreenshot = async (removeListener = false): Promise<void> => {
*/
const preventScreenshot = async (): Promise<void> => {
if (Platform.OS === 'android') {
await requestPermission();
return await CaptureProtectionModule?.preventScreenshot?.();
}
if (Platform.OS === 'ios') {
Expand Down Expand Up @@ -265,7 +267,15 @@ const isScreenRecording = async (): Promise<boolean | undefined> => {
*/
const requestPermission = async (): Promise<boolean> => {
if (Platform.OS === 'android') {
return await CaptureProtectionModule?.requestPermission?.();
try {
return await CaptureProtectionModule?.requestPermission?.();
} catch (e) {
console.error(
'[React-native-capture-protection] requestPermission throw error',
e
);
return false;
}
} else {
console.error(
'[React-native-capture-protection] requestPermission is only available on Android'
Expand Down Expand Up @@ -450,4 +460,4 @@ export const CaptureProtection = {
preventBackground,
};

export { CaptureProtectionModuleStatus, CaptureEventStatus } from './type';
export { CaptureEventStatus, CaptureProtectionModuleStatus } from './type';

0 comments on commit c1e38e3

Please sign in to comment.