diff --git a/apps/app/lib/lifecycle.native.ts b/apps/app/lib/lifecycle.native.ts index 7adbadb4..940025c5 100644 --- a/apps/app/lib/lifecycle.native.ts +++ b/apps/app/lib/lifecycle.native.ts @@ -1,4 +1,4 @@ -import { AppState, BackHandler } from 'react-native' +import { Alert, AppState, BackHandler, PermissionsAndroid } from 'react-native' import { EventType } from '@notifee/react-native' import * as transmission from './transmission' @@ -7,6 +7,8 @@ import { destroyPersistentNotification, registerEvents, } from './persistentNotification' +import { requestStoragePermission } from './permissionsAndroid' +import i18n from '../i18n' const handleEvents = async ({ type, detail }) => { // Start all @@ -34,16 +36,29 @@ const handleEvents = async ({ type, detail }) => { let isInitialized = false -const init = () => { +const createPermissionDeniedAlert = () => + Alert.alert( + i18n.t('alerts.storagePermissionDenied.title'), + i18n.t('alerts.storagePermissionDenied.description'), + [{ text: 'Quit', onPress: quitApp }] + ) + +const init = async () => { + isInitialized = true + // Init transmission & persistent notification transmission.init() createPersistentNotification() - isInitialized = true + + const granted = await requestStoragePermission() + if (granted !== PermissionsAndroid.RESULTS.GRANTED) { + createPermissionDeniedAlert() + } } export const initApp = async () => { // Next time the app become active, re-init if quitApp has been called - AppState.addEventListener('change', (nextAppState) => { + AppState.addEventListener('change', async (nextAppState) => { if (nextAppState === 'active' && !isInitialized) { init() } @@ -52,8 +67,6 @@ export const initApp = async () => { // background events handler should be registered only once registerEvents(handleEvents) init() - - isInitialized = true } export const quitApp = async () => { diff --git a/apps/app/lib/permissionsAndroid.ts b/apps/app/lib/permissionsAndroid.ts new file mode 100644 index 00000000..848d5d96 --- /dev/null +++ b/apps/app/lib/permissionsAndroid.ts @@ -0,0 +1,15 @@ +import { PermissionsAndroid } from 'react-native' +import i18n from '../i18n' + +export const requestStoragePermission = async () => { + const granted = await PermissionsAndroid.request( + PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, + { + title: i18n.t('alerts.storagePermissionRequest.title'), + message: i18n.t('alerts.storagePermissionRequest.description'), + buttonPositive: 'OK', + } + ) + + return granted +} diff --git a/apps/app/locales/en.json b/apps/app/locales/en.json index c53a4eff..c2050e0f 100644 --- a/apps/app/locales/en.json +++ b/apps/app/locales/en.json @@ -179,5 +179,15 @@ "library": { "download": "Download", "moreToCome": "More to come..." + }, + "alerts": { + "storagePermissionRequest": { + "title": "Storage permission", + "description": "PikaTorrent needs access to storage to save downloaded data." + }, + "storagePermissionDenied": { + "title": "Storage permission denied", + "description": "'You need to allow storage permission for PikaTorrent.'" + } } }