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 package com.android.settings.deviceinfo.legal 17 18 import android.content.Context 19 import android.content.Intent 20 import android.content.pm.ApplicationInfo 21 import android.content.pm.ResolveInfo 22 import androidx.annotation.StringRes 23 import com.android.settingslib.metadata.PreferenceAvailabilityProvider 24 import com.android.settingslib.metadata.PreferenceMetadata 25 import com.android.settingslib.metadata.PreferenceTitleProvider 26 27 // LINT.IfChange 28 class LegalPreference( 29 override val key: String, 30 @StringRes val defaultTitle: Int = 0, 31 val intentAction: String, 32 ) : PreferenceMetadata, PreferenceTitleProvider, PreferenceAvailabilityProvider { 33 getTitlenull34 override fun getTitle(context: Context): CharSequence? { 35 val resolveInfo = 36 findMatchingSpecificActivity(context) ?: return context.getText(defaultTitle) 37 return resolveInfo.loadLabel(context.packageManager) 38 } 39 isAvailablenull40 override fun isAvailable(context: Context) = (findMatchingSpecificActivity(context) != null) 41 42 override fun intent(context: Context) = 43 findMatchingSpecificActivity(context)?.let { 44 Intent() 45 .setClassName(it.activityInfo.packageName, it.activityInfo.name) 46 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 47 } 48 findMatchingSpecificActivitynull49 private fun findMatchingSpecificActivity(context: Context): ResolveInfo? { 50 val intent = Intent(intentAction) 51 // Find the activity that is in the system image 52 val list: List<ResolveInfo> = context.packageManager.queryIntentActivities(intent, 0) 53 return list.firstOrNull { 54 (it.activityInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0 55 } 56 } 57 } 58 // LINT.ThenChange(LegalPreferenceController.java) 59