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 router from '@system.router'; 17import fileshare from '@ohos.fileshare'; 18import wantConstant from '@ohos.ability.wantConstant'; 19import { Log } from '@ohos/common/src/main/ets/default/utils/Log'; 20import { BusinessError } from '@ohos.base'; 21import ability from '@ohos.ability.ability'; 22import { GlobalContext } from '@ohos/common/src/main/ets/default/utils/GlobalContext'; 23import { ComponentIdKeys } from '@ohos/common/src/main/ets/default/utils/ComponentIdKeys'; 24 25@Entry 26@Component 27struct ThirdPreviewView { 28 private TAG: string = '[ThirdPreviewView]:'; 29 private photoWidth: string = ''; 30 private photoHeight: string = ''; 31 private photoUri: string = ''; 32 private videoUri: string = ''; 33 private mode: string = ''; 34 private callBundleName: string = ''; 35 @State controls: boolean = false; 36 @State isShowVideoButton: boolean = true; 37 myVideoController: VideoController = new VideoController(); 38 39 aboutToAppear() { 40 Log.info(`${this.TAG} aboutToAppear E`); 41 let routerParams = router.getParams(); 42 if (routerParams === undefined || routerParams === null) { 43 return; 44 } 45 this.photoWidth = routerParams.width?.toString(); 46 this.photoHeight = routerParams.height?.toString(); 47 this.photoUri = routerParams.uri?.toString(); 48 this.mode = routerParams.mode?.toString(); 49 this.videoUri = routerParams.videoUri?.toString(); 50 this.callBundleName = routerParams.callBundleName?.toString(); 51 Log.info(`${this.TAG} aboutToAppear routerParams= ${JSON.stringify(routerParams)}`); 52 Log.info(`${this.TAG} aboutToAppear X`); 53 } 54 55 backCalledApp(resourceUri: string): void { 56 Log.info(`${this.TAG} backCalledApp E resourceUri` + resourceUri); 57 let that = this; 58 if (GlobalContext.get().getPickerUri() !== '' && GlobalContext.get().getPickerUri() !== undefined) { 59 that.terminateSelfWithResult(resourceUri, resourceUri === '' ? -1 : 0); 60 } else { 61 try { 62 fileshare.grantUriPermission(resourceUri, this.callBundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION) 63 .then(() => { 64 Log.info(`${this.TAG} grantUriPermission success`); 65 that.terminateSelfWithResult(resourceUri, 0); 66 }).catch((error: BusinessError) => { 67 Log.error(`${this.TAG} grantUriPermission error= ${error} `); 68 }); 69 } catch (error) { 70 Log.error(`${this.TAG} grantUriPermission error= ${error} `); 71 that.terminateSelfWithResult(resourceUri, 0); 72 } 73 74 } 75 Log.info(`${this.TAG} backCalledApp X`); 76 } 77 78 terminateSelfWithResult(resourceUri: string, resultCode : number): void { 79 Log.info(`${this.TAG} terminateSelfWithResult start`); 80 let abilityResult: ability.AbilityResult = { 81 resultCode: resultCode, 82 want: { 83 parameters: { 84 resourceUri: resourceUri, 85 mode: this.mode 86 } 87 } 88 }; 89 if (GlobalContext.get().getSession()) { 90 GlobalContext.get().getSession().terminateSelfWithResult(abilityResult, (error: BusinessError) => { 91 if (error) { 92 Log.error(`${this.TAG} 1Operation failed. Cause: ${error.code}`); 93 Log.error(`${this.TAG} 2Operation failed. Cause: ${error?.data}`); 94 return; 95 } 96 Log.info(`${this.TAG} Operation succeeded`); 97 }); 98 } else { 99 GlobalContext.get().getCameraAbilityContext().terminateSelfWithResult(abilityResult, (error: BusinessError) => { 100 if (error) { 101 Log.error(`${this.TAG} Operation failed. Cause: ${error}`); 102 return; 103 } 104 Log.info(`${this.TAG} Operation succeeded`); 105 }); 106 } 107 108 } 109 110 private getVideoPlayIcon() { 111 if (vp2px(1) >= 1 && vp2px(1) < 2) { 112 return $r('app.media.ic_video_play_btn_hdpi'); 113 } else if (vp2px(1) == 2) { 114 return $r('app.media.ic_video_play_btn_xhdpi'); 115 } else if (vp2px(1) == 3) { 116 return $r('app.media.ic_video_play_btn_xxhdpi'); 117 } else { 118 return $r('app.media.ic_video_play_btn_xxxhdpi'); 119 } 120 } 121 122 build() { 123 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 124 Stack() { 125 if (this.mode === "PHOTO") { 126 Column() { 127 Image(this.photoUri) 128 .width('100%') 129 .height('100%') 130 } 131 .width(this.photoWidth) 132 .height(this.photoHeight) 133 } else { 134 Video({ 135 src: this.videoUri, 136 previewUri: `${this.videoUri}/thumbnail/${this.photoWidth?.split('px')[0]}/${this.photoHeight?.split('px')[0]}`, 137 controller: this.myVideoController 138 }) 139 .controls(this.controls) 140 .objectFit(ImageFit.Contain) 141 .width(this.photoWidth) 142 .height(this.photoHeight) 143 .onClick(() => { 144 this.controls = !this.controls; 145 }) 146 .onFinish(() => { 147 this.controls = true; 148 }) 149 .zIndex(1) 150 if (this.isShowVideoButton) { 151 Column() { 152 Flex({ 153 direction: FlexDirection.Column, 154 alignItems: ItemAlign.Center, 155 justifyContent: FlexAlign.Center 156 }) { 157 Image(this.getVideoPlayIcon()).objectFit(ImageFit.Contain).width(56).height(56) 158 .onClick(() => { 159 this.myVideoController.start(); 160 this.isShowVideoButton = false; 161 }) 162 } 163 }.zIndex(2) 164 } 165 } 166 } 167 .width(this.photoWidth) 168 .height(this.photoHeight) 169 170 Flex({ 171 direction: FlexDirection.Row, 172 alignItems: ItemAlign.Center, 173 justifyContent: FlexAlign.SpaceBetween 174 }) { 175 Image($r('app.media.ic_system_cancel')) 176 .key(ComponentIdKeys.THIRDPARTYCALL_REMAKE_1) 177 .width(24) 178 .aspectRatio(1) 179 .onClick(() => { 180 router.back(); 181 }) 182 Image($r('app.media.ic_system_confirm')) 183 .key(ComponentIdKeys.THIRDPARTYCALL_CONFIRM_1) 184 .width(24) 185 .aspectRatio(1) 186 .onClick(() => { 187 this.backCalledApp(this.mode === "PHOTO" ? this.photoUri : this.videoUri); 188 }) 189 } 190 .width('100%') 191 .height(48) 192 .margin({ top: '24vp' }) 193 .padding({ top: '12vp', left: '24vp', right: '24vp' }) 194 .position({ x: 0, y: 0 }) 195 }.width('100%').height('100%').backgroundColor('#000') 196 } 197}