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 Ability from '@ohos.app.ability.UIAbility' 17import AbilityConstant from '@ohos.app.ability.AbilityConstant' 18import accessControl from "@ohos.abilityAccessCtrl"; 19import bundle from '@ohos.bundle'; 20import distributedObject from '@ohos.data.distributedDataObject'; 21import Logger from './Logger'; 22 23const BUNDLE_NAME = "com.ohos.continuationManualTestSuite.hmservice" 24const PERMISSION_REJECT = -1 25 26class ContentStorage { 27} 28 29const g_object = distributedObject.createDistributedObject({name: undefined}); 30 31export default class MainAbility extends Ability { 32 33 contentStorage : ContentStorage 34 sessionId : string 35 36 handleParam(want, launchParam) { 37 if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { 38 this.sessionId = want.parameters.session 39 Logger.info(`continuation sessionId: ${this.sessionId}`) 40 41 g_object.name = undefined; 42 Logger.info(`set g_object.name undefined`) 43 44 g_object.setSessionId(this.sessionId); // set session id, so it will sync data from remote device 45 Logger.info(`g_object_name = ${g_object.name}`); 46 47 AppStorage.SetOrCreate<string>('ContinueStudy', g_object.name) 48 let workInput = want.parameters.work // get user data from want params 49 Logger.info(`work input ${workInput}`) 50 AppStorage.SetOrCreate<string>('ContinueWork', workInput) 51 this.contentStorage = new ContentStorage(); 52 Logger.info('ready to restore'); 53 this.context.restoreWindowStage(this.contentStorage); 54 } 55 } 56 57 onCreate(want, launchParam) { 58 Logger.info("[Demo] MainAbility onCreate") 59 globalThis.abilityWant = want; 60 this.handleParam(want, launchParam) 61 } 62 63 onContinue(wantParam : {[key: string]: any}) { 64 Logger.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`) 65 let workInput = AppStorage.Get<string>('ContinueWork'); 66 Logger.info(`onContinue work input = ${workInput}`); 67 68 if (g_object.__sessionId === undefined) { 69 this.sessionId = distributedObject.genSessionId() 70 Logger.info(`onContinue generate new sessionId`) 71 } 72 else { 73 this.sessionId = g_object.__sessionId; 74 } 75 76 wantParam["session"] = this.sessionId 77 g_object.name = AppStorage.Get<string>('ContinueStudy'); 78 Logger.info(`onContinue sessionId = ${this.sessionId}, name = ${g_object.name}`) 79 g_object.setSessionId(this.sessionId); 80 g_object.save(wantParam.targetDevice, (result, data)=>{ 81 Logger.info("save callback"); 82 Logger.info("save sessionId " + data.sessionId); 83 Logger.info("save version " + data.version); 84 Logger.info("save deviceId " + data.deviceId); 85 }); 86 wantParam["work"] = workInput // set user input data into want params 87 return 0; 88 } 89 90 onNewWant(want, launchParam) { 91 Logger.info("MainAbility onNewWant") 92 this.handleParam(want, launchParam) 93 } 94 95 requestPermissions = async () => { 96 let permissions: Array<string> = [ 97 "ohos.permission.DISTRIBUTED_DATASYNC" 98 ]; 99 let needGrantPermission = false 100 let accessManger = accessControl.createAtManager() 101 Logger.info("app permission get bundle info") 102 let bundleInfo = await bundle.getApplicationInfo(BUNDLE_NAME, 0, 100) 103 Logger.info(`app permission query permission ${bundleInfo.accessTokenId.toString()}`) 104 for (const permission of permissions) { 105 Logger.info(`app permission query grant status ${permission}`) 106 try { 107 let grantStatus = await accessManger.verifyAccessToken(bundleInfo.accessTokenId, permission) 108 if (grantStatus === PERMISSION_REJECT) { 109 needGrantPermission = true 110 break; 111 } 112 } catch (err) { 113 Logger.error(`app permission query grant status error ${permission} ${JSON.stringify(err)}`) 114 needGrantPermission = true 115 break; 116 } 117 } 118 if (needGrantPermission) { 119 Logger.info("app permission needGrantPermission") 120 try { 121 await this.context.requestPermissionsFromUser(permissions) 122 } catch (err) { 123 Logger.error(`app permission ${JSON.stringify(err)}`) 124 } 125 } else { 126 Logger.info("app permission already granted") 127 } 128 } 129 130 onDestroy() { 131 Logger.info("MainAbility onDestroy") 132 } 133 134 onWindowStageCreate(windowStage) { 135 // Main window is created, set main page for this ability 136 Logger.info("MainAbility onWindowStageCreate") 137 this.requestPermissions() 138 windowStage.setUIContent(this.context, "pages/index", null) 139 } 140 141 onWindowStageRestore(windowStage) { 142 Logger.info("MainAbility onWindowStageRestore") 143 this.requestPermissions() 144 } 145 146 onWindowStageDestroy() { 147 // Main window is destroyed, release UI related resources 148 Logger.info("MainAbility onWindowStageDestroy") 149 } 150 151 onForeground() { 152 // Ability has brought to foreground 153 Logger.info("MainAbility onForeground") 154 } 155 156 onBackground() { 157 // Ability has back to background 158 Logger.info("MainAbility onBackground") 159 } 160}; 161