1/*
2 * Copyright (c) 2022 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 type { MenuOperation, MenuOperationCallback, } from '@ohos/common';
17import {
18  BrowserOperationFactory,
19  BrowserConstants as Constants,
20  FileAsset,
21  Log,
22  MenuContext,
23  UserFileManagerAccess
24} from '@ohos/common';
25
26const TAG: string = 'browser_RecoverMenuOperation';
27
28export class RecoverMenuOperation implements MenuOperation, MenuOperationCallback {
29  private menuContext: MenuContext;
30
31  constructor(menuContext: MenuContext) {
32    this.menuContext = menuContext;
33  }
34
35  doAction(): void {
36    if (this.menuContext == null) {
37      Log.error(TAG, 'menuContext is null, return');
38      return;
39    }
40    this.doRecover();
41  }
42
43  onCompleted(): void {
44    Log.info(TAG, 'Recover data succeed!');
45  }
46
47  onError(): void {
48    Log.error(TAG, 'Recover data failed!');
49  }
50
51  private async doRecover() {
52    let mediaItem = this.menuContext.mediaItem;
53    if (mediaItem == null) {
54      Log.error(TAG, 'mediaItem is null, return');
55      return;
56    }
57    let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO);
58    try {
59      let fileAssets = new Array<FileAsset>();
60      fileAssets.push(await UserFileManagerAccess.getInstance().getTrashAssetByUri(mediaItem.uri));
61      await operationImpl.recoverFromTrash(fileAssets);
62      this.onCompleted()
63
64    } catch (error) {
65      Log.error(TAG, `recover error: ${error}`);
66      this.onError();
67    }
68
69    this.menuContext.broadCast.emit(Constants.DELETE, []);
70    Log.info(TAG, 'Single Recover confirm');
71  }
72}