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