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