!3083 【sample】增加资源overlay sample工程

Merge pull request !3083 from maoziduanl/master
This commit is contained in:
openharmony_ci 2023-07-07 05:08:45 +00:00 committed by Gitee
commit 8ba153e3c7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
49 changed files with 1040 additions and 252 deletions

View File

@ -2,31 +2,65 @@
### 介绍
示例使用[@ohos.app.ability.common](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-app-ability-common.md)
工程使用[@ohos.app.ability.common](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-app-ability-common.md)
接口中的AbilityContext类获取资源管理器resourceManager使用[@ohos.resourceManager.d.ts](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-resource-manager.md)
中的接口,实现了格式化字符串查询、基于指定屏幕分辨率查询媒体资源、获取系统资源管理对象等基础功能。
中的接口,展示了格式化字符串查询、基于指定屏幕分辨率查询媒体资源、获取系统资源管理对象等基础功能以及展示了资源静态overlay以及运行时overlay的特性功能。
### 效果预览
|主页|
|---|
|![main](sceenshots/device/main.png)|
|![main](sceenshots/device/Scene-directory.png)|
使用说明
#### 使用说明
应用界面中展示了资源管理API各类接口的调用。
此界面为主页面其中展示了资源管理API各类接口的调用以及特性Overlay场景功能。其作用有
1、读取字符串、字符串数组、根据指定数量获取指定ID字符串表示的单复数字符串
1、点击资源API调用示例按钮可跳转到资源API示例页面
2、指定资源ID对应的媒体文件内容字节数、指定资源ID对应的图片资源Base64编码字符串的长度
2、点击Overlay使用示例可以跳转到Overlay的使用示例界面。
3、获取设备状态和设备支持的能力
#### 资源API调用示例
4、调用格式化字符串查询接口
|资源API测试|
|---|
|![main](sceenshots/device/basicResources.png)|
5、调用指定屏幕分辨率查询媒体资源能力接口
#### 使用说明
6、调用获取仅系统资源管理对象,并查询系统资源。
此页面展示了当前资源管理接口的调用以及接口对应的返回结果。
#### 静态overlay场景
|overlay场景|
|---|
|![main](sceenshots/device/overlayTest.png)|
#### 使用说明
此页面展示静态overlay功能功能使用如下
1、静态overlay是默认使能的当前显示的是静态overlay中的字符串和图标。
2、点击Disable可以触发去使能重启应用可以恢复显示应用的字符串和图标。
3、点击enable可以触发使能重启应用可以再次显示overlay中的字符串和图标。
源码参考:[Overlay示例](entry/src/main/ets/pages/Overlay.ets)
#### 运行时overlay场景
|运行时overlay场景|
|---|
|![main](sceenshots/device/overlayRuntimeTest.png)|
#### 使用说明
此页面展示运行时overlay功能功能使用如下
1、点击addResource可以触发运行时overlay此时会使用运行时overlay中的资源覆盖之前的字符串和图标。
2、点击removeResource可以触发移除运行时overlay此时会移除运行时overlay恢复到覆盖前的字符串和图标。
### 工程目录
@ -34,12 +68,17 @@
entry/src/main/ets/
|---entryability
|---pages
| |---Index.ets // 首页
|---utils // 日志工具
| |---Index.ets // 场景首页
| |---BasicResources.ets // 基础资源场景
| |---Overlay.ets // overlay场景
|---libraryOverlay // 静态overlay
|---libraryRuntimeOverlay // 运行时overlay
```
### 具体实现
#### 资源API调用示例具体实现
1、使用getContext()接口获取context对象使用context.resourceManager获取资源管理对象然后调用resourceManager内部的相关接口获取对应资源例如
* 获取字符串资源resourceManager.getStringValue()
* 获取字符串数组资源resourceManager.getStringArrayValue()
@ -49,7 +88,44 @@ entry/src/main/ets/
2、导包resourceManager使用resourceManager.getSystemResourceManager()获取系统资源管理对象,然后获取系统资源。
源码参考:[Index.ets](entry/src/main/ets/pages/Index.ets)
源码参考:[资源API调用示例](entry/src/main/ets/pages/BasicResources.ets)
#### overlay场景的具体实现
1、静态overlay主要是通过加载overly中的资源实现资源覆盖需要在对应的module.json中添加"targetModuleName":"entry" 表示覆盖entry中的资源默认使能也可调用包管理接口进行使能和去使能。
使用步骤为在安装完entry的hap后需要把library模块生成的library-default-signed.hsp推送到/data/test下使用bm install命令进行安装。
脚本语言如下:
hdc_std shell mount -o remount,rw /
hdc_std install ./entry-default-signed.hap
hdc_std shell mkdir /data/test
hdc_std file send ./libraryOverlay-default-signed.hsp /data/test
hdc_std shell bm install -p "/data/test/libraryOverlay-default-signed.hsp"
pause
2、运行时overlay资源加载主要是在应用运行过程中实现资源的覆盖需要应用主动调用资源的addResource接口实现资源的覆盖以及资源的移除此功能不持久化。
使用步骤为: 在安装完entry的hap后需要把libraryRuntimeOverlay模块生成的libraryRuntimeOverlay-default-signed.hsp推送到应用对应的安装目录下。
脚本语言如下:
hdc_std shell mount -o remount,rw /
hdc_std install ./entry-default-signed.hap
hdc_std file send ./libraryRuntimeOverlay-default-signed.hsp /data/app/el1/bundle/public/ohos.samples.resourcemanager
pause
源码参考:[Overlay示例](entry/src/main/ets/pages/Overlay.ets)
### 相关权限
@ -61,11 +137,11 @@ entry/src/main/ets/
### 约束与限制
1. 本示例仅支持标准系统上运行。
1. 本工程示例仅支持标准系统上运行。
2. 本示例仅支持API version 10版本SDK版本号4.0.8.2
2. 本工程仅支持API version 10版本SDK版本号4.0.9.1及以上
3. 本示例需要使用DevEco Studio 3.1 Release (Build Version: 3.1.0.500, built on April 28, 2023)及以上版本才可编译运行。
3. 本工程示例需要使用DevEco Studio 3.1 Release (Build Version: 3.1.0.500, built on April 28, 2023)及以上版本才可编译运行。
### 下载
@ -74,7 +150,7 @@ entry/src/main/ets/
```
git init
git config core.sparsecheckout true
echo code/BasicFeature/Internationalnation/ResourceManager/ > .git/info/sparse-checkout
echo code/BasicFeature/Resource/ResourceManager/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master
```

View File

@ -22,8 +22,7 @@
"name": "default",
"signingConfig": "default"
}
],
"signingConfigs": []
]
},
"modules": [
{
@ -37,6 +36,30 @@
]
}
]
},
{
"name": "libraryOverlay",
"srcPath": "./libraryOverlay",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
},
{
"name": "libraryRuntimeOverlay",
"srcPath": "./libraryRuntimeOverlay",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}
}

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"license": "ISC",
"devDependencies": {},

View File

@ -29,6 +29,7 @@ export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: Window.WindowStage) {
// Main window is created, set main page for this ability
Logger.info('Ability onWindowStageCreate')
AppStorage.SetOrCreate('ResMgrTitle', this.context.resourceManager.getStringSync($r("app.string.title").id));
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
Logger.error(`Failed to load the content. Cause: ${JSON.stringify(err)}`)

View File

@ -0,0 +1,252 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import resourceManager from '@ohos.resourceManager'
import { Logger } from '../utils/Logger'
const SPECIFIED_NUM = 2;
async function getString(resId: number): Promise<string> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getStringValue(resId);
return value;
} catch (error) {
Logger.error(`getStringValue failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getStringArray(resource: resourceManager.Resource): Promise<Array<string>> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getStringArrayValue(resource);
return value;
} catch (error) {
Logger.error(`getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getPluralString(resId: number, num: number): Promise<string> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getPluralStringValue(resId, num);
return value;
} catch (error) {
Logger.error(`getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getDeviceCapability(): Promise<resourceManager.DeviceCapability> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getDeviceCapability();
return value;
} catch (error) {
Logger.error(`getDeviceCapability failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getConfiguration(): Promise<resourceManager.Configuration> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getConfiguration();
return value;
} catch (error) {
Logger.error(`getConfiguration failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getMedia(resId: number): Promise<Uint8Array> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getMediaContent(resId);
return value;
} catch (error) {
Logger.error(`getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getMediaBase64(resId: number): Promise<string> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getMediaContentBase64(resId);
return value;
} catch (error) {
Logger.error(`getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
}
function getFormatString(resId: number, world: string): string {
let resMgr = getContext(this).resourceManager;
try {
let value = resMgr.getStringSync(resId, world);
return value;
} catch (error) {
Logger.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getDensityMediaBase64(resId: number, density: number): Promise<string> {
let resMgr = getContext(this).resourceManager;
try {
let value = await resMgr.getMediaContentBase64(resId, density);
return value;
} catch (error) {
Logger.error(`getDensityMediaBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
}
async function getSystemMediaBase64(resId: number): Promise<string> {
// 获取仅系统资源管理对象
let sysMgr = resourceManager.getSystemResourceManager();
try {
let value = await sysMgr.getMediaContentBase64(resId);
return value;
} catch (error) {
Logger.error(`getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
}
}
@Entry
@Component
struct Index {
@State string_str: string = 'string'
@State strArray: string = 'stringArray'
@State plural: string = 'plural'
@State configuration: string = 'configuration'
@State capability: string = 'capability'
@State media: string = 'media'
@State mediaBase: string = 'mediaBase'
@State formatStr: string = 'Format String'
@State densityMedia: string = 'Density Media'
@State systemRes: string = 'System Res'
async aboutToAppear() {
this.string_str = await getString($r('app.string.string_str').id);
let resource = {
bundleName: "ohos.samples.resourcemanager",
moduleName: "entry",
id: $r('app.strarray.str_array').id
}
this.strArray = JSON.stringify(await getStringArray(resource));
this.plural = await getPluralString($r('app.plural.eat_apple').id, SPECIFIED_NUM);
this.configuration = JSON.stringify(await getConfiguration());
this.capability = JSON.stringify(await getDeviceCapability());
this.media = JSON.stringify((await getMedia($r('app.media.app_icon').id)).length);
this.mediaBase = JSON.stringify((await getMediaBase64($r('app.media.app_icon').id)).length);
this.formatStr = getFormatString($r('app.string.formatStr').id,
await getString($r('app.string.world').id));
this.densityMedia = await getDensityMediaBase64($r('app.media.density').id, 640);
this.systemRes = await getSystemMediaBase64($r('sys.media.ohos_app_icon').id);
}
build() {
Column() {
Text(AppStorage.Get('ResMgrTitle'))
.width('100%')
.height(50)
.backgroundColor($r('app.color.text_color'))
.fontColor(Color.White)
.fontSize(20)
.padding({ left: 15 })
Scroll() {
Column() {
Text($r('app.string.stringDesc'))
.fontSize(25)
Text(this.string_str)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.stringArrayDesc'))
.fontSize(25)
Text(this.strArray)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.pluralStringDesc'))
.fontSize(25)
Text(this.plural)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.configurationDesc'))
.fontSize(25)
Text(this.configuration)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.capabilityDesc'))
.fontSize(25)
Text(this.capability)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.mediaDesc'))
.fontSize(25)
Text(this.media)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.mediaBase64Desc'))
.fontSize(25)
Text(this.mediaBase)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.formatStrDesc'))
.fontSize(25)
Text(this.formatStr)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.densityMediaDesc'))
.fontSize(25)
Image(this.densityMedia)
.id('getDensityMedia')
.height('10%')
Text($r('app.string.systemResDesc'))
.fontSize(25)
Image(this.systemRes)
.id('getSystemMedia')
.height('10%')
}
.width('100%')
.padding(10)
.alignItems(HorizontalAlign.Start)
}
}
.width('100%')
.height('100%')
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -12,149 +12,48 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import router from '@ohos.router';
import CommonEventManager from '@ohos.commonEventManager'
import resourceManager from '@ohos.resourceManager'
import { Logger } from '../utils/Logger'
const SPECIFIED_NUM = 2
async function getString(resId: number): Promise<string> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getStringValue(resId)
return value
} catch (error) {
Logger.error(`getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
let subscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
// 订阅者信息
let subscribeInfo = {
events: ["TestResourcesEvent"]
};
// 订阅事件回调
function JumpToResApiExample (err, data) {
if (err) {
console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
} else {
router.pushUrl({ url: 'pages/BasicResources' })
}
}
async function getStringArray(resource: resourceManager.Resource): Promise<Array<string>> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getStringArrayValue(resource)
return value
} catch (error) {
Logger.error(`getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
// 创建订阅者回调
function createTestResources(err, commonEventSubscriber) {
if(!err) {
subscriber = commonEventSubscriber;
try {
CommonEventManager.subscribe(subscriber, JumpToResApiExample);
} catch (err) {
console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
}
} else {
console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
}
}
async function getPluralString(resId: number, num: number): Promise<string> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getPluralStringValue(resId, num)
return value
} catch (error) {
Logger.error(`getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
}
}
async function getDeviceCapability(): Promise<resourceManager.DeviceCapability> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getDeviceCapability()
return value
} catch (error) {
Logger.error(`getDeviceCapability failed, error code: ${error.code}, message: ${error.message}.`)
}
}
async function getConfiguration(): Promise<resourceManager.Configuration> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getConfiguration()
return value
} catch (error) {
Logger.error(`getConfiguration failed, error code: ${error.code}, message: ${error.message}.`)
}
}
async function getMedia(resId: number): Promise<Uint8Array> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getMediaContent(resId)
return value
} catch (error) {
Logger.error(`getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
}
}
async function getMediaBase64(resId: number): Promise<string> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getMediaContentBase64(resId)
return value
} catch (error) {
Logger.error(`getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
}
function getFormatString(resId: number, world: string): string {
let resMgr = getContext(this).resourceManager
try {
let value = resMgr.getStringSync(resId, world)
return value
} catch (error) {
Logger.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
}
async function getDensityMediaBase64(resId: number, density: number): Promise<string> {
let resMgr = getContext(this).resourceManager
try {
let value = await resMgr.getMediaContentBase64(resId, density)
return value
} catch (error) {
Logger.error(`getDensityMediaBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
}
async function getSystemMediaBase64(resId: number): Promise<string> {
// 获取仅系统资源管理对象
let sysMgr = resourceManager.getSystemResourceManager()
try {
let value = await sysMgr.getMediaContentBase64(resId)
return value
} catch (error) {
Logger.error(`getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
}
// 创建订阅者
try {
CommonEventManager.createSubscriber(subscribeInfo, createTestResources);
} catch (err) {
console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
}
@Entry
@Component
struct Index {
@State string_str: string = 'string'
@State strArray: string = 'stringArray'
@State plural: string = 'plural'
@State configuration: string = 'configuration'
@State capability: string = 'capability'
@State media: string = 'media'
@State mediaBase: string = 'mediaBase'
@State formatStr: string = 'Format String'
@State densityMedia: string = 'Density Media'
@State systemRes: string = 'System Res'
async aboutToAppear() {
this.string_str = await getString($r('app.string.string_str').id)
let resource = {
bundleName: "ohos.samples.resourcemanager",
moduleName: "entry",
id: $r('app.strarray.str_array').id
}
this.strArray = JSON.stringify(await getStringArray(resource))
this.plural = await getPluralString($r('app.plural.eat_apple').id, SPECIFIED_NUM)
this.configuration = JSON.stringify(await getConfiguration())
this.capability = JSON.stringify(await getDeviceCapability())
this.media = JSON.stringify((await getMedia($r('app.media.app_icon').id)).length)
this.mediaBase = JSON.stringify((await getMediaBase64($r('app.media.app_icon').id)).length)
this.formatStr = getFormatString($r('app.string.formatStr').id,
await getString($r('app.string.world').id))
this.densityMedia = await getDensityMediaBase64($r('app.media.density').id, 640)
this.systemRes = await getSystemMediaBase64($r('sys.media.ohos_app_icon').id)
}
build() {
Column() {
Text($r("app.string.title"))
Column({ space: 20 }) {
Text(AppStorage.Get('ResMgrTitle'))
.width('100%')
.height(50)
.backgroundColor($r('app.color.text_color'))
@ -162,92 +61,26 @@ struct Index {
.fontSize(20)
.padding({ left: 15 })
Scroll() {
Column() {
Text($r('app.string.stringDesc'))
.fontSize(25)
Button($r('app.string.testResources')).onClick(() => {
router.pushUrl({ url: 'pages/BasicResources' });
})
.width('80%')
.height(50)
.margin({ top: 200 })
.fontWeight(FontWeight.Bold)
.align(Alignment.Center)
.position({x:50, y:50});
Text(this.string_str)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Button($r('app.string.testOverlay')).onClick(() => {
router.pushUrl({ url: 'pages/Overlay' });
})
.width('80%')
.height(50)
.margin({ top: 200 })
.fontWeight(FontWeight.Bold)
.align(Alignment.Center)
.position({x:50, y:120})
Text($r('app.string.stringArrayDesc'))
.fontSize(25)
Text(this.strArray)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.pluralStringDesc'))
.fontSize(25)
Text(this.plural)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.configurationDesc'))
.fontSize(25)
Text(this.configuration)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.capabilityDesc'))
.fontSize(25)
Text(this.capability)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.mediaDesc'))
.fontSize(25)
Text(this.media)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.mediaBase64Desc'))
.fontSize(25)
Text(this.mediaBase)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.formatStrDesc'))
.fontSize(25)
Text(this.formatStr)
.fontSize(25)
.fontColor('#ffff0000')
.fontWeight(FontWeight.Bold)
Text($r('app.string.densityMediaDesc'))
.fontSize(25)
Image(this.densityMedia)
.id('getDensityMedia')
.height('10%')
Text($r('app.string.systemResDesc'))
.fontSize(25)
Image(this.systemRes)
.id('getSystemMedia')
.height('10%')
}
.width('100%')
.padding(10)
.alignItems(HorizontalAlign.Start)
}
}
.width('100%')
.height('100%')
}.width('100%').height(100)
}
}

View File

@ -0,0 +1,152 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import overlay from '@ohos.bundle.overlay';
@Entry
@Component
struct Overlay {
private resmgr = getContext().resourceManager;
@State message: string = 'Test Overlay'
@State resources: string = this.resmgr.getStringSync($r("app.string.test_string").id)
@State pixmap: PixelMap = this.resmgr.getDrawableDescriptor($r("app.media.icon").id).getPixelMap()
build() {
Column() {
Text(AppStorage.Get('ResMgrTitle'))
.width('100%')
.height(50)
.backgroundColor($r('app.color.text_color'))
.fontColor(Color.White)
.fontSize(20)
.padding({ left: 15 })
Text(`${this.message}`)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({
top: 40
})
Button() {
Text('disable')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 50
})
.backgroundColor('#0D9FFB')
.width('50%')
.height('5%')
.onClick(() => {
// 非使能
overlay.setOverlayEnabled("libraryOverlay", false, (err, data) => {
if (err && err.code != 0) {
console.log("error:" + JSON.stringify(err));
this.message = this.resmgr.getStringSync($r('app.string.unEnableFailed').id);
} else {
console.log("data:" + JSON.stringify(data));
this.message = this.resmgr.getStringSync($r('app.string.unEnableSuccess').id);
}
})
})
Button() {
Text('enable')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('50%')
.height('5%')
.onClick(() => {
// 使能
overlay.setOverlayEnabled("libraryOverlay", true, (err, data) => {
if (err && err.code != 0) {
console.log("error:" + JSON.stringify(err));
this.message = this.resmgr.getStringSync($r('app.string.enableFailed').id);
} else {
this.message = this.resmgr.getStringSync($r('app.string.enableSuccess').id);
}
})
})
Button() {
Text('addResource')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('50%')
.height('5%')
.onClick(() => {
let path = getContext().bundleCodeDir + "/libraryRuntimeOverlay-default-signed.hsp";
try {
// @ts-ignore
let ret = this.resmgr.addResource(path);
console.error("addResource: ret" + JSON.stringify(ret));
} catch (error) {
console.error(`addResource failed, error code: ${error.code}, message: ${error.message}.`);
}
this.pixmap = this.resmgr.getDrawableDescriptor($r("app.media.icon").id).getPixelMap();
this.resources = this.resmgr.getStringSync($r("app.string.test_string").id);
})
Button() {
Text('removeResource')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#0D9FFB')
.width('50%')
.height('5%')
.onClick(() => {
let path = getContext().bundleCodeDir + "/libraryRuntimeOverlay-default-signed.hsp";
try {
// @ts-ignore
this.resmgr.removeResource(path);
} catch (error) {
console.error(`removeResource failed, error code: ${error.code}, message: ${error.message}.`);
}
this.pixmap = this.resmgr.getDrawableDescriptor($r("app.media.icon").id).getPixelMap();
this.resources = this.resmgr.getStringSync($r("app.string.test_string").id);
})
Image(this.pixmap)
.width(100)
.height(100)
Text(this.resources)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.height('100%')
}
}

View File

@ -79,6 +79,34 @@
{
"name": "systemResDesc",
"value": "SystemResource: "
},
{
"name": "test_string",
"value": "base_test"
},
{
"name": "unEnableFailed",
"value": "overlay unEnable failed"
},
{
"name": "unEnableSuccess",
"value": "overlay unEnable success"
},
{
"name": "enableFailed",
"value": "overlay enable failed"
},
{
"name": "enableSuccess",
"value": "overlay enable success"
},
{
"name": "testResources",
"value": "Example of using API"
},
{
"name": "testOverlay",
"value": "Example of using Overlay"
}
]
}

View File

@ -1,5 +1,7 @@
{
"src": [
"pages/Index"
"pages/Index",
"pages/Overlay",
"pages/BasicResources"
]
}

View File

@ -79,6 +79,34 @@
{
"name": "systemResDesc",
"value": "SystemResource: "
},
{
"name": "test_string",
"value": "en_US_test"
},
{
"name": "unEnableFailed",
"value": "overlay unEnable failed"
},
{
"name": "unEnableSuccess",
"value": "overlay unEnable success"
},
{
"name": "enableFailed",
"value": "overlay enable failed"
},
{
"name": "enableSuccess",
"value": "overlay enable success"
},
{
"name": "testResources",
"value": "Example of resource API"
},
{
"name": "testOverlay",
"value": "Example of using Overlay"
}
]
}

View File

@ -71,6 +71,34 @@
{
"name": "systemResDesc",
"value": "系统资源查询: "
},
{
"name": "test_string",
"value": "测试"
},
{
"name": "unEnableFailed",
"value": "overlay 去使能失败"
},
{
"name": "unEnableSuccess",
"value": "overlay 去使能成功"
},
{
"name": "enableFailed",
"value": "overlay 使能失败"
},
{
"name": "enableSuccess",
"value": "overlay 使能成功"
},
{
"name": "testResources",
"value": "资源API调用示例"
},
{
"name": "testOverlay",
"value": "Overlay使用示例"
}
]
}

View File

@ -17,6 +17,8 @@ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from
import { Driver, ON, MatchPattern } from '@ohos.UiTest'
import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'
import hilog from '@ohos.hilog'
import CommonEventManager from '@ohos.commonEventManager'
import router from '@ohos.router';
const TAG = '[Sample_ResourceManager]'
const DOMAIN = 0xF811
@ -85,7 +87,7 @@ async function getConfiguration(): Promise<string> {
}
export default function appTest() {
describe('ActsAbilityTest', function () {
describe('ActsResMgrTest', function () {
/**
* 拉起应用
*/
@ -96,6 +98,18 @@ export default function appTest() {
await abilityDelegator.startAbility({
bundleName: 'ohos.samples.resourcemanager',
abilityName: 'EntryAbility'
}).then(async => {
function publishCB(err) {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("publish");
}
}
setTimeout(()=>{
CommonEventManager.publish("TestResourcesEvent", publishCB); // 发布事件
done()
}, 1500)
})
done()
} catch (error) {

View File

@ -0,0 +1,6 @@
/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"apiType": 'stageMode',
"buildOption": {
},
"targets": [
{
"name": "default"
}
]
}

View File

@ -0,0 +1,2 @@
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
module.exports = require('@ohos/hvigor-ohos-plugin').hspTasks

View File

@ -0,0 +1,10 @@
{
"license": "Apache-2.0",
"devDependencies": {},
"author": "",
"name": "libraryoverlay",
"description": "Please describe the basic information.",
"main": "./src/main/ets/Index.ets",
"version": "1.0.0",
"dependencies": {}
}

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { add } from "./utils/Calc"

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}

View File

@ -0,0 +1,3 @@
export function add(a:number, b:number) {
return a + b;
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"module": {
"name": "libraryOverlay",
"type": "shared",
"description": "$string:shared_desc",
"targetModuleName":"entry",
"deviceTypes": [
"default",
"tablet"
],
"deliveryWithInstall": true,
"pages": "$profile:main_pages"
}
}

View File

@ -0,0 +1,8 @@
{
"color": [
{
"name": "white",
"value": "#FFFFFF"
}
]
}

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "shared_desc",
"value": "description"
},
{
"name": "test_string",
"value": "base_overlay_test"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -0,0 +1,5 @@
{
"src": [
"pages/Index"
]
}

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "shared_desc",
"value": "overlay description"
},
{
"name": "test_string",
"value": "overlay en_US_test"
}
]
}

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "shared_desc",
"value": "overlay description"
},
{
"name": "test_string",
"value": "overlay测试"
}
]
}

View File

@ -0,0 +1,6 @@
/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"apiType": 'stageMode',
"buildOption": {
},
"targets": [
{
"name": "default"
}
]
}

View File

@ -0,0 +1,2 @@
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
module.exports = require('@ohos/hvigor-ohos-plugin').hspTasks

View File

@ -0,0 +1,10 @@
{
"license": "Apache-2.0",
"devDependencies": {},
"author": "",
"name": "libraryruntimeoverlay",
"description": "Please describe the basic information.",
"main": "./src/main/ets/Index.ets",
"version": "1.0.0",
"dependencies": {}
}

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { add } from "./utils/Calc"

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}

View File

@ -0,0 +1,3 @@
export function add(a:number, b:number) {
return a + b;
}

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"module": {
"name": "libraryRuntimeOverlay",
"type": "shared",
"description": "$string:shared_desc",
"deviceTypes": [
"default",
"tablet"
],
"deliveryWithInstall": true,
"pages": "$profile:main_pages"
}
}

View File

@ -0,0 +1,8 @@
{
"color": [
{
"name": "white",
"value": "#FFFFFF"
}
]
}

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "shared_desc",
"value": "description"
},
{
"name": "test_string",
"value": "en_US_overlay_RUNTIME"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "shared_desc",
"value": "overlay description"
},
{
"name": "test_string",
"value": "overlay en_US_test runtime"
}
]
}

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "shared_desc",
"value": "描述"
},
{
"name": "test_string",
"value": "overlay运行时测试"
}
]
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
"license": "ISC",
"devDependencies": {

View File

@ -1,13 +1,19 @@
|测试功能|预置条件|输入|预期输出|测试结果|
|--------------------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------|
|拉起应用| 设备正常运行|不涉及|成功拉起应用|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示字符串文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示字符串数组文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示单复数字符串文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示设备状态文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示设备支持能力文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示图片字节数文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示图片编码长度文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常展示格式化字符串文本|Pass|
|进入主页| 位于主页|不涉及| 页面正常显示指定屏幕分辨率图片|Pass|
|进入主页| 位于主页|不涉及| 页面正常显示仅系统查询图片|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示字符串文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示字符串数组文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示单复数字符串文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示设备状态文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示设备支持能力文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示图片字节数文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示图片编码长度文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常展示格式化字符串文本|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常显示指定屏幕分辨率图片|Pass|
|进入主页| 拉起资源API示例页面|不涉及|页面正常显示仅系统查询图片|Pass|
|进入主页| 拉起Overlay示例页面根据静态overlay脚本安装共享hsp包|不涉及|页面正常展示静态overlay的字符串和图片|Pass|
|进入主页| 拉起Overlay示例页面|点击disable去使能重启应用|页面正常展示entry应用的字符串和图片|Pass|
|进入主页| 拉起Overlay示例页面|点击enable使能重启应用|页面正常展示静态overlay的字符串和图片|Pass|
|进入主页| 拉起Overlay示例页面根据运行时overlay脚本安装共享hsp包|点击addResource进行资源加载|页面正常展示运行时overlay的字符串和图片|Pass|
|进入主页| 拉起Overlay示例页面根据运行时overlay脚本安装共享hsp包|点击removeResource进行资源移除|页面正常展示运行时overlay覆盖前的字符串和图片|Pass|
|进入主页| 拉起Overlay示例页面根据运行时overlay脚本安装共享hsp包|点击addResource进行资源加载杀掉进程。重启应用|运行时overlay失效页面正常展示运行时overlay覆盖前的字符串和图片|Pass|

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB