From a7fde4bf8948c45aa13f34aa98f5818799c1c3b8 Mon Sep 17 00:00:00 2001 From: Sharjeel Yunus <61178058+sharjeelyunus@users.noreply.github.com> Date: Sun, 13 Oct 2024 05:12:51 +0500 Subject: [PATCH] fix android permissions to include meta data as well --- starter/scripts/modules/enable_camera.dart | 8 +- starter/scripts/modules/enable_contacts.dart | 3 +- starter/scripts/modules/enable_deeplink.dart | 3 +- starter/scripts/modules/enable_files.dart | 3 +- starter/scripts/modules/enable_location.dart | 3 +- .../scripts/modules/enable_notifications.dart | 5 +- starter/scripts/utils.dart | 81 +++++++------------ 7 files changed, 44 insertions(+), 62 deletions(-) diff --git a/starter/scripts/modules/enable_camera.dart b/starter/scripts/modules/enable_camera.dart index 388739a86..3a63c33bd 100644 --- a/starter/scripts/modules/enable_camera.dart +++ b/starter/scripts/modules/enable_camera.dart @@ -35,10 +35,6 @@ ensemble_camera: } ]; - final androidPermissions = [ - '', - ]; - final iOSPermissions = [ { 'key': 'camera_description', @@ -59,7 +55,9 @@ ensemble_camera: // Add the camera permissions to AndroidManifest.xml if (platforms.contains('android')) { - updateAndroidPermissions(androidManifestFilePath, androidPermissions); + updateAndroidPermissions(androidManifestFilePath, permissions: [ + '' + ]); } // Add the camera usage description to the iOS Info.plist file diff --git a/starter/scripts/modules/enable_contacts.dart b/starter/scripts/modules/enable_contacts.dart index 0879d213f..36adeb5dc 100644 --- a/starter/scripts/modules/enable_contacts.dart +++ b/starter/scripts/modules/enable_contacts.dart @@ -50,7 +50,8 @@ ensemble_contacts: // Add the contacts permissions to AndroidManifest.xml if (platforms.contains('android')) { - updateAndroidPermissions(androidManifestFilePath, androidPermissions); + updateAndroidPermissions(androidManifestFilePath, + permissions: androidPermissions); } // Add the contacts usage description to the iOS Info.plist file diff --git a/starter/scripts/modules/enable_deeplink.dart b/starter/scripts/modules/enable_deeplink.dart index bd074d6b6..2e19a8e20 100644 --- a/starter/scripts/modules/enable_deeplink.dart +++ b/starter/scripts/modules/enable_deeplink.dart @@ -102,7 +102,8 @@ ensemble_deeplink: '', '', ]; - addMetaDataInAndroidManifest(androidManifestFilePath, branchMetaData); + updateAndroidPermissions(androidManifestFilePath, + metaData: branchMetaData); updateAndroidManifestWithDeeplink( androidManifestFilePath, scheme: scheme ?? '', diff --git a/starter/scripts/modules/enable_files.dart b/starter/scripts/modules/enable_files.dart index d4c761df7..7770a668a 100644 --- a/starter/scripts/modules/enable_files.dart +++ b/starter/scripts/modules/enable_files.dart @@ -72,7 +72,8 @@ ensemble_file_manager: // Add the storage permissions to AndroidManifest.xml if (platforms.contains('android')) { - updateAndroidPermissions(androidManifestFilePath, androidPermissions); + updateAndroidPermissions(androidManifestFilePath, + permissions: androidPermissions); } // Add the required keys and descriptions to the Info.plist file for iOS diff --git a/starter/scripts/modules/enable_location.dart b/starter/scripts/modules/enable_location.dart index 3b6efe4a5..42635d6a7 100644 --- a/starter/scripts/modules/enable_location.dart +++ b/starter/scripts/modules/enable_location.dart @@ -68,7 +68,8 @@ ensemble_location: // Add the location permissions to AndroidManifest.xml if (platforms.contains('android')) { - updateAndroidPermissions(androidManifestFilePath, androidPermissions); + updateAndroidPermissions(androidManifestFilePath, + permissions: androidPermissions); } // Add the location usage description to the iOS Info.plist file diff --git a/starter/scripts/modules/enable_notifications.dart b/starter/scripts/modules/enable_notifications.dart index c15b99806..47b49578a 100644 --- a/starter/scripts/modules/enable_notifications.dart +++ b/starter/scripts/modules/enable_notifications.dart @@ -72,9 +72,8 @@ void main(List arguments) { // Configure Android-specific settings if (platforms.contains('android')) { - updateAndroidPermissions(androidManifestFilePath, androidPermissions); - addMetaDataInAndroidManifest( - androidManifestFilePath, notificationsMetaData); + updateAndroidPermissions(androidManifestFilePath, + permissions: androidPermissions, metaData: notificationsMetaData); } // Configure iOS-specific settings diff --git a/starter/scripts/utils.dart b/starter/scripts/utils.dart index b8673a813..cf8ccea01 100644 --- a/starter/scripts/utils.dart +++ b/starter/scripts/utils.dart @@ -61,22 +61,6 @@ void writeFileContent(String filePath, String content) { file.writeAsStringSync(content); } -// Add permission to AndroidManifest.xml -void addPermissionToAndroidManifest( - String manifestPath, String comment, String permission) { - String manifestContent = readFileContent(manifestPath); - - if (!manifestContent.contains(permission)) { - manifestContent = manifestContent.replaceFirst( - comment, - '$comment\n $permission', - ); - writeFileContent(manifestPath, manifestContent); - } else { - throw Exception('Permission already exists in AndroidManifest.xml'); - } -} - // Add permission descriptions to Info.plist void addPermissionDescriptionToInfoPlist( String plistPath, String key, dynamic description, @@ -216,20 +200,41 @@ void updatePubspec( } // Update AndroidManifest.xml with permissions and throw error if not updated -void updateAndroidPermissions( - String manifestFilePath, List permissions) { +void updateAndroidPermissions(String manifestFilePath, + {List? permissions, List? metaData}) { String manifestContent = readFileContent(manifestFilePath); - for (var permission in permissions) { - // Check if the permission already exists in the manifest - if (!manifestContent.contains(permission)) { - // Add the permission if it doesn't exist - addPermissionToAndroidManifest( - manifestFilePath, - '', - permission); + if (permissions != null && permissions.isNotEmpty) { + String comment = + ''; + + for (var permission in permissions) { + if (!manifestContent.contains(permission)) { + manifestContent = manifestContent.replaceFirst( + comment, + '$comment\n $permission', + ); + } + } + } + + // Handle meta-data if provided + if (metaData != null && metaData.isNotEmpty) { + final applicationEndIndex = manifestContent.lastIndexOf(''); + if (applicationEndIndex == -1) { + throw Exception( + 'Error: Could not find tag in AndroidManifest.xml'); + } + + for (String metaDataContent in metaData) { + if (!manifestContent.contains(metaDataContent)) { + manifestContent = manifestContent.replaceRange(applicationEndIndex, + applicationEndIndex, ' $metaDataContent\n '); + } } } + + writeFileContent(manifestFilePath, manifestContent); } // Update Info.plist for iOS with permissions and descriptions @@ -381,30 +386,6 @@ $deeplinkEntries } } -void addMetaDataInAndroidManifest( - String manifestFilePath, List metaDataContents) { - File manifestFile = File(manifestFilePath); - if (!manifestFile.existsSync()) { - throw Exception('Error: File does not exist at $manifestFilePath'); - } - String manifestContent = manifestFile.readAsStringSync(); - - final applicationEndIndex = manifestContent.lastIndexOf(''); - if (applicationEndIndex == -1) { - throw Exception( - 'Error: Could not find tag in AndroidManifest.xml'); - } - - // Iterate over each meta-data item in the array and add if it's missing - for (String metaDataContent in metaDataContents) { - if (!manifestContent.contains(metaDataContent)) { - manifestContent = manifestContent.replaceRange(applicationEndIndex, - applicationEndIndex, ' $metaDataContent\n '); - } - } - manifestFile.writeAsStringSync(manifestContent); -} - void updateFirebaseInitialization( List platforms, { String? androidApiKey,