1 /* 2 * Copyright (C) 2022 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.settings.spa.notification 18 19 import android.content.pm.ApplicationInfo 20 import android.service.notification.Adjustment 21 import androidx.lifecycle.LiveData 22 import androidx.lifecycle.MutableLiveData 23 import com.android.settings.spa.app.storage.StorageAppListModel 24 import com.android.settings.spa.app.storage.StorageType 25 26 class AppNotificationController( 27 private val repository: AppNotificationRepository, 28 private val app: ApplicationInfo, 29 private val listType: ListType, 30 ) { 31 val isEnabled: LiveData<Boolean> 32 get() = _isEnabled 33 getEnablednull34 fun getEnabled() = _isEnabled.get() 35 36 fun setEnabled(enabled: Boolean) { 37 if (repository.setEnabled(app, enabled)) { 38 _isEnabled.postValue(enabled) 39 } 40 } 41 42 private val _isEnabled = object : MutableLiveData<Boolean>() { onActivenull43 override fun onActive() { 44 postValue(repository.isEnabled(app)) 45 } 46 onInactivenull47 override fun onInactive() { 48 } 49 <lambda>null50 fun get(): Boolean = value ?: repository.isEnabled(app).also { 51 postValue(it) 52 } 53 } 54 55 val isAllowed: LiveData<Boolean> 56 get() = _isAllowed 57 getAllowednull58 fun getAllowed() = _isAllowed.get() 59 60 fun setAllowed(enabled: Boolean) { 61 when (listType) { 62 ListType.ExcludeSummarization -> { 63 if (repository.setAdjustmentSupportedForPackage( 64 app, Adjustment.KEY_SUMMARIZATION, enabled)) { 65 _isAllowed.postValue(enabled) 66 } 67 } 68 ListType.ExcludeClassification -> { 69 if (repository.setAdjustmentSupportedForPackage( 70 app, Adjustment.KEY_TYPE, enabled)) { 71 _isAllowed.postValue(enabled) 72 } 73 } 74 else -> {} 75 } 76 } 77 78 private val _isAllowed = object : MutableLiveData<Boolean>() { onActivenull79 override fun onActive() { 80 when (listType) { 81 ListType.ExcludeSummarization -> { 82 postValue(repository.isAdjustmentSupportedForPackage( 83 app, Adjustment.KEY_SUMMARIZATION)) 84 } 85 ListType.ExcludeClassification -> { 86 postValue(repository.isAdjustmentSupportedForPackage( 87 app, Adjustment.KEY_TYPE)) 88 } 89 else -> {} 90 } 91 } 92 onInactivenull93 override fun onInactive() { 94 } 95 getnull96 fun get(): Boolean = when (listType) { 97 ListType.ExcludeSummarization -> { 98 value ?: repository.isAdjustmentSupportedForPackage( 99 app, Adjustment.KEY_SUMMARIZATION).also { 100 postValue(it) 101 } 102 } 103 ListType.ExcludeClassification -> { 104 value ?: repository.isAdjustmentSupportedForPackage( 105 app, Adjustment.KEY_TYPE).also { 106 postValue(it) 107 } 108 } 109 else -> false 110 } 111 } 112 } 113