-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from GeoTecINIT/exact-alarm-perm
Android 12 exact alarm permissions + fixes to battery savings permission request
- Loading branch information
Showing
13 changed files
with
258 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...ts/internal/tasks/schedulers/time-based/android/exact-alarm-perms-manager.android.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ExactAlarmPermsManager } from "nativescript-task-dispatcher/internal/tasks/schedulers/time-based/android/alarms/exact-alarm-perms-manager.android"; | ||
import { | ||
createOsAlarmManagerMock, | ||
createOsForegroundActivityMock, | ||
isSdkBelow, | ||
} from "~/tests/internal/tasks/schedulers/time-based/android/index"; | ||
import { createScheduleExactAlarmPermRequestIntent } from "nativescript-task-dispatcher/internal/tasks/schedulers/time-based/android/intents.android"; | ||
import { Utils } from "@nativescript/core"; | ||
|
||
describe("Exact alarm perms manager", () => { | ||
if (typeof android === "undefined") { | ||
return; | ||
} | ||
const alarmManagerMock = createOsAlarmManagerMock(); | ||
const foregroundActivityMock = createOsForegroundActivityMock(); | ||
beforeEach(() => { | ||
spyOn(alarmManagerMock, "canScheduleExactAlarms").and.returnValue( | ||
false | ||
); | ||
spyOn(foregroundActivityMock, "startActivity"); | ||
}); | ||
|
||
it("scheduling exact alarms is allowed by default when api level is lower than 31", () => { | ||
const exactAlarmPermsManager = new ExactAlarmPermsManager( | ||
alarmManagerMock, | ||
30 | ||
); | ||
|
||
const isGranted = exactAlarmPermsManager.isGranted(); | ||
|
||
expect(isGranted).toBeTruthy(); | ||
expect(alarmManagerMock.canScheduleExactAlarms).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("checks if schedule exact alarm permission is granted", () => { | ||
if (isSdkBelow(31)) return; | ||
|
||
const exactAlarmPermsManager = new ExactAlarmPermsManager( | ||
alarmManagerMock | ||
); | ||
|
||
const isGranted = exactAlarmPermsManager.isGranted(); | ||
|
||
expect(isGranted).toBeFalse(); | ||
expect(alarmManagerMock.canScheduleExactAlarms).toHaveBeenCalled(); | ||
}); | ||
|
||
it("requests the permission when it has to", () => { | ||
if (isSdkBelow(31)) return; | ||
|
||
const exactAlarmPermsManager = new ExactAlarmPermsManager( | ||
alarmManagerMock, | ||
android.os.Build.VERSION.SDK_INT, | ||
() => foregroundActivityMock | ||
); | ||
|
||
exactAlarmPermsManager.request(); | ||
|
||
expect(foregroundActivityMock.startActivity).toHaveBeenCalledOnceWith( | ||
createScheduleExactAlarmPermRequestIntent( | ||
Utils.android.getApplicationContext().getPackageName() | ||
) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 41 additions & 13 deletions
54
.../tests/internal/tasks/schedulers/time-based/android/power-savings-manager.android.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/internal/tasks/schedulers/time-based/android/alarms/exact-alarm-perms-manager.android.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { getLogger, Logger } from "../../../../../utils/logger"; | ||
import { Application, Utils } from "@nativescript/core"; | ||
import { createScheduleExactAlarmPermRequestIntent } from "../intents.android"; | ||
import { waitForActivityResume } from "./perm-request-common"; | ||
|
||
export class ExactAlarmPermsManager { | ||
private logger: Logger; | ||
private readonly appPackage: string; | ||
private askedOnce: boolean; | ||
|
||
constructor( | ||
private alarmManager: android.app.AlarmManager = Utils.android | ||
.getApplicationContext() | ||
.getSystemService(android.content.Context.ALARM_SERVICE), | ||
private skdVersion = android.os.Build.VERSION.SDK_INT, | ||
private activityGetter = () => Application.android.foregroundActivity | ||
) { | ||
this.logger = getLogger("ExactAlarmPermsManager"); | ||
this.appPackage = Utils.android.getApplicationContext().getPackageName(); | ||
} | ||
|
||
isGranted(): boolean { | ||
if (this.skdVersion < 31) { | ||
return true; | ||
} | ||
|
||
return this.alarmManager.canScheduleExactAlarms(); | ||
} | ||
|
||
async request(): Promise<void> { | ||
if (this.askedOnce || this.isGranted()) { | ||
return; | ||
} | ||
|
||
const visibleActivity = this.activityGetter(); | ||
if (!visibleActivity) { | ||
this.logger.warn("Schedule exact alarms can not be asked in background"); | ||
|
||
return; | ||
} | ||
this.askedOnce = true; | ||
|
||
const activityResume = waitForActivityResume(); | ||
|
||
const intent = createScheduleExactAlarmPermRequestIntent(this.appPackage); | ||
visibleActivity.startActivity(intent); | ||
|
||
await activityResume; | ||
|
||
if (!this.isGranted()) { | ||
throw new Error( | ||
"Schedule exact alarms permission is required for the app to work as expected!" | ||
); | ||
} | ||
} | ||
} | ||
|
||
let exactAlarmPermsManager: ExactAlarmPermsManager; | ||
export function getExactAlarmPermsManager(): ExactAlarmPermsManager { | ||
if (!exactAlarmPermsManager) { | ||
exactAlarmPermsManager = new ExactAlarmPermsManager(); | ||
} | ||
return exactAlarmPermsManager; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/internal/tasks/schedulers/time-based/android/alarms/perm-request-common.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Application } from "@nativescript/core"; | ||
import { AndroidApplication } from "@nativescript/core"; | ||
|
||
export function waitForActivityResume(): Promise<void> { | ||
return new Promise<void>((resolve) => { | ||
const resumeHandler = () => { | ||
Application.android.off( | ||
AndroidApplication.activityResumedEvent, | ||
resumeHandler | ||
); | ||
resolve(); | ||
}; | ||
|
||
Application.android.on( | ||
AndroidApplication.activityResumedEvent, | ||
resumeHandler | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/internal/tasks/schedulers/time-based/android/boot-receiver.android.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/// <reference path="./node_modules/@nativescript/types-android/lib/android-26.d.ts" /> | ||
/// <reference path="./node_modules/@nativescript/types-android/lib/android-31.d.ts" /> | ||
/// <reference path="./node_modules/@nativescript/types-ios/index.d.ts" /> | ||
|
||
/// <reference path="./typings/android/native-lib.d.ts" /> |
Oops, something went wrong.