Merge changes I54a3f033,I2e894501

* changes:
  Fix [a11y] Wi-Fi Easy Connect Talkback issues
  Support to share Wi-Fi hotspot via QR code
This commit is contained in:
TreeHugger Robot
2019-03-12 00:42:40 +00:00
committed by Android (Google) Code Review
22 changed files with 465 additions and 135 deletions

View File

@@ -23,5 +23,4 @@
android:minHeight="@dimen/min_tap_target_size"
android:layout_gravity="center"
android:background="@null"
android:visibility="gone"
android:contentDescription="@string/wifi_add_network" />
android:visibility="gone"/>

View File

@@ -71,7 +71,7 @@
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/ic_scan_24dp"
android:contentDescription="@string/wifi_add_network" />
android:contentDescription="@string/wifi_dpp_scan_qr_code"/>
</RelativeLayout>
<LinearLayout android:id="@+id/ssid_too_long_warning"
@@ -307,7 +307,7 @@
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/ic_scan_24dp"
android:contentDescription="@string/wifi_add_network" />
android:contentDescription="@string/wifi_dpp_scan_qr_code"/>
</RelativeLayout>
</LinearLayout>

View File

@@ -40,6 +40,14 @@
android:src="@drawable/ic_devices_check_circle_green"
android:scaleType="fitCenter"/>
<!-- Add title_summary_container to group content for Talkback -->
<LinearLayout
android:id="@+id/title_summary_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true">
<TextView
android:id="@android:id/title"
style="@style/TextAppearance.EntityHeaderTitle"
@@ -62,5 +70,5 @@
android:layout_marginTop="2dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"/>
</LinearLayout>
</LinearLayout>

View File

@@ -2157,6 +2157,10 @@
<string name="wifi_dpp_sharing_wifi_with_this_device">Sharing Wi\u2011Fi with this device\u2026</string>
<!-- Hint for Wi-Fi DPP handshake running [CHAR LIMIT=NONE] -->
<string name="wifi_dpp_connecting">Connecting\u2026</string>
<!-- Title for the fragment to show that the QR code is for sharing Wi-Fi hotspot network [CHAR LIMIT=50] -->
<string name="wifi_dpp_share_hotspot">Share hotspot</string>
<!-- Hint for the user to share Wi-Fi hotspot network [CHAR LIMIT=NONE] -->
<string name="wifi_dpp_scan_qr_code_to_share_hotspot">Scan this QR code with another device to join hotspot \u201c<xliff:g id="ssid" example="OfficeWifi">%1$s</xliff:g>\u201d</string>
<!-- Label for the try again button [CHAR LIMIT=20]-->
<string name="retry">Retry</string>
<!-- Label for the check box to share a network with other users on the same device -->

View File

@@ -23,7 +23,7 @@
settings:searchable="false"
settings:initialExpandedChildrenCount="3">
<com.android.settings.widget.ValidatedEditTextPreference
<com.android.settings.wifi.tether.WifiTetherSsidPreference
android:key="wifi_tether_network_name"
android:title="@string/wifi_hotspot_name_title"
android:summary="@string/summary_placeholder"/>

View File

