• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { Constants } from '../../model/common/Constants';
17import { Log } from '../../utils/Log';
18import type { MenuOperation } from './MenuOperation';
19import { MenuContext } from './MenuContext';
20import type { AsyncCallback } from '../../model/common/AsyncCallback';
21import { SelectManager } from '../../model/browser/SelectManager';
22import { MediaItem } from '../../model/browser/photo/MediaItem';
23import { UserFileManagerAccess } from '../../access/UserFileManagerAccess';
24import { UiUtil } from '../../utils/UiUtil';
25import { BigDataConstants, ReportToBigDataUtil } from '../../utils/ReportToBigDataUtil';
26import common from '@ohos.app.ability.common';
27
28const TAG: string = 'common_ShareMenuOperation';
29
30export class ShareMenuOperation implements MenuOperation, AsyncCallback<MediaItem[]> {
31  private static readonly ACTION_PHOTOS_SHARE_DATA: string = 'ability.ACTION_PHOTOS_SHARE_DATA';
32  private static readonly SHARE_TYPE_IMAGE: string = 'image/*';
33  private static readonly SHARE_TYPE_VIDEO: string = 'video/*';
34  private static readonly SHARE_TYPE_MULTIPLE: string = 'application/*';
35  private pickerWant: any = {
36    deviceId: '',
37    bundleName: '',
38    abilityName: '',
39    uri: '',
40    type: '',
41    action: 'ohos.want.action.select',
42    parameters: {
43      'ability.picker.type': '',
44      'ability.picker.text': '',
45      'ability.picker.uri': '',
46      'ability.picker.fileNames': [],
47      'ability.picker.fileSizes': [],
48      'ability.want.params': {
49        'action': ShareMenuOperation.ACTION_PHOTOS_SHARE_DATA,
50        'parameters': {
51          'ability.params.streams': []
52        }
53      }
54    }
55  };
56  private menuContext: MenuContext;
57  private hasImage: boolean = false;
58  private hasVideo: boolean = false;
59  private fileNames: string[] = [];
60  private fileSizes: number[] = [];
61  private uris: string[] = [];
62
63  constructor(menuContext: MenuContext) {
64    this.menuContext = menuContext;
65  }
66
67  doAction(): void {
68    if (this.menuContext == null || this.menuContext.fromSelectMode == null) {
69      Log.error(TAG, 'cannot get fromSelectMode, return');
70      return;
71    }
72    if (this.menuContext.fromSelectMode) {
73      let selectManager: SelectManager = this.menuContext.selectManager;
74      if (selectManager != null) {
75        let count = selectManager.getSelectedCount();
76        if (count <= 0) {
77          Log.error(TAG, 'count <= 0, return');
78          return;
79        }
80        selectManager.getSelectedItems(this.callback.bind(this));
81      } else {
82        Log.error(TAG, 'select mode not have manager.');
83      }
84    } else {
85      let mediaItem: MediaItem = this.menuContext.mediaItem;
86      if (mediaItem != null) {
87        this.callback([mediaItem]);
88      } else {
89        Log.error(TAG, 'single mode not have item.');
90      }
91    }
92  }
93
94  callback(items: MediaItem[]): void {
95    Log.info(TAG, `share file length is ${items.length}`);
96    if (items.length > Constants.DEFAULT_MAX_SHARE_COUNT) {
97      UiUtil.showToast($r('app.string.number_of_shared_exceeds_the_limit_tips'))
98      return;
99    }
100    let imageCount: number = 0;
101    let videoCount: number = 0;
102    for (let i = 0; i < items.length; i++) {
103      let item = items[i];
104      Log.debug(TAG, `share item${i}: ${JSON.stringify(item)}`);
105      if (item.mediaType == UserFileManagerAccess.MEDIA_TYPE_VIDEO) {
106        videoCount++;
107        this.hasVideo = true;
108      } else if (item.mediaType == UserFileManagerAccess.MEDIA_TYPE_IMAGE) {
109        imageCount++;
110        this.hasImage = true;
111      }
112      this.fileNames[i] = this.getTitle(item);
113      this.fileSizes[i] = item.size;
114      this.uris[i] = item.uri;
115    }
116    let want = this.getParameters();
117    this.reportToBigData(want.parameters['ability.picker.type'], imageCount, videoCount);
118    let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('photosAbilityContext');
119    context.startAbility(want);
120  }
121
122  private reportToBigData(shareType: string, imageCount: number, videoCount: number): void {
123    let count: number = 1;
124    if (AppStorage.get('click_share_count') != null) {
125      let oldCount: number = AppStorage.get('click_share_count');
126      count = oldCount + 1;
127    }
128    AppStorage.SetOrCreate('click_share_count', count);
129    let msg = {
130      'count': count,
131      'shareType': shareType,
132      'imageCount': imageCount,
133      'videoCount': videoCount,
134    }
135    ReportToBigDataUtil.report(BigDataConstants.MULTI_SHARE_ID, msg);
136  }
137
138  private getParameters(): any {
139    let newWant = this.pickerWant;
140    newWant.parameters['ability.picker.type'] = this.getShareType();
141    newWant.parameters['ability.picker.fileNames'] = this.fileNames;
142    newWant.parameters['ability.picker.fileSizes'] = this.fileSizes;
143    newWant.parameters['ability.want.params'].parameters['ability.params.streams'] = this.uris;
144    Log.debug(TAG, `newWant: ${JSON.stringify(newWant)}`);
145    return newWant;
146  }
147
148  private getTitle(item: MediaItem): string {
149    let title = item.getTitle();
150    if (!!title) {
151      return title;
152    }
153    if (!!item.displayName) {
154      return item.displayName.substr(0, item.displayName.lastIndexOf('.'));
155    }
156    return '';
157  }
158
159  private getShareType(): string {
160    let shareType = ShareMenuOperation.SHARE_TYPE_MULTIPLE;
161    if (this.hasVideo && this.hasImage) {
162      shareType = ShareMenuOperation.SHARE_TYPE_MULTIPLE;
163    } else if (this.hasVideo || this.hasImage) {
164      if (this.hasVideo) {
165        shareType = ShareMenuOperation.SHARE_TYPE_VIDEO;
166      } else {
167        shareType = ShareMenuOperation.SHARE_TYPE_IMAGE;
168      }
169    } else {
170      shareType = ShareMenuOperation.SHARE_TYPE_MULTIPLE;
171    }
172    Log.info(TAG, `shareType: ${shareType}, hasVideo: ${this.hasVideo}, hasImage: ${this.hasImage}`);
173    return shareType;
174  }
175}