1 /* 2 * Copyright (C) 2023 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.datausage 18 19 import android.content.Context 20 import androidx.annotation.OpenForTesting 21 import androidx.lifecycle.Lifecycle 22 import androidx.lifecycle.LifecycleOwner 23 import androidx.lifecycle.lifecycleScope 24 import androidx.lifecycle.repeatOnLifecycle 25 import androidx.preference.PreferenceGroup 26 import androidx.preference.PreferenceScreen 27 import com.android.settings.core.BasePreferenceController 28 import com.android.settings.datausage.lib.AppPreferenceRepository 29 import kotlinx.coroutines.Dispatchers 30 import kotlinx.coroutines.launch 31 import kotlinx.coroutines.withContext 32 33 @OpenForTesting 34 open class AppDataUsageListController @JvmOverloads constructor( 35 context: Context, 36 preferenceKey: String, 37 private val repository: AppPreferenceRepository = AppPreferenceRepository(context), 38 ) : BasePreferenceController(context, preferenceKey) { 39 40 private var uids: List<Int> = emptyList() 41 private lateinit var preference: PreferenceGroup 42 initnull43 fun init(uids: List<Int>) { 44 this.uids = uids 45 } 46 getAvailabilityStatusnull47 override fun getAvailabilityStatus() = AVAILABLE 48 49 override fun displayPreference(screen: PreferenceScreen) { 50 super.displayPreference(screen) 51 preference = screen.findPreference(preferenceKey)!! 52 } 53 onViewCreatednull54 override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) { 55 viewLifecycleOwner.lifecycleScope.launch { 56 viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 57 updateList() 58 } 59 } 60 } 61 updateListnull62 private suspend fun updateList() { 63 if (uids.size <= 1) { 64 preference.isVisible = false 65 return 66 } 67 preference.isVisible = true 68 val appPreferences = withContext(Dispatchers.Default) { 69 repository.loadAppPreferences(uids) 70 } 71 preference.removeAll() 72 for (appPreference in appPreferences) { 73 preference.addPreference(appPreference) 74 } 75 } 76 } 77