@@ -49,6 +49,7 @@ public class ButtonPreference extends Preference {
private ImageButton mImageButton;
private Drawable mButtonIcon;
private View.OnClickListener mClickListener;
private String mContentDescription;
// Used for dummy pref.
public ButtonPreference(Context context, AttributeSet attrs) {
@@ -57,6 +58,7 @@ public class ButtonPreference extends Preference {
mImageButton = null;
mButtonIcon = null;
mClickListener = null;
mContentDescription = null;
}
public ButtonPreference(Context context) {
@@ -83,6 +85,7 @@ public class ButtonPreference extends Preference {
if (mImageButton != null) {
mImageButton.setImageDrawable(mButtonIcon);
mImageButton.setOnClickListener(mClickListener);
mImageButton.setContentDescription(mContentDescription);
}
setButtonVisibility();
}
@@ -96,9 +99,9 @@ public class ButtonPreference extends Preference {
/**
* Sets the drawable to be displayed in button.
*/
public ButtonPreference setButtonIcon(@DrawableRes int iconResId) {
public void setButtonIcon(@DrawableRes int iconResId) {
if (iconResId == 0) {
return this;
return;
}
try {
@@ -107,17 +110,26 @@ public class ButtonPreference extends Preference {
} catch (Resources.NotFoundException exception) {
Log.e(TAG, "Resource does not exist: " + iconResId);
}
return this;
}
/**
* Register a callback to be invoked when button is clicked.
*/
public ButtonPreference setButtonOnClickListener(View.OnClickListener listener) {
public void setButtonOnClickListener(View.OnClickListener listener) {
if (listener != mClickListener) {
mClickListener = listener;
notifyChanged();
}
return this;
}
/**
* A content description briefly describes the button and is primarily used for accessibility
* support to determine how a button should be presented to the user.
*/
public void setButtonContentDescription(String contentDescription) {
if (contentDescription != mContentDescription) {
mContentDescription = contentDescription;
notifyChanged();
}
}
}

View File

@@ -247,6 +247,7 @@ public class WifiSettings extends RestrictedSettingsFragment
getContext().startActivity(
WifiDppUtils.getEnrolleeQrCodeScannerIntent(/* ssid */ null));
});
mAddPreference.setButtonContentDescription(getString(R.string.wifi_dpp_scan_qr_code));
mStatusMessagePreference = (LinkablePreference) findPreference(PREF_KEY_STATUS_MESSAGE);
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());

View File

@@ -21,16 +21,12 @@ import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.hardware.biometrics.BiometricPrompt;
import android.os.CancellationSignal;
import android.os.Looper;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.LinkAddress;
@@ -619,48 +615,16 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
* Share the wifi network with QR code.
*/
private void shareNetwork() {
final KeyguardManager keyguardManager = (KeyguardManager) mContext.getSystemService(
Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardSecure()) {
// Show authentication screen to confirm credentials (pin, pattern or password) for
// the current user of the device.
final String title = mContext.getString(
R.string.lockpassword_confirm_your_pattern_header);
final String description = String.format(
mContext.getString(R.string.wifi_sharing_message),
mAccessPoint.getSsidStr());
final BiometricPrompt.Builder builder = new BiometricPrompt.Builder(mContext)
.setTitle(title)
.setDescription(description);
if (keyguardManager.isDeviceSecure()) {
builder.setDeviceCredentialAllowed(true);
WifiDppUtils.showLockScreen(mContext, title, description,
() -> launchWifiDppConfiguratorActivity());
}
final BiometricPrompt bp = builder.build();
final Handler handler = new Handler(Looper.getMainLooper());
bp.authenticate(new CancellationSignal(),
runnable -> handler.post(runnable),
mAuthenticationCallback);
} else {
launchWifiDppConfiguratorActivity();
}
}
private BiometricPrompt.AuthenticationCallback mAuthenticationCallback =
new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
launchWifiDppConfiguratorActivity();
}
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
//Do nothing
}
};
/**
* Sign in to the captive portal found on this wifi network associated with this preference.
*/

View File

@@ -29,6 +29,7 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
@@ -99,6 +100,7 @@ public class WifiDppAddDeviceFragment extends WifiDppQrCodeBaseFragment {
if (!isConfigurationChange) {
mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_SUCCESS;
changeFocusAndAnnounceChange(mButtonRight, mTitle);
}
}
@@ -168,15 +170,17 @@ public class WifiDppAddDeviceFragment extends WifiDppQrCodeBaseFragment {
mButtonLeft.setVisibility(View.INVISIBLE);
}
if (!isConfigurationChange) {
mLatestStatusCode = code;
}
if (isGoingInitiator()) {
mSummary.setText(R.string.wifi_dpp_sharing_wifi_with_this_device);
}
mProgressBar.setVisibility(isGoingInitiator() ? View.VISIBLE : View.INVISIBLE);
mButtonRight.setVisibility(isGoingInitiator() ? View.INVISIBLE : View.VISIBLE);
if (!isConfigurationChange) {
mLatestStatusCode = code;
changeFocusAndAnnounceChange(mButtonRight, mSummary);
}
}
private boolean hasRetryButton(int code) {
@@ -277,6 +281,7 @@ public class WifiDppAddDeviceFragment extends WifiDppQrCodeBaseFragment {
mButtonRight.setVisibility(View.INVISIBLE);
startWifiDppConfiguratorInitiator();
updateSummary();
mTitleSummaryContainer.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
});
if (savedInstanceState != null) {
@@ -288,6 +293,8 @@ public class WifiDppAddDeviceFragment extends WifiDppQrCodeBaseFragment {
} else {
showErrorUi(mLatestStatusCode, /* isConfigurationChange */ true);
}
} else {
changeFocusAndAnnounceChange(mButtonRight, mTitleSummaryContainer);
}
}
@@ -354,4 +361,17 @@ public class WifiDppAddDeviceFragment extends WifiDppQrCodeBaseFragment {
mSummary.setText(getString(R.string.wifi_dpp_add_device_to_wifi, getSsid()));
}
}
/**
* This fragment will change UI display and text messages for events. To improve Talkback user
* experienience, using this method to focus on a right component and announce a changed text
* after an UI changing event.
*
* @param focusView The UI component which will be focused
* @param announceView The UI component's text will be talked
*/
private void changeFocusAndAnnounceChange(View focusView, View announceView) {
focusView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
announceView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
}
}

