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.spa.network 17 18 import android.content.Context 19 import android.content.pm.PackageManager 20 import androidx.compose.material.icons.Icons 21 import androidx.compose.material.icons.outlined.AirplanemodeActive 22 import androidx.compose.runtime.Composable 23 import androidx.compose.runtime.getValue 24 import androidx.compose.runtime.livedata.observeAsState 25 import androidx.compose.runtime.remember 26 import androidx.compose.ui.platform.LocalContext 27 import androidx.lifecycle.LiveData 28 import androidx.lifecycle.MutableLiveData 29 import com.android.settings.AirplaneModeEnabler 30 import com.android.settings.AirplaneModeEnabler.OnAirplaneModeChangedListener 31 import com.android.settings.R 32 import com.android.settingslib.spa.widget.preference.SwitchPreference 33 import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel 34 import com.android.settingslib.spa.widget.ui.SettingsIcon 35 36 @Composable 37 fun AirplaneModePreference() { 38 val context = LocalContext.current 39 val controller = remember { AirplaneModeController(context) } 40 if (!controller.isAvailable()) return 41 val checked by controller.airplaneModeState.observeAsState( 42 initial = controller.isAirplaneModeOn() 43 ) 44 SwitchPreference(object : SwitchPreferenceModel { 45 override val title = context.getString(R.string.airplane_mode) 46 override val checked = { checked } 47 override val onCheckedChange = { newChecked: Boolean -> 48 controller.setChecked(newChecked) 49 } 50 override val icon = @Composable { 51 SettingsIcon(imageVector = Icons.Outlined.AirplanemodeActive) 52 } 53 }) 54 } 55 56 private class AirplaneModeController(private val context: Context) : OnAirplaneModeChangedListener { 57 private var airplaneModeEnabler = AirplaneModeEnabler(context, this) 58 private val _airplaneModeState = MutableLiveData<Boolean>() 59 val airplaneModeState: LiveData<Boolean> 60 get() = _airplaneModeState 61 onAirplaneModeChangednull62 override fun onAirplaneModeChanged(isAirplaneModeOn: Boolean) { 63 _airplaneModeState.postValue(isAirplaneModeOn) 64 } 65 isAvailablenull66 fun isAvailable(): Boolean { 67 return context.resources.getBoolean(R.bool.config_show_toggle_airplane) 68 && !context.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK) 69 } 70 isAirplaneModeOnnull71 fun isAirplaneModeOn(): Boolean { 72 return airplaneModeEnabler.isAirplaneModeOn() 73 } 74 setCheckednull75 fun setChecked(newChecked: Boolean) { 76 if (isAirplaneModeOn() == newChecked) { 77 return 78 } 79 airplaneModeEnabler.setAirplaneMode(newChecked) 80 } 81 82 } 83