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.qs.shared 18 19 import android.content.Context 20 import android.content.Intent 21 import android.content.pm.PackageManager 22 import android.os.UserHandle 23 import android.provider.Settings 24 import com.android.systemui.dagger.SysUISingleton 25 import com.android.systemui.dagger.qualifiers.Background 26 import com.android.systemui.user.data.repository.UserRepository 27 import javax.inject.Inject 28 import kotlinx.coroutines.CoroutineScope 29 import kotlinx.coroutines.launch 30 31 /** 32 * Provides the cached package name of the default Settings application. 33 * 34 * This repository retrieves and stores the package name to avoid repeated lookups. The package name 35 * is retrieved in a background thread when the `init()` method is called. 36 */ 37 @SysUISingleton 38 @Suppress("ShadeDisplayAwareContextChecker") 39 class QSSettingsPackageRepository 40 @Inject 41 constructor( 42 private val context: Context, 43 @Background private val backgroundScope: CoroutineScope, 44 private val userRepository: UserRepository, 45 ) { 46 private var settingsPackageName: String? = null 47 48 /** 49 * Initializes the repository by determining and caching the package name of the Settings app. 50 */ initnull51 fun init() { 52 backgroundScope.launch { 53 val mainUserId = userRepository.mainUserId 54 val mainUserContext = 55 context.createContextAsUser(UserHandle.of(mainUserId), /* flags */ 0) 56 val pm = mainUserContext.packageManager 57 settingsPackageName = 58 pm.queryIntentActivities( 59 Intent(Settings.ACTION_SETTINGS), 60 PackageManager.MATCH_SYSTEM_ONLY or PackageManager.MATCH_DEFAULT_ONLY, 61 ) 62 .firstOrNull() 63 ?.activityInfo 64 ?.packageName ?: DEFAULT_SETTINGS_PACKAGE_NAME 65 } 66 } 67 68 /** 69 * Returns the cached package name of the Settings app. 70 * 71 * If the package name has not been initialized yet, this method will return the default 72 * Settings package name. 73 * 74 * @return The package name of the Settings app. 75 */ getSettingsPackageNamenull76 fun getSettingsPackageName(): String { 77 return settingsPackageName ?: DEFAULT_SETTINGS_PACKAGE_NAME 78 } 79 80 companion object { 81 private const val DEFAULT_SETTINGS_PACKAGE_NAME = "com.android.settings" 82 } 83 } 84