Move AdvancedWifiSettings into ConfigureWifiSettings.
Bug:34719854 Test: make -j40 RunSettingsRoboTests clicked on preferences and made sure functionality did not change. Change-Id: Ia5f0c7f5ea0f88d1322008ab58ca79028cc1a2bb
This commit is contained in:
@@ -1,184 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.wifi;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.net.wifi.WpsInfo;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.security.Credentials;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.Preference.OnPreferenceClickListener;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.RestrictedSettingsFragment;
|
||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class AdvancedWifiSettings extends RestrictedSettingsFragment {
|
||||
private static final String TAG = "AdvancedWifiSettings";
|
||||
|
||||
private static final String KEY_INSTALL_CREDENTIALS = "install_credentials";
|
||||
private static final String KEY_WIFI_DIRECT = "wifi_direct";
|
||||
private static final String KEY_WPS_PUSH = "wps_push_button";
|
||||
private static final String KEY_WPS_PIN = "wps_pin_entry";
|
||||
|
||||
private boolean mUnavailable;
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
initPreferences();
|
||||
}
|
||||
};
|
||||
|
||||
public AdvancedWifiSettings() {
|
||||
super(UserManager.DISALLOW_CONFIG_WIFI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.WIFI_ADVANCED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (isUiRestricted()) {
|
||||
mUnavailable = true;
|
||||
setPreferenceScreen(new PreferenceScreen(getPrefContext(), null));
|
||||
} else {
|
||||
addPreferencesFromResource(R.xml.wifi_advanced_settings);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
getEmptyTextView().setText(R.string.wifi_advanced_not_available);
|
||||
if (mUnavailable) {
|
||||
getPreferenceScreen().removeAll();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (!mUnavailable) {
|
||||
getActivity().registerReceiver(mReceiver,
|
||||
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
|
||||
initPreferences();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
if (!mUnavailable) {
|
||||
getActivity().unregisterReceiver(mReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
private void initPreferences() {
|
||||
final Context context = getActivity();
|
||||
Intent intent = new Intent(Credentials.INSTALL_AS_USER_ACTION);
|
||||
intent.setClassName("com.android.certinstaller",
|
||||
"com.android.certinstaller.CertInstallerMain");
|
||||
intent.putExtra(Credentials.EXTRA_INSTALL_AS_UID, android.os.Process.WIFI_UID);
|
||||
Preference pref = findPreference(KEY_INSTALL_CREDENTIALS);
|
||||
pref.setIntent(intent);
|
||||
|
||||
final WifiManager wifiManager =
|
||||
(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
|
||||
Intent wifiDirectIntent = new Intent(context,
|
||||
com.android.settings.Settings.WifiP2pSettingsActivity.class);
|
||||
Preference wifiDirectPref = findPreference(KEY_WIFI_DIRECT);
|
||||
wifiDirectPref.setIntent(wifiDirectIntent);
|
||||
wifiDirectPref.setEnabled(wifiManager.isWifiEnabled());
|
||||
|
||||
// WpsDialog: Create the dialog like WifiSettings does.
|
||||
Preference wpsPushPref = findPreference(KEY_WPS_PUSH);
|
||||
wpsPushPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||
public boolean onPreferenceClick(Preference arg0) {
|
||||
WpsFragment wpsFragment = new WpsFragment(WpsInfo.PBC);
|
||||
wpsFragment.show(getFragmentManager(), KEY_WPS_PUSH);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
wpsPushPref.setEnabled(wifiManager.isWifiEnabled());
|
||||
|
||||
// WpsDialog: Create the dialog like WifiSettings does.
|
||||
Preference wpsPinPref = findPreference(KEY_WPS_PIN);
|
||||
wpsPinPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
|
||||
public boolean onPreferenceClick(Preference arg0) {
|
||||
WpsFragment wpsFragment = new WpsFragment(WpsInfo.DISPLAY);
|
||||
wpsFragment.show(getFragmentManager(), KEY_WPS_PIN);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
wpsPinPref.setEnabled(wifiManager.isWifiEnabled());
|
||||
}
|
||||
|
||||
/* Wrapper class for the WPS dialog to properly handle life cycle events like rotation. */
|
||||
public static class WpsFragment extends InstrumentedDialogFragment {
|
||||
private static int mWpsSetup;
|
||||
|
||||
// Public default constructor is required for rotation.
|
||||
public WpsFragment() {
|
||||
super();
|
||||
}
|
||||
|
||||
public WpsFragment(int wpsSetup) {
|
||||
super();
|
||||
mWpsSetup = wpsSetup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.DIALOG_WPS_SETUP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
return new WpsDialog(getActivity(), mWpsSetup);
|
||||
}
|
||||
}
|
||||
|
||||
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
|
||||
boolean enabled) {
|
||||
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||
sir.xmlResId = R.xml.wifi_advanced_settings;
|
||||
return Arrays.asList(sir);
|
||||
}
|
||||
};
|
||||
}
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.android.settings.wifi;
|
||||
|
||||
import static android.content.Context.WIFI_SERVICE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.provider.SearchIndexableResource;
|
||||
@@ -25,13 +27,12 @@ import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settings.wifi.p2p.WifiP2pPreferenceController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static android.content.Context.WIFI_SERVICE;
|
||||
|
||||
public class ConfigureWifiSettings extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "ConfigureWifiSettings";
|
||||
@@ -63,6 +64,9 @@ public class ConfigureWifiSettings extends DashboardFragment {
|
||||
controllers.add(new NotifyOpenNetworksPreferenceController(context, getLifecycle()));
|
||||
controllers.add(new WifiWakeupPreferenceController(context, getLifecycle()));
|
||||
controllers.add(new WifiSleepPolicyPreferenceController(context));
|
||||
controllers.add(new WifiP2pPreferenceController(context, getLifecycle(), mWifiManager));
|
||||
controllers.add(new WpsPreferenceController(
|
||||
context, getLifecycle(), mWifiManager, getFragmentManager()));
|
||||
return controllers;
|
||||
}
|
||||
|
||||
|
@@ -39,8 +39,9 @@ public class WifiPickerActivity extends SettingsActivity implements ButtonBarHan
|
||||
protected boolean isValidFragment(String fragmentName) {
|
||||
if (WifiSettings.class.getName().equals(fragmentName)
|
||||
|| WifiP2pSettings.class.getName().equals(fragmentName)
|
||||
|| SavedAccessPointsWifiSettings.class.getName().equals(fragmentName)
|
||||
|| AdvancedWifiSettings.class.getName().equals(fragmentName)) return true;
|
||||
|| SavedAccessPointsWifiSettings.class.getName().equals(fragmentName)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -93,7 +93,6 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
|
||||
/* package */ static final int MENU_ID_WPS_PBC = Menu.FIRST;
|
||||
private static final int MENU_ID_WPS_PIN = Menu.FIRST + 1;
|
||||
private static final int MENU_ID_ADVANCED = Menu.FIRST + 4;
|
||||
private static final int MENU_ID_SCAN = Menu.FIRST + 5;
|
||||
private static final int MENU_ID_CONNECT = Menu.FIRST + 6;
|
||||
private static final int MENU_ID_FORGET = Menu.FIRST + 7;
|
||||
@@ -113,6 +112,7 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
private static final String PREF_KEY_EMPTY_WIFI_LIST = "wifi_empty_list";
|
||||
private static final String PREF_KEY_ACCESS_POINTS = "access_points";
|
||||
private static final String PREF_KEY_ADDITIONAL_SETTINGS = "additional_settings";
|
||||
private static final String PREF_KEY_CONFIGURE_WIFI_SETTINGS = "configure_settings";
|
||||
private static final String PREF_KEY_SAVED_NETWORKS = "saved_networks";
|
||||
|
||||
protected WifiManager mWifiManager;
|
||||
@@ -155,6 +155,7 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
private PreferenceCategory mAccessPointsPreferenceCategory;
|
||||
private PreferenceCategory mAdditionalSettingsPreferenceCategory;
|
||||
private Preference mAddPreference;
|
||||
private Preference mConfigureWifiSettingsPreference;
|
||||
private Preference mSavedNetworksPreference;
|
||||
private LinkablePreference mStatusMessagePreference;
|
||||
|
||||
@@ -186,6 +187,7 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
(PreferenceCategory) findPreference(PREF_KEY_ACCESS_POINTS);
|
||||
mAdditionalSettingsPreferenceCategory =
|
||||
(PreferenceCategory) findPreference(PREF_KEY_ADDITIONAL_SETTINGS);
|
||||
mConfigureWifiSettingsPreference = findPreference(PREF_KEY_CONFIGURE_WIFI_SETTINGS);
|
||||
mSavedNetworksPreference = findPreference(PREF_KEY_SAVED_NETWORKS);
|
||||
|
||||
Context prefContext = getPrefContext();
|
||||
@@ -359,9 +361,6 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
* @param menu
|
||||
*/
|
||||
void addOptionsMenuItems(Menu menu) {
|
||||
menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
|
||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
|
||||
|
||||
final boolean wifiIsEnabled = mWifiTracker.isWifiEnabled();
|
||||
mScanMenuItem = menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.menu_stats_refresh)
|
||||
.setIcon(com.android.internal.R.drawable.ic_menu_refresh);
|
||||
@@ -425,17 +424,6 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_FORCE_SCAN);
|
||||
mWifiTracker.forceScan();
|
||||
return true;
|
||||
case MENU_ID_ADVANCED:
|
||||
if (getActivity() instanceof SettingsActivity) {
|
||||
((SettingsActivity) getActivity()).startPreferencePanel(
|
||||
AdvancedWifiSettings.class.getCanonicalName(), null,
|
||||
R.string.wifi_advanced_titlebar, null, this, 0);
|
||||
} else {
|
||||
startFragment(this, AdvancedWifiSettings.class.getCanonicalName(),
|
||||
R.string.wifi_advanced_titlebar, -1 /* Do not request a results */,
|
||||
null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
@@ -689,11 +677,11 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
mAccessPointsPreferenceCategory.addPreference(pref);
|
||||
mAddPreference.setOrder(index++);
|
||||
mAccessPointsPreferenceCategory.addPreference(mAddPreference);
|
||||
setSavedNetworkPreferenceVisibility();
|
||||
setConfigureWifiSettingsVisibility();
|
||||
} else {
|
||||
mAddPreference.setOrder(index++);
|
||||
mAccessPointsPreferenceCategory.addPreference(mAddPreference);
|
||||
setSavedNetworkPreferenceVisibility();
|
||||
setConfigureWifiSettingsVisibility();
|
||||
setProgressBarVisible(false);
|
||||
}
|
||||
if (mScanMenuItem != null) {
|
||||
@@ -713,7 +701,7 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
|
||||
case WifiManager.WIFI_STATE_DISABLED:
|
||||
setOffMessage();
|
||||
setSavedNetworkPreferenceVisibility();
|
||||
setConfigureWifiSettingsVisibility();
|
||||
setProgressBarVisible(false);
|
||||
if (mScanMenuItem != null) {
|
||||
mScanMenuItem.setEnabled(false);
|
||||
@@ -722,7 +710,12 @@ public class WifiSettings extends RestrictedSettingsFragment
|
||||
}
|
||||
}
|
||||
|
||||
private void setSavedNetworkPreferenceVisibility() {
|
||||
private void setConfigureWifiSettingsVisibility() {
|
||||
if (isUiRestricted()) {
|
||||
mAdditionalSettingsPreferenceCategory.removeAll();
|
||||
return;
|
||||
}
|
||||
mAdditionalSettingsPreferenceCategory.addPreference(mConfigureWifiSettingsPreference);
|
||||
if (mWifiTracker.doSavedNetworksExist()) {
|
||||
mAdditionalSettingsPreferenceCategory.addPreference(mSavedNetworksPreference);
|
||||
} else {
|
||||
|
154
src/com/android/settings/wifi/WpsPreferenceController.java
Normal file
154
src/com/android/settings/wifi/WpsPreferenceController.java
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.wifi;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.FragmentManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.net.wifi.WpsInfo;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||
import com.android.settings.core.lifecycle.Lifecycle;
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
|
||||
/**
|
||||
* {@link PreferenceController} that shows Dialog for WPS progress. Disabled when Wi-Fi is off.
|
||||
*/
|
||||
public class WpsPreferenceController extends PreferenceController implements
|
||||
LifecycleObserver, OnPause, OnResume {
|
||||
|
||||
private static final String KEY_WPS_PUSH = "wps_push_button";
|
||||
private static final String KEY_WPS_PIN = "wps_pin_entry";
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
private final FragmentManager mFragmentManager;
|
||||
@VisibleForTesting
|
||||
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
togglePreferences();
|
||||
}
|
||||
};
|
||||
private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
||||
|
||||
private Preference mWpsPushPref;
|
||||
private Preference mWpsPinPref;
|
||||
|
||||
public WpsPreferenceController(
|
||||
Context context,
|
||||
Lifecycle lifecycle,
|
||||
WifiManager wifiManager,
|
||||
FragmentManager fragmentManager) {
|
||||
super(context);
|
||||
mWifiManager = wifiManager;
|
||||
mFragmentManager = fragmentManager;
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
// Always show preference.
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
// Returns null because this controller contains more than 1 preference.
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mWpsPushPref = screen.findPreference(KEY_WPS_PUSH);
|
||||
mWpsPinPref = screen.findPreference(KEY_WPS_PIN);
|
||||
if (mWpsPushPref == null || mWpsPinPref == null) {
|
||||
return;
|
||||
}
|
||||
// WpsDialog: Create the dialog like WifiSettings does.
|
||||
mWpsPushPref.setOnPreferenceClickListener((arg) -> {
|
||||
WpsFragment wpsFragment = new WpsFragment(WpsInfo.PBC);
|
||||
wpsFragment.show(mFragmentManager, KEY_WPS_PUSH);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
// WpsDialog: Create the dialog like WifiSettings does.
|
||||
mWpsPinPref.setOnPreferenceClickListener((arg) -> {
|
||||
WpsFragment wpsFragment = new WpsFragment(WpsInfo.DISPLAY);
|
||||
wpsFragment.show(mFragmentManager, KEY_WPS_PIN);
|
||||
return true;
|
||||
});
|
||||
togglePreferences();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
mContext.registerReceiver(mReceiver, mFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
mContext.unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
private void togglePreferences() {
|
||||
if (mWpsPushPref != null && mWpsPinPref != null) {
|
||||
boolean enabled = mWifiManager.isWifiEnabled();
|
||||
mWpsPushPref.setEnabled(enabled);
|
||||
mWpsPinPref.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fragment for Dialog to show WPS progress.
|
||||
*/
|
||||
public static class WpsFragment extends InstrumentedDialogFragment {
|
||||
private static int mWpsSetup;
|
||||
|
||||
// Public default constructor is required for rotation.
|
||||
public WpsFragment() {
|
||||
super();
|
||||
}
|
||||
|
||||
public WpsFragment(int wpsSetup) {
|
||||
super();
|
||||
mWpsSetup = wpsSetup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.DIALOG_WPS_SETUP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
return new WpsDialog(getActivity(), mWpsSetup);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.wifi.p2p;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.core.lifecycle.Lifecycle;
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
|
||||
/**
|
||||
* {@link PreferenceController} to toggle Wifi Direct preference on Wi-Fi state.
|
||||
*/
|
||||
public class WifiP2pPreferenceController extends PreferenceController implements
|
||||
LifecycleObserver, OnPause, OnResume {
|
||||
|
||||
private static final String KEY_WIFI_DIRECT = "wifi_direct";
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
@VisibleForTesting
|
||||
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
togglePreferences();
|
||||
}
|
||||
};
|
||||
private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
||||
|
||||
private Preference mWifiDirectPref;
|
||||
|
||||
public WifiP2pPreferenceController(
|
||||
Context context, Lifecycle lifecycle, WifiManager wifiManager) {
|
||||
super(context);
|
||||
mWifiManager = wifiManager;
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mWifiDirectPref = screen.findPreference(KEY_WIFI_DIRECT);
|
||||
togglePreferences();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
mContext.registerReceiver(mReceiver, mFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
mContext.unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
// Always show preference.
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_WIFI_DIRECT;
|
||||
}
|
||||
|
||||
private void togglePreferences() {
|
||||
if (mWifiDirectPref != null) {
|
||||
mWifiDirectPref.setEnabled(mWifiManager.isWifiEnabled());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user