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 */ 15 16import deviceInfo from '@ohos.deviceInfo'; 17import { logger } from '../utils/Logger'; 18import systemParameterEnhance from '@ohos.systemParameterEnhance'; 19 20const TAG = '[DeviceUtil] : '; 21 22export enum DeviceType { 23 TYPE_PC = 0x0C, 24 TYPE_PHONE = 0x0E, 25 TYPE_TABLET = 0x11, 26 TYPE_2IN1 = 0xA2F, 27 TYPE_UNKNOWN = 0x0, 28} 29 30enum FoldPhoneTypeValue { 31 INVALID_VALUE = -1, //无效值 32 STRAIGHT = 0, //直板机 33 LARGE_FOLD = 1, //大折(内折) 34 SMALL_FOLD = 2, //小折叠(只有内折)(L) 35 EXTERNAL_FOLD = 3, //外折 36 EXPANDING_NEW_FORMS = 4, //扩展新形态(V) 37} 38 39const TYPE_DEFAULT: string = 'default'; 40const TYPE_PHONE: string = 'phone'; 41const TYPE_TABLET: string = 'tablet'; 42const TYPE_PC: string = '2in1'; 43 44export class DeviceUtil { 45 public static readonly IS_PC: boolean = deviceInfo.deviceType === TYPE_PC; 46 public static foldProductTypeValue: FoldPhoneTypeValue = FoldPhoneTypeValue.INVALID_VALUE; 47 48 public static isPhone(): boolean { 49 logger.info(`${TAG} isPhone in`); 50 return (deviceInfo.deviceType === TYPE_PHONE || deviceInfo.deviceType === TYPE_DEFAULT); 51 } 52 53 public static isPC(): boolean { 54 logger.info(`${TAG} isPC in`); 55 return deviceInfo.deviceType === TYPE_PC; 56 } 57 58 public static isDeviceTablet(deviceType: number): boolean { 59 logger.info(`${TAG} isDeviceTablet in. deviceType: ${deviceType}`); 60 return deviceType === DeviceType.TYPE_TABLET; 61 } 62 63 public static isExpandingNewForms(deviceType: number): boolean { 64 logger.info(`${TAG} isExpandingNewForms in. deviceType: ${deviceType}`); 65 return FoldPhoneTypeValue.EXPANDING_NEW_FORMS === DeviceUtil.getFoldProductType(); 66 } 67 68 /** 69 * 获取代表产品形态的配置项值 70 * 71 * @returns FoldPhoneTypeValue 72 */ 73 private static getFoldProductType(): number { 74 logger.info(`${TAG} getFoldProductType in.`); 75 if (DeviceUtil.foldProductTypeValue !== FoldPhoneTypeValue.INVALID_VALUE) { 76 return DeviceUtil.foldProductTypeValue; 77 } 78 try { 79 let productValue: string = systemParameterEnhance.getSync('const.window.foldscreen.type', '0.0.0.0'); 80 logger.info(`${TAG} productValue: ${productValue}`); 81 let result: string[] = productValue?.split(','); 82 if (result.length > 0) { 83 let res = Number.parseInt(result[0]); 84 logger.info(`${TAG} productValue res(num): ${res}`); 85 return res; 86 } 87 } catch (err) { 88 logger.error(`${TAG} getFoldProductType failed. error.message: ${err.message}`); 89 } 90 return FoldPhoneTypeValue.INVALID_VALUE; 91 } 92 93 /** 94 * 是否是小内折产品(L+V) 95 * 96 * @returns boolean 97 */ 98 public static isSmallFoldProduct(): boolean { 99 logger.info(`${TAG} isSmallFoldProduct in.`); 100 return [FoldPhoneTypeValue.SMALL_FOLD, 101 FoldPhoneTypeValue.EXPANDING_NEW_FORMS].includes(DeviceUtil.getFoldProductType()); 102 } 103 104 private static deviceTypeToNumber(deviceType: DeviceType): number { 105 switch (deviceType) { 106 case DeviceType.TYPE_TABLET: 107 return 1; 108 case DeviceType.TYPE_PHONE: 109 return 2; 110 case DeviceType.TYPE_2IN1: 111 return 3; 112 case DeviceType.TYPE_PC: 113 return 4; 114 default: 115 return 5; 116 } 117 } 118 119 private static deviceTypeToString(deviceType: number): string { 120 switch (deviceType) { 121 case DeviceType.TYPE_2IN1: 122 return TYPE_PC; 123 case DeviceType.TYPE_PHONE: 124 return TYPE_PHONE; 125 case DeviceType.TYPE_TABLET: 126 return TYPE_TABLET; 127 default: 128 return JSON.stringify(deviceType); 129 } 130 } 131}