View File

@@ -23,6 +23,7 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Button;
import android.widget.ListView;
@@ -105,5 +106,11 @@ public class WifiDppChooseSavedWifiNetworkFragment extends WifiDppQrCodeBaseFrag
mButtonRight = view.findViewById(R.id.button_right);
mButtonRight.setVisibility(View.GONE);
if (savedInstanceState == null) {
// For Talkback to describe this fragment
mTitleSummaryContainer.sendAccessibilityEvent(
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
}
}
}

View File

@@ -75,6 +75,7 @@ public class WifiDppConfiguratorActivity extends InstrumentedActivity implements
private static final String KEY_WIFI_PRESHARED_KEY = "key_wifi_preshared_key";
private static final String KEY_WIFI_HIDDEN_SSID = "key_wifi_hidden_ssid";
private static final String KEY_WIFI_NETWORK_ID = "key_wifi_network_id";
private static final String KEY_IS_HOTSPOT = "key_is_hotspot";
private FragmentManager mFragmentManager;
@@ -104,14 +105,15 @@ public class WifiDppConfiguratorActivity extends InstrumentedActivity implements
mWifiDppQrCode = WifiQrCode.getValidWifiDppQrCodeOrNull(qrCode);
String security = savedInstanceState.getString(KEY_WIFI_SECURITY);
String ssid = savedInstanceState.getString(KEY_WIFI_SSID);
String preSharedKey = savedInstanceState.getString(KEY_WIFI_PRESHARED_KEY);
boolean hiddenSsid = savedInstanceState.getBoolean(KEY_WIFI_HIDDEN_SSID);
int networkId = savedInstanceState.getInt(KEY_WIFI_NETWORK_ID);
final String security = savedInstanceState.getString(KEY_WIFI_SECURITY);
final String ssid = savedInstanceState.getString(KEY_WIFI_SSID);
final String preSharedKey = savedInstanceState.getString(KEY_WIFI_PRESHARED_KEY);
final boolean hiddenSsid = savedInstanceState.getBoolean(KEY_WIFI_HIDDEN_SSID);
final int networkId = savedInstanceState.getInt(KEY_WIFI_NETWORK_ID);
final boolean isHotspot = savedInstanceState.getBoolean(KEY_IS_HOTSPOT);
mWifiNetworkConfig = WifiNetworkConfig.getValidConfigOrNull(security, ssid,
preSharedKey, hiddenSsid, networkId);
preSharedKey, hiddenSsid, networkId, isHotspot);
} else {
handleIntent(getIntent());
}
@@ -361,6 +363,7 @@ public class WifiDppConfiguratorActivity extends InstrumentedActivity implements
outState.putString(KEY_WIFI_PRESHARED_KEY, mWifiNetworkConfig.getPreSharedKey());
outState.putBoolean(KEY_WIFI_HIDDEN_SSID, mWifiNetworkConfig.getHiddenSsid());
outState.putInt(KEY_WIFI_NETWORK_ID, mWifiNetworkConfig.getNetworkId());
outState.putBoolean(KEY_IS_HOTSPOT, mWifiNetworkConfig.isHotspot());
}
super.onSaveInstanceState(outState);
@@ -393,7 +396,8 @@ public class WifiDppConfiguratorActivity extends InstrumentedActivity implements
wifiConfiguration.getPrintableSsid(),
wifiConfiguration.preSharedKey,
/* hiddenSsid */ false,
wifiConfiguration.networkId);
wifiConfiguration.networkId,
/* isHotspot */ false);
}
}

View File

