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