diff --git a/application/AppScope/app.json b/application/AppScope/app.json index e3c09a7ead3289c2f3d46f6244d4998c12671cec..5ead3fcf4942376fd3e64f0f9699e618fde7586c 100644 --- a/application/AppScope/app.json +++ b/application/AppScope/app.json @@ -2,8 +2,8 @@ "app": { "bundleName": "com.ohos.dhardwareui", "vendor": "example", - "versionCode": 10000034, - "versionName": "1.0.34", + "versionCode": 10000035, + "versionName": "1.0.35", "icon": "$media:app_icon", "label": "$string:app_name", "minAPIVersion": 12, diff --git a/application/AppScope/app.json5 b/application/AppScope/app.json5 index e969d9a1ee21043eef79f3382d3ce841ae02ff85..d626d03bd5bf6d2a2d10419bfe62d44ed0289c4b 100644 --- a/application/AppScope/app.json5 +++ b/application/AppScope/app.json5 @@ -16,8 +16,8 @@ "app": { "bundleName": "com.ohos.dhardwareui", "vendor": "example", - "versionCode": 10000034, - "versionName": "1.0.34", + "versionCode": 10000035, + "versionName": "1.0.35", "icon": "$media:app_icon", "label": "$string:app_name", "minAPIVersion": 12, diff --git a/application/entry/src/main/ets/continueswitchability/ContinueSwitchAbility.ets b/application/entry/src/main/ets/continueswitchability/ContinueSwitchAbility.ets index 3167e7f8453251076b78260701730e33e00951df..31e0b81eb60693375d35da8e8227612956247bfa 100644 --- a/application/entry/src/main/ets/continueswitchability/ContinueSwitchAbility.ets +++ b/application/entry/src/main/ets/continueswitchability/ContinueSwitchAbility.ets @@ -49,7 +49,7 @@ export default class ContinueSwitchAbility extends UIExtensionAbility { let pushParams = want.parameters?.pushParams as string | undefined; let startReason = ''; let isShowBack = - pushParams?.includes('isShowBack') ? (pushParams?.includes('isShowBack:false') ? false : true) : true; + pushParams?.includes('isShowBack') ? (pushParams.includes('isShowBack:false') ? false : true) : true; if (parameters) { startReason = parameters['startReason'] as string; } @@ -61,8 +61,7 @@ export default class ContinueSwitchAbility extends UIExtensionAbility { }; let storage: LocalStorage = new LocalStorage(param); session.loadContent('pages/ContinueSwitch', storage); - console.log(TAG, - `onSessionCreate end. AppStorage.get('startReason')= ${AppStorage.get('startReason')}`); + console.log(TAG, `onSessionCreate end. startReason: ${startReason},isShowBack: ${isShowBack}`); } onSessionDestroy(session: UIExtensionContentSession) { diff --git a/application/entry/src/main/ets/pages/ContinueSwitch.ets b/application/entry/src/main/ets/pages/ContinueSwitch.ets index 6e5f95e3ce2c6dc13dab81231b7ebce787c1c688..b1533a2411b561256307af5149b2b76235e09091 100644 --- a/application/entry/src/main/ets/pages/ContinueSwitch.ets +++ b/application/entry/src/main/ets/pages/ContinueSwitch.ets @@ -28,6 +28,7 @@ import osAccount from '@ohos.account.osAccount'; import systemParameterEnhance from '@ohos.systemParameterEnhance'; import i18n from '@ohos.i18n'; import { LengthMetrics } from '@ohos.arkui.node'; +import { DeviceUtil } from '../utils/DeviceUtil'; const TAG = '[ContinueSwitch_Page] : '; let context = getContext(this) as common.UIAbilityContext; @@ -43,7 +44,7 @@ let switchState: switchStatus = { close: CommonConstants.SWITCH_STATUS_CLOSE } -@Entry +@Entry(localStorage) @Component struct ContinueSwitch { @StorageLink('isSwitchOn') isSwitchOn: boolean | undefined = true; @@ -87,6 +88,7 @@ struct ContinueSwitch { private continueDesc: ResourceStr = $r('app.string.continue_desc_text', ''); private accountManager: osAccount.AccountManager = osAccount.getAccountManager(); private startReason?: string = ''; + private isSmallFoldProduct: boolean = DeviceUtil.isSmallFoldProduct(); onPortrait(mediaQueryResult: mediaQuery.MediaQueryResult): void { logger.info(`${TAG} 'onPortrait in`); @@ -255,14 +257,11 @@ struct ContinueSwitch { this.listener.on('change', (mediaQueryResult: mediaQuery.MediaQueryResult) => { this.onPortrait(mediaQueryResult); }) + this.registerFoldChangeListener(); this.extContext = localStorage.get('context'); this.startReason = AppStorage.get('startReason'); this.isShowBack = AppStorage.get('isShowBack') ?? true; logger.info(`${TAG} aboutToAppear: startReason is ${this.startReason}, isShowBack: ${this.isShowBack}`); - if (this.isPhone()) { - this.checkFoldBackButton(); - } - this.checkPcPadBackButton(); this.phoneSwitchTextMarginInit(); setTimeout(() => { this.isAnimatorDone = true; @@ -271,6 +270,7 @@ struct ContinueSwitch { aboutToDisappear() { logger.info(`${TAG} aboutToDisappear in`); + this.unRegisterFoldChangeListener(); } onBackPress() { @@ -508,6 +508,15 @@ struct ContinueSwitch { if (typeof newArea.width === 'number' && typeof newArea.height === 'number') { let width = newArea.width as number; let halfHeight = (newArea.height as number) / 2; + try { + let displayInfo: display.Display | undefined = display.getDefaultDisplaySync(); + if (displayInfo) { + halfHeight = px2vp(displayInfo.height / 2); + logger.info(`${TAG} display: halfHeight = ${halfHeight}`); + } + } catch (error) { + logger.error(`${TAG} getDefaultDisplaySync failed. error.message: ${error.message}`); + } if (width > halfHeight) { this.imageAnimatorWidth = halfHeight * 0.8; this.imageAnimatorHeight = this.imageAnimatorWidth * 2 / 3; @@ -546,60 +555,59 @@ struct ContinueSwitch { return CommonConstants.ANIMATOR_WIDTH_PHONE; } - private checkPcPadBackButton(): void { - logger.info(`${TAG} checkPcPadBackButton in`); - if (this.startReason === 'from_search') { - if (deviceInfo.deviceType === '2in1' || deviceInfo.deviceType === 'tablet') { - this.isShowBack = false; - } - } - } - private isPhone(): boolean { logger.info(`${TAG} isPhone in`); return (deviceInfo.deviceType === 'phone' || deviceInfo.deviceType === 'default'); } + /** + * 根据折叠状态更新是否显示返回按钮 + */ private refreshFoldStatus(foldStatus: display.FoldStatus): void { logger.info(`${TAG} refreshFoldStatus in. foldStatus: ${foldStatus}. startReason: ${this.startReason}`); - if (this.startReason === 'from_search') { - if (foldStatus === display.FoldStatus.FOLD_STATUS_FOLDED) { - this.isShowBack = true; - logger.info(`${TAG} foldStatus: ${foldStatus}. this.isShowBack: ${this.isShowBack}`); - } else { - this.isShowBack = false; - logger.info(`${TAG} foldStatus: ${foldStatus}. this.isShowBack: ${this.isShowBack}`); - } - } else { - this.isShowBack = true; - logger.info(`${TAG} startReason: ${this.startReason}. this.isShowBack: ${this.isShowBack}`); - } + this.isShowBack = (this.startReason !== 'from_search' || foldStatus === display.FoldStatus.FOLD_STATUS_FOLDED); + logger.info(`${TAG} refreshFoldStatus end. this.isShowBack: ${this.isShowBack}.`); + } + + /** + * 折叠状态发生变化 + */ + private displayCallback: Callback = (data: display.FoldStatus) => { + this.refreshFoldStatus(data); } - private checkFoldBackButton(): void { - logger.info(`${TAG} checkFoldBackButton in`); + private registerFoldChangeListener(): void { + logger.info(`${TAG} registerFoldChangeListener in.isSmallFoldProduct: ${this.isSmallFoldProduct}`); try { - if (display.isFoldable()) { - display.on('foldStatusChange', (foldStatus: display.FoldStatus) => { - let foldStatusValue = foldStatus.valueOf(); - logger.info(`${TAG} checkFoldBackButton: foldStatusValue is ${foldStatusValue}`); - this.refreshFoldStatus(foldStatusValue); - }) + // 首次非L+V折叠刷新isShowBack + if (!this.isSmallFoldProduct && display.isFoldable()) { let data: display.FoldStatus = display.getFoldStatus(); this.refreshFoldStatus(data); } + display.on('foldStatusChange', this.displayCallback); + } catch (err) { + logger.error(`${TAG} registerFoldChangeListener failed. err.message: ${err.message}`); + } + } + + private unRegisterFoldChangeListener(): void { + logger.info(`${TAG} unRegisterFoldChangeListener in.`); + try { + display.off('foldStatusChange', this.displayCallback); } catch (err) { - logger.error(`${TAG} display.isFoldable,on failed. exception: ${err.message}`); + logger.error(`${TAG} unRegisterFoldChangeListener failed. err.message: ${err.message}`); } } - private isMirrorLanguages(): boolean { + private isMirrorLanguages(): + boolean { logger.info(`${TAG} isMirrorLanguages in`); let locale = new Intl.Locale(i18n.System.getSystemLanguage()).toString(); return CommonConstants.MIRROR_LANGUAGES.includes(locale); } - private isTibetanLanguages(): boolean { + private isTibetanLanguages(): + boolean { logger.info(`${TAG} isTibetanLanguages in`); let locale = new Intl.Locale(i18n.System.getSystemLanguage()).toString(); logger.info(`${TAG} isTibetanLanguages: ${locale}`); diff --git a/application/entry/src/main/ets/utils/DeviceUtil.ets b/application/entry/src/main/ets/utils/DeviceUtil.ets new file mode 100644 index 0000000000000000000000000000000000000000..4b5effc9607e41f597e127f8fa046e0ee8f07cdb --- /dev/null +++ b/application/entry/src/main/ets/utils/DeviceUtil.ets @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import deviceInfo from '@ohos.deviceInfo'; +import { logger } from '../utils/Logger'; +import systemParameterEnhance from '@ohos.systemParameterEnhance'; + +const TAG = '[DeviceUtil] : '; + +export enum DeviceType { + TYPE_PC = 0x0C, + TYPE_PHONE = 0x0E, + TYPE_TABLET = 0x11, + TYPE_2IN1 = 0xA2F, + TYPE_UNKNOWN = 0x0, +} + +enum FoldPhoneTypeValue { + INVALID_VALUE = -1, //无效值 + STRAIGHT = 0, //直板机 + LARGE_FOLD = 1, //大折(内折) + SMALL_FOLD = 2, //小折叠(只有内折)(L) + EXTERNAL_FOLD = 3, //外折 + EXPANDING_NEW_FORMS = 4, //扩展新形态(V) +} + +const TYPE_DEFAULT: string = 'default'; +const TYPE_PHONE: string = 'phone'; +const TYPE_TABLET: string = 'tablet'; +const TYPE_PC: string = '2in1'; + +export class DeviceUtil { + public static readonly IS_PC: boolean = deviceInfo.deviceType === TYPE_PC; + public static foldProductTypeValue: FoldPhoneTypeValue = FoldPhoneTypeValue.INVALID_VALUE; + + private static isPhone(): boolean { + logger.info(`${TAG} isPhone in`); + return (deviceInfo.deviceType === TYPE_PHONE || deviceInfo.deviceType === TYPE_DEFAULT); + } + + private static isPC(): boolean { + logger.info(`${TAG} isPC in`); + return deviceInfo.deviceType === TYPE_PC; + } + + private static isDeviceTablet(deviceType: number): boolean { + logger.info(`${TAG} isDeviceTablet in. deviceType: ${deviceType}`); + return deviceType === DeviceType.TYPE_TABLET; + } + + private static isExpandingNewForms(deviceType: number): boolean { + logger.info(`${TAG} isExpandingNewForms in. deviceType: ${deviceType}`); + return FoldPhoneTypeValue.EXPANDING_NEW_FORMS === DeviceUtil.getFoldProductType(); + } + + /** + * 获取代表产品形态的配置项值 + * + * @returns FoldPhoneTypeValue + */ + private static getFoldProductType(): number { + logger.info(`${TAG} getFoldProductType in.`); + if (DeviceUtil.foldProductTypeValue !== FoldPhoneTypeValue.INVALID_VALUE) { + return DeviceUtil.foldProductTypeValue; + } + try { + let productValue: string = systemParameterEnhance.getSync('const.window.foldscreen.type', '0.0.0.0'); + logger.info(`${TAG} productValue: ${productValue}`); + let result: string[] = productValue?.split(','); + if (result.length > 0) { + let res = Number.parseInt(result[0]); + logger.info(`${TAG} productValue res(num): ${res}`); + return res; + } + } catch (err) { + logger.error(`${TAG} getFoldProductType failed. error.message: ${err.message}`); + } + return FoldPhoneTypeValue.INVALID_VALUE; + } + + /** + * 是否是小内折产品(L+V) + * + * @returns boolean + */ + public static isSmallFoldProduct(): boolean { + logger.info(`${TAG} isSmallFoldProduct in.`); + return [FoldPhoneTypeValue.SMALL_FOLD, + FoldPhoneTypeValue.EXPANDING_NEW_FORMS].includes(DeviceUtil.getFoldProductType()); + } + + private static deviceTypeToNumber(deviceType: DeviceType): number { + switch (deviceType) { + case DeviceType.TYPE_TABLET: + return 1; + case DeviceType.TYPE_PHONE: + return 2; + case DeviceType.TYPE_2IN1: + return 3; + case DeviceType.TYPE_PC: + return 4; + default: + return 5; + } + } + + private static deviceTypeToString(deviceType: number): string { + switch (deviceType) { + case DeviceType.TYPE_2IN1: + return TYPE_PC; + case DeviceType.TYPE_PHONE: + return TYPE_PHONE; + case DeviceType.TYPE_TABLET: + return TYPE_TABLET; + default: + return JSON.stringify(deviceType); + } + } +} \ No newline at end of file