1 /* 2 * Copyright (C) 2017 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.wifi; 17 18 import android.content.Context; 19 20 import androidx.preference.PreferenceScreen; 21 22 import com.android.settings.R; 23 import com.android.settings.core.PreferenceControllerMixin; 24 import com.android.settings.widget.MasterSwitchController; 25 import com.android.settings.widget.MasterSwitchPreference; 26 import com.android.settings.widget.SummaryUpdater; 27 import com.android.settingslib.core.AbstractPreferenceController; 28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 29 import com.android.settingslib.core.lifecycle.LifecycleObserver; 30 import com.android.settingslib.core.lifecycle.events.OnPause; 31 import com.android.settingslib.core.lifecycle.events.OnResume; 32 import com.android.settingslib.core.lifecycle.events.OnStart; 33 import com.android.settingslib.core.lifecycle.events.OnStop; 34 35 public class WifiMasterSwitchPreferenceController extends AbstractPreferenceController 36 implements PreferenceControllerMixin, SummaryUpdater.OnSummaryChangeListener, 37 LifecycleObserver, OnResume, OnPause, OnStart, OnStop { 38 39 public static final String KEY_TOGGLE_WIFI = "toggle_wifi"; 40 41 private MasterSwitchPreference mWifiPreference; 42 private WifiEnabler mWifiEnabler; 43 private final WifiSummaryUpdater mSummaryHelper; 44 private final MetricsFeatureProvider mMetricsFeatureProvider; 45 WifiMasterSwitchPreferenceController(Context context, MetricsFeatureProvider metricsFeatureProvider)46 public WifiMasterSwitchPreferenceController(Context context, 47 MetricsFeatureProvider metricsFeatureProvider) { 48 super(context); 49 mMetricsFeatureProvider = metricsFeatureProvider; 50 mSummaryHelper = new WifiSummaryUpdater(mContext, this); 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 mWifiPreference = screen.findPreference(KEY_TOGGLE_WIFI); 57 } 58 59 @Override isAvailable()60 public boolean isAvailable() { 61 return mContext.getResources().getBoolean(R.bool.config_show_wifi_settings); 62 } 63 64 @Override getPreferenceKey()65 public String getPreferenceKey() { 66 return KEY_TOGGLE_WIFI; 67 } 68 69 @Override onResume()70 public void onResume() { 71 mSummaryHelper.register(true); 72 if (mWifiEnabler != null) { 73 mWifiEnabler.resume(mContext); 74 } 75 } 76 77 @Override onPause()78 public void onPause() { 79 if (mWifiEnabler != null) { 80 mWifiEnabler.pause(); 81 } 82 mSummaryHelper.register(false); 83 } 84 85 @Override onStart()86 public void onStart() { 87 mWifiEnabler = new WifiEnabler(mContext, new MasterSwitchController(mWifiPreference), 88 mMetricsFeatureProvider); 89 } 90 91 @Override onStop()92 public void onStop() { 93 if (mWifiEnabler != null) { 94 mWifiEnabler.teardownSwitchController(); 95 } 96 } 97 98 @Override onSummaryChanged(String summary)99 public void onSummaryChanged(String summary) { 100 if (mWifiPreference != null) { 101 mWifiPreference.setSummary(summary); 102 } 103 } 104 105 } 106