1/* 2 * Copyright (c) 2023-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 WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'; 17import http from '@ohos.net.http'; 18import Notification from '@ohos.notification'; 19import { WorkSchedulerSystem } from '../feature/WorkSchedulerSystem'; 20import { Logger } from '../utils/Logger'; 21 22const VERSION: string = '1.0'; 23const HTTP_STATUS_CODE: number = 200; 24const URL: string = 'https://gitee.com/openharmony/applications_app_samples/raw/master/code/DataMock/' + 25 'WorkScheduler/UpdateWorkScheduler.hap'; 26const BUNDLE_NAME = ['ohos.samples.workschedulerextensionability']; 27 28export default class WorkSchedulerAbility extends WorkSchedulerExtensionAbility { 29 onWorkStart(workInfo) { 30 Logger.info('WorkScheduler', 'WorkSchedulerAbility start'); 31 let filePath = JSON.parse(workInfo.parameters).filePath; 32 if (JSON.parse(workInfo.parameters).version === VERSION) { 33 let result: Promise<http.HttpResponse> = WorkSchedulerSystem.getNewHap(URL); 34 result.then(data => { 35 Logger.info('WorkScheduler', 'getNewHap success'); 36 if (data.responseCode === HTTP_STATUS_CODE) { 37 WorkSchedulerSystem.saveFile(filePath, data.result as ArrayBuffer); 38 WorkSchedulerSystem.publishNotification('download', 'isReady', 'download hap to update the application'); 39 Notification.subscribe({ 40 onConsume: (data) => { 41 if (data.request.content.normal.text === 'allow') { 42 let path: string[] = []; 43 path.push(filePath); 44 WorkSchedulerSystem.installBundle(path); 45 } 46 } 47 }, { 48 bundleNames: BUNDLE_NAME 49 }) 50 } 51 }) 52 } 53 } 54}