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 */
15
16import {
17  BrowserDataFactory,
18  BrowserOperationFactory,
19  BrowserConstants as Constants,
20  Log,
21  MediaConstants,
22  MenuContext,
23  MenuOperation,
24  UserFileManagerAccess
25} from '@ohos/common';
26import image from '@ohos.multimedia.image';
27
28const TAG: string = 'browser_RotateMenuOperation';
29
30export class RotateMenuOperation implements MenuOperation {
31  private menuContext: MenuContext;
32
33  constructor(menuContext: MenuContext) {
34    this.menuContext = menuContext;
35  }
36
37  doAction(): void {
38    if (this.menuContext == null) {
39      Log.error(TAG, 'menuContext is null, return');
40      return;
41    }
42    let mediaItem = this.menuContext.mediaItem;
43    if (mediaItem == null) {
44      Log.error(TAG, 'mediaItem is null, return');
45      return;
46    }
47    let orientation = mediaItem.orientation;
48    this.changeOrientation(mediaItem.uri, orientation);
49    Log.info(TAG, `changeOrientation: id: ${mediaItem.uri} orientation: ${orientation}`);
50  }
51
52  getPropertyValidOrientation(orientation: number): string {
53    Log.info(TAG, `getPropertyValidOrientation ${orientation}`);
54    orientation = orientation % MediaConstants.ROTATE_AROUND;
55    switch (orientation) {
56      case MediaConstants.ROTATE_NONE:
57        return '1';
58      case MediaConstants.ROTATE_THIRD:
59        return '8';
60      case MediaConstants.ROTATE_TWICE:
61        return '3';
62      case MediaConstants.ROTATE_ONCE:
63        return '6';
64      default:
65        return '';
66    }
67  }
68
69  private async changeOrientation(uri, orientation) {
70    Log.info(TAG, 'changeOrientation start');
71    let dataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_PHOTO);
72    let fileAsset = await dataImpl.getDataByUri(uri);
73    try {
74      let fd: number = await UserFileManagerAccess.getInstance().openAsset('RW', fileAsset);
75      if (fd < 0) {
76        Log.error(TAG, 'open asset failed.');
77        return;
78      }
79      let imageSourceApi: image.ImageSource = image.createImageSource(fd);
80      await imageSourceApi.modifyImageProperty('Orientation', this.getPropertyValidOrientation(orientation));
81      Log.debug(TAG, `changeOrientation end: ${JSON.stringify(await imageSourceApi.getImageProperty('Orientation'))}`);
82      imageSourceApi.release();
83      UserFileManagerAccess.getInstance().closeAsset(fd, fileAsset);
84      this.menuContext.broadCast.emit(Constants.ROTATE, [orientation]);
85    } catch (err) {
86      Log.error(TAG, `setOrientation err ${JSON.stringify(err)}`);
87    }
88  }
89}