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