1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 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 17 package com.android.systemui.volume.panel.component.anc.ui.viewmodel 18 19 import android.content.Intent 20 import androidx.slice.Slice 21 import androidx.slice.SliceItem 22 import com.android.systemui.volume.panel.component.anc.domain.AncAvailabilityCriteria 23 import com.android.systemui.volume.panel.component.anc.domain.interactor.AncSliceInteractor 24 import com.android.systemui.volume.panel.component.anc.domain.model.AncSlices 25 import com.android.systemui.volume.panel.dagger.scope.VolumePanelScope 26 import javax.inject.Inject 27 import kotlinx.coroutines.CoroutineScope 28 import kotlinx.coroutines.ExperimentalCoroutinesApi 29 import kotlinx.coroutines.flow.Flow 30 import kotlinx.coroutines.flow.SharingStarted 31 import kotlinx.coroutines.flow.StateFlow 32 import kotlinx.coroutines.flow.filterIsInstance 33 import kotlinx.coroutines.flow.map 34 import kotlinx.coroutines.flow.stateIn 35 36 /** Volume Panel ANC component view model. */ 37 @OptIn(ExperimentalCoroutinesApi::class) 38 @VolumePanelScope 39 class AncViewModel 40 @Inject 41 constructor( 42 @VolumePanelScope private val coroutineScope: CoroutineScope, 43 private val interactor: AncSliceInteractor, 44 private val availabilityCriteria: AncAvailabilityCriteria, 45 ) { 46 47 val isAvailable: Flow<Boolean> 48 get() = availabilityCriteria.isAvailable() 49 50 /** ANC [Slice]. Null when there is no slice available for ANC. */ 51 val popupSlice: StateFlow<Slice?> = 52 interactor.ancSlices 53 .filterIsInstance<AncSlices.Ready>() <lambda>null54 .map { it.popupSlice } 55 .stateIn(coroutineScope, SharingStarted.Eagerly, null) 56 57 /** Button [Slice] to be shown in the VolumePanel. Null when there is no ANC Slice available. */ 58 val buttonSlice: StateFlow<Slice?> = 59 interactor.ancSlices 60 .filterIsInstance<AncSlices.Ready>() <lambda>null61 .map { it.buttonSlice } 62 .stateIn(coroutineScope, SharingStarted.Eagerly, null) 63 isClickablenull64 fun isClickable(slice: Slice?): Boolean { 65 slice ?: return false 66 val slices = ArrayDeque<SliceItem>() 67 slices.addAll(slice.items) 68 while (slices.isNotEmpty()) { 69 val item: SliceItem = slices.removeFirst() 70 when (item.format) { 71 android.app.slice.SliceItem.FORMAT_ACTION -> { 72 val itemActionIntent: Intent? = item.action?.intent 73 if (itemActionIntent?.hasExtra(EXTRA_ANC_ENABLED) == true) { 74 return itemActionIntent.getBooleanExtra(EXTRA_ANC_ENABLED, true) 75 } 76 } 77 android.app.slice.SliceItem.FORMAT_SLICE -> { 78 item.slice?.items?.let(slices::addAll) 79 } 80 } 81 } 82 return true 83 } 84 85 private companion object { 86 const val EXTRA_ANC_ENABLED = "EXTRA_ANC_ENABLED" 87 } 88 89 /** Call this to update [popupSlice] width in a reaction to container size change. */ onPopupSliceWidthChangednull90 fun onPopupSliceWidthChanged(width: Int) { 91 interactor.onPopupSliceWidthChanged(width) 92 } 93 94 /** Call this to update [buttonSlice] width in a reaction to container size change. */ onButtonSliceWidthChangednull95 fun onButtonSliceWidthChanged(width: Int) { 96 interactor.onButtonSliceWidthChanged(width) 97 } 98 } 99