• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 */
15import router from '@ohos.router';
16import { MenuOperation } from '@ohos/common';
17import {
18  Action,
19  BroadCast,
20  BroadCastConstants,
21  BroadCastManager,
22  Constants,
23  DateUtil,
24  Log,
25  ScreenManager,
26  TraceControllerUtils,
27  UiUtil,
28  WindowUtil
29} from '@ohos/common';
30import { PhotoBrowserActionBar } from '@ohos/browser/BrowserComponents';
31import common from '@ohos.app.ability.common';
32
33const TAG: string = 'VideoBrowser';
34
35interface Params {
36  uri: string;
37  dateTaken: number;
38  previewUri: string;
39}
40
41@Entry
42@Component
43struct VideoBrowser {
44  @Provide('dateTitle') photoDate: string = '';
45  @Provide('timeLocationTitle') timeAndLocation: string = '';
46  @Provide isShowBar: boolean = false;
47  @Provide menuList: Array<Action> = new Array<Action>();
48  @Provide moreMenuList: Array<Action> = new Array<Action>();
49  @Provide hidePopup: boolean = false;
50  private broadCast: BroadCast = new BroadCast();
51  private myVideoController: VideoController = new VideoController();
52  private mVideoStatus: string = Constants.VIDEO_STATUS_INITIAL;
53  private isNeedRecoveryStatus: boolean = false;
54  private uri = '';
55  private dateTaken = 0;
56  private previewUri = '';
57
58  onBackPress() {
59    Log.info(TAG, 'onBackPress');
60    router.back();
61    return true;
62  }
63
64  aboutToAppear() {
65    TraceControllerUtils.startTrace('VideoBrowserAboutToAppear');
66    let param: Params = router.getParams() as Params;
67    this.uri = param.uri;
68    this.dateTaken = param.dateTaken;
69    this.previewUri = param.previewUri;
70    if (this.uri == undefined) {
71      return;
72    }
73    Log.info(TAG, `uri is ${this.uri}`);
74    if (this.previewUri) {
75      Log.debug(TAG, `previewUri: ${JSON.stringify(this.previewUri)}`);
76    } else {
77      Log.debug(TAG, 'previewUri is null');
78    }
79    this.photoDate = DateUtil.getLocalizedDate(this.dateTaken);
80    this.timeAndLocation = DateUtil.getLocalizedTime(this.dateTaken);
81    TraceControllerUtils.finishTrace('VideoBrowserAboutToAppear');
82  }
83
84  aboutToDisappear(): void {
85    this.myVideoController.stop();
86  }
87
88  onPageShow() {
89    ScreenManager.getInstance().setSystemUi(false);
90    this.photoDate = DateUtil.getLocalizedDate(this.dateTaken);
91    this.timeAndLocation = DateUtil.getLocalizedTime(this.dateTaken);
92    BroadCastManager.getInstance().getBroadCast().emit(BroadCastConstants.THIRD_ROUTE_PAGE, []);
93    if (this.mVideoStatus === Constants.VIDEO_STATUS_PAUSE && this.isNeedRecoveryStatus) {
94      this.myVideoController.start();
95      this.isNeedRecoveryStatus = false;
96    }
97  }
98
99  onPageHide() {
100    Log.info(TAG, 'onPageHide');
101    if (this.mVideoStatus === Constants.VIDEO_STATUS_PLAYING) {
102      this.isNeedRecoveryStatus = true;
103      this.myVideoController.pause();
104    }
105  }
106
107  build() {
108    Stack({ alignContent: Alignment.TopStart }) {
109      Video({ src: this.uri, controller: this.myVideoController, previewUri: this.previewUri })
110        .controls(this.isShowBar)
111        .objectFit(ImageFit.Contain)
112        .onClick(() => {
113          this.isShowBar = !this.isShowBar;
114        })
115        .onStart(() => {
116          this.mVideoStatus = Constants.VIDEO_STATUS_PLAYING;
117          WindowUtil.setWindowKeepScreenOn(AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext, true);
118        })
119        .onPause(() => {
120          this.mVideoStatus = Constants.VIDEO_STATUS_PAUSE;
121          WindowUtil.setWindowKeepScreenOn(AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext, false);
122        })
123        .onFinish(() => {
124          this.mVideoStatus = Constants.VIDEO_STATUS_FINISH;
125          this.isShowBar = true;
126          WindowUtil.setWindowKeepScreenOn(AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext, false);
127        })
128        .onError(() => {
129          this.mVideoStatus = Constants.VIDEO_STATUS_ERROR;
130          this.isShowBar = true;
131          WindowUtil.setWindowKeepScreenOn(AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext, false);
132        })
133        .autoPlay(true)
134
135      PhotoBrowserActionBar({
136        onMenuClicked: (action: Action): void => this.onMenuClicked(action),
137        isVideoPage: true
138      })
139    }
140    .width('100%')
141    .height('100%')
142    .backgroundColor($r('app.color.black'))
143  }
144
145  pageTransition() {
146    PageTransitionEnter({ type: RouteType.None, duration: 0 })
147      .opacity(0)
148    PageTransitionExit({ type: RouteType.None, duration: 0 })
149      .opacity(0)
150  }
151
152  private onMenuClicked(action: Action) {
153    Log.info(TAG, `onMenuClicked, action: ${action.actionID}`);
154    let menuOperation: MenuOperation;
155    if (action.actionID === Action.BACK.actionID) {
156      this.onBackPress();
157    }
158  }
159}
160
161