1 /* 2 * Copyright (C) 2020 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 17 package com.android.settings.network; 18 19 import android.content.Context; 20 21 import androidx.lifecycle.Lifecycle; 22 import androidx.lifecycle.LifecycleObserver; 23 import androidx.lifecycle.OnLifecycleEvent; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.overlay.FeatureFactory; 27 import com.android.settings.widget.GenericSwitchController; 28 import com.android.settings.wifi.WifiEnabler; 29 import com.android.settingslib.RestrictedSwitchPreference; 30 import com.android.settingslib.core.AbstractPreferenceController; 31 32 /** 33 * This controller helps to manage the state of wifi switch preference. 34 */ 35 public class WifiSwitchPreferenceController extends AbstractPreferenceController implements 36 LifecycleObserver { 37 38 public static final String KEY = "main_toggle_wifi"; 39 40 private RestrictedSwitchPreference mPreference; 41 42 private WifiEnabler mWifiEnabler; 43 WifiSwitchPreferenceController(Context context, Lifecycle lifecycle)44 public WifiSwitchPreferenceController(Context context, Lifecycle lifecycle) { 45 super(context); 46 if (lifecycle == null) { 47 throw new IllegalArgumentException("Lifecycle must be set"); 48 } 49 50 lifecycle.addObserver(this); 51 } 52 53 @Override getPreferenceKey()54 public String getPreferenceKey() { 55 return KEY; 56 } 57 58 @Override isAvailable()59 public boolean isAvailable() { 60 return true; 61 } 62 63 @Override displayPreference(PreferenceScreen screen)64 public void displayPreference(PreferenceScreen screen) { 65 super.displayPreference(screen); 66 mPreference = screen.findPreference(getPreferenceKey()); 67 } 68 69 /** Lifecycle.Event.ON_START */ 70 @OnLifecycleEvent(Lifecycle.Event.ON_START) onStart()71 public void onStart() { 72 if (mPreference != null) { 73 mWifiEnabler = new WifiEnabler(mContext, new GenericSwitchController(mPreference), 74 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider()); 75 } 76 } 77 78 /** Lifecycle.Event.ON_STOP */ 79 @OnLifecycleEvent(Lifecycle.Event.ON_STOP) onStop()80 public void onStop() { 81 if (mWifiEnabler != null) { 82 mWifiEnabler.teardownSwitchController(); 83 } 84 } 85 86 /** Lifecycle.Event.ON_RESUME */ 87 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) onResume()88 public void onResume() { 89 if (mWifiEnabler != null) { 90 mWifiEnabler.resume(mContext); 91 } 92 } 93 94 /** Lifecycle.Event.ON_PAUSE */ 95 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) onPause()96 public void onPause() { 97 if (mWifiEnabler != null) { 98 mWifiEnabler.pause(); 99 } 100 } 101 } 102