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 package com.android.settings.datausage 17 18 import android.app.settings.SettingsEnums 19 import android.content.Context 20 import android.os.Bundle 21 import android.telephony.SubscriptionManager 22 import android.view.View 23 import com.android.settings.R 24 import com.android.settings.SettingsActivity 25 import com.android.settings.dashboard.DashboardFragment 26 import com.android.settings.search.BaseSearchIndexProvider 27 import com.android.settings.widget.SettingsMainSwitchBar 28 import com.android.settingslib.search.SearchIndexable 29 30 @SearchIndexable 31 class DataSaverSummary : DashboardFragment() { 32 private lateinit var switchBar: SettingsMainSwitchBar 33 private lateinit var dataSaverBackend: DataSaverBackend 34 35 // Flag used to avoid infinite loop due if user switch it on/off too quick. 36 private var switching = false 37 38 override fun onCreate(bundle: Bundle?) { 39 super.onCreate(bundle) 40 41 if (!requireContext().isDataSaverVisible()) { 42 finishFragment() 43 return 44 } 45 46 if (!isCatalystEnabled) { 47 dataSaverBackend = DataSaverBackend(requireContext()) 48 } 49 } 50 51 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 52 super.onViewCreated(view, savedInstanceState) 53 if (!isCatalystEnabled) { 54 switchBar = (activity as SettingsActivity).switchBar.apply { 55 setTitle(getString(R.string.data_saver_switch_title)) 56 show() 57 addOnSwitchChangeListener { _, isChecked -> onSwitchChanged(isChecked) } 58 } 59 } 60 } 61 62 override fun onResume() { 63 super.onResume() 64 if (!isCatalystEnabled) { 65 dataSaverBackend.addListener(dataSaverBackendListener) 66 } 67 } 68 69 override fun onPause() { 70 super.onPause() 71 if (!isCatalystEnabled) { 72 dataSaverBackend.remListener(dataSaverBackendListener) 73 } 74 } 75 76 private fun onSwitchChanged(isChecked: Boolean) { 77 synchronized(this) { 78 if (!switching) { 79 switching = true 80 dataSaverBackend.isDataSaverEnabled = isChecked 81 } 82 } 83 } 84 85 override fun getPreferenceScreenResId() = R.xml.data_saver 86 override fun getMetricsCategory() = SettingsEnums.DATA_SAVER_SUMMARY 87 override fun getHelpResource() = R.string.help_url_data_saver 88 override fun getLogTag() = TAG 89 90 override fun getPreferenceScreenBindingKey(context: Context) = DataSaverScreen.KEY 91 92 private val dataSaverBackendListener = object : DataSaverBackend.Listener { 93 override fun onDataSaverChanged(isDataSaving: Boolean) { 94 synchronized(this) { 95 switchBar.isChecked = isDataSaving 96 switching = false 97 } 98 } 99 } 100 101 companion object { 102 private const val TAG = "DataSaverSummary" 103 104 private fun Context.isDataSaverVisible(): Boolean = 105 resources.getBoolean(R.bool.config_show_data_saver) 106 107 @JvmField 108 val SEARCH_INDEX_DATA_PROVIDER = object : BaseSearchIndexProvider(R.xml.data_saver) { 109 override fun isPageSearchEnabled(context: Context): Boolean = 110 context.isDataSaverVisible() && 111 DataUsageUtils.hasMobileData(context) && 112 (DataUsageUtils.getDefaultSubscriptionId(context) != 113 SubscriptionManager.INVALID_SUBSCRIPTION_ID) 114 } 115 } 116 } 117