Skip to content

Commit d6d5a81

Browse files
only21milclaudecodex
committed
fix(device): report failed Android permission changes
Grant and Revoke ignored every `pm` failure, so a mistyped or uninstalled app ID, or a permission the app does not declare, left the permission unchanged while the Device panel reported success. Keep running each compatibility permission, and fail with the first command error only when none of them applied, so Photos still works on images without READ_MEDIA_IMAGES. Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 67c0fc0 commit d6d5a81

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

‎apps/server/src/device/DeviceActions.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,70 @@ describe("runDeviceAction", () => {
227227
}),
228228
);
229229

230+
it.effect.each([
231+
["camera", "grant", 1],
232+
["photos", "revoke", 2],
233+
] as const)(
234+
"reports an Android %s %s that no pm command applied",
235+
([permission, decision, commands]) =>
236+
Effect.gen(function* () {
237+
const { ready, calls } = makeReady((call) => ({
238+
code: 1,
239+
stderr: `Unknown package: ${call.args.at(-1)}`,
240+
}));
241+
const error = yield* Effect.flip(
242+
runDeviceAction(ready, "android", {
243+
type: "setPermission",
244+
deviceId: "emulator-5554",
245+
appId: "com.example.missing",
246+
permission,
247+
decision,
248+
}),
249+
);
250+
expect(calls).toHaveLength(commands);
251+
expect(error._tag).toBe("DeviceOperationError");
252+
expect(error.operation).toBe("permission");
253+
const firstPermission = calls[0]?.args.at(-1);
254+
expect(firstPermission).toBe(
255+
permission === "camera"
256+
? "android.permission.CAMERA"
257+
: "android.permission.READ_MEDIA_IMAGES",
258+
);
259+
expect(error._tag === "DeviceOperationError" && error.cause).toMatchObject({
260+
stderr: `Unknown package: ${firstPermission}`,
261+
});
262+
}),
263+
);
264+
265+
it.effect("keeps a permission change that one compatibility permission applied", () =>
266+
Effect.gen(function* () {
267+
// Images before Android 13 do not define READ_MEDIA_IMAGES.
268+
const { ready, calls } = makeReady((call) =>
269+
call.args.at(-1) === "android.permission.READ_MEDIA_IMAGES" ? { code: 1 } : {},
270+
);
271+
yield* runDeviceAction(ready, "android", {
272+
type: "setPermission",
273+
deviceId: "emulator-5554",
274+
appId: "com.example.app",
275+
permission: "photos",
276+
decision: "grant",
277+
});
278+
expect(calls.map((call) => call.args.at(-1))).toEqual([
279+
"android.permission.READ_MEDIA_IMAGES",
280+
"android.permission.READ_EXTERNAL_STORAGE",
281+
]);
282+
expect(calls.at(-1)?.args).toEqual([
283+
"-s",
284+
"emulator-5554",
285+
"shell",
286+
"pm",
287+
"grant",
288+
"com.example.app",
289+
"android.permission.READ_EXTERNAL_STORAGE",
290+
]);
291+
}),
292+
);
293+
230294
it.effect("wraps a bare push string in an APNs alert and feeds it on stdin", () =>
231295
Effect.gen(function* () {
232296
const { ready, calls } = makeReady();

‎apps/server/src/device/DeviceActions.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type DeviceTextSize,
1818
} from "@t3tools/contracts";
1919
import * as Effect from "effect/Effect";
20+
import * as Result from "effect/Result";
2021
import * as Schema from "effect/Schema";
2122

2223
import type { DeviceHostReady } from "./DeviceHost.ts";
@@ -436,9 +437,13 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
436437
});
437438
}
438439
const verb = input.decision === "grant" ? "grant" : "revoke";
439-
for (const permission of permissions) {
440-
yield* shell(["pm", verb, input.appId, permission], "permission").pipe(Effect.ignore);
441-
}
440+
// `pm` rejects a permission the app or system image does not declare, and Photos lists both
441+
// the Android 13 and the older storage permission, so fail only when none of them applied.
442+
const results = yield* Effect.forEach(permissions, (permission) =>
443+
shell(["pm", verb, input.appId, permission], "permission").pipe(Effect.result),
444+
);
445+
const failure = results.find(Result.isFailure);
446+
if (failure && !results.some(Result.isSuccess)) return yield* failure.failure;
442447
return;
443448
}
444449
case "openUrl":

0 commit comments

Comments
 (0)