1// @ts-nocheck 2/* 3 * Copyright (c) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import mediaLibrary from '@ohos.multimedia.mediaLibrary' 18 19import Logger from '../model/Logger' 20import { myMedia } from '../model/myMedia' 21import MediaUtils from '../model/MediaUtils' 22import { promptShowToast } from '../model/Prompt' 23 24import { mainDialog } from '../common/mainDialog' 25import { RenameDialog } from '../common/RenameDialog' 26import { DeleteDialog } from '../common/DeleteDialog' 27import { TitleBar } from '../common/TitleBar' 28import { BroadcastControl } from '../common/BroadcastControl' 29import { Toolkit } from '../common/Toolkit' 30import { StreamingMedia } from '../common/StreamingMedia' 31 32 33const TAG: string = 'IndexMain' 34 35@Entry 36@Component 37struct Index { 38 @State fillIndex: number = -1 39 @State currentTime: number = 0 40 @State isOpacity: number = 0.4 41 @State isSurface: boolean = true 42 @State isVolume: boolean = false 43 @State fillMediaType: number = -1 44 @State isShowMenu: boolean = false 45 @State btnEnabled: boolean = false 46 @State isXComponent: boolean = true 47 @State isStreamShow: boolean = false 48 @State isInformation: boolean = false 49 @State myMedia: myMedia = new myMedia() 50 @State streamBtnText: Resource = $r('app.string.index_play') 51 @State streamOpacity: number = 0.4 52 @State itemDate: mediaLibrary.FileAsset = undefined 53 @State audioData: Array<mediaLibrary.FileAsset> = [] 54 @State videoData: Array<mediaLibrary.FileAsset> = [] 55 @State @Watch('onChangeLoop') isLoop: boolean = false 56 @State @Watch('onChangePlay') isPlaying: boolean = false 57 @State streamBackgroundColor: string = '#99B9F8' 58 private renameDialogController: CustomDialogController = null 59 private deleteDialogController: CustomDialogController = null 60 private mXComponentController: XComponentController = new XComponentController() 61 private mainDialogController: CustomDialogController = new CustomDialogController({ 62 builder: mainDialog(), 63 autoCancel: false, 64 customStyle: true 65 }) 66 67 private context: any = getContext(this) 68 69 async aboutToAppear() { 70 MediaUtils.mediaLib = await mediaLibrary.getMediaLibrary(this.context) 71 const PERMISSIONS: Array<string> = [ 72 'ohos.permission.WRITE_MEDIA', 73 'ohos.permission.READ_MEDIA', 74 'ohos.permission.INTERNET' 75 ] 76 await this.context.requestPermissionsFromUser(PERMISSIONS) 77 await this.getAudioData() 78 await this.getVideoData() 79 this.mainDialogController.open() 80 } 81 async convertResourceToString(resource: Resource){ 82 return await this.context.resourceManager.getString(resource) 83 } 84 onPageShow() { 85 MediaUtils.onDateChange(this.getAudioData, this.getVideoData) 86 } 87 88 onPageHide() { 89 MediaUtils.offDateChange() 90 } 91 92 async getAudioData() { 93 this.audioData = [] 94 let fileAsset = MediaUtils.getFileAssetsFromType(mediaLibrary.MediaType.AUDIO) 95 fileAsset.then(fileList => { 96 Logger.info(TAG, 'getFileList callback') 97 this.audioData = fileList 98 this.myMedia.getAudioData(this.audioData) 99 }).catch(err => { 100 Logger.error(TAG, `getFileList err,err = ${err}`) 101 }) 102 } 103 104 async getVideoData() { 105 this.videoData = [] 106 let fileAsset = MediaUtils.getFileAssetsFromType(mediaLibrary.MediaType.VIDEO) 107 fileAsset.then(fileList => { 108 Logger.info(TAG, 'getFileList callback') 109 this.videoData = fileList 110 this.myMedia.getVideoData(this.videoData) 111 }).catch(err => { 112 Logger.error(TAG, `getFileList err,err = ${err}`) 113 }) 114 } 115 116 onChangePlay() { 117 this.isPlaying ? this.myMedia.getPlay() : this.myMedia.getPause() 118 } 119 120 onChangeLoop() { 121 this.myMedia.setIsLoopCallBack(this.isLoop) 122 } 123 124 async renameDialogShow(index, mediaType) { 125 this.fillIndex = index 126 this.fillMediaType = mediaType 127 let disPlayName 128 let disPlayType 129 if (mediaType == mediaLibrary.MediaType.AUDIO) { 130 disPlayName = this.audioData[index].displayName 131 let audio = await this.convertResourceToString($r('app.string.phoneMain_audioTitle')) 132 disPlayType = audio 133 } else { 134 disPlayName = this.videoData[index].displayName 135 let video = await this.convertResourceToString($r('app.string.phoneMain_videoTitle')) 136 disPlayType = video 137 } 138 this.renameDialogController = new CustomDialogController({ 139 builder: RenameDialog({ 140 title: disPlayName, 141 disPlayType: disPlayType, 142 setRename: this.setRename.bind(this) 143 }), 144 autoCancel: true, 145 customStyle: true, 146 }) 147 this.renameDialogController.open() 148 } 149 150 setRename(newName) { 151 if (this.fillMediaType == mediaLibrary.MediaType.AUDIO) { 152 this.audioData[this.fillIndex].displayName = newName 153 this.audioData[this.fillIndex].commitModify((err) => { 154 if (err !== undefined) { 155 console.info(`commitModify, err: ${err}`) 156 promptShowToast($r('app.string.phoneMain_legitimateStr')) 157 return 158 } 159 this.renameDialogController.close() 160 this.getAudioData() 161 }) 162 } else { 163 this.videoData[this.fillIndex].displayName = newName 164 this.videoData[this.fillIndex].commitModify((err) => { 165 if (err !== undefined) { 166 console.info(`commitModify, err: ${err}`) 167 promptShowToast($r('app.string.phoneMain_legitimateStr')) 168 return 169 } 170 this.renameDialogController.close() 171 this.getVideoData() 172 }) 173 } 174 } 175 176 deleteDialogShow(index, mediaType) { 177 let mediaData 178 if (mediaType == mediaLibrary.MediaType.AUDIO) { 179 mediaData = this.audioData 180 } else { 181 mediaData = this.videoData 182 } 183 this.deleteDialogController = new CustomDialogController({ 184 builder: DeleteDialog({ index: index, mediaData: mediaData }), 185 autoCancel: true, 186 customStyle: true, 187 }) 188 this.deleteDialogController.open() 189 } 190 191 build() { 192 Column() { 193 Flex({ 194 alignItems: ItemAlign.Center, 195 justifyContent: FlexAlign.SpaceBetween 196 }) { 197 TitleBar() 198 StreamingMedia({ 199 myMedia: $myMedia, 200 isPlaying: $isPlaying, 201 isShowMenu: $isShowMenu, 202 currentTime: $currentTime, 203 isStreamShow: $isStreamShow, 204 isXComponent: $isXComponent, 205 streamOpacity: $streamOpacity, 206 streamBtnText: $streamBtnText, 207 streamBackgroundColor: $streamBackgroundColor, 208 mXComponentController: this.mXComponentController, 209 }) 210 } 211 .height(50) 212 .width('98%') 213 .margin({ 214 top: 24, 215 bottom: 24 216 }) 217 218 Flex({ 219 justifyContent: FlexAlign.SpaceBetween 220 }) { 221 BroadcastControl({ 222 isLoop: $isLoop, 223 myMedia: $myMedia, 224 itemData: $itemDate, 225 isSurface: $isSurface, 226 isPlaying: $isPlaying, 227 isOpacity: $isOpacity, 228 btnEnabled: $btnEnabled, 229 isShowMenu: $isShowMenu, 230 currentTime: $currentTime, 231 isXComponent: $isXComponent, 232 context:this.context, 233 mXComponentController: this.mXComponentController 234 }) 235 Toolkit({ 236 myMedia: $myMedia, 237 itemData: $itemDate, 238 context:this.context, 239 audioData: $audioData, 240 videoData: $videoData, 241 isSurface: $isSurface, 242 isPlaying: $isPlaying, 243 isOpacity: $isOpacity, 244 btnEnabled: $btnEnabled, 245 isShowMenu: $isShowMenu, 246 currentTime: $currentTime, 247 isStreamShow: $isStreamShow, 248 isXComponent: $isXComponent, 249 streamBtnText: $streamBtnText, 250 streamOpacity: $streamOpacity, 251 mXComponentController: this.mXComponentController, 252 deleteDialogShow: this.deleteDialogShow.bind(this), 253 renameDialogShow: this.renameDialogShow.bind(this) 254 }) 255 } 256 .width('97%') 257 .height('83%') 258 } 259 .width('100%') 260 .height('100%') 261 .backgroundColor('#222324') 262 } 263}