@@ -38,6 +38,7 @@ public abstract class WifiDppQrCodeBaseFragment extends InstrumentedFragment {
private ImageView mDevicesCheckCircleGreenHeaderIcon;
protected TextView mTitle;
protected TextView mSummary;
protected View mTitleSummaryContainer;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
@@ -48,6 +49,10 @@ public abstract class WifiDppQrCodeBaseFragment extends InstrumentedFragment {
view.findViewById(R.id.devices_check_circle_green_icon);
mTitle = view.findViewById(android.R.id.title);
mSummary = view.findViewById(android.R.id.summary);
// This is the LinearLayout which groups mTitle and mSummary for Talkback to announce the
// content in a way that reflects its natural groupings.
mTitleSummaryContainer = view.findViewById(R.id.title_summary_container);
}
protected void setHeaderIconImageResource(int resId) {

View File

@@ -60,6 +60,14 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// setTitle for Talkback
final WifiNetworkConfig wifiNetworkConfig = getWifiNetworkConfigFromHostActivity();
if (wifiNetworkConfig.isHotspot()) {
getActivity().setTitle(R.string.wifi_dpp_share_hotspot);
} else {
getActivity().setTitle(R.string.wifi_dpp_share_wifi);
}
setHasOptionsMenu(true);
final ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null) {
@@ -86,7 +94,8 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
final WifiNetworkConfig wifiNetworkConfig = getWifiNetworkConfigFromHostActivity();
MenuItem menuItem;
if (wifiNetworkConfig.isSupportWifiDpp(getActivity())) {
if (!wifiNetworkConfig.isHotspot() &&
wifiNetworkConfig.isSupportWifiDpp(getActivity())) {
menuItem = menu.add(0, Menu.FIRST, 0, R.string.next_label);
menuItem.setIcon(R.drawable.ic_scan_24dp);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
@@ -127,9 +136,15 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
setHeaderIconImageResource(R.drawable.ic_qrcode_24dp);
final WifiNetworkConfig wifiNetworkConfig = getWifiNetworkConfigFromHostActivity();
if (wifiNetworkConfig.isHotspot()) {
mTitle.setText(R.string.wifi_dpp_share_hotspot);
mSummary.setText(getString(R.string.wifi_dpp_scan_qr_code_to_share_hotspot,
wifiNetworkConfig.getSsid()));
} else {
mTitle.setText(R.string.wifi_dpp_share_wifi);
mSummary.setText(getString(R.string.wifi_dpp_scan_qr_code_with_another_device,
wifiNetworkConfig.getSsid()));
}
mQrCode = wifiNetworkConfig.getQrCode();
setQrCode();

View File

@@ -41,6 +41,7 @@ import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.ProgressBar;
import android.widget.TextView;
@@ -174,6 +175,13 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// setTitle for Talkback
if (mIsConfiguratorMode) {
getActivity().setTitle(R.string.wifi_dpp_add_device_to_network);
} else {
getActivity().setTitle(R.string.wifi_dpp_scan_qr_code);
}
final ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
@@ -380,6 +388,7 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
public void showErrorMessage(String message) {
mErrorMessage.setVisibility(View.VISIBLE);
mErrorMessage.setText(message);
mErrorMessage.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
mHandler.removeMessages(MESSAGE_HIDE_ERROR_MESSAGE);
mHandler.sendEmptyMessageDelayed(MESSAGE_HIDE_ERROR_MESSAGE,
@@ -410,6 +419,8 @@ public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment impl
mProgressBar.setVisibility(View.VISIBLE);
startWifiDppEnrolleeInitiator((WifiQrCode)msg.obj);
updateEnrolleeSummary();
mSummary.sendAccessibilityEvent(
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
}
break;

View File

@@ -16,11 +16,16 @@
package com.android.settings.wifi.dpp;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.hardware.biometrics.BiometricPrompt;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
@@ -70,6 +75,9 @@ public class WifiDppUtils {
/** The data corresponding to {@code WifiConfiguration} networkId */
public static final String EXTRA_WIFI_NETWORK_ID = "networkId";
/** The data to recognize if it's a Wi-Fi hotspot for configuration */
public static final String EXTRA_IS_HOTSPOT = "isHotspot";
/** Used by {@link android.provider.Settings#ACTION_PROCESS_WIFI_EASY_CONNECT_URI} to
* indicate test mode UI should be shown. Test UI does not make API calls. Value is a boolean.*/
public static final String EXTRA_TEST = "test";
@@ -142,24 +150,12 @@ public class WifiDppUtils {
return str.substring(begin, end+1);
}
private static String getSecurityString(AccessPoint accessPoint) {
switch(accessPoint.getSecurity()) {
case AccessPoint.SECURITY_WEP:
return WifiQrCode.SECURITY_WEP;
case AccessPoint.SECURITY_PSK:
return WifiQrCode.SECURITY_WPA_PSK;
case AccessPoint.SECURITY_SAE:
return WifiQrCode.SECURITY_SAE;
default:
return WifiQrCode.SECURITY_NO_PASSWORD;
}
}
static String getSecurityString(WifiConfiguration config) {
if (config.allowedKeyManagement.get(KeyMgmt.SAE)) {
return WifiQrCode.SECURITY_SAE;
}
if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ||
config.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) {
return WifiQrCode.SECURITY_WPA_PSK;
}
return (config.wepKeys[0] == null) ?
@@ -171,6 +167,9 @@ public class WifiDppUtils {
* security. It may return null if the security is not supported by QR code generator nor
* scanner.
*
* Do not use this method for Wi-Fi hotspot network, use
* {@code getHotspotConfiguratorIntentOrNull} instead.
*
* @param context The context to use for the content resolver
* @param wifiManager An instance of {@link WifiManager}
* @param accessPoint An instance of {@link AccessPoint}
@@ -187,15 +186,63 @@ public class WifiDppUtils {
return null;
}
final WifiConfiguration wifiConfig = accessPoint.getConfig();
final String ssid = removeFirstAndLastDoubleQuotes(wifiConfig.SSID);
final String security = getSecurityString(accessPoint);
String preSharedKey = wifiConfig.preSharedKey;
final WifiConfiguration wifiConfiguration = accessPoint.getConfig();
setConfiguratorIntentExtra(intent, wifiManager, wifiConfiguration);
if (wifiConfiguration.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
throw new IllegalArgumentException("Invalid network ID");
} else {
intent.putExtra(EXTRA_WIFI_NETWORK_ID, wifiConfiguration.networkId);
}
return intent;
}
/**
* Returns an intent to launch QR code generator for the Wi-Fi hotspot. It may return null if
* the security is not supported by QR code generator.
*
* @param context The context to use for the content resolver
* @param wifiManager An instance of {@link WifiManager}
* @param wifiConfiguration {@link WifiConfiguration} of the Wi-Fi hotspot
* @return Intent for launching QR code generator
*/
public static Intent getHotspotConfiguratorIntentOrNull(Context context,
WifiManager wifiManager, WifiConfiguration wifiConfiguration) {
final Intent intent = new Intent(context, WifiDppConfiguratorActivity.class);
if (isSupportHotspotConfiguratorQrCodeGenerator(wifiConfiguration)) {
intent.setAction(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR);
} else {
return null;
}
setConfiguratorIntentExtra(intent, wifiManager, wifiConfiguration);
intent.putExtra(EXTRA_WIFI_NETWORK_ID, WifiConfiguration.INVALID_NETWORK_ID);
intent.putExtra(EXTRA_IS_HOTSPOT, true);
return intent;
}
/**
* Set all extra except {@code EXTRA_WIFI_NETWORK_ID} for the intent to
* launch configurator activity later.
*
* @param intent the target to set extra
* @param wifiManager an instance of {@code WifiManager}
* @param wifiConfiguration the Wi-Fi network for launching configurator activity
*/
private static void setConfiguratorIntentExtra(Intent intent, WifiManager wifiManager,
WifiConfiguration wifiConfiguration) {
final String ssid = removeFirstAndLastDoubleQuotes(wifiConfiguration.SSID);
final String security = getSecurityString(wifiConfiguration);
String preSharedKey = wifiConfiguration.preSharedKey;
if (preSharedKey != null) {
// When the value of this key is read, the actual key is not returned, just a "*".
// Call privileged system API to obtain actual key.
preSharedKey = removeFirstAndLastDoubleQuotes(getPresharedKey(wifiManager, wifiConfig));
preSharedKey = removeFirstAndLastDoubleQuotes(getPresharedKey(wifiManager,
wifiConfiguration));
}
if (!TextUtils.isEmpty(ssid)) {
@@ -207,13 +254,6 @@ public class WifiDppUtils {
if (!TextUtils.isEmpty(preSharedKey)) {
intent.putExtra(EXTRA_WIFI_PRE_SHARED_KEY, preSharedKey);
}
if (wifiConfig.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
throw new IllegalArgumentException("Invalid network ID");
} else {
intent.putExtra(EXTRA_WIFI_NETWORK_ID, wifiConfig.networkId);
}
return intent;
}
/**
@@ -228,6 +268,54 @@ public class WifiDppUtils {
isSupportConfiguratorQrCodeGenerator(accessPoint);
}
/**
* Shows authentication screen to confirm credentials (pin, pattern or password) for the current
* user of the device.
*
* @param context The {@code Context} used to get {@code KeyguardManager} service
* @param title The title on lock screen
* @param description The description on lock screen
* @param successRunnable The {@code Runnable} which will be executed if the user does not setup
* device security or if lock screen is unlocked
*/
public static void showLockScreen(Context context, String title, String description,
Runnable successRunnable) {
final KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(
Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardSecure()) {
final BiometricPrompt.AuthenticationCallback authenticationCallback =
new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(
BiometricPrompt.AuthenticationResult result) {
successRunnable.run();
}
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
//Do nothing
}
};
final BiometricPrompt.Builder builder = new BiometricPrompt.Builder(context)
.setTitle(title)
.setDescription(description);
if (keyguardManager.isDeviceSecure()) {
builder.setDeviceCredentialAllowed(true);
}
final BiometricPrompt bp = builder.build();
final Handler handler = new Handler(Looper.getMainLooper());
bp.authenticate(new CancellationSignal(),
runnable -> handler.post(runnable),
authenticationCallback);
} else {
successRunnable.run();
}
}
private static boolean isSupportConfiguratorQrCodeScanner(Context context,
AccessPoint accessPoint) {
if (!isWifiDppEnabled(context)) {
@@ -254,4 +342,13 @@ public class WifiDppUtils {
return false;
}
private static boolean isSupportHotspotConfiguratorQrCodeGenerator(
WifiConfiguration wifiConfiguration) {
// QR code generator produces QR code with ZXing's Wi-Fi network config format,
// it supports PSK and WEP and non security
// KeyMgmt.NONE is for WEP or non security
return wifiConfiguration.allowedKeyManagement.get(KeyMgmt.WPA2_PSK) ||
wifiConfiguration.allowedKeyManagement.get(KeyMgmt.NONE);
}
}

View File

@@ -30,7 +30,6 @@ import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Keep;
import androidx.annotation.VisibleForTesting;
/**
@@ -52,15 +51,17 @@ public class WifiNetworkConfig {
private String mPreSharedKey;
private boolean mHiddenSsid;
private int mNetworkId;
private boolean mIsHotspot;
@VisibleForTesting
WifiNetworkConfig(String security, String ssid, String preSharedKey,
boolean hiddenSsid, int networkId) {
boolean hiddenSsid, int networkId, boolean isHotspot) {
mSecurity = security;
mSsid = ssid;
mPreSharedKey = preSharedKey;
mHiddenSsid = hiddenSsid;
mNetworkId = networkId;
mIsHotspot = isHotspot;
}
public WifiNetworkConfig(WifiNetworkConfig config) {
@@ -69,6 +70,7 @@ public class WifiNetworkConfig {
mPreSharedKey = config.mPreSharedKey;
mHiddenSsid = config.mHiddenSsid;
mNetworkId = config.mNetworkId;
mIsHotspot = config.mIsHotspot;
}
/**
@@ -86,23 +88,26 @@ public class WifiNetworkConfig {
* android.settings.WIFI_DPP_CONFIGURATOR_QR_CODE_SCANNER
*/
public static WifiNetworkConfig getValidConfigOrNull(Intent intent) {
String security = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SECURITY);
String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID);
String preSharedKey = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_PRE_SHARED_KEY);
boolean hiddenSsid = intent.getBooleanExtra(WifiDppUtils.EXTRA_WIFI_HIDDEN_SSID, false);
int networkId = intent.getIntExtra(WifiDppUtils.EXTRA_WIFI_NETWORK_ID,
final String security = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SECURITY);
final String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID);
final String preSharedKey = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_PRE_SHARED_KEY);
final boolean hiddenSsid = intent.getBooleanExtra(WifiDppUtils.EXTRA_WIFI_HIDDEN_SSID,
false);
final int networkId = intent.getIntExtra(WifiDppUtils.EXTRA_WIFI_NETWORK_ID,
WifiConfiguration.INVALID_NETWORK_ID);
final boolean isHotspot = intent.getBooleanExtra(WifiDppUtils.EXTRA_IS_HOTSPOT, false);
return getValidConfigOrNull(security, ssid, preSharedKey, hiddenSsid, networkId);
return getValidConfigOrNull(security, ssid, preSharedKey, hiddenSsid, networkId, isHotspot);
}
public static WifiNetworkConfig getValidConfigOrNull(String security, String ssid,
String preSharedKey, boolean hiddenSsid, int networkId) {
String preSharedKey, boolean hiddenSsid, int networkId, boolean isHotspot) {
if (!isValidConfig(security, ssid, preSharedKey, hiddenSsid)) {
return null;
}
return new WifiNetworkConfig(security, ssid, preSharedKey, hiddenSsid, networkId);
return new WifiNetworkConfig(security, ssid, preSharedKey, hiddenSsid, networkId,
isHotspot);
}
public static boolean isValidConfig(WifiNetworkConfig config) {
@@ -174,31 +179,30 @@ public class WifiNetworkConfig {
return barcode;
}
@Keep
public String getSecurity() {
return mSecurity;
}
@Keep
public String getSsid() {
return mSsid;
}
@Keep
public String getPreSharedKey() {
return mPreSharedKey;
}
@Keep
public boolean getHiddenSsid() {
return mHiddenSsid;
}
@Keep
public int getNetworkId() {
return mNetworkId;
}
public boolean isHotspot() {
return mIsHotspot;
}
public void connect(Context context, WifiManager.ActionListener listener) {
WifiConfiguration wifiConfiguration = getWifiConfigurationOrNull();
if (wifiConfiguration == null) {

View File

@@ -218,7 +218,7 @@ public class WifiNetworkListFragment extends SettingsPreferenceFragment implemen
final WifiNetworkConfig networkConfig = WifiNetworkConfig.getValidConfigOrNull(
selectedAccessPoint.getSecurityString(/* concise */ true),
wifiConfig.getPrintableSsid(), wifiConfig.preSharedKey, /* hiddenSsid */ false,
wifiConfig.networkId);
wifiConfig.networkId, /* isHotspot */ false);
if (mOnChooseNetworkListener != null) {
mOnChooseNetworkListener.onChooseNetwork(networkConfig);
}
@@ -232,7 +232,8 @@ public class WifiNetworkListFragment extends SettingsPreferenceFragment implemen
/* ssid */ WifiNetworkConfig.FAKE_SSID,
/* preSharedKey */ WifiNetworkConfig.FAKE_PASSWORD,
/* hiddenSsid */ true,
/* networkId */ WifiConfiguration.INVALID_NETWORK_ID));
/* networkId */ WifiConfiguration.INVALID_NETWORK_ID,
/* isHotspot*/ false));
}
} else {
return super.onPreferenceTreeClick(preference);

View File

@@ -133,7 +133,7 @@ public class WifiQrCode {
password = removeBackSlash(password);
mWifiNetworkConfig = WifiNetworkConfig.getValidConfigOrNull(security, ssid, password,
hiddenSsid, WifiConfiguration.INVALID_NETWORK_ID);
hiddenSsid, WifiConfiguration.INVALID_NETWORK_ID, /* isHotspot */ false);
if (mWifiNetworkConfig == null) {
throw new IllegalArgumentException("Invalid format");

View File

@@ -17,13 +17,18 @@
package com.android.settings.wifi.tether;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.util.Log;
import android.view.View;
import androidx.annotation.VisibleForTesting;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settings.wifi.dpp.WifiDppUtils;
public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController
implements ValidatedEditTextPreference.Validator {
@@ -56,6 +61,23 @@ public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreference
mSSID = DEFAULT_SSID;
}
((ValidatedEditTextPreference) mPreference).setValidator(this);
if (mWifiManager.isWifiApEnabled() && config != null) {
final Intent intent = WifiDppUtils.getHotspotConfiguratorIntentOrNull(mContext,
mWifiManager, config);
if (intent == null) {
Log.e(TAG, "Invalid security to share hotspot");
((WifiTetherSsidPreference) mPreference).setButtonVisible(false);
} else {
((WifiTetherSsidPreference) mPreference).setButtonOnClickListener(
view -> shareHotspotNetwork(intent));
((WifiTetherSsidPreference) mPreference).setButtonVisible(true);
}
} else {
((WifiTetherSsidPreference) mPreference).setButtonVisible(false);
}
updateSsidDisplay((EditTextPreference) mPreference);
}
@@ -80,4 +102,19 @@ public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreference
preference.setText(mSSID);
preference.setSummary(mSSID);
}
private void shareHotspotNetwork(Intent intent) {
final String title = mContext.getString(
R.string.lockpassword_confirm_your_pattern_header);
final String description = String.format(
mContext.getString(R.string.wifi_sharing_message), mSSID);
WifiDppUtils.showLockScreen(mContext, title, description,
() -> mContext.startActivity(intent));
}
@VisibleForTesting
boolean isQrCodeButtonAvailable() {
return ((WifiTetherSsidPreference) mPreference).isQrCodeButtonAvailable();
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.tether;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import androidx.annotation.DrawableRes;
import androidx.preference.PreferenceViewHolder;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.widget.ValidatedEditTextPreference;
/**
* Support a QR code share button for {@code EditTextPreference} that supports input validation.
*/
public class WifiTetherSsidPreference extends ValidatedEditTextPreference {
private static final String TAG = "WifiTetherSsidPreference";
private ImageButton mImageButton;
private Drawable mButtonIcon;
private View.OnClickListener mClickListener;
private boolean mVisible;
public WifiTetherSsidPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize();
}
public WifiTetherSsidPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
public WifiTetherSsidPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public WifiTetherSsidPreference(Context context) {
super(context);
initialize();
}
private void initialize() {
setWidgetLayoutResource(R.layout.wifi_button_preference_widget);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
if (mImageButton == null) {
mImageButton = (ImageButton) holder.findViewById(R.id.button_icon);
mImageButton.setContentDescription(
getContext().getString(R.string.wifi_dpp_share_hotspot));
setButtonIcon(R.drawable.ic_qrcode_24dp);
mImageButton.setImageDrawable(mButtonIcon);
}
if (mVisible) {
mImageButton.setOnClickListener(mClickListener);
mImageButton.setVisibility(View.VISIBLE);
} else {
mImageButton.setVisibility(View.GONE);
}
}
public void setButtonOnClickListener(View.OnClickListener listener) {
mClickListener = listener;
}
public void setButtonVisible(boolean visible) {
mVisible = visible;
}
private void setButtonIcon(@DrawableRes int iconResId) {
try {
mButtonIcon = getContext().getDrawable(iconResId);
} catch (Resources.NotFoundException exception) {
Log.e(TAG, "Resource does not exist: " + iconResId);
}
}
@VisibleForTesting
boolean isQrCodeButtonAvailable() {
return mVisible && mClickListener != null;
}
}

