• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 UIAbility from '@ohos.app.ability.UIAbility';
17import deviceInfo from '@ohos.deviceInfo';
18import AbilityConstant from '@ohos.app.ability.AbilityConstant';
19import fileio from '@ohos.fileio';
20import inputMethod from '@ohos.inputMethod';
21import { LogUtil } from '@ohos/utils/src/main/ets/default/baseUtil/LogUtil';
22import display from '@ohos.display';
23import window from '@ohos.window';
24import util from '@ohos.util';
25import common from '@ohos.app.ability.common';
26import relationalStore from '@ohos.data.relationalStore';
27
28AppStorage.SetOrCreate<relationalStore.RdbStore>('rdbStore', undefined);
29
30export default class MainAbility extends UIAbility {
31  private Tag = 'MainAbility_Tablet';
32
33  onCreate(want, launchParam) {
34
35    AppStorage.SetOrCreate('context', this.context);
36    LogUtil.info(this.Tag, " onCreate, launchReason is " + launchParam.launchReason + ", deviceType" + deviceInfo.deviceType);
37    if (deviceInfo.deviceType === 'phone' || deviceInfo.deviceType === 'default') {
38      AppStorage.SetOrCreate<boolean>('Expand', false);
39      AppStorage.SetOrCreate<boolean>('Choose', true);
40    }
41    if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) {
42      // 设置迁移标记
43      AppStorage.SetOrCreate<boolean>('IsContinue', true);
44      // 获取对端的迁移数据
45      let Search: boolean = want.parameters["Search"];
46      let continueNote: string = want.parameters["ContinueNote"];
47      let continueSection: number = want.parameters["ContinueSection"];
48      let scrollTopPercent: number = want.parameters["ScrollTopPercent"];
49      let isFocusOnSearch: boolean = want.parameters["isFocusOnSearch"];
50      LogUtil.info(this.Tag, " continueSection : " + continueSection);
51      AppStorage.SetOrCreate<boolean>('Search', Search);
52      AppStorage.SetOrCreate<string>('ContinueNote', continueNote);
53      AppStorage.SetOrCreate<number>('ContinueSection', continueSection);
54      // 使用新的key保存数据,防止迁移过来的数据在使用前被本地操作覆盖
55      AppStorage.SetOrCreate<number>('remoteScrollTopPercent', scrollTopPercent);
56      AppStorage.SetOrCreate<boolean>('isRemoteFocusOnSearch', isFocusOnSearch);
57      // 来自手机的迁移
58      let continueChoose: boolean = want.parameters["ContinueChoose"];
59      if (continueChoose) {
60        LogUtil.info(this.Tag, " continue from phone");
61        AppStorage.SetOrCreate<boolean>('ContinueFromPhone', true);
62      } else {
63        AppStorage.SetOrCreate<boolean>('ContinueFromTablet', true);
64        LogUtil.error(this.Tag, " continue from tablet");
65      }
66      this.context.restoreWindowStage(null);
67    }
68    AppStorage.SetOrCreate<common.UIAbilityContext>('noteContext', this.context);
69
70  }
71
72  onDestroy() {
73    LogUtil.info(this.Tag, " onDestroy");
74  }
75
76  onWindowStageCreate(windowStage) {
77    windowStage.getMainWindow((err, data) => {
78      let windowClass = data;
79      try {
80        windowClass.on('windowSizeChange', (data) => {
81          this.screenBreakPoints(data.width);
82        })
83        // 窗口规避区域
84        windowClass.on('avoidAreaChange', ({ type, area }) => {
85          if (type === window.AvoidAreaType.TYPE_SYSTEM) {
86            AppStorage.setOrCreate<number>('topHeight', area.topRect.height);
87            AppStorage.setOrCreate<number>('topWidth', area.topRect.width);
88          }
89        });
90        windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
91        // 设置主窗口沉浸式
92        windowClass.setWindowLayoutFullScreen(true);
93        // 设置主窗口导航栏、状态栏、文字颜色等属性
94        const sysBarProps: window.SystemBarProperties = {
95          statusBarColor: "#ffffff",
96          navigationBarColor: '#ffffff',
97          statusBarContentColor: '#000000',
98          navigationBarContentColor: '#000000'
99        };
100        // 加载状态变量
101        windowClass.setWindowSystemBarProperties(sysBarProps);
102      } catch (exception) {
103        LogUtil.error(this.Tag, 'windowSizeChange fail');
104      }
105    })
106    window.getLastWindow(this.context, (err, data) => {
107      if (data && data.getWindowProperties()) {
108        let windowWidth = data.getWindowProperties().windowRect.width;
109        LogUtil.info(this.Tag, " getLastWindow:" + windowWidth);
110        this.screenBreakPoints(windowWidth);
111      } else {
112        LogUtil.error(this.Tag, "getWindowProperties error:" + JSON.stringify(err));
113      }
114    })
115    LogUtil.info(this.Tag, " onWindowStageCreate");
116    windowStage.setUIContent(this.context, "pages/MyNoteHome", null);
117  }
118
119  onWindowStageDestroy() {
120    LogUtil.info(this.Tag, " onWindowStageDestroy");
121  }
122
123  onForeground() {
124    LogUtil.info(this.Tag, " onForeground");
125  }
126
127  onBackground() {
128    LogUtil.info(this.Tag, " onBackground");
129    // 退出键盘
130    inputMethod.getController().stopInputSession();
131  }
132
133  onContinue(wantParam: { [key: string]: any }) {
134    LogUtil.info(this.Tag, " onContinue");
135    // 获取本端的迁移数据
136    let Search = AppStorage.Get<boolean>('Search');
137    let continueNote = AppStorage.Get<string>('ContinueNote');
138    if (continueNote == undefined || continueNote == null) {
139      LogUtil.info(this.Tag, " onContinue, continueNote is error, default [0]");
140      continueNote = JSON.stringify(AppStorage.Get('AllNoteArray')[0].toNoteObject());
141    }
142
143    let continueSection = AppStorage.Get<number>('ContinueSection');
144    if (continueSection == undefined || continueSection == null) {
145      LogUtil.info(this.Tag, " onContinue, continueSection is error, default 3");
146      continueSection = 3;
147    }
148    LogUtil.info(this.Tag, " onContinue, continueSection : " + continueSection);
149
150    let scrollTopPercent = AppStorage.Get<number>('ScrollTopPercent');
151    if (scrollTopPercent == undefined || scrollTopPercent == null) {
152      LogUtil.info(this.Tag, " onContinue, scrollTopPercent is error, default 0");
153      scrollTopPercent = 0;
154    }
155
156    let isFocusOnSearch = AppStorage.Get<boolean>('isFocusOnSearch');
157    if (isFocusOnSearch == undefined || isFocusOnSearch == null) {
158      LogUtil.info(this.Tag, " onContinue, isFocusOnSearch is error, default true");
159      isFocusOnSearch = true;
160    }
161
162    // 保存本端的迁移数据
163    wantParam["Search"] = Search;
164    wantParam["ContinueNote"] = continueNote;
165    wantParam["ContinueSection"] = continueSection;
166    wantParam["ScrollTopPercent"] = scrollTopPercent;
167    wantParam["isFocusOnSearch"] = isFocusOnSearch;
168    if (deviceInfo.deviceType === 'phone' || deviceInfo.deviceType === 'default') {
169      wantParam["ContinueChoose"] = true;
170    }
171
172    // save img to DisFileDir
173    LogUtil.info(this.Tag, " onContinue, save img to DisFileDir");
174    let continueNoteObj = JSON.parse(continueNote);
175    let srcArray = this.getSrcFromHtml(continueNoteObj.content_text);
176    srcArray.forEach((src: string) => {
177      let lastIndex = src.lastIndexOf('/');
178      if (lastIndex != -1) {
179        let imgName = src.substring(lastIndex + 1);
180        this.writeToDisFileDir(imgName);
181      }
182    })
183    LogUtil.info(this.Tag, " onContinue end");
184    return AbilityConstant.OnContinueResult.AGREE;
185  }
186
187  getSrcFromHtml(html: string): any {
188    let srcArray = [];
189    if (html == undefined || html == null || html == "") {
190      return srcArray;
191    }
192    let base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
193    let realHtml;
194    if (base64regex.test(html)) {
195      let base64 = new util.Base64Helper;
196      realHtml = base64.decodeSync(html).toString();
197    } else {
198      realHtml = html;
199    }
200    let imgReg = /<img[^>]+>/g;
201    let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
202    let imgArray = realHtml.match(imgReg);
203    if (imgArray != null) {
204      for (let i = 0; i < imgArray.length; i++) {
205        let src = imgArray[i].match(srcReg);
206        if (src != null && src.length > 1) {
207          LogUtil.info(this.Tag, " getSrcFromHtml, src[1] : " + src[1]);
208          srcArray.push(src[1]);
209        }
210      }
211    }
212    return srcArray
213  }
214
215  writeToDisFileDir(fileName: string) {
216    LogUtil.info(this.Tag, " writeToDisFileDir, fileName : " + fileName);
217    let filesDir = this.context.filesDir;
218    let srcPath = filesDir + "/" + fileName;
219    let distributedFilesDir = this.context.distributedFilesDir;
220    let desPath = distributedFilesDir + "/" + fileName;
221    try {
222      fileio.copyFileSync(srcPath, desPath);
223      LogUtil.info(this.Tag, " onContinue, writeToDisFileDir, copyFile successfully" + desPath + " " + srcPath);
224    } catch (err) {
225      LogUtil.error(this.Tag, " onContinue, writeToDisFileDir, copyFile failed : " + err);
226    }
227  }
228
229  screenBreakPoints(data) {
230    let displayClass = null;
231    let screenDpi = null;
232    displayClass = display.getDefaultDisplaySync();
233    screenDpi = displayClass.densityDPI;
234    AppStorage.SetOrCreate('dpi', screenDpi);
235    let windowWidth = data / (screenDpi / 160);
236    LogUtil.debug(this.Tag, " screenBreakPoints windowWidth: " + windowWidth);
237    if (windowWidth >= 320 && windowWidth < 520 || windowWidth < 320) {
238      AppStorage.SetOrCreate('breakPoint', 'sm');
239    } else if (windowWidth >= 520 && windowWidth < 840) {
240      AppStorage.SetOrCreate('breakPoint', 'md');
241    } else if (windowWidth >= 840) {
242      AppStorage.SetOrCreate('breakPoint', 'lg');
243    }
244  }
245}