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