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 mediaLibrary from '@ohos.multimedia.mediaLibrary' 17import Logger from '../model/Logger' 18import { myMedia } from '../model/myMedia' 19import MediaUtils from '../model/MediaUtils' 20import { promptShowToast } from '../model/Prompt' 21import { DeleteDialog } from '../common/DeleteDialog' 22import { RenameDialog } from '../common/RenameDialog' 23import { TitleBar } from '../phoneView/TitleBar' 24import { Toolkit } from '../phoneView/Toolkit' 25import { BroadcastControl } from '../phoneView/BroadcastControl' 26 27const TAG = 'PhoneIndex: ' 28 29@Entry 30@Component 31struct Send { 32 @State surfaceId: number = -1 33 @State myMedia: myMedia = new myMedia() 34 @State audioData: Array<mediaLibrary.FileAsset> = [] 35 @State videoData: Array<mediaLibrary.FileAsset> = [] 36 @State fillIndex: number = -1 37 @State fillMediaType: number = -1 38 @State currentTime: number = 0 39 @State @Watch('onChangePlay') isPlaying: boolean = false 40 @State isXComponent: boolean = true 41 @State httpUrl: string = '' 42 private renameDialogController: CustomDialogController = null 43 private deleteDialogController: CustomDialogController = null 44 private timerOut: number = undefined 45 private mXComponentController: XComponentController = new XComponentController() 46 private context: any = getContext(this) 47 48 async aboutToAppear() { 49 MediaUtils.mediaLib = await mediaLibrary.getMediaLibrary(this.context) 50 const PERMISSIONS: Array<string> = [ 51 'ohos.permission.WRITE_MEDIA', 52 'ohos.permission.READ_MEDIA', 53 'ohos.permission.INTERNET' 54 ] 55 await this.context.requestPermissionsFromUser(PERMISSIONS) 56 await this.getAudioData() 57 await this.getVideoData() 58 } 59 60 async getAudioData() { 61 this.audioData = [] 62 let fileAsset = MediaUtils.getFileAssetsFromType(mediaLibrary.MediaType.AUDIO) 63 fileAsset.then(fileList => { 64 Logger.info(TAG, 'getFileList callback') 65 this.audioData = fileList 66 this.myMedia.getAudioData(this.audioData) 67 68 }).catch(err => { 69 Logger.error(TAG, `getFileList err,err = ${err}`) 70 }) 71 } 72 73 async getVideoData() { 74 this.videoData = [] 75 let fileAsset = MediaUtils.getFileAssetsFromType(mediaLibrary.MediaType.VIDEO) 76 fileAsset.then(fileList => { 77 Logger.info(TAG, 'getFileList callback') 78 this.videoData = fileList 79 this.myMedia.getVideoData(this.videoData) 80 }).catch(err => { 81 Logger.error(TAG, `getFileList err,err = ${err}`) 82 }) 83 } 84 85 onChangePlay() { 86 this.isPlaying ? this.myMedia.getPlay() : this.myMedia.getPause() 87 } 88 async convertResourceToString(resource: Resource){ 89 return await this.context.resourceManager.getString(resource) 90 } 91 async renameDialogShow(index, mediaType) { 92 this.fillIndex = index 93 this.fillMediaType = mediaType 94 let disPlayName 95 let disPlayType 96 if (mediaType == mediaLibrary.MediaType.AUDIO) { 97 disPlayName = this.audioData[index].displayName 98 let audio = await this.convertResourceToString($r('app.string.phoneMain_audioTitle')) 99 disPlayType = audio 100 } else { 101 disPlayName = this.videoData[index].displayName 102 let video = await this.convertResourceToString($r('app.string.phoneMain_videoTitle')) 103 disPlayType = video 104 } 105 this.renameDialogController = new CustomDialogController({ 106 builder: RenameDialog({ title: disPlayName, disPlayType: disPlayType, setRename: this.setRename.bind(this) }), 107 autoCancel: true, 108 customStyle: true, 109 }) 110 this.renameDialogController.open() 111 } 112 113 setRename(newName) { 114 if (this.fillMediaType == mediaLibrary.MediaType.AUDIO) { 115 this.audioData[this.fillIndex].displayName = newName 116 this.audioData[this.fillIndex].commitModify((err) => { 117 if (err !== undefined) { 118 console.info(`commitModify, err: ${err}`) 119 promptShowToast($r('app.string.phoneMain_legitimateStr')) 120 return 121 } 122 this.renameDialogController.close() 123 this.getAudioData() 124 }) 125 } else { 126 this.videoData[this.fillIndex].displayName = newName 127 this.videoData[this.fillIndex].commitModify((err) => { 128 if (err !== undefined) { 129 console.info(`commitModify, err: ${err}`) 130 promptShowToast($r('app.string.phoneMain_legitimateStr')) 131 return 132 } 133 this.renameDialogController.close() 134 this.getVideoData() 135 }) 136 } 137 } 138 139 deleteDialogShow(index, mediaType) { 140 let mediaData 141 if (mediaType == mediaLibrary.MediaType.AUDIO) { 142 mediaData = this.audioData 143 } else { 144 mediaData = this.videoData 145 } 146 this.deleteDialogController = new CustomDialogController({ 147 builder: DeleteDialog({ index: index, mediaData: mediaData }), 148 autoCancel: true, 149 customStyle: true, 150 }) 151 this.deleteDialogController.open() 152 } 153 154 build() { 155 Column() { 156 TitleBar({ 157 myMedia: $myMedia, 158 isPlaying: $isPlaying, 159 currentTime: $currentTime, 160 httpUrl: this.httpUrl, 161 isXComponent: $isXComponent, 162 mXComponentController: this.mXComponentController, 163 }) 164 BroadcastControl({ 165 myMedia: $myMedia, 166 isXComponent: $isXComponent, 167 surfaceId: $surfaceId, 168 isPlaying: $isPlaying, 169 currentTime: $currentTime, 170 mXComponentController: this.mXComponentController, 171 }) 172 Toolkit({ 173 myMedia: $myMedia, 174 currentTime: $currentTime, 175 isXComponent: $isXComponent, 176 audioData: $audioData, 177 videoData: $videoData, 178 deleteDialogShow: this.deleteDialogShow.bind(this), 179 renameDialogShow: this.renameDialogShow.bind(this), 180 isPlaying: $isPlaying, 181 surfaceId: $surfaceId, 182 context: this.context 183 }) 184 }.height('100%').backgroundColor('#222324') 185 } 186} 187