From 6a1205afccfdab169e1b21c9a3acf13d4f9d10ec Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Mon, 20 Nov 2023 13:07:33 -0300 Subject: [PATCH] fix: call media permissions in android 13+ (#5326) * add android permissions * create askAndroidMediaPermissions helper * ask android permission * fix i18n import * remove useless const * wip * revert login changes --- android/app/src/main/AndroidManifest.xml | 5 ++ .../helpers/askAndroidMediaPermissions.ts | 54 +++++++++++++++++++ app/lib/methods/helpers/index.ts | 1 + app/views/ShareListView/index.tsx | 18 +++---- 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 app/lib/methods/helpers/askAndroidMediaPermissions.ts diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 895f3da03..d3aab5b46 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -17,6 +17,11 @@ + + + + + + permissions.every(permission => permissionsStatus[permission] === PermissionsAndroid.RESULTS.GRANTED); + +/** + * Requests permission for reading media on Android. + * + * @return {Promise} A promise that resolves to a boolean indicating whether the permissions were granted. + */ +export const askAndroidMediaPermissions = async (): Promise => { + if (Platform.OS !== 'android') return true; + + // For Android versions that require the new permissions model (API Level >= 33) + if (Platform.constants.Version >= 33) { + const permissions = [ + 'android.permission.READ_MEDIA_IMAGES', + 'android.permission.READ_MEDIA_VIDEO', + 'android.permission.READ_MEDIA_AUDIO' + ]; + + const permissionsStatus = await PermissionsAndroid.requestMultiple(permissions as Permission[]); + return areAllPermissionsGranted(permissionsStatus, permissions); + } + + // For older Android versions + const result = await PermissionsAndroid.request( + PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, + readExternalStorageRationale + ); + + return result === PermissionsAndroid.RESULTS.GRANTED; +}; diff --git a/app/lib/methods/helpers/index.ts b/app/lib/methods/helpers/index.ts index 4098203a0..4284fbc92 100644 --- a/app/lib/methods/helpers/index.ts +++ b/app/lib/methods/helpers/index.ts @@ -15,3 +15,4 @@ export * from './url'; export * from './isValidEmail'; export * from './random'; export * from './image'; +export * from './askAndroidMediaPermissions'; diff --git a/app/views/ShareListView/index.tsx b/app/views/ShareListView/index.tsx index 5e0bc61f8..b0dbd1675 100644 --- a/app/views/ShareListView/index.tsx +++ b/app/views/ShareListView/index.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { StackNavigationProp } from '@react-navigation/stack'; -import { BackHandler, FlatList, Keyboard, PermissionsAndroid, ScrollView, Text, View, Rationale } from 'react-native'; +import { BackHandler, FlatList, Keyboard, ScrollView, Text, View } from 'react-native'; import ShareExtension from 'rn-extensions-share'; import * as FileSystem from 'expo-file-system'; import { connect } from 'react-redux'; @@ -24,7 +24,7 @@ import styles from './styles'; import ShareListHeader from './Header'; import { TServerModel, TSubscriptionModel } from '../../definitions'; import { ShareInsideStackParamList } from '../../definitions/navigationTypes'; -import { getRoomAvatar, isAndroid, isIOS } from '../../lib/methods/helpers'; +import { getRoomAvatar, isAndroid, isIOS, askAndroidMediaPermissions } from '../../lib/methods/helpers'; interface IDataFromShare { value: string; @@ -63,12 +63,6 @@ interface IShareListViewProps extends INavigationOption { theme: TSupportedThemes; } -const permission: Rationale = { - title: I18n.t('Read_External_Permission'), - message: I18n.t('Read_External_Permission_Message'), - buttonPositive: 'Ok' -}; - const getItemLayout = (data: any, index: number) => ({ length: data.length, offset: ROW_HEIGHT * index, index }); const keyExtractor = (item: TSubscriptionModel) => item.rid; @@ -282,8 +276,8 @@ class ShareListView extends React.Component { askForPermission = async (data: IDataFromShare[]) => { const mediaIndex = data.findIndex(item => item.type === 'media'); if (mediaIndex !== -1) { - const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, permission); - if (result !== PermissionsAndroid.RESULTS.GRANTED) { + const result = await askAndroidMediaPermissions(); + if (!result) { this.setState({ needsPermission: true }); return Promise.reject(); } @@ -441,8 +435,8 @@ class ShareListView extends React.Component { style={{ backgroundColor: themes[theme].backgroundColor }} contentContainerStyle={[styles.container, styles.centered, { backgroundColor: themes[theme].backgroundColor }]} > - {permission.title} - {permission.message} + {I18n.t('Read_External_Permission')} + {I18n.t('Read_External_Permission_Message')} );