forked from tauri-apps/tauri
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: integrate tauri-plugin-ohos-permissions #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sb-fy-sb
wants to merge
1
commit into
Eulogizethesun:ohdev
Choose a base branch
from
sb-fy-sb:feat/ohos-permissions
base: ohdev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
crates/tauri-cli/templates/mobile/open-harmony/ohos-permissions/build-profile.json5
This file contains hidden or 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,17 @@ | ||
| { | ||
| "apiType": "stageMode", | ||
| "buildOption": { | ||
| "arkOptions": { | ||
| "obfuscation": { | ||
| "ruleOptions": { | ||
| "enable": false | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "targets": [ | ||
| { | ||
| "name": "default" | ||
| } | ||
| ] | ||
| } |
6 changes: 6 additions & 0 deletions
6
crates/tauri-cli/templates/mobile/open-harmony/ohos-permissions/hvigorfile.ts
This file contains hidden or 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,6 @@ | ||
| import { harTasks } from '@ohos/hvigor-ohos-plugin'; | ||
|
|
||
| export default { | ||
| system: harTasks, | ||
| plugins: [] | ||
| } |
12 changes: 12 additions & 0 deletions
12
crates/tauri-cli/templates/mobile/open-harmony/ohos-permissions/oh-package.json5
This file contains hidden or 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,12 @@ | ||
| { | ||
| "name": "@tauri/plugin-ohos-permissions", | ||
| "version": "0.1.0", | ||
| "description": "OHOS permissions plugin for Tauri on OpenHarmony", | ||
| "main": "src/main/ets/index.ets", | ||
| "author": "Tauri Programme within The Commons Conservancy", | ||
| "license": "Apache-2.0 OR MIT", | ||
| "dependencies": { | ||
| "@tauri/app": "file:../tauri" | ||
| }, | ||
| "type": "module" | ||
| } |
96 changes: 96 additions & 0 deletions
96
crates/tauri-cli/templates/mobile/open-harmony/ohos-permissions/src/main/ets/Plugin.ets
This file contains hidden or 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,96 @@ | ||
| import { abilityAccessCtrl, bundleManager, common, Permissions } from '@kit.AbilityKit'; | ||
| import { BusinessError } from '@kit.BasicServicesKit'; | ||
| import { Plugin, Invoke } from '@tauri/app'; | ||
|
|
||
| // ─── Permission name mapping ─── | ||
|
|
||
| const PERMISSION_MAP: Record<string, Permissions> = { | ||
| 'camera': 'ohos.permission.CAMERA', | ||
| 'microphone': 'ohos.permission.MICROPHONE', | ||
| }; | ||
|
|
||
| // ─── Plugin Implementation ─── | ||
|
|
||
| export class OhosPermissionsPlugin extends Plugin { | ||
|
|
||
| getCommands(): Map<string, (invoke: Invoke) => void> { | ||
| const commands: Map<string, (invoke: Invoke) => void> = new Map(); | ||
| commands.set('checkCameraPermission', (invoke: Invoke): void => { this.handleCheck(invoke, 'camera'); }); | ||
| commands.set('requestCameraPermission', (invoke: Invoke): void => { this.handleRequest(invoke, 'camera'); }); | ||
| commands.set('checkMicrophonePermission', (invoke: Invoke): void => { this.handleCheck(invoke, 'microphone'); }); | ||
| commands.set('requestMicrophonePermission', (invoke: Invoke): void => { this.handleRequest(invoke, 'microphone'); }); | ||
| return commands; | ||
| } | ||
|
|
||
| // ─── Generic check handler ─── | ||
|
|
||
| private handleCheck(invoke: Invoke, permissionKey: string): void { | ||
| this.checkPermission(permissionKey).then((granted: boolean) => { | ||
| invoke.resolve(JSON.stringify(granted)); | ||
| }).catch((err: BusinessError) => { | ||
| console.error('[OhosPermissionsPlugin] checkPermission failed for ' + permissionKey + ': ' + err.message); | ||
| invoke.resolve(JSON.stringify(false)); | ||
| }); | ||
| } | ||
|
|
||
| // ─── Generic request handler ─── | ||
|
|
||
| private handleRequest(invoke: Invoke, permissionKey: string): void { | ||
| const ohosPermission = PERMISSION_MAP[permissionKey]; | ||
| if (ohosPermission === undefined) { | ||
| invoke.reject('Unknown permission: ' + permissionKey); | ||
| return; | ||
| } | ||
|
|
||
| const permissions: Array<Permissions> = [ohosPermission]; | ||
| let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); | ||
|
|
||
| if (this.context === undefined || this.context === null) { | ||
| console.error('[OhosPermissionsPlugin] UIAbilityContext not available'); | ||
| invoke.reject('UIAbilityContext not available'); | ||
| return; | ||
| } | ||
|
|
||
| atManager.requestPermissionsFromUser(this.context, permissions).then((data) => { | ||
| const authResults: number[] = data.authResults; | ||
| if (authResults.length > 0 && authResults[0] === 0) { | ||
| invoke.resolve(''); | ||
| } else { | ||
| console.warn('[OhosPermissionsPlugin] Permission ' + permissionKey + ' denied by user.'); | ||
| invoke.resolve(''); | ||
| } | ||
| }).catch((err: BusinessError) => { | ||
| console.error('[OhosPermissionsPlugin] requestPermissionsFromUser failed: code=' + err.code + ', message=' + err.message); | ||
| invoke.resolve(''); | ||
| }); | ||
| } | ||
|
|
||
| // ─── Core: check if a permission is granted ─── | ||
|
|
||
| private async checkPermission(permissionKey: string): Promise<boolean> { | ||
| const ohosPermission = PERMISSION_MAP[permissionKey]; | ||
| if (ohosPermission === undefined) { | ||
| throw new Error('Unknown permission: ' + permissionKey); | ||
| } | ||
|
|
||
| const tokenId = await this.getAccessTokenId(); | ||
| const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); | ||
|
|
||
| const grantStatus: abilityAccessCtrl.GrantStatus = | ||
| await atManager.checkAccessToken(tokenId, ohosPermission); | ||
|
|
||
| return grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED; | ||
| } | ||
|
|
||
| // ─── Core: get the app's access token ID ─── | ||
|
|
||
| private async getAccessTokenId(): Promise<number> { | ||
| const bundleInfo: bundleManager.BundleInfo = | ||
| await bundleManager.getBundleInfoForSelf( | ||
| bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | ||
| ); | ||
| return bundleInfo.appInfo.accessTokenId; | ||
| } | ||
| } | ||
|
|
||
| export default OhosPermissionsPlugin; | ||
1 change: 1 addition & 0 deletions
1
crates/tauri-cli/templates/mobile/open-harmony/ohos-permissions/src/main/ets/index.ets
This file contains hidden or 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 @@ | ||
| export { OhosPermissionsPlugin as default } from './Plugin'; |
34 changes: 34 additions & 0 deletions
34
crates/tauri-cli/templates/mobile/open-harmony/ohos-permissions/src/main/module.json5
This file contains hidden or 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,34 @@ | ||
| { | ||
| "module": { | ||
| "name": "ohospermissions", | ||
| "type": "har", | ||
| "deviceTypes": [ | ||
| "default", | ||
| "phone", | ||
| "tablet", | ||
| "2in1" | ||
| ], | ||
| "requestPermissions": [ | ||
| { | ||
| "name": "ohos.permission.CAMERA", | ||
| "reason": "$string:ohos_permissions_camera_reason", | ||
| "usedScene": { | ||
| "abilities": [ | ||
| "EntryAbility" | ||
| ], | ||
| "when": "inuse" | ||
| } | ||
| }, | ||
| { | ||
| "name": "ohos.permission.MICROPHONE", | ||
| "reason": "$string:ohos_permissions_microphone_reason", | ||
| "usedScene": { | ||
| "abilities": [ | ||
| "EntryAbility" | ||
| ], | ||
| "when": "inuse" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
...emplates/mobile/open-harmony/ohos-permissions/src/main/resources/base/element/string.json
This file contains hidden or 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,12 @@ | ||
| { | ||
| "string": [ | ||
| { | ||
| "name": "ohos_permissions_camera_reason", | ||
| "value": "This app needs camera access to take photos and record videos." | ||
| }, | ||
| { | ||
| "name": "ohos_permissions_microphone_reason", | ||
| "value": "This app needs microphone access to record audio." | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 [F3/spec] denial 分支仅
console.warn+invoke.resolve(''),未调用atManager.requestPermissionOnSetting(this.context, [ohosPermission])引导用户到系统设置。这与 spec(camera/microphone 的 “永久拒绝后引导设置” Scenario,SHALL 措辞)、design.md(Goals + R2)、tasks.md 3.4(标记 [x] 但 step ③ 未实现)三处冲突。
建议在此分支补上
requestPermissionOnSetting调用;若该 API 对三方应用不可用,应同步更新 spec/design 并修正 tasks.md 3.4 状态。