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 androidx.lifecycle.LiveData 21 import androidx.lifecycle.MutableLiveData 22 23 class AppNotificationController( 24 private val repository: AppNotificationRepository, 25 private val app: ApplicationInfo, 26 ) { 27 val isEnabled: LiveData<Boolean> 28 get() = _isEnabled 29 getEnablednull30 fun getEnabled() = _isEnabled.get() 31 32 fun setEnabled(enabled: Boolean) { 33 if (repository.setEnabled(app, enabled)) { 34 _isEnabled.postValue(enabled) 35 } 36 } 37 38 private val _isEnabled = object : MutableLiveData<Boolean>() { onActivenull39 override fun onActive() { 40 postValue(repository.isEnabled(app)) 41 } 42 onInactivenull43 override fun onInactive() { 44 } 45 <lambda>null46 fun get(): Boolean = value ?: repository.isEnabled(app).also { 47 postValue(it) 48 } 49 } 50 } 51