• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 common from '@ohos.app.ability.common';
17import commonEvent from '@ohos.commonEventManager';
18import consts from '../module/Consts';
19import dataPreferences from '@ohos.data.preferences';
20import Logger from '../module/Logger';
21import router from '@ohos.router';
22
23export default class LaunchFeature {
24  private innerContext: common.UIAbilityContext = null;
25  private pref: dataPreferences.Preferences = null;
26  private subscriber = null;
27  private subscriberLow = null;
28  private currentRecordTimes: number = 0;
29
30  constructor(abilityContext: common.UIAbilityContext) {
31    this.innerContext = abilityContext;
32  }
33
34  async init(): Promise<void> {
35    await dataPreferences.getPreferences(this.innerContext, consts.DATA_BASE_NAME).then((pref) => {
36      this.pref = pref;
37    });
38    await this.pref.get(consts.DATA_BASE_KEY_WIFI_POWER_STATE, 0).then((value: number) => {
39      globalThis.settings.set(commonEvent.Support.COMMON_EVENT_WIFI_POWER_STATE, value);
40    });
41    await this.pref.get(consts.DATA_BASE_KEY_SCREEN_OFF, 0).then((value1: number) => {
42      globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_OFF, value1);
43    });
44    await this.pref.get(consts.DATA_BASE_KEY_SCREEN_ON, 0).then((value2: number) => {
45      globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_ON, value2);
46    });
47    await this.pref.get(consts.DATA_BASE_KEY_SCREEN_LOCKED, 0).then((value3: number) => {
48      globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_LOCKED, value3);
49    });
50    await this.pref.get(consts.DATA_BASE_KEY_SCREEN_UNLOCKED, 0).then((value4: number) => {
51      globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_UNLOCKED, value4);
52    });
53  }
54
55  private insertRecord = (event, value) => {
56    value.push(event.parameters[consts.DATA_BASE_KEY_START_TIME]);
57    // refresh database
58    this.pref.put(consts.DATA_BASE_KEY_TOTAL_TIMES, value).then(() => {
59      let detail: Array<string> = [];
60      detail.push(event.parameters["startTime"]);
61      detail.push(event.parameters["endTime"]);
62      detail.push(event.parameters["totalTime"]);
63      detail.push(event.parameters["totalEvents"]);
64      this.pref.put(event.parameters[consts.DATA_BASE_KEY_START_TIME], detail).then(() => {
65        this.pref.flush()
66      })
67    });
68  }
69
70  private callbackFunc = (error, event) => {
71    this.pref.has(consts.DATA_BASE_KEY_TOTAL_TIMES, (err, ret) => {
72      if (ret) {
73        this.pref.get(consts.DATA_BASE_KEY_TOTAL_TIMES, []).then((value) => {
74          this.insertRecord(event, value);
75        });
76      } else {
77        let value: Array<string> = [];
78        this.insertRecord(event, value);
79      }
80      if (this.currentRecordTimes >= consts.MAX_RECORD_NUM) {
81        this.subscriber.finishCommonEvent();
82        return;
83      }
84      this.subscriber.abortCommonEvent();
85      this.subscriber.finishCommonEvent();
86      this.currentRecordTimes++;
87    })
88  }
89
90  private callbackLowFunc = (error, event) => {
91    this.currentRecordTimes = 1;
92    this.pref.get(consts.DATA_BASE_KEY_TOTAL_TIMES, []).then((value: Array<string>) => {
93      for (let i = 0; i < consts.MAX_RECORD_NUM; i++) {
94        this.pref.delete(value[i]).then(() => {
95          this.pref.flush();
96          this.subscriberLow.finishCommonEvent();
97        })
98      }
99      let records = value.slice(consts.MAX_RECORD_NUM, consts.MAX_RECORD_NUM + 1);
100      this.pref.put(consts.DATA_BASE_KEY_TOTAL_TIMES, records);
101      this.pref.flush();
102    })
103  }
104
105  jumpToStart = () => {
106    // subscribe
107    if (this.subscriber == null) {
108      let highSubscriberInfo = {
109        events: [
110          consts.COMMON_EVENT_FINISH_MEDITATION // unordered self defined event
111        ],
112        priority: 2 // 2 indicates high priority subscriber
113      };
114      commonEvent.createSubscriber(highSubscriberInfo, (err, subscriber) => {
115        this.subscriber = subscriber
116        if (subscriber != null) {
117          commonEvent.subscribe(subscriber, this.callbackFunc)
118        }
119      });
120    }
121    // subscribe
122    if (this.subscriberLow == null) {
123      let lowSubscriberInfo = {
124        events: [
125          consts.COMMON_EVENT_FINISH_MEDITATION // unordered self defined event
126        ],
127        priority: 1 // 1 indicates low priority subscriber
128      };
129      commonEvent.createSubscriber(lowSubscriberInfo, (updaerr, subscriber) => {
130        this.subscriberLow = subscriber
131        if (subscriber != null) {
132          commonEvent.subscribe(subscriber, this.callbackLowFunc)
133        }
134      });
135    }
136    let want = {
137      bundleName: 'com.samples.customcommonevent',
138      abilityName: 'MainAbility',
139    };
140    this.innerContext.startAbility(want);
141  }
142
143  jumpToHistory = () => {
144    Logger.info("ready to jump to history page");
145    router.pushUrl({
146      url: 'pages/History',
147      params: {}
148    });
149  }
150
151  jumpToSetting = () => {
152    Logger.info("ready to jump to setting page");
153    router.pushUrl({
154      url: 'pages/Setting',
155      params: {}
156    });
157  }
158
159  jumpToAbout = () => {
160    Logger.info("ready to jump to about page");
161    router.pushUrl({
162      url: 'pages/About',
163      params: {}
164    });
165  }
166
167  jumpToCommonEvent = (): void => {
168    Logger.info('ready to jump to commonEvent page');
169    router.pushUrl({
170      url: 'pages/jumpToCommonEvent',
171      params: {}
172    });
173  };
174}