• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  @State controls: boolean = false;
29  @State isShowVideoButton: boolean = true;
30  myVideoController: VideoController = new VideoController();
31  private TAG: string = '[ThirdPreviewView]:';
32  private photoWidth: string = '';
33  private photoHeight: string = '';
34  private photoUri: string = '';
35  private videoUri: string = '';
36  private mode: string = '';
37  private callBundleName: string = '';
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  build() {
111    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
112      Stack() {
113        if (this.mode === "PHOTO") {
114          Column() {
115            Image(this.photoUri)
116              .width('100%')
117              .height('100%')
118          }
119          .width(this.photoWidth)
120          .height(this.photoHeight)
121        } else {
122          Video({
123            src: this.videoUri,
124            previewUri: `${this.videoUri}/thumbnail/${this.photoWidth?.split('px')[0]}/${this.photoHeight?.split('px')[0]}`,
125            controller: this.myVideoController
126          })
127            .controls(this.controls)
128            .objectFit(ImageFit.Contain)
129            .width(this.photoWidth)
130            .height(this.photoHeight)
131            .onClick(() => {
132              this.controls = !this.controls;
133            })
134            .onFinish(() => {
135              this.controls = true;
136            })
137            .zIndex(1)
138          if (this.isShowVideoButton) {
139            Column() {
140              Flex({
141                direction: FlexDirection.Column,
142                alignItems: ItemAlign.Center,
143                justifyContent: FlexAlign.Center
144              }) {
145                Image(this.getVideoPlayIcon()).objectFit(ImageFit.Contain).width(56).height(56)
146                  .onClick(() => {
147                    this.myVideoController.start();
148                    this.isShowVideoButton = false;
149                  })
150              }
151            }.zIndex(2)
152          }
153        }
154      }
155      .width(this.photoWidth)
156      .height(this.photoHeight)
157
158      Flex({
159        direction: FlexDirection.Row,
160        alignItems: ItemAlign.Center,
161        justifyContent: FlexAlign.SpaceBetween
162      }) {
163        Image($r('app.media.ic_system_cancel'))
164          .key(ComponentIdKeys.THIRDPARTYCALL_REMAKE_1)
165          .width(24)
166          .aspectRatio(1)
167          .onClick(() => {
168            router.back();
169          })
170        Image($r('app.media.ic_system_confirm'))
171          .key(ComponentIdKeys.THIRDPARTYCALL_CONFIRM_1)
172          .width(24)
173          .aspectRatio(1)
174          .onClick(() => {
175            this.backCalledApp(this.mode === "PHOTO" ? this.photoUri : this.videoUri);
176          })
177      }
178      .width('100%')
179      .height(48)
180      .margin({ top: '24vp' })
181      .padding({ top: '12vp', left: '24vp', right: '24vp' })
182      .position({ x: 0, y: 0 })
183    }.width('100%').height('100%').backgroundColor('#000')
184  }
185
186  private getVideoPlayIcon() {
187    if (vp2px(1) >= 1 && vp2px(1) < 2) {
188      return $r('app.media.ic_video_play_btn_hdpi');
189    } else if (vp2px(1) == 2) {
190      return $r('app.media.ic_video_play_btn_xhdpi');
191    } else if (vp2px(1) == 3) {
192      return $r('app.media.ic_video_play_btn_xxhdpi');
193    } else {
194      return $r('app.media.ic_video_play_btn_xxxhdpi');
195    }
196  }
197}