• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.connecteddevice
18 
19 import android.app.settings.SettingsEnums
20 import android.content.Context
21 import android.content.Intent
22 import android.util.Log
23 import androidx.preference.Preference
24 import com.android.settings.R
25 import com.android.settings.bluetooth.Utils
26 import com.android.settings.core.SubSettingLauncher
27 import com.android.settings.location.BluetoothScanningFragment
28 import com.android.settings.widget.FooterPreferenceBinding
29 import com.android.settings.widget.FooterPreferenceMetadata
30 import com.android.settingslib.metadata.PreferenceMetadata
31 import com.android.settingslib.metadata.PreferenceTitleProvider
32 import com.android.settingslib.widget.FooterPreference
33 
34 class BluetoothFooterPreference(private val bluetoothDataStore: BluetoothDataStore) :
35     FooterPreferenceMetadata, FooterPreferenceBinding, PreferenceTitleProvider {
36 
37     override val key: String
38         get() = KEY
39 
dependenciesnull40     override fun dependencies(context: Context) = arrayOf(BluetoothPreference.KEY)
41 
42     override fun intent(context: Context): Intent? = subSettingLauncher(context).toIntent()
43 
44     override fun bind(preference: Preference, metadata: PreferenceMetadata) {
45         super.bind(preference, metadata)
46         val bluetoothDisabled = bluetoothDataStore.getBoolean(BluetoothPreference.KEY) != true
47         val footerPreference = preference as FooterPreference
48         val context = preference.context
49         if (bluetoothDisabled && Utils.isBluetoothScanningEnabled(context)) {
50             footerPreference.setLearnMoreText(context.getString(R.string.bluetooth_scan_change))
51             footerPreference.setLearnMoreAction { subSettingLauncher(context).launch() }
52         } else {
53             footerPreference.setLearnMoreText("")
54             footerPreference.setLearnMoreAction(null)
55         }
56     }
57 
subSettingLaunchernull58     private fun subSettingLauncher(context: Context) =
59         SubSettingLauncher(context)
60             .setDestination(BluetoothScanningFragment::class.java.name)
61             .setSourceMetricsCategory(SettingsEnums.BLUETOOTH_FRAGMENT)
62 
63     override fun getTitle(context: Context): CharSequence? {
64         val bluetoothDisabled = bluetoothDataStore.getBoolean(BluetoothPreference.KEY) != true
65         val resId =
66             if (bluetoothDisabled && Utils.isBluetoothScanningEnabled(context)) {
67                 when (isAutoOnFeatureAvailable()) {
68                     true -> R.string.bluetooth_scanning_on_info_message_auto_on_available
69                     else -> R.string.bluetooth_scanning_on_info_message
70                 }
71             } else {
72                 when (isAutoOnFeatureAvailable()) {
73                     true -> R.string.bluetooth_empty_list_bluetooth_off_auto_on_available
74                     else -> R.string.bluetooth_empty_list_bluetooth_off
75                 }
76             }
77         return context.getString(resId)
78     }
79 
isAutoOnFeatureAvailablenull80     private fun isAutoOnFeatureAvailable() =
81         try {
82             bluetoothDataStore.bluetoothAdapter?.isAutoOnSupported == true
83         } catch (e: Exception) {
84             Log.e(TAG, "isAutoOnSupported failed", e)
85             false
86         }
87 
88     companion object {
89         const val KEY = "bluetooth_screen_footer"
90         const val TAG = "BluetoothFooterPreference"
91     }
92 }
93