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.p2p; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.net.wifi.WifiManager; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.core.AbstractPreferenceController; 31 import com.android.settingslib.core.lifecycle.Lifecycle; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnPause; 34 import com.android.settingslib.core.lifecycle.events.OnResume; 35 import com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils; 36 37 /** 38 * {@link PreferenceControllerMixin} to toggle Wifi Direct preference on Wi-Fi state. 39 */ 40 public class WifiP2pPreferenceController extends AbstractPreferenceController 41 implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnResume { 42 43 private static final String KEY_WIFI_DIRECT = "wifi_direct"; 44 45 private final WifiManager mWifiManager; 46 @VisibleForTesting 47 final BroadcastReceiver mReceiver = new BroadcastReceiver() { 48 @Override 49 public void onReceive(Context context, Intent intent) { 50 togglePreferences(); 51 } 52 }; 53 private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); 54 55 private Preference mWifiDirectPref; 56 @VisibleForTesting 57 boolean mIsWifiDirectAllow; 58 WifiP2pPreferenceController( Context context, Lifecycle lifecycle, WifiManager wifiManager)59 public WifiP2pPreferenceController( 60 Context context, Lifecycle lifecycle, WifiManager wifiManager) { 61 super(context); 62 mWifiManager = wifiManager; 63 mIsWifiDirectAllow = WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(context); 64 lifecycle.addObserver(this); 65 } 66 67 @Override displayPreference(PreferenceScreen screen)68 public void displayPreference(PreferenceScreen screen) { 69 super.displayPreference(screen); 70 mWifiDirectPref = screen.findPreference(KEY_WIFI_DIRECT); 71 togglePreferences(); 72 if (!mIsWifiDirectAllow) { 73 mWifiDirectPref.setSummary(R.string.not_allowed_by_ent); 74 } 75 } 76 77 @Override updateState(Preference preference)78 public void updateState(Preference preference) { 79 super.updateState(preference); 80 preference.setEnabled(isWifiP2pAvailable()); 81 } 82 83 @Override onResume()84 public void onResume() { 85 mContext.registerReceiver(mReceiver, mFilter); 86 } 87 88 @Override onPause()89 public void onPause() { 90 mContext.unregisterReceiver(mReceiver); 91 } 92 93 @Override isAvailable()94 public boolean isAvailable() { 95 // Always show preference. 96 return true; 97 } 98 @Override getPreferenceKey()99 public String getPreferenceKey() { 100 return KEY_WIFI_DIRECT; 101 } 102 togglePreferences()103 private void togglePreferences() { 104 if (mWifiDirectPref != null) { 105 mWifiDirectPref.setEnabled(isWifiP2pAvailable()); 106 } 107 } isWifiP2pAvailable()108 private boolean isWifiP2pAvailable() { 109 return mWifiManager.isWifiEnabled() && mIsWifiDirectAllow; 110 } 111 112 } 113