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