View File

@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager;
import androidx.preference.PreferenceScreen;
@@ -56,12 +57,12 @@ public class WifiTetherSSIDPreferenceControllerTest {
private PreferenceScreen mScreen;
private WifiTetherSSIDPreferenceController mController;
private ValidatedEditTextPreference mPreference;
private WifiTetherSsidPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application);
mPreference = new WifiTetherSsidPreference(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
@@ -121,4 +122,28 @@ public class WifiTetherSSIDPreferenceControllerTest {
assertThat(mController.getSSID()).isEqualTo(config.SSID);
assertThat(mPreference.getSummary()).isEqualTo(config.SSID);
}
@Test
public void displayPreference_wifiApDisabled_shouldHideQrCodeIcon() {
when(mWifiManager.isWifiApEnabled()).thenReturn(false);
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen);
assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(false);
}
@Test
public void displayPreference_wifiApEnabled_shouldShowQrCodeIcon() {
when(mWifiManager.isWifiApEnabled()).thenReturn(true);
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen);
assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(true);
}
}

View File

@@ -147,7 +147,7 @@ public class WifiDppConfiguratorActivityTest {
@Test
public void rotateScreen_shouldGetCorrectWifiNetworkConfig() {
final WifiNetworkConfig wifiNetworkConfig = new WifiNetworkConfig("WPA", "WifiSsid",
"password", /* hiddenSsid */ false, /* networkId */ 0);
"password", /* hiddenSsid */ false, /* networkId */ 0, /* isHotspot */ true);
final Intent intent = new Intent(Settings.ACTION_PROCESS_WIFI_EASY_CONNECT_URI);
intent.setData(Uri.parse(VALID_WIFI_DPP_QR_CODE));
@@ -173,5 +173,6 @@ public class WifiDppConfiguratorActivityTest {
assertThat(restoredWifiNetworkConfig.getPreSharedKey()).isEqualTo("password");
assertThat(restoredWifiNetworkConfig.getHiddenSsid()).isFalse();
assertThat(restoredWifiNetworkConfig.getNetworkId()).isEqualTo(0);
assertThat(restoredWifiNetworkConfig.isHotspot()).isTrue();
}
}