1/* 2 * Copyright (c) 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 */ 15import common from '@ohos.app.ability.common'; 16import bundleManager from '@ohos.bundle.bundleManager'; 17import hilog from '@ohos.hilog'; 18import Want from '@ohos.app.ability.Want'; 19import { BusinessError } from '@ohos.base'; 20import util from '@ohos.util'; 21 22const DOMAIN_ID = 0x3F3F; 23const TAG = 'TipsJump'; 24const APP_BUNDLE_NAME = ''; 25const TIPS_APP_ID = ''; 26const MODULE_NAME_PHONE = 'phone_widget'; 27const MODULE_NAME_PC = 'pc_widget'; 28const URI_FORMATTER = ''; 29const MODULE_NAME_APPGALLERY = ''; 30const URI_APPGALLERY = ''; 31const CODE_BUNDLE_NAME_NOT_FOUND = 17700001; 32const CODE_SUCCESS_ERROR = 0; 33const FUN_NUM_HOME = 'SF-20005815_f101'; 34type startAbleContext = common.UIAbilityContext | common.ServiceExtensionContext; 35 36export class TipsJumpUtils { 37 /** 38 * Jump to Tips Home 39 * 40 * @param startAbleContext:common.UIAbilityContext | common.ServiceExtensionContext 41 * @param type: entry type ID, used for dotting 42 * @param moduleName: phone_widget: jump phone, pc_widget: jump pc 43 */ 44 public static jumpHome(context: startAbleContext, type: string, moduleName: string = MODULE_NAME_PHONE) { 45 TipsJumpUtils.jumpTips(context, FUN_NUM_HOME, type, moduleName ?? MODULE_NAME_PHONE); 46 } 47 48 /** 49 * Jump to the specified page of Tips by FUN_NUM 50 * 51 * @param startAbleContext:common.UIAbilityContext | common.ServiceExtensionContext 52 * @param funNum: page FUN_NUM 53 * @param type: entry type ID, used for dotting 54 * @param moduleName: phone_widget: jump phone, pc_widget: jump pc 55 */ 56 public static jumpTips(context: startAbleContext, funNum: string, type: string, 57 moduleName: string = MODULE_NAME_PHONE) { 58 let uri = util.format(URI_FORMATTER, funNum, type); 59 TipsJumpUtils.jumpTipsByUri(context, uri, moduleName ?? MODULE_NAME_PHONE); 60 } 61 62 /** 63 * Jump to the specified page of Tips by uri 64 * 65 * @param startAbleContext:common.UIAbilityContext | common.ServiceExtensionContext 66 * @param uri: uri format:hwtips://?funNum=xxx&type=xxx 67 * @param moduleName: phone_widget: jump phone, pc_widget: jump pc 68 */ 69 public static jumpTipsByUri(context: startAbleContext, uri: string, moduleName: string = MODULE_NAME_PHONE) { 70 if (moduleName !== MODULE_NAME_PHONE && moduleName !== MODULE_NAME_PC) { 71 hilog.error(DOMAIN_ID, TAG, `unknown moduleName, supported value:"${MODULE_NAME_PHONE}" or "${MODULE_NAME_PC}"`); 72 return; 73 } 74 TipsJumpUtils.isAppInstalled().then((isAppInstalled: boolean) => { 75 if (isAppInstalled) { 76 TipsJumpUtils.jumpAppByUri(context, uri); 77 } else { 78 TipsJumpUtils.jumpAppGallery(context); 79 } 80 }) 81 } 82 83 /** 84 * Jump to Tips APP by uri 85 * 86 * @param startAbleContext:common.UIAbilityContext | common.ServiceExtensionContext 87 * @param uri: uri format:hwtips://?funNum=xxx&type=xxx 88 */ 89 private static jumpAppByUri(context: startAbleContext, uri: string) { 90 hilog.info(DOMAIN_ID, TAG, 'try jump to tips app'); 91 let want: Want = { 92 bundleName: APP_BUNDLE_NAME, 93 action: 'ohos.want.action.viewData', 94 entities: ['entity.system.browsable'], 95 uri 96 }; 97 context.startAbility(want, (err: BusinessError) => { 98 if (err.code !== CODE_SUCCESS_ERROR) { 99 hilog.warn(DOMAIN_ID, TAG, 'jump to [tips app] failed, error: %{private}s', JSON.stringify(err)); 100 } else { 101 hilog.info(DOMAIN_ID, TAG, 'jump to [tips app] success'); 102 } 103 }); 104 } 105 106 /** 107 * Jump to AppGallery by uri 108 * 109 * @param startAbleContext:common.UIAbilityContext | common.ServiceExtensionContext 110 */ 111 private static jumpAppGallery(context: startAbleContext) { 112 hilog.info(DOMAIN_ID, TAG, 'try to jump to AppGallery'); 113 const want: Want = { bundleName: MODULE_NAME_APPGALLERY, uri: URI_APPGALLERY }; 114 context.startAbility(want).then(() => { 115 hilog.info(DOMAIN_ID, TAG, 'jump to AppGallery success'); 116 }).catch(() => { 117 hilog.warn(DOMAIN_ID, TAG, 'jump to AppGallery error'); 118 }); 119 } 120 121 private static isAppInstalled(): Promise<boolean> { 122 return bundleManager.getBundleInfo(APP_BUNDLE_NAME, bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO) 123 .then((info) => { 124 if (TIPS_APP_ID === info.signatureInfo.appId) { 125 hilog.info(DOMAIN_ID, TAG, 'tips app is installed'); 126 return true; 127 } else { 128 hilog.warn(DOMAIN_ID, TAG, 'tips app is forged'); 129 return false; 130 } 131 }) 132 .catch((err: BusinessError) => { 133 if (err.code === CODE_BUNDLE_NAME_NOT_FOUND) { 134 hilog.info(DOMAIN_ID, TAG, 'tips app is not installed') 135 } else { 136 hilog.info(DOMAIN_ID, TAG, 'failed to check if tips app is installed, err: %{private}s', JSON.stringify(err)); 137 } 138 return false; 139 }) 140 } 141}