• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 AbilityConstant from '@ohos.app.ability.AbilityConstant';
17import hilog from '@ohos.hilog';
18import UIAbility from '@ohos.app.ability.UIAbility';
19import Want from '@ohos.app.ability.Want';
20import window from '@ohos.window';
21import hardwareManager from '@ohos.distributedHardware.hardwareManager';
22import { BusinessError } from '@ohos.base';
23
24const TAG = '[testTag_DHardwareUIAbility] : '
25
26PersistentStorage.persistProp('camera', 0);
27AppStorage.setOrCreate('camera', 0);
28console.log(TAG + "OUT : AppStorage.get<number>('camera') : " + AppStorage.get<number>('camera'));
29
30PersistentStorage.persistProp('mic', 0);
31AppStorage.setOrCreate('mic', 0);
32console.log(TAG + "OUT : AppStorage.get<number>('mic') : " + AppStorage.get<number>('mic'));
33
34export default class DHardwareUIAbility extends UIAbility {
35  isInit: boolean = true;
36  //是否是第一次初始化
37  isStopAbility: boolean = false;
38  cameraCount: number = 0;
39
40  stop() {
41    console.info(TAG + 'stop');
42    try {
43      hardwareManager.stopDistributedHardware(AppStorage.get('description') as hardwareManager.HardwareDescriptor)
44        .then(() => {
45          console.log('stop distributed hardware successfully');
46        })
47        .catch((error: BusinessError) => {
48          console.error('stop distributed hardware failed, cause:' + error);
49        })
50      console.log('stop distributed hardware successfully');
51    } catch (error) {
52      console.error('stop distributed hardware failed:' + error);
53    }
54  }
55
56  pause() {
57    console.log(TAG + 'pause');
58    try {
59      hardwareManager.pauseDistributedHardware(AppStorage.get('description') as hardwareManager.HardwareDescriptor)
60        .then(() => {
61          console.log('pause distributed hardware successfully');
62        })
63        .catch((error: BusinessError) => {
64          console.error('pause distributed hardware failed, cause:' + error);
65        })
66      console.log('pause distributed hardware successfully');
67    } catch (error) {
68      console.error('pause distributed hardware failed:' + error);
69    }
70  }
71
72  resume() {
73    console.log(TAG + 'resume');
74    try {
75      hardwareManager.resumeDistributedHardware(AppStorage.get('description') as hardwareManager.HardwareDescriptor)
76        .then(() => {
77          console.log('resume distributed hardware successfully');
78        })
79        .catch((error: BusinessError) => {
80          console.error('resume distributed hardware failed, cause:' + error);
81        })
82      console.log('resume distributed hardware successfully');
83    } catch (error) {
84      console.error('resume distributed hardware failed:' + error);
85    }
86  }
87
88
89  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
90    console.info(TAG + 'onCreate : ' + 'Ability onCreate');
91
92    // 每次被拉起时,默认设置没有执行StopAbility
93    this.isStopAbility = false;
94    console.info(TAG + 'onCreate : ' + 'this.isStopAbility : ' + this.isStopAbility);
95
96    if (want.parameters != undefined) {
97      console.info(TAG + 'onCreate : want.parameters.type : ' + JSON.stringify(want.parameters['type']));
98      console.info(TAG + 'onCreate : want.parameters.srcNetWorkId : ' + JSON.stringify(want.parameters['srcNetWorkId']));
99      console.info(TAG + 'onCreate : want.parameters.deviceName : ' + JSON.stringify(want.parameters['deviceName']));
100      console.info(TAG + 'onCreate : want.parameters.deviceType : ' + JSON.stringify(want.parameters['deviceType']));
101      console.info(TAG + 'onCreate : want.parameters.returnCode : ' + JSON.stringify(want.parameters['returnCode']));
102
103      AppStorage.setOrCreate('type', want.parameters['type']);
104      AppStorage.setOrCreate('srcNetworkId', want.parameters['srcNetworkId']);
105      AppStorage.setOrCreate('deviceName', want.parameters['deviceName']);
106      AppStorage.setOrCreate('deviceType', want.parameters['deviceType']);
107
108      console.log(TAG + "oncreate : after set want -- type --");
109
110      // startAbility时,每次修改type的值+1
111      if (want.parameters['type'] == 1) {
112        let camera: number = AppStorage.get<number>('camera') as number + 1;
113        AppStorage.set('camera', camera);
114        console.log(TAG + "onCreate : " + "AppStorage.get<number>('camera') : " + AppStorage.get<number>('camera'));
115      } else if (want.parameters['type'] == 1024) {
116        let mic: number = AppStorage.get<number>('mic') as number + 1;
117        AppStorage.set('mic', mic);
118        console.log(TAG + "onCreate : " + "AppStorage.get<number>('mic') : " + AppStorage.get<number>('mic'));
119      }
120
121      if (want.parameters['returnCode'] == 24200102) {
122        try {
123          this.context.terminateSelf((err: BusinessError) => {
124            if (err.code) {
125              console.error(`terminateSelf failed,code is ${err.code},message is ${err.message}`);
126              return;
127            }
128            console.log(TAG + 'terminateSelf succeed');
129          });
130        } catch (err) {
131          let code = (err as BusinessError).code;
132          let message = (err as BusinessError).message;
133          console.error(`terminateSelf failed,code is ${code},message is ${message}`);
134        }
135      }
136    }
137  }
138
139  onNewWant(want: Want) {
140    console.info(TAG + 'onNewWant : ' + 'Ability onNewWant');
141
142    if (want.parameters != undefined) {
143      console.info(TAG + 'onNewWant : want.parameters : ' + JSON.stringify(want.parameters));
144      console.info(TAG + "onNewWant : want.parameters['returnCode'] : " + JSON.stringify(want.parameters['returnCode']));
145      console.info(TAG + "onNewWant : want.parameters['type'] : " + JSON.stringify(want.parameters['type']));
146
147      AppStorage.setOrCreate('returnCode', want.parameters['returnCode']);
148      AppStorage.setOrCreate('type', want.parameters['type']);
149
150      let returnCode: number = want.parameters['returnCode'] as number;
151      console.info(TAG + 'onNewWant : returnCode : ' + returnCode);
152      AppStorage.setOrCreate('returnCode', returnCode);
153
154      // returnCode为undefined,表明新拉起了type,对应type+1
155      if (returnCode == undefined) {
156        // 切换前后置后刷新按钮文本为‘暂停’
157        AppStorage.setOrCreate('isPause', true);
158        console.log(TAG + "onNewWant : refresh Btn Text : " + "AppStorage.get<boolean>('isPause') : " + AppStorage.get<boolean>('isPause'));
159
160        if (want.parameters['type'] == 1) {
161          let camera: number = AppStorage.get<number>('camera') as number + 1;
162          AppStorage.set('camera', camera);
163          AppStorage.setOrCreate('switchedCamera', camera);
164          console.log(TAG + "onNewWant : " + "[returnCode==undefined]--[stop camera]--AppStorage.get<number>('camera') : " + AppStorage.get<number>('camera'));
165        } else if (want.parameters['type'] == 1024) {
166          let mic: number = AppStorage.get<number>('mic') as number + 1;
167          AppStorage.set('mic', mic);
168          console.log(TAG + "onNewWant : " + "[returnCode==undefined]--[stop mic]--AppStorage.get<number>('mic') : " + AppStorage.get<number>('mic'));
169        }
170      }
171
172      // 接收到的returnCode如果满足条件,则表面已调用stopAbility
173      if (returnCode == 24200102) {
174        this.isStopAbility = true;
175        console.info(TAG + 'onNewWant :[returnCode == 24200102] this.isStopAbility : ' + this.isStopAbility);
176
177        // stopAbility时,每次修改type的值
178        if (want.parameters['type'] == 1) {
179          let camera: number = AppStorage.get<number>('camera') as number - 1;
180          AppStorage.set('camera', camera);
181          console.log(TAG + "onNewWant : " + "[returnCode==24200102]--[stop camera]--AppStorage.get<number>('camera') : " + AppStorage.get<number>('camera'));
182        } else if (want.parameters['type'] == 1024) {
183          let mic: number = AppStorage.get<number>('mic') as number - 1;
184          AppStorage.set('mic', mic);
185          console.log(TAG + "onNewWant : " + "[returnCode==24200102]--[stop mic]--AppStorage.get<number>('mic') : " + AppStorage.get<number>('mic'));
186        }
187
188        if (AppStorage.get<number>('camera') == 0 && AppStorage.get<number>('mic') == 0) {
189          try {
190            this.context.terminateSelf((err: BusinessError) => {
191              if (err.code) {
192                console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
193                return;
194              }
195              console.info(TAG + 'terminateSelf succeed');
196            });
197          } catch (err) {
198            let code = (err as BusinessError).code;
199            let message = (err as BusinessError).message;
200            console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
201          }
202        }
203      }
204    }
205  }
206
207  onDestroy() {
208    console.info(TAG + 'onDestroy : ' + 'Ability onDestroy');
209
210    let sign: string = AppStorage.get<string>('sign') as string;
211    if (sign == 'stop') {
212      return;
213    }
214    let stopFlag = AppStorage.get<number>('camera') == 0 && AppStorage.get<number>('mic') == 0;
215
216    // stopAbility后应用销毁自身:不再执行停止函数
217    if (!stopFlag && AppStorage.get<string>('sign') != 'onBackPress') {
218      console.info(TAG + 'onDestroy : ' + 'this.isStopAbility : ' + this.isStopAbility);
219      console.info(TAG + 'onDestroy : ' + "AppStorage.get<string>('sign') : " + AppStorage.get<string>('sign'));
220
221      this.stop();
222      console.info(TAG + 'onDestroy : ' + 'Distributed Hardware stopped');
223    }
224
225  }
226
227  onWindowStageCreate(windowStage: window.WindowStage) {
228    // Main window is created, set main page for this ability
229    console.info(TAG + 'onWindowStageCreate : ' + 'Ability onWindowStageCreate');
230
231    windowStage.loadContent('pages/DHardwareUI', (err, data) => {
232      if (err.code) {
233        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
234        return;
235      }
236      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
237    });
238  }
239
240  onWindowStageDestroy() {
241    // Main window is destroyed, release UI related resources
242    console.info(TAG + 'onWindowStageDestroy : ' + 'Ability onWindowStageDestroy');
243
244  }
245
246  eventFunc() {
247    console.info(TAG + 'call anonymous eventFunc : pause');
248  }
249
250  onForeground() {
251    // Ability has brought to foreground
252    console.info(TAG + 'onForeground : ' + 'Ability onForeground');
253
254    // 存储一个标志:isInit 记录应用是否是第一次初始化进入后台
255    AppStorage.setOrCreate('isInit', this.isInit);
256    console.info(TAG + 'onForeground : ' + " AppStorage.get<boolean>('isInit') : " + AppStorage.get<boolean>('isInit'));
257    console.info(TAG + 'onForeground : ' + " AppStorage.get<boolean>('isPauseTag') : " + AppStorage.get<boolean>('isPauseTag'));
258
259    // 再次回到前台:既不是第一次初始化页面,也不是暂停状态,此时触发恢复函数
260    if (!this.isInit && !AppStorage.get<boolean>('isPauseTag')) {
261      console.info(TAG + 'onForeground : ' + 'this.isInit : ' + this.isInit);
262
263      if (AppStorage.get<number>('camera') != 0 || AppStorage.get<number>('mic') != 0) {
264        console.log(TAG + "onForeground : " + "AppStorage.get<number>('camera') : " + AppStorage.get<number>('camera'));
265        console.log(TAG + "onForeground : " + "AppStorage.get<number>('mic') : " + AppStorage.get<number>('mic'));
266
267        this.resume();
268      }
269    }
270    this.isInit = false;
271    AppStorage.setOrCreate('isInit', false);
272    // 恢复未点击暂停状态:点击暂停状态初始化
273    AppStorage.setAndLink('isPause', true);
274    console.info(TAG + 'onForeground : ' + "AppStorage.get<boolean>('isPause') : " + AppStorage.get<boolean>('isPause'));
275  }
276
277  onBackground() {
278    // Ability has back to background
279    console.info(TAG + 'onBackground : ' + 'Ability onBackground');
280
281    let sign: string = AppStorage.get<string>('sign') as string;
282    if (sign == 'stop') {
283      return;
284    }
285    // stopAbility后应用销毁自身:不再执行暂停函数
286    let stopFlag = AppStorage.get<number>('camera') == 0 && AppStorage.get<number>('mic') == 0;
287    console.log(TAG + 'onBackground : stopFlag : ' + stopFlag)
288    if (!stopFlag) {
289      console.info(TAG + 'onBackground : ' + 'this.isStopAbility' + this.isStopAbility);
290      this.pause();
291      console.info(TAG + 'onBackground : ' + 'Distributed Hardware paused');
292    }
293  }
294
295  onBackPressed() {
296    return true;
297  }
298}
299