[Wi-Fi] Create WifiNetworkDetailsFragment related version 2 files for WifiTracker2 development

Create below version 2 files for WifiTracker2 development, we can
check the feature flag only a few times and easily remove version 1
files in the future.

    src/com/android/settings/wifi/details2/
    src/com/android/settings/wifi/savedaccesspoints2/
    tests/robotests/src/com/android/settings/wifi/details2/
    tests/robotests/src/com/android/settings/wifi/savedaccesspoints2/

Bug: 143326832
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.wifi.details2
      make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.wifi.savedaccesspoints2

Change-Id: I4d2caf1ce313871605252395764b02747240f217
This commit is contained in:
Arc Wang
2019-11-04 17:03:15 +08:00
parent b4a6245d8d
commit 75dc89b122
25 changed files with 4704 additions and 38 deletions

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2019 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.details2;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
import androidx.preference.Preference;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.wifi.AccessPoint;
/**
* {@link BasePreferenceController} that launches Wi-Fi Easy Connect configurator flow
*/
public class AddDevicePreferenceController2 extends BasePreferenceController {
private static final String TAG = "AddDevicePreferenceController2";
private static final String KEY_ADD_DEVICE = "add_device_to_network";
private AccessPoint mAccessPoint;
private WifiManager mWifiManager;
public AddDevicePreferenceController2(Context context) {
super(context, KEY_ADD_DEVICE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
/**
* Initiate with an {@link AccessPoint}.
*/
public AddDevicePreferenceController2 init(AccessPoint accessPoint) {
mAccessPoint = accessPoint;
return this;
}
@Override
public int getAvailabilityStatus() {
if (WifiDppUtils.isSupportConfiguratorQrCodeScanner(mContext, mAccessPoint)) {
return AVAILABLE;
} else {
return CONDITIONALLY_UNAVAILABLE;
}
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (KEY_ADD_DEVICE.equals(preference.getKey())) {
WifiDppUtils.showLockScreen(mContext, () -> launchWifiDppConfiguratorQrCodeScanner());
return true; /* click is handled */
}
return false; /* click is not handled */
}
private void launchWifiDppConfiguratorQrCodeScanner() {
final Intent intent = WifiDppUtils.getConfiguratorQrCodeScannerIntentOrNull(mContext,
mWifiManager, mAccessPoint);
if (intent == null) {
Log.e(TAG, "Launch Wi-Fi QR code scanner with a wrong Wi-Fi network!");
} else {
mContext.startActivity(intent);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2019 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.details2;
import android.app.backup.BackupManager;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import androidx.annotation.VisibleForTesting;
import androidx.preference.DropDownPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.wifi.WifiDialog;
import com.android.settingslib.core.AbstractPreferenceController;
/**
* {@link AbstractPreferenceController} that controls whether the wifi network is metered or not
*/
public class WifiMeteredPreferenceController2 extends BasePreferenceController implements
Preference.OnPreferenceChangeListener, WifiDialog.WifiDialogListener {
private static final String KEY_WIFI_METERED = "metered";
private WifiConfiguration mWifiConfiguration;
private WifiManager mWifiManager;
private Preference mPreference;
public WifiMeteredPreferenceController2(Context context, WifiConfiguration wifiConfiguration) {
super(context, KEY_WIFI_METERED);
mWifiConfiguration = wifiConfiguration;
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
@Override
public void updateState(Preference preference) {
final DropDownPreference dropDownPreference = (DropDownPreference) preference;
final int meteredOverride = getMeteredOverride();
dropDownPreference.setValue(Integer.toString(meteredOverride));
updateSummary(dropDownPreference, meteredOverride);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mWifiConfiguration != null) {
mWifiConfiguration.meteredOverride = Integer.parseInt((String) newValue);
}
mWifiManager.updateNetwork(mWifiConfiguration);
// Stage the backup of the SettingsProvider package which backs this up
BackupManager.dataChanged("com.android.providers.settings");
updateSummary((DropDownPreference) preference, getMeteredOverride());
return true;
}
@VisibleForTesting
int getMeteredOverride() {
if (mWifiConfiguration != null) {
// Wrap the meteredOverride since robolectric cannot recognize it
return mWifiConfiguration.meteredOverride;
}
return WifiConfiguration.METERED_OVERRIDE_NONE;
}
private void updateSummary(DropDownPreference preference, int meteredOverride) {
preference.setSummary(preference.getEntries()[meteredOverride]);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void onSubmit(WifiDialog dialog) {
if (dialog.getController() != null) {
final WifiConfiguration newConfig = dialog.getController().getConfig();
if (newConfig == null || mWifiConfiguration == null) {
return;
}
if (newConfig.meteredOverride != mWifiConfiguration.meteredOverride) {
mWifiConfiguration = newConfig;
onPreferenceChange(mPreference, String.valueOf(newConfig.meteredOverride));
}
}
}
}

View File

@@ -0,0 +1,167 @@
/*
* Copyright (C) 2019 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.details2;
import static com.android.settings.wifi.WifiSettings.WIFI_DIALOG_ID;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.wifi.WifiConfigUiBase;
import com.android.settings.wifi.WifiDialog;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.wifi.AccessPoint;
import java.util.ArrayList;
import java.util.List;
/**
* Detail page for the currently connected wifi network.
*
* <p>The AccessPoint should be saved to the intent Extras when launching this class via
* {@link AccessPoint#saveWifiState(Bundle)} in order to properly render this page.
*/
public class WifiNetworkDetailsFragment2 extends DashboardFragment implements
WifiDialog.WifiDialogListener {
private static final String TAG = "WifiNetworkDetailsFrg2";
private AccessPoint mAccessPoint;
private WifiDetailPreferenceController2 mWifiDetailPreferenceController2;
private List<WifiDialog.WifiDialogListener> mWifiDialogListeners = new ArrayList<>();
@Override
public void onAttach(Context context) {
mAccessPoint = new AccessPoint(context, getArguments());
super.onAttach(context);
}
@Override
public int getMetricsCategory() {
return SettingsEnums.WIFI_NETWORK_DETAILS;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.wifi_network_details_fragment;
}
@Override
public int getDialogMetricsCategory(int dialogId) {
if (dialogId == WIFI_DIALOG_ID) {
return SettingsEnums.DIALOG_WIFI_AP_EDIT;
}
return 0;
}
@Override
public Dialog onCreateDialog(int dialogId) {
if (getActivity() == null || mWifiDetailPreferenceController2 == null
|| mAccessPoint == null) {
return null;
}
return WifiDialog.createModal(getActivity(), this, mAccessPoint,
WifiConfigUiBase.MODE_MODIFY);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.wifi_modify);
item.setIcon(com.android.internal.R.drawable.ic_mode_edit);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case Menu.FIRST:
if (!mWifiDetailPreferenceController2.canModifyNetwork()) {
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
RestrictedLockUtilsInternal.getDeviceOwner(getContext()));
} else {
showDialog(WIFI_DIALOG_ID);
}
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
mWifiDetailPreferenceController2 = WifiDetailPreferenceController2.newInstance(
mAccessPoint,
cm,
context,
this,
new Handler(Looper.getMainLooper()), // UI thread.
getSettingsLifecycle(),
context.getSystemService(WifiManager.class),
mMetricsFeatureProvider);
controllers.add(mWifiDetailPreferenceController2);
controllers.add(new AddDevicePreferenceController2(context).init(mAccessPoint));
final WifiMeteredPreferenceController2 meteredPreferenceController2 =
new WifiMeteredPreferenceController2(context, mAccessPoint.getConfig());
controllers.add(meteredPreferenceController2);
final WifiPrivacyPreferenceController2 privacyController2 =
new WifiPrivacyPreferenceController2(context);
privacyController2.setWifiConfiguration(mAccessPoint.getConfig());
privacyController2.setIsEphemeral(mAccessPoint.isEphemeral());
privacyController2.setIsPasspoint(
mAccessPoint.isPasspoint() || mAccessPoint.isPasspointConfig());
controllers.add(privacyController2);
// Sets callback listener for wifi dialog.
mWifiDialogListeners.add(mWifiDetailPreferenceController2);
mWifiDialogListeners.add(privacyController2);
mWifiDialogListeners.add(meteredPreferenceController2);
return controllers;
}
@Override
public void onSubmit(WifiDialog dialog) {
for (WifiDialog.WifiDialogListener listener : mWifiDialogListeners) {
listener.onSubmit(dialog);
}
}
}

View File

@@ -0,0 +1,162 @@
/*
* Copyright (C) 2019 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.details2;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import androidx.annotation.VisibleForTesting;
import androidx.preference.DropDownPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.wifi.WifiDialog;
import com.android.settingslib.core.AbstractPreferenceController;
/**
* {@link AbstractPreferenceController} that controls whether the wifi network is mac randomized
* or not
*/
public class WifiPrivacyPreferenceController2 extends BasePreferenceController implements
Preference.OnPreferenceChangeListener, WifiDialog.WifiDialogListener {
private static final String KEY_WIFI_PRIVACY = "privacy";
private WifiConfiguration mWifiConfiguration;
private WifiManager mWifiManager;
private boolean mIsEphemeral = false;
private boolean mIsPasspoint = false;
private Preference mPreference;
public WifiPrivacyPreferenceController2(Context context) {
super(context, KEY_WIFI_PRIVACY);
mWifiConfiguration = null;
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public void setWifiConfiguration(WifiConfiguration wifiConfiguration) {
mWifiConfiguration = wifiConfiguration;
}
public void setIsEphemeral(boolean isEphemeral) {
mIsEphemeral = isEphemeral;
}
public void setIsPasspoint(boolean isPasspoint) {
mIsPasspoint = isPasspoint;
}
@Override
public int getAvailabilityStatus() {
return mWifiManager.isConnectedMacRandomizationSupported()
? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void updateState(Preference preference) {
final DropDownPreference dropDownPreference = (DropDownPreference) preference;
final int randomizationLevel = getRandomizationValue();
dropDownPreference.setValue(Integer.toString(randomizationLevel));
updateSummary(dropDownPreference, randomizationLevel);
// Makes preference not selectable, when this is a ephemeral network.
if (mIsEphemeral || mIsPasspoint) {
preference.setSelectable(false);
dropDownPreference.setSummary(R.string.wifi_privacy_settings_ephemeral_summary);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mWifiConfiguration != null) {
mWifiConfiguration.macRandomizationSetting = Integer.parseInt((String) newValue);
mWifiManager.updateNetwork(mWifiConfiguration);
// To activate changing, we need to reconnect network. WiFi will auto connect to
// current network after disconnect(). Only needed when this is connected network.
final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
if (wifiInfo != null && wifiInfo.getNetworkId() == mWifiConfiguration.networkId) {
mWifiManager.disconnect();
}
}
updateSummary((DropDownPreference) preference, Integer.parseInt((String) newValue));
return true;
}
@VisibleForTesting
int getRandomizationValue() {
if (mWifiConfiguration != null) {
return mWifiConfiguration.macRandomizationSetting;
}
return WifiConfiguration.RANDOMIZATION_PERSISTENT;
}
private static final int PREF_RANDOMIZATION_PERSISTENT = 0;
private static final int PREF_RANDOMIZATION_NONE = 1;
/**
* Returns preference index value.
*
* @param macRandomized is mac randomized value
* @return index value of preference
*/
public static int translateMacRandomizedValueToPrefValue(int macRandomized) {
return (macRandomized == WifiConfiguration.RANDOMIZATION_PERSISTENT)
? PREF_RANDOMIZATION_PERSISTENT : PREF_RANDOMIZATION_NONE;
}
/**
* Returns mac randomized value.
*
* @param prefMacRandomized is preference index value
* @return mac randomized value
*/
public static int translatePrefValueToMacRandomizedValue(int prefMacRandomized) {
return (prefMacRandomized == PREF_RANDOMIZATION_PERSISTENT)
? WifiConfiguration.RANDOMIZATION_PERSISTENT : WifiConfiguration.RANDOMIZATION_NONE;
}
private void updateSummary(DropDownPreference preference, int macRandomized) {
// Translates value here to set RANDOMIZATION_PERSISTENT as first item in UI for better UX.
final int prefMacRandomized = translateMacRandomizedValueToPrefValue(macRandomized);
preference.setSummary(preference.getEntries()[prefMacRandomized]);
}
@Override
public void onSubmit(WifiDialog dialog) {
if (dialog.getController() != null) {
final WifiConfiguration newConfig = dialog.getController().getConfig();
if (newConfig == null || mWifiConfiguration == null) {
return;
}
if (newConfig.macRandomizationSetting != mWifiConfiguration.macRandomizationSetting) {
mWifiConfiguration = newConfig;
onPreferenceChange(mPreference, String.valueOf(newConfig.macRandomizationSetting));
}
}
}
}