• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import overlay from '@ohos.bundle.overlay';
17import { BusinessError } from '@ohos.base';
18
19@Entry
20@Component
21struct Overlay {
22  private resmgr = getContext().resourceManager;
23  @State message: string = 'Test Overlay'
24  @State resources: string = this.resmgr.getStringSync($r("app.string.test_string").id)
25  @State pixmap: PixelMap = this.resmgr.getDrawableDescriptor($r("app.media.icon").id).getPixelMap()
26
27  build() {
28    Column() {
29      Text($r('app.string.title'))
30        .width('100%')
31        .height(50)
32        .backgroundColor($r('app.color.text_color'))
33        .fontColor(Color.White)
34        .fontSize(20)
35        .padding({ left: 15 })
36
37      Text(`${this.message}`)
38        .fontSize(50)
39        .fontWeight(FontWeight.Bold)
40        .margin({
41          top: 40
42        })
43
44      Button() {
45        Text('disable')
46          .fontSize(20)
47          .fontWeight(FontWeight.Bold)
48      }
49      .type(ButtonType.Capsule)
50      .margin({
51        top: 50
52      })
53      .backgroundColor('#0D9FFB')
54      .width('50%')
55      .height('5%')
56      .onClick(() => {
57        // 非使能
58        overlay.setOverlayEnabled("libraryOverlay", false, (err, data) => {
59          if (err && err.code != 0) {
60            console.log("error:" + JSON.stringify(err));
61            this.message = this.resmgr.getStringSync($r('app.string.unEnableFailed').id);
62          } else {
63            console.log("data:" + JSON.stringify(data));
64            this.message = this.resmgr.getStringSync($r('app.string.unEnableSuccess').id);
65          }
66        })
67      })
68
69      Button() {
70        Text('enable')
71          .fontSize(20)
72          .fontWeight(FontWeight.Bold)
73      }
74      .type(ButtonType.Capsule)
75      .margin({
76        top: 20
77      })
78      .backgroundColor('#0D9FFB')
79      .width('50%')
80      .height('5%')
81      .onClick(() => {
82        // 使能
83        overlay.setOverlayEnabled("libraryOverlay", true, (err, data) => {
84          if (err && err.code != 0) {
85            console.log("error:" + JSON.stringify(err));
86            this.message = this.resmgr.getStringSync($r('app.string.enableFailed').id);
87          } else {
88            this.message = this.resmgr.getStringSync($r('app.string.enableSuccess').id);
89          }
90        })
91      })
92
93      Button() {
94        Text('addResource')
95          .fontSize(20)
96          .fontWeight(FontWeight.Bold)
97      }
98      .type(ButtonType.Capsule)
99      .margin({
100        top: 20
101      })
102      .backgroundColor('#0D9FFB')
103      .width('50%')
104      .height('5%')
105      .onClick(() => {
106        let path = getContext().bundleCodeDir + "/libraryRuntimeOverlay-default-signed.hsp";
107        try {
108          let ret = this.resmgr.addResource(path);
109          console.error("addResource: ret" + JSON.stringify(ret));
110        } catch (error) {
111          let code = (error as BusinessError).code;
112          let message = (error as BusinessError).message;
113          console.error(`addResource failed, error code: ${code}, message: ${message}.`);
114        }
115        this.pixmap = this.resmgr.getDrawableDescriptor($r("app.media.icon").id).getPixelMap();
116        this.resources = this.resmgr.getStringSync($r("app.string.test_string").id);
117      })
118
119      Button() {
120        Text('removeResource')
121          .fontSize(20)
122          .fontWeight(FontWeight.Bold)
123      }
124      .type(ButtonType.Capsule)
125      .margin({
126        top: 20
127      })
128      .backgroundColor('#0D9FFB')
129      .width('50%')
130      .height('5%')
131      .onClick(() => {
132        let path = getContext().bundleCodeDir + "/libraryRuntimeOverlay-default-signed.hsp";
133        try {
134          this.resmgr.removeResource(path);
135        } catch (error) {
136          let code = (error as BusinessError).code;
137          let message = (error as BusinessError).message;
138          console.error(`removeResource failed, error code: ${code}, message: ${message}.`);
139        }
140        this.pixmap = this.resmgr.getDrawableDescriptor($r("app.media.icon").id).getPixelMap();
141        this.resources = this.resmgr.getStringSync($r("app.string.test_string").id);
142      })
143
144      Image(this.pixmap)
145        .width(100)
146        .height(100)
147
148      Text(this.resources)
149        .fontSize(50)
150        .fontWeight(FontWeight.Bold)
151    }
152    .width('100%')
153    .height('100%')
154  }
155}