Skip to content

Commit

Permalink
fix android permissions to include meta data as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sharjeelyunus committed Oct 13, 2024
1 parent 281df3d commit a7fde4b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 62 deletions.
8 changes: 3 additions & 5 deletions starter/scripts/modules/enable_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ ensemble_camera:
}
];

final androidPermissions = [
'<uses-permission android:name="android.permission.CAMERA" />',
];

final iOSPermissions = [
{
'key': 'camera_description',
Expand All @@ -59,7 +55,9 @@ ensemble_camera:

// Add the camera permissions to AndroidManifest.xml
if (platforms.contains('android')) {
updateAndroidPermissions(androidManifestFilePath, androidPermissions);
updateAndroidPermissions(androidManifestFilePath, permissions: [
'<uses-permission android:name="android.permission.CAMERA" />'
]);
}

// Add the camera usage description to the iOS Info.plist file
Expand Down
3 changes: 2 additions & 1 deletion starter/scripts/modules/enable_contacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion starter/scripts/modules/enable_deeplink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ ensemble_deeplink:
'<meta-data android:name="io.branch.sdk.BranchKey.test" android:value="$branchIOTestKey" />',
'<meta-data android:name="io.branch.sdk.TestMode" android:value="${useTestKey.toString().capitalize()}" />',
];
addMetaDataInAndroidManifest(androidManifestFilePath, branchMetaData);
updateAndroidPermissions(androidManifestFilePath,
metaData: branchMetaData);
updateAndroidManifestWithDeeplink(
androidManifestFilePath,
scheme: scheme ?? '',
Expand Down
3 changes: 2 additions & 1 deletion starter/scripts/modules/enable_files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion starter/scripts/modules/enable_location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions starter/scripts/modules/enable_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ void main(List<String> 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
Expand Down
81 changes: 31 additions & 50 deletions starter/scripts/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -216,20 +200,41 @@ void updatePubspec(
}

// Update AndroidManifest.xml with permissions and throw error if not updated
void updateAndroidPermissions(
String manifestFilePath, List<String> permissions) {
void updateAndroidPermissions(String manifestFilePath,
{List<String>? permissions, List<String>? 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,
'<!-- UPDATE for your Starter. These are default permissions -->',
permission);
if (permissions != null && permissions.isNotEmpty) {
String comment =
'<!-- UPDATE for your Starter. These are default permissions -->';

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('</application>');
if (applicationEndIndex == -1) {
throw Exception(
'Error: Could not find </application> 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
Expand Down Expand Up @@ -381,30 +386,6 @@ $deeplinkEntries
}
}

void addMetaDataInAndroidManifest(
String manifestFilePath, List<String> 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('</application>');
if (applicationEndIndex == -1) {
throw Exception(
'Error: Could not find </application> 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<String> platforms, {
String? androidApiKey,
Expand Down

0 comments on commit a7fde4b

Please sign in to comment.