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