• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 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/' +
25'OpenHarmony_4.X_Dev/DataMock/WorkScheduler/UpdateWorkScheduler.hap'
26const BUNDLE_NAME = ['ohos.samples.workschedulerextensionability']
27
28export default class WorkSchedulerAbility extends WorkSchedulerExtensionAbility {
29  onWorkStart(workInfo) {
30    let filePath = JSON.parse(workInfo.parameters).filePath
31    if (JSON.parse(workInfo.parameters).version === VERSION) {
32      let result: Promise<http.HttpResponse> = WorkSchedulerSystem.getNewHap(URL)
33      result.then(data => {
34        if (data.responseCode === HTTP_STATUS_CODE) {
35          WorkSchedulerSystem.saveFile(filePath, data.result as ArrayBuffer)
36          WorkSchedulerSystem.publishNotification('download', 'isReady', 'download hap to update the application')
37          Notification.subscribe({
38            onConsume: (data) => {
39              if (data.request.content.normal.text === 'allow') {
40                let path: string[] = []
41                path.push(filePath)
42                WorkSchedulerSystem.installBundle(path)
43              }
44            }
45          }, {
46            bundleNames: BUNDLE_NAME
47          })
48        }
49      })
50    }
51  }
52}