From cb58ba5cc3d5c9cb9f20de5ce67fb0011f3b098a Mon Sep 17 00:00:00 2001 From: Coder2 Date: Mon, 4 Aug 2025 15:50:42 +0800 Subject: [PATCH] OpenHarmony: shell update --- .../resources/base/element/string.json | 4 +++ ohos-project/entry/oh-package-lock.json5 | 3 +- ohos-project/entry/src/main/cpp/napi_init.cpp | 5 +++- .../main/ets/entryability/EntryAbility.ets | 30 +++++++++++++++++-- .../entry/src/main/ets/pages/Index.ets | 13 ++++---- ohos-project/oh-package-lock.json5 | 3 +- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/ohos-project/AppScope/resources/base/element/string.json b/ohos-project/AppScope/resources/base/element/string.json index 1080233f01..a1b9d1934d 100644 --- a/ohos-project/AppScope/resources/base/element/string.json +++ b/ohos-project/AppScope/resources/base/element/string.json @@ -3,6 +3,10 @@ { "name": "app_name", "value": "MyApplication" + }, + { + "name": "perm_reason", + "value": "Subsystem for SDL / SDL 子系统" } ] } diff --git a/ohos-project/entry/oh-package-lock.json5 b/ohos-project/entry/oh-package-lock.json5 index ca4ac9f408..caf0346aa3 100644 --- a/ohos-project/entry/oh-package-lock.json5 +++ b/ohos-project/entry/oh-package-lock.json5 @@ -1,6 +1,7 @@ { "meta": { - "stableOrder": true + "stableOrder": true, + "enableUnifiedLockfile": false }, "lockfileVersion": 3, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", diff --git a/ohos-project/entry/src/main/cpp/napi_init.cpp b/ohos-project/entry/src/main/cpp/napi_init.cpp index 8730b430e4..6e36ddbf18 100644 --- a/ohos-project/entry/src/main/cpp/napi_init.cpp +++ b/ohos-project/entry/src/main/cpp/napi_init.cpp @@ -5,6 +5,7 @@ #include "SDL3/SDL_timer.h" #include "SDL3/SDL_video.h" #include "SDL3/SDL_vulkan.h" +#include "SDL3/SDL_locale.h" #include "napi/native_api.h" #include "SDL3/SDL_log.h" #include "SDL3/SDL_hints.h" @@ -73,7 +74,9 @@ int main() SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_Init(SDL_INIT_VIDEO); - SDL_Log("Main func invoke !!!"); + int i = 0; + auto t = SDL_GetPreferredLocales(&i); + SDL_Log("Main func invoke !!! %s %s", t[0]->country, t[0]->language); // SDL_GL_LoadLibrary("libGLESv2.so"); SDL_Log("sdl error: %s", SDL_GetError()); SDL_Window* win = SDL_CreateWindow("test", 1024, 1024, SDL_WINDOW_OPENGL); diff --git a/ohos-project/entry/src/main/ets/entryability/EntryAbility.ets b/ohos-project/entry/src/main/ets/entryability/EntryAbility.ets index f562838e87..ad296b578a 100644 --- a/ohos-project/entry/src/main/ets/entryability/EntryAbility.ets +++ b/ohos-project/entry/src/main/ets/entryability/EntryAbility.ets @@ -1,10 +1,35 @@ -import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { abilityAccessCtrl, AbilityConstant, + common, + ConfigurationConstant, + Permissions, + UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { window } from '@kit.ArkUI'; +import { promptAction, window } from '@kit.ArkUI'; import { KeyboardAvoidMode } from '@ohos.arkui.UIContext'; +import { BusinessError } from '@kit.BasicServicesKit'; const DOMAIN = 0x0000; +const permissions: Array = ['ohos.permission.INPUT_KEYBOARD_CONTROLLER']; +function reqPermissionsFromUser(permissions: Array, context: common.UIAbilityContext): void { + let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); + atManager.requestPermissionsFromUser(context, permissions).then((data) => { + let grantStatus: Array = data.authResults; + let length: number = grantStatus.length; + for (let i = 0; i < length; i++) { + if (grantStatus[i] === 0) { + promptAction.showDialog({title: "permissions", message: "granted", buttons: [{text: 'Ok', color: '#999999'}]}); + } else { + promptAction.showDialog({title: "permissions", message: "not granted", buttons: [{text: 'Ok', color: '#999999'}]}); + return; + } + } + // 授权成功 + }).catch((err: BusinessError) => { + console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); + }) +} + export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); @@ -20,6 +45,7 @@ export default class EntryAbility extends UIAbility { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err) => { + reqPermissionsFromUser(permissions, this.context); if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; diff --git a/ohos-project/entry/src/main/ets/pages/Index.ets b/ohos-project/entry/src/main/ets/pages/Index.ets index 6942023db0..09c74502d4 100644 --- a/ohos-project/entry/src/main/ets/pages/Index.ets +++ b/ohos-project/entry/src/main/ets/pages/Index.ets @@ -10,18 +10,15 @@ export class ArkNapiCallback { sdltest.sdlLaunchMain("libentry.so", "main") } showDialog(title: string, message: string) { - promptAction.showDialog({title: title, message: message, buttons: [{text: 'Ok', color: '#999999'}]}); + promptAction.showDialog({title: title, message: message, buttons: [{text: 'Ok', color: '#999999'}]}) } fetchLocale(): string { - let locale = new intl.Locale(); - return locale.toString(); + let locale = new intl.Locale() + return locale.language + "_" + locale.region } test(): number { - hilog.info(DOMAIN, 'testTag', 'Call from native !!!'); - focusControl.requestFocus("inputHandler") - let locale = new intl.Locale(); - hilog.info(DOMAIN, 'testTag', locale.toString()); - return 1; + hilog.info(DOMAIN, 'testTag', 'Call from native !!!') + return 1 } } diff --git a/ohos-project/oh-package-lock.json5 b/ohos-project/oh-package-lock.json5 index c6f99f5c73..a392505690 100644 --- a/ohos-project/oh-package-lock.json5 +++ b/ohos-project/oh-package-lock.json5 @@ -1,6 +1,7 @@ { "meta": { - "stableOrder": true + "stableOrder": true, + "enableUnifiedLockfile": false }, "lockfileVersion": 3, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",