1/* 2* Copyright (C) 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 audio from '@ohos.multimedia.audio'; 17import router from '@ohos.router'; 18 19export class ModeType { 20 id: number; 21 name: Resource; 22 23 constructor(id: number, name: Resource) { 24 this.id = id; 25 this.name = name; 26 } 27} 28 29@Component 30struct ModeItem_1 { 31 private mode: ModeType = new ModeType(-1, $r('app.string.CONTENT_TYPE_UNKNOWN')); 32 @Consume contentTypeIndex?: number; 33 @Builder renderModeItem(fontColor: string, bgColor: string, value: Resource) { 34 Flex() { 35 Text(value) 36 .fontSize(16) 37 .fontWeight(400) 38 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')) 39 .fontColor(fontColor); 40 } 41 .height(44) 42 .width(148) 43 .backgroundColor(bgColor) 44 .borderRadius(12) 45 .padding({ left: 14, top: 14, bottom: 14 }); 46 47 if (this.mode.id !== 5) { 48 Flex() { 49 Flex() { 50 }.height(1).width('100%') 51 .backgroundColor('#F3F3F3'); 52 } 53 .padding({ left: 12, right: 12 }); 54 } 55 } 56 57 build() { 58 Flex({ direction: FlexDirection.Column }) { 59 if (this.contentTypeIndex === this.mode.id) { 60 this.renderModeItem('#007DFF', 'rgba(0,125,255,0.20)', this.mode.name); 61 } else { 62 this.renderModeItem('rgba(0,0,0,0.9)', '', this.mode.name); 63 } 64 }.height(48).width(156); 65 } 66} 67 68@Component 69struct ModeItem_2 { 70 private mode: ModeType = new ModeType(-1, $r('app.string.CONTENT_TYPE_UNKNOWN')); 71 @Consume streamUsageIndex?: number; 72 @Builder renderModeItem(fontColor: string, bgColor: string, value: Resource) { 73 Flex() { 74 Text(value) 75 .fontSize(16) 76 .fontColor(fontColor) 77 .fontWeight(400) 78 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')); 79 } 80 .height(44) 81 .width(148) 82 .backgroundColor(bgColor) 83 .borderRadius(12) 84 .padding({ left: 14, top: 14, bottom: 14 }); 85 86 if (this.mode.id !== 14) { 87 Flex() { 88 Flex() { 89 }.height(1).width('100%').backgroundColor('#F3F3F3') 90 } 91 .padding({ left: 12, right: 12 }); 92 } 93 } 94 95 build() { 96 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 97 if (this.streamUsageIndex === this.mode.id) { 98 this.renderModeItem('#007DFF', 'rgba(0,125,255,0.20)', this.mode.name); 99 } else { 100 this.renderModeItem('rgba(0,0,0,0.9)', '', this.mode.name); 101 } 102 }.height(48).width(156); 103 } 104} 105 106@Entry 107@Component 108struct DetectPresetEffect { 109 scroller: Scroller = new Scroller(); 110 111 private audioEffectInfoArray : Array<number> = [1]; 112 private audioManager?: audio.AudioManager; 113 private audioStreamManager?: audio.AudioStreamManager; 114 private contentTypeOptions : ModeType[] = [ 115 new ModeType(0, $r('app.string.CONTENT_TYPE_UNKNOWN')), 116 new ModeType(1, $r('app.string.CONTENT_TYPE_SPEECH')), 117 new ModeType(2, $r('app.string.CONTENT_TYPE_MUSIC')), 118 new ModeType(3, $r('app.string.CONTENT_TYPE_MOVIE')), 119 new ModeType(4, $r('app.string.CONTENT_TYPE_SONIFICATION')), 120 new ModeType(5, $r('app.string.CONTENT_TYPE_RINGTONE')) 121 ]; 122 123 private streamUsageOptions : ModeType[]= [ 124 new ModeType(0, $r('app.string.STREAM_USAGE_UNKNOWN')), 125 new ModeType(1, $r('app.string.STREAM_USAGE_MEDIA')), 126 new ModeType(2, $r('app.string.STREAM_USAGE_MUSIC')), 127 new ModeType(3, $r('app.string.STREAM_USAGE_VOICE_COMMUNICATION')), 128 new ModeType(4, $r('app.string.STREAM_USAGE_VOICE_ASSISTANT')), 129 new ModeType(5, $r('app.string.STREAM_USAGE_ALARM')), 130 new ModeType(6, $r('app.string.STREAM_USAGE_VOICE_MESSAGE')), 131 new ModeType(7, $r('app.string.STREAM_USAGE_NOTIFICATION_RINGTONE')), 132 new ModeType(8, $r('app.string.STREAM_USAGE_RINGTONE')), 133 new ModeType(9, $r('app.string.STREAM_USAGE_NOTIFICATION')), 134 new ModeType(10, $r('app.string.STREAM_USAGE_ACCESSIBILITY')), 135 new ModeType(11, $r('app.string.STREAM_USAGE_MOVIE')), 136 new ModeType(12, $r('app.string.STREAM_USAGE_GAME')), 137 new ModeType(13, $r('app.string.STREAM_USAGE_AUDIOBOOK')), 138 new ModeType(14, $r('app.string.STREAM_USAGE_NAVIGATION')) 139 ]; 140 141 @State showSelector_1: boolean = false; 142 @State showSelector_2: boolean = false; 143 @State queryResult: Resource = $r('app.string.BLANK'); 144 @Provide contentTypeIndex: number = 0; 145 @Provide streamUsageIndex: number = 0; 146 147 mapContentIndexToType(index : number): number | undefined { 148 // map the index of options to the content type in @ohos.multimedia.audio 149 let index2Content = new Map([ 150 [0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5] 151 ]); 152 console.info("get content type:" + index2Content.get(index)); 153 return index2Content.get(index); 154 } 155 156 mapStreamIndexToUsage(index : number): number | undefined { 157 // map the index of options to the stream usage in @ohos.multimedia.audio 158 let index2Usage = new Map([ 159 [0, 0], [1, 1], [2, 1], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6], 160 [8, 6], [9, 7], [10, 8], [11, 10], [12, 11], [13, 12], [14, 13] 161 ]); 162 console.info("get stream usage:" + index2Usage.get(index)); 163 return index2Usage.get(index); 164 } 165 166 build() { 167 Column() { 168 Column() { 169 Row() { 170 Navigation() { 171 NavRouter() { 172 NavDestination() { 173 } 174 } 175 .onStateChange(async (isActivated: boolean) => { 176 console.info("hello"); 177 await router.pushUrl({ url: 'pages/Index' }); 178 }) 179 } 180 .id('back_btn_preset') 181 .height(56) 182 .width(384) 183 .hideBackButton(false) 184 .titleMode(NavigationTitleMode.Mini) 185 .title($r('app.string.EffectManager')) 186 .mode(NavigationMode.Stack) 187 .backgroundColor('#F1F3F5'); 188 } 189 190 Row() { 191 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 192 Row() { 193 Text(this.contentTypeOptions[this.contentTypeIndex].name) 194 .textAlign(TextAlign.Center) 195 .textOverflow({ overflow: TextOverflow.None }) 196 .fontSize(16) 197 .fontWeight(500) 198 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')); 199 if (this.showSelector_1) { 200 Image($r('app.media.ic_arrow_up_small')).height(7).width(10) 201 .margin({ left: 8 }); 202 } else { 203 Image($r('app.media.ic_arrow_down_small')).height(7).width(10) 204 .margin({ left: 8 }); 205 } 206 } 207 } 208 .height(40) 209 .width(140) 210 .backgroundColor('#F1F3F5') 211 .borderRadius(16) 212 .margin({ left: 37, right: 6 }) 213 .padding({ 214 left: 16, 215 right: 16 216 }) 217 .id('select_content_preset') 218 .onClick(() => { 219 this.showSelector_1 = !this.showSelector_1; 220 this.showSelector_2 = false; 221 }); 222 223 if (this.showSelector_1) { 224 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) { 225 ForEach(this.contentTypeOptions, (item:ModeType) => { 226 Flex() { 227 ModeItem_1({ mode: item }); 228 }.width(156) 229 .onClick(() => { 230 if (this.contentTypeIndex !== item.id) { 231 this.contentTypeIndex = item.id; 232 console.info('this.contentTypeIndex===' + this.contentTypeIndex); 233 this.queryResult = $r('app.string.BLANK'); 234 } 235 this.showSelector_1 = false; 236 }); 237 }, (item:ModeType) => item.id.toString()); 238 } 239 .height(296) 240 .width(156) 241 .backgroundColor('#fff') 242 .borderRadius(16) 243 .shadow({ radius: 50, color: 'rgba(0,0,30,0.1500)' }) 244 .padding({ left: 4, right: 4, top: 4, bottom: 4 }) 245 .position({ x: 54, y: 40 }) 246 .zIndex(1); 247 } 248 249 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 250 Row() { 251 Text(this.streamUsageOptions[this.streamUsageIndex].name) 252 .textAlign(TextAlign.Center) 253 .fontSize(16) 254 .fontWeight(500) 255 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')); 256 if (this.showSelector_2) { 257 Image($r('app.media.ic_arrow_up_small')).height(7).width(10) 258 .margin({ left: 8 }); 259 } else { 260 Image($r('app.media.ic_arrow_down_small')).height(7).width(10) 261 .margin({ left: 8 }); 262 } 263 } 264 } 265 .height(40) 266 .width(140) 267 .margin({ right: 37 }) 268 .backgroundColor('#F1F3F5') 269 .borderRadius(16) 270 .padding({ 271 left: 16, 272 right: 16 273 }) 274 .id('select_usage_preset') 275 .onClick(() => { 276 this.showSelector_1 = false; 277 this.showSelector_2 = !this.showSelector_2; 278 }); 279 280 if (this.showSelector_2) { 281 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) { 282 Scroll(this.scroller) { 283 Column() { 284 ForEach(this.streamUsageOptions, (item:ModeType) => { 285 Flex() { 286 ModeItem_2({ mode: item}); 287 }.width(156) 288 .onClick(() => { 289 if (this.streamUsageIndex !== item.id) { 290 this.streamUsageIndex = item.id; 291 console.info('this.contentTypeIndex===' + this.streamUsageIndex); 292 this.queryResult = $r('app.string.BLANK'); 293 } 294 this.showSelector_2 = false; 295 }) 296 }, (item:ModeType) => item.id.toString()); 297 } 298 } 299 .scrollable(ScrollDirection.Vertical) 300 .scrollBar(BarState.On) 301 .scrollBarColor(Color.Gray) 302 .scrollBarWidth(10) 303 .edgeEffect(EdgeEffect.None); 304 } 305 .height(344) 306 .width(156) 307 .backgroundColor('#fff') 308 .borderRadius(16) 309 .shadow({ radius: 50, color: 'rgba(0,0,30,0.1500)' }) 310 .padding({ left: 4, right: 4, top: 4, bottom: 4 }) 311 .position({ x: 145, y: 40 }) 312 .zIndex(1); 313 } 314 } 315 .width(360) 316 .zIndex(1); 317 318 Column() { 319 Row() { 320 Text($r('app.string.SYSTEM_PRESET_AUDIO_EFFECT')) 321 .fontSize(16) 322 .fontWeight(500) 323 .height(20) 324 .width(96) 325 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')); 326 Button($r('app.string.QUERY'), { type: ButtonType.Capsule, stateEffect: true }) 327 .backgroundColor('rgba(24,36,49,0.05)') 328 .fontColor('#007DFF') 329 .fontSize(12) 330 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')) 331 .fontWeight(500) 332 .height(28) 333 .width(72) 334 .onClick(async () => { 335 console.info('Button onClick'); 336 this.showSelector_1 = false; 337 this.showSelector_2 = false; 338 this.audioManager = audio.getAudioManager(); 339 this.audioStreamManager = this.audioManager.getStreamManager(); 340 this.audioEffectInfoArray = await this.audioStreamManager 341 .getAudioEffectInfoArray(this.mapStreamIndexToUsage(this.streamUsageIndex) as audio.StreamUsage); 342 let defaultIndex : number = 1; 343 if (this.audioEffectInfoArray.indexOf(defaultIndex) === -1) { 344 this.queryResult = $r('app.string.EFFECT_NONE'); 345 } else { 346 this.queryResult = $r('app.string.EFFECT_NONE_AND_DEFAULT'); 347 } 348 console.info('query result:' + this.queryResult); 349 }) 350 .id('query_btn_preset'); 351 } 352 .justifyContent(FlexAlign.SpaceBetween) 353 .height('50%') 354 .width('100%'); 355 356 Row() { 357 } 358 .height(1).width('100%') 359 .backgroundColor('#F1F3F5'); 360 361 Row() { 362 Text($r('app.string.QUERY_RESULT')) 363 .fontSize(16) 364 .fontWeight(500) 365 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')); 366 Text(this.queryResult) 367 .fontSize(14) 368 .fontWeight(400) 369 .enabled(false) 370 .fontColor(Color.Grey) 371 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')) 372 .id('query_result_preset'); 373 } 374 .justifyContent(FlexAlign.SpaceBetween) 375 .height('50%') 376 .width('100%'); 377 378 } 379 .height(104) 380 .width(360) 381 .backgroundColor(Color.White) 382 .padding({ left: 12, right: 12 }) 383 .borderRadius(20) 384 .zIndex(0); 385 } 386 387 Row() { 388 Column() { 389 Image($r('app.media.ic_Silent_select')) 390 .width(24) 391 .height(24) 392 .margin({ top: 7, bottom: 4 }); 393 394 Text($r('app.string.PRESET_AUDIO_EFFECT_QUERY')) 395 .fontSize(10) 396 .height(14) 397 .fontColor('#007DFF'); 398 } 399 .height(56) 400 .width(156) 401 .margin({ left: 24 }); 402 403 Column() { 404 Image($r('app.media.ic_Sound_normal')) 405 .width(24) 406 .height(24) 407 .margin({ top: 7, bottom: 4 }); 408 409 Text($r('app.string.REALTIME_AUDIO_EFFECT_SETTING')) 410 .fontSize(10) 411 .height(14); 412 413 } 414 .height(56) 415 .width(156) 416 .margin({ right: 24 }) 417 .id('switch_btn_preset') 418 .onClick(async () => { 419 await router.replaceUrl({ url: 'pages/RealtimeEffect' }); 420 }) 421 } 422 .height(56) 423 .width(360); 424 } 425 .height('100%') 426 .width('100%') 427 .justifyContent(FlexAlign.SpaceBetween) 428 .backgroundColor('#F1F3F5') 429 .onClick(() => { 430 this.showSelector_1 = false; 431 this.showSelector_2 = false; 432 }); 433 } 434} 435