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