Skip to content

Commit

Permalink
added a check before adding it to the manifest and updated the test
Browse files Browse the repository at this point in the history
  • Loading branch information
RevanToma committed Nov 22, 2024
1 parent 4655783 commit cc6c354
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
18 changes: 15 additions & 3 deletions config-plugin/withMotionActivityPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ const withMotionActivityPermissions: ConfigPlugin = (config) => {
config = withAndroidManifest(config, (config) => {
const androidManifest = config.modResults;
const permissions = androidManifest.manifest["uses-permission"] || [];
permissions.push({
$: { "android:name": "android.permission.ACTIVITY_RECOGNITION" },
});

// Check if the permission already exists
const hasActivityRecognitionPermission = permissions.some(
(permission) =>
permission.$["android:name"] ===
"android.permission.ACTIVITY_RECOGNITION",
);

// Add the permission only if it doesn't exist
if (!hasActivityRecognitionPermission) {
permissions.push({
$: { "android:name": "android.permission.ACTIVITY_RECOGNITION" },
});
}

androidManifest.manifest["uses-permission"] = permissions;
return config;
});
Expand Down
44 changes: 37 additions & 7 deletions tests/withMotionActivityPermissions.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { withInfoPlist, withAndroidManifest } from "@expo/config-plugins";
import withMotionActivityPermissions from "../config-plugin/withMotionActivityPermissions";
import type { ExpoConfig } from "@expo/config-types";

import withMotionActivityPermissions from "../config-plugin/withMotionActivityPermissions";

interface ExpoConfigWithModResults extends ExpoConfig {
modResults?: {
// For iOS
NSMotionUsageDescription?: string;
// For Android
manifest?: {
"uses-permission"?: Array<{ $: { "android:name": string } }>;
"uses-permission"?: { $: { "android:name": string } }[];
};
};
}
Expand All @@ -19,7 +20,7 @@ jest.mock("@expo/config-plugins", () => ({
...config,
modResults: {
...config.modResults,
NSMotionUsageDescription: undefined,
NSMotionUsageDescription: undefined,
},
};
const modifiedConfig = callback(updatedConfig);
Expand All @@ -35,7 +36,7 @@ jest.mock("@expo/config-plugins", () => ({
...config.modResults,
manifest: {
...config.modResults?.manifest,
"uses-permission": [],
"uses-permission": [],
},
},
};
Expand All @@ -61,13 +62,13 @@ describe("withMotionActivityPermissions", () => {
withMotionActivityPermissions(config);

expect(updatedConfig.modResults?.NSMotionUsageDescription).toBe(
"This app uses motion activity tracking."
"This app uses motion activity tracking.",
);

expect(withInfoPlist).toHaveBeenCalled();
});

it("should add Android motion activity recognition permission", () => {
it("should add Android motion activity recognition permission if it does not exist", () => {
const config: ExpoConfigWithModResults = {
name: "mock-app",
slug: "mock-app",
Expand All @@ -85,7 +86,36 @@ describe("withMotionActivityPermissions", () => {
expect(permissions).toEqual(
expect.arrayContaining([
{ $: { "android:name": "android.permission.ACTIVITY_RECOGNITION" } },
])
]),
);

expect(withAndroidManifest).toHaveBeenCalled();
});

it("should not add duplicate Android motion activity recognition permission", () => {
const config: ExpoConfigWithModResults = {
name: "mock-app",
slug: "mock-app",
modResults: {
manifest: {
"uses-permission": [
{
$: { "android:name": "android.permission.ACTIVITY_RECOGNITION" },
},
],
},
},
};

const updatedConfig: ExpoConfigWithModResults =
withMotionActivityPermissions(config);

const permissions = updatedConfig.modResults?.manifest?.["uses-permission"];
expect(permissions).toHaveLength(1); // Ensure no duplicates are added
expect(permissions).toEqual(
expect.arrayContaining([
{ $: { "android:name": "android.permission.ACTIVITY_RECOGNITION" } },
]),
);

expect(withAndroidManifest).toHaveBeenCalled();
Expand Down

0 comments on commit cc6c354

Please sign in to comment.