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 fs from '@ohos.file.fs' 17import Notification from '@ohos.notification' 18import bundle from '@ohos.bundle.installer' 19import account from '@ohos.account.osAccount' 20import workScheduler from '@ohos.resourceschedule.workScheduler' 21import http from '@ohos.net.http' 22import { Logger } from '../utils/Logger' 23 24const FILE_NAME = '/UpdateWorkScheduler.hap' 25const BUNDLE_NAMES = ['ohos.samples.workschedulerextensionability'] 26const INSTALL_PARAMETER = 1 27 28export namespace WorkSchedulerSystem { 29/** 30 * Store the file to the specified directory. 31 * 32 * @param pathDir Path to save the file. 33 * @param content The contents of the file to be saved. 34 */ 35 export function saveFile(pathDir: string, content: ArrayBuffer): void { 36 try { 37 let filePath = pathDir + FILE_NAME 38 let fd = fs.openSync(filePath, 0o2 | 0o100).fd 39 fs.writeSync(fd, content) 40 fs.closeSync(fd) 41 } catch (err) { 42 Logger.error(`saveFile failed, code is ${err.code}, message is ${err.message}`) 43 } 44 } 45 46 /** 47 * Sending a Notification. 48 * 49 * @param bundleName Check the name of the application that has permission. 50 * @permission ohos.permission.NOTIFICATION_CONTROLLER 51 */ 52 export async function handleNotification(bundleName: string): Promise<void> { 53 let isHavePermission = await Notification.isNotificationEnabled({ 54 bundle: bundleName 55 }) 56 if (!isHavePermission) { 57 await Notification.enableNotification({ 58 bundle: bundleName 59 }, true) 60 } 61 Notification.subscribe({ 62 onConsume: (data) => { 63 if (data.request.content.normal.text === 'isReady') { 64 AppStorage.SetOrCreate('isShowDialog', true) 65 } 66 } 67 }, { 68 bundleNames: BUNDLE_NAMES 69 }) 70 } 71 72 /** 73 * Publishes a notification of the specified content. 74 * 75 * @param title Title of Notice. 76 * @param text Content of Notification Text. 77 * @param additionalText Additional text. 78 * @permission ohos.permission.NOTIFICATION_CONTROLLER 79 */ 80 export function publishNotification(title: string, text: string, additionalText: string): void { 81 Notification.publish({ 82 content: { 83 contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 84 normal: { 85 title, 86 text, 87 additionalText 88 } 89 } 90 }) 91 } 92 93 /** 94 * Install the application package in the specified path. 95 * 96 * @param filePath An array of paths to hold the installation package. 97 * @permission ohos.permission.INSTALL_BUNDLE 98 */ 99 export async function installBundle(filePath: Array<string>): Promise<void> { 100 try { 101 let bundleInstall = await bundle.getBundleInstaller() 102 let userId = await account.getAccountManager().getOsAccountLocalIdFromProcess() 103 bundleInstall.install(filePath, { 104 userId: userId, 105 installFlag: INSTALL_PARAMETER, 106 isKeepData: false 107 }, (status, statusMessage) => { 108 Logger.info(`installBundle filepath is ${filePath}`) 109 Logger.info(`installBundle code is ${status.code}, message is ${JSON.stringify(statusMessage)}`) 110 }) 111 } catch (err) { 112 Logger.error(`installBundle failed, code is ${err.code}, message is ${err.message}`) 113 } 114 } 115 116 /** 117 * Register the delayed task and pass the parameters. 118 * 119 * @param version Current application version. 120 * @param bundleName The name of the application package for which the task needs to be registered. 121 * @param filePath Storage address of the application package. 122 */ 123 export async function startUpdateSample(version: string, bundleName: string, filePath: string): Promise<void> { 124 try { 125 let workInfo = { 126 workId: 1, 127 bundleName: bundleName, 128 abilityName: 'FormAbility', 129 networkType: workScheduler.NetworkType.NETWORK_TYPE_WIFI, 130 parameters: { 131 version: version, 132 filePath: filePath 133 } 134 } 135 Logger.info(`LSQ: info is ${JSON.stringify(workInfo)}`) 136 workScheduler.startWork(workInfo) 137 } 138 catch (err) { 139 Logger.error(`startWork failed, code is ${err.code}, message is ${err.message}`) 140 } 141 } 142 143 /** 144 * Register the delayed task and pass the parameters. 145 * 146 * @param url Url of the application package. 147 * @permission ohos.permission.INTERNET 148 */ 149 export async function getNewHap(url: string): Promise<http.HttpResponse> { 150 try { 151 return await http.createHttp().request( 152 url, 153 { 154 expectDataType: http.HttpDataType.ARRAY_BUFFER 155 }) 156 } catch (err) { 157 Logger.error(`get result failed, code is ${err.code}, message is ${err.message}`) 158 } 159 } 160} 161