1 /* 2 * Copyright (C) 2025 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.bluetooth 18 19 import android.app.settings.SettingsEnums 20 import android.content.ComponentName 21 import android.content.Context 22 import android.content.Intent 23 import android.content.pm.PackageManager 24 import android.content.pm.PackageManager.NameNotFoundException 25 import android.provider.Settings 26 import android.text.TextUtils 27 import android.view.View 28 import android.widget.TextView 29 import androidx.preference.PreferenceScreen 30 import com.android.settings.R 31 import com.android.settings.core.BasePreferenceController 32 import com.android.settings.overlay.FeatureFactory 33 import com.android.settingslib.widget.LayoutPreference 34 35 /** Preference controller for Nearby Share. */ 36 class NearbySharePreferenceController(private val context: Context, key: String) : 37 BasePreferenceController(context, key) { 38 private lateinit var intent: Intent 39 private var nearbyComponentName: ComponentName? = null 40 private var nearbyLabel: CharSequence? = null 41 initnull42 fun init(sendIntent: Intent) { 43 this.intent = sendIntent 44 val componentString = 45 Settings.Secure.getString( 46 context.getContentResolver(), 47 Settings.Secure.NEARBY_SHARING_COMPONENT, 48 ) 49 if (TextUtils.isEmpty(componentString)) { 50 return 51 } 52 nearbyComponentName = ComponentName.unflattenFromString(componentString)?.also { 53 intent.setComponent(it) 54 nearbyLabel = getNearbyLabel(it) 55 } 56 } 57 getAvailabilityStatusnull58 override fun getAvailabilityStatus(): Int { 59 if (nearbyLabel == null) { 60 return CONDITIONALLY_UNAVAILABLE 61 } 62 return AVAILABLE 63 } 64 displayPreferencenull65 override fun displayPreference(screen: PreferenceScreen) { 66 super.displayPreference(screen) 67 val preference: LayoutPreference = screen.findPreference(preferenceKey) ?: return 68 69 preference.findViewById<TextView>(R.id.nearby_sharing_suggestion_title).text = 70 context.getString(R.string.bluetooth_try_nearby_share_title, nearbyLabel) 71 FeatureFactory.featureFactory.metricsFeatureProvider.action( 72 SettingsEnums.PAGE_UNKNOWN, 73 SettingsEnums.ACTION_NEARBY_SHARE_ENTRYPOINT_SHOWN, 74 SettingsEnums.BLUETOOTH_DEVICE_PICKER, 75 "", 76 0 77 ) 78 preference.findViewById<View>(R.id.card_container).setOnClickListener { 79 FeatureFactory.featureFactory.metricsFeatureProvider.clicked( 80 SettingsEnums.BLUETOOTH_DEVICE_PICKER, 81 preferenceKey 82 ) 83 context.startActivity(intent) 84 true 85 } 86 } 87 getNearbyLabelnull88 private fun getNearbyLabel(componentName: ComponentName): CharSequence? = 89 try { 90 context.packageManager 91 .getActivityInfo(componentName, PackageManager.GET_META_DATA) 92 .loadLabel(context.packageManager) 93 } catch(_: NameNotFoundException) { 94 null 95 } 96 } 97