• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.GenericSwitchController;
25 import com.android.settings.widget.PrimarySwitchPreference;
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 /**
36  * PreferenceController to update the wifi state.
37  */
38 // TODO(b/167474581): Should clean up this controller when Provider Model finished.
39 public class WifiPrimarySwitchPreferenceController extends AbstractPreferenceController
40         implements PreferenceControllerMixin, SummaryUpdater.OnSummaryChangeListener,
41         LifecycleObserver, OnResume, OnPause, OnStart, OnStop {
42     //TODO(b/151133650): Replace AbstractPreferenceController with BasePreferenceController.
43 
44     public static final String KEY_TOGGLE_WIFI = "main_toggle_wifi";
45 
46     private PrimarySwitchPreference mWifiPreference;
47     private WifiEnabler mWifiEnabler;
48     private final WifiSummaryUpdater mSummaryHelper;
49     private final MetricsFeatureProvider mMetricsFeatureProvider;
50 
WifiPrimarySwitchPreferenceController(Context context, MetricsFeatureProvider metricsFeatureProvider)51     public WifiPrimarySwitchPreferenceController(Context context,
52             MetricsFeatureProvider metricsFeatureProvider) {
53         super(context);
54         mMetricsFeatureProvider = metricsFeatureProvider;
55         mSummaryHelper = new WifiSummaryUpdater(mContext, this);
56     }
57 
58     @Override
displayPreference(PreferenceScreen screen)59     public void displayPreference(PreferenceScreen screen) {
60         super.displayPreference(screen);
61         mWifiPreference = screen.findPreference(KEY_TOGGLE_WIFI);
62     }
63 
64     @Override
isAvailable()65     public boolean isAvailable() {
66         return mContext.getResources().getBoolean(R.bool.config_show_wifi_settings);
67     }
68 
69     @Override
getPreferenceKey()70     public String getPreferenceKey() {
71         return KEY_TOGGLE_WIFI;
72     }
73 
74     @Override
onResume()75     public void onResume() {
76         mSummaryHelper.register(true);
77         if (mWifiEnabler != null) {
78             mWifiEnabler.resume(mContext);
79         }
80     }
81 
82     @Override
onPause()83     public void onPause() {
84         if (mWifiEnabler != null) {
85             mWifiEnabler.pause();
86         }
87         mSummaryHelper.register(false);
88     }
89 
90     @Override
onStart()91     public void onStart() {
92         mWifiEnabler = new WifiEnabler(mContext, new GenericSwitchController(mWifiPreference),
93                 mMetricsFeatureProvider);
94     }
95 
96     @Override
onStop()97     public void onStop() {
98         if (mWifiEnabler != null) {
99             mWifiEnabler.teardownSwitchController();
100         }
101     }
102 
103     @Override
onSummaryChanged(String summary)104     public void onSummaryChanged(String summary) {
105         if (mWifiPreference != null) {
106             mWifiPreference.setSummary(summary);
107         }
108     }
109 
110 }
111