1 /* <lambda>null2 * 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 android.content.Intent 21 import android.os.UserHandle 22 import androidx.annotation.OpenForTesting 23 import androidx.annotation.VisibleForTesting 24 import androidx.lifecycle.Lifecycle 25 import androidx.lifecycle.LifecycleOwner 26 import androidx.lifecycle.lifecycleScope 27 import androidx.lifecycle.repeatOnLifecycle 28 import androidx.preference.Preference 29 import androidx.preference.PreferenceScreen 30 import com.android.settings.core.BasePreferenceController 31 import kotlinx.coroutines.Dispatchers 32 import kotlinx.coroutines.launch 33 import kotlinx.coroutines.withContext 34 35 @OpenForTesting 36 open class AppDataUsageAppSettingsController(context: Context, preferenceKey: String) : 37 BasePreferenceController(context, preferenceKey) { 38 39 private var packageNames: Iterable<String> = emptyList() 40 private var userId: Int = -1 41 private lateinit var preference: Preference 42 private var resolvedIntent: Intent? = null 43 44 private val packageManager = mContext.packageManager 45 46 override fun getAvailabilityStatus() = AVAILABLE 47 48 fun init(packageNames: Iterable<String>, userId: Int) { 49 this.packageNames = packageNames 50 this.userId = userId 51 } 52 53 override fun displayPreference(screen: PreferenceScreen) { 54 super.displayPreference(screen) 55 preference = screen.findPreference(preferenceKey)!! 56 } 57 58 override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) { 59 viewLifecycleOwner.lifecycleScope.launch { 60 viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 61 update() 62 } 63 } 64 } 65 66 private suspend fun update() { 67 resolvedIntent = withContext(Dispatchers.Default) { 68 packageNames.map { packageName -> 69 Intent(SettingsIntent).setPackage(packageName) 70 }.firstOrNull { intent -> 71 packageManager.resolveActivityAsUser(intent, 0, userId) != null 72 } 73 } 74 preference.isVisible = resolvedIntent != null 75 } 76 77 override fun handlePreferenceTreeClick(preference: Preference): Boolean { 78 if (preference.key == mPreferenceKey) { 79 resolvedIntent?.let { mContext.startActivityAsUser(it, UserHandle.of(userId)) } 80 return true 81 } 82 return false 83 } 84 85 private companion object { 86 val SettingsIntent = Intent(Intent.ACTION_MANAGE_NETWORK_USAGE).apply { 87 addCategory(Intent.CATEGORY_DEFAULT) 88 } 89 } 90 } 91