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