• 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 */
15
16import { Log } from '../../utils/Log';
17import { MenuContext } from './MenuContext';
18import { ProcessMenuOperation } from './ProcessMenuOperation';
19import { BroadCastConstants } from '../../model/common/BroadCastConstants';
20import { SelectManager } from '../../model/browser/SelectManager';
21import { MediaOperationType } from '../../model/common/MediaOperationType';
22import { TraceControllerUtils } from '../../utils/TraceControllerUtils';
23import type { FileAsset } from '../../access/UserFileManagerAccess';
24import { UserFileManagerAccess } from '../../access/UserFileManagerAccess';
25import { BigDataConstants, ReportToBigDataUtil } from '../../utils/ReportToBigDataUtil';
26
27const TAG: string = 'common_AddMenuOperation';
28
29export class AddMenuOperation extends ProcessMenuOperation {
30  albumUri: string;
31  albumName: string;
32
33  constructor(menuContext: MenuContext) {
34    super(menuContext);
35    this.albumUri = menuContext.albumUri;
36  }
37
38  doAction(): void {
39    Log.info(TAG, 'doAction');
40    if (this.menuContext == null) {
41      Log.error(TAG, 'menuContext is null, return');
42      return;
43    }
44
45    if (this.menuContext.selectManager) {
46      let selectManager: SelectManager = this.menuContext.selectManager;
47      if (selectManager === null || selectManager === undefined) {
48        Log.error(TAG, 'selectManager is null, return');
49        return;
50      }
51
52      this.count = selectManager.getSelectedCount();
53      if (this.count <= 0) {
54        Log.error(TAG, 'count <= 0, return');
55        return;
56      }
57      let msg = {
58        'Type': BigDataConstants.MEDIA_COPY,
59        'Count': this.count
60      };
61      ReportToBigDataUtil.report(BigDataConstants.ALBUM_OPERATION_ID, msg);
62
63      this.onOperationEnd = this.menuContext.onOperationEnd;
64
65      selectManager.getSelection(this);
66
67      let onOperationStart: Function = this.menuContext.onOperationStart;
68      onOperationStart && onOperationStart();
69    } else {
70      this.count = 1;
71      let mediaItem = this.menuContext.mediaItem;
72      this.onOperationEnd = this.menuContext.onOperationEnd;
73      if (mediaItem == null) {
74        Log.error(TAG, 'mediaItem is null, return');
75        return;
76      }
77      this.callback([mediaItem.uri]);
78    }
79
80    this.menuContext.broadCast.emit(BroadCastConstants.SHOW_PROGRESS_DIALOG,
81      [$r('app.string.add_progress_message', this.albumName),
82      MediaOperationType.Add, this.cancelFunc.bind(this)]);
83  }
84
85  callback(uris: string[]): void {
86    this.uris = uris;
87    this.processOperation();
88  }
89
90  // Move a batch of data
91  requestOneBatchOperation(): void {
92    if (this.isCancelled) {
93      return;
94    }
95    this.currentBatch++;
96    let startIndex = (this.currentBatch - 1) * this.BATCH_SIZE;
97    let endIndex = this.currentBatch * this.BATCH_SIZE;
98    let batchUris: string[] = this.uris.slice(startIndex, Math.min(endIndex, this.uris.length));
99    if (batchUris[0] == undefined) {
100      this.onOperateContinue();
101      return;
102    }
103    Log.info(TAG, `requestOneBatchOperation ${this.albumName}`);
104
105    TraceControllerUtils.startTraceWithTaskId('getFileCopyOrMoveInfo', this.currentBatch);
106    this.getFileAddOrMoveInfo(batchUris[0]).then((fileAsset: FileAsset) => {
107      TraceControllerUtils.finishTraceWithTaskId('getFileCopyOrMoveInfo', this.currentBatch);
108      this.copy(fileAsset);
109    });
110  }
111
112  cancelFunc(): void {
113    Log.info(TAG, `progress cancel`);
114    this.onOperatePause();
115    let cancelMessage = $r('app.string.copy_cancel_message', `${this.getExpectProgress().toString()}%`);
116
117    this.menuContext.broadCast && this.menuContext.broadCast.emit(BroadCastConstants.CANCEL_OPERATE,
118      [cancelMessage, this.onOperateContinue.bind(this), this.onOperateCancelled.bind(this)]);
119  }
120
121  // Copy cancel callback
122  onOperateContinue(): void {
123    Log.info(TAG, 'Operate Continue');
124    this.isPause = false;
125    this.cyclicOperation();
126  }
127
128  private async copy(source: FileAsset): Promise<void> {
129    Log.debug(TAG, 'debug-copy Parameters: source:' + JSON.stringify(source));
130    try {
131      await UserFileManagerAccess.getInstance().addFileToAlbum(this.albumUri, source);
132      this.onCompleted();
133    } catch (error) {
134      let msg = {
135        'Type': BigDataConstants.MEDIA_COPY_ERROR,
136        'errMsg': JSON.stringify(error)
137      };
138      ReportToBigDataUtil.errEventReport(BigDataConstants.ALBUM_OPERATION_ERROR_ID, msg);
139      Log.error(TAG, `copyFile is error ${error}`);
140      this.onError();
141    }
142  }
143}