Merge "Implemented nearby button that displays wifi sharing intent and long press button that opens wifi qr share fragment" am: d1b9b7f87f am: 72dad8bc9d

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Settings/+/1552081

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Iae883594bbcf485c096b77152a82154b3caafdef
This commit is contained in:
Alex Salo
2021-02-05 01:27:56 +00:00
committed by Automerger Merge Worker
5 changed files with 340 additions and 9 deletions

View File

@@ -103,6 +103,7 @@ public class WifiSettings2 extends RestrictedSettingsFragment
@VisibleForTesting
static final int MENU_ID_FORGET = Menu.FIRST + 3;
static final int MENU_ID_MODIFY = Menu.FIRST + 4;
static final int MENU_ID_SHARE = Menu.FIRST + 5;
// Max age of tracked WifiEntries
private static final long MAX_SCAN_AGE_MILLIS = 15_000;
@@ -485,7 +486,8 @@ public class WifiSettings2 extends RestrictedSettingsFragment
}
if (mSelectedWifiEntry.canDisconnect()) {
menu.add(Menu.NONE, MENU_ID_DISCONNECT, 0 /* order */,
menu.add(Menu.NONE, MENU_ID_SHARE, 0 /* order */, R.string.share);
menu.add(Menu.NONE, MENU_ID_DISCONNECT, 1 /* order */,
R.string.wifi_disconnect_button_text);
}
@@ -524,6 +526,10 @@ public class WifiSettings2 extends RestrictedSettingsFragment
case MENU_ID_FORGET:
forget(mSelectedWifiEntry);
return true;
case MENU_ID_SHARE:
WifiDppUtils.showLockScreen(getContext(),
() -> launchWifiDppConfiguratorActivity(mSelectedWifiEntry));
return true;
case MENU_ID_MODIFY:
showDialog(mSelectedWifiEntry, WifiConfigUiBase2.MODE_MODIFY);
return true;
@@ -558,6 +564,23 @@ public class WifiSettings2 extends RestrictedSettingsFragment
return true;
}
private void launchWifiDppConfiguratorActivity(WifiEntry wifiEntry) {
final Intent intent = WifiDppUtils.getConfiguratorQrCodeGeneratorIntentOrNull(getContext(),
mWifiManager, wifiEntry);
if (intent == null) {
Log.e(TAG, "Launch Wi-Fi DPP QR code generator with a wrong Wi-Fi network!");
} else {
mMetricsFeatureProvider.action(SettingsEnums.PAGE_UNKNOWN,
SettingsEnums.ACTION_SETTINGS_SHARE_WIFI_QR_CODE,
SettingsEnums.SETTINGS_WIFI_DPP_CONFIGURATOR,
/* key */ null,
/* value */ Integer.MIN_VALUE);
startActivity(intent);
}
}
private void showDialog(WifiEntry wifiEntry, int dialogMode) {
if (WifiUtils.isNetworkLockedDown(getActivity(), wifiEntry.getWifiConfiguration())
&& wifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED) {
@@ -698,8 +721,8 @@ public class WifiSettings2 extends RestrictedSettingsFragment
*/
private void updateWifiEntryPreferencesDelayed() {
// Safeguard from some delayed event handling
if (getActivity() != null && !mIsRestricted &&
mWifiPickerTracker.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
if (getActivity() != null && !mIsRestricted
&& mWifiPickerTracker.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
final View view = getView();
final Handler handler = view.getHandler();
if (handler != null && handler.hasCallbacks(mUpdateWifiEntryPreferencesRunnable)) {

View File

@@ -16,9 +16,17 @@
package com.android.settings.wifi.dpp;
import android.annotation.Nullable;
import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
@@ -27,9 +35,13 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.chooser.DisplayResolveInfo;
import com.android.internal.app.chooser.TargetInfo;
import com.android.settings.R;
import com.android.settings.wifi.qrcode.QrCodeGenerator;
@@ -45,6 +57,15 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
private ImageView mQrCodeView;
private String mQrCode;
private static final String CHIP_LABEL_METADATA_KEY = "android.service.chooser.chip_label";
private static final String CHIP_ICON_METADATA_KEY = "android.service.chooser.chip_icon";
private static final String EXTRA_WIFI_CREDENTIALS_BUNDLE =
"android.intent.extra.WIFI_CREDENTIALS_BUNDLE";
private static final String EXTRA_SSID = "android.intent.extra.SSID";
private static final String EXTRA_PASSWORD = "android.intent.extra.PASSWORD";
private static final String EXTRA_SECURITY_TYPE = "android.intent.extra.SECURITY_TYPE";
private static final String EXTRA_HIDDEN_SSID = "android.intent.extra.HIDDEN_SSID";
@Override
public int getMetricsCategory() {
return SettingsEnums.SETTINGS_WIFI_DPP_CONFIGURATOR;
@@ -56,10 +77,12 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
// 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);
if (getActivity() != null) {
if (wifiNetworkConfig.isHotspot()) {
getActivity().setTitle(R.string.wifi_dpp_share_hotspot);
} else {
getActivity().setTitle(R.string.wifi_dpp_share_wifi);
}
}
}
@@ -112,10 +135,130 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
}
}
final Intent intent = new Intent().setComponent(getNearbySharingComponent());
addActionButton(view.findViewById(R.id.wifi_dpp_layout), createNearbyButton(intent, v -> {
intent.setAction(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Bundle wifiCredentialBundle = new Bundle();
String ssid = WifiDppUtils.removeFirstAndLastDoubleQuotes(wifiNetworkConfig.getSsid());
String passwordExtra = wifiNetworkConfig.getPreSharedKey();
String securityType = wifiNetworkConfig.getSecurity();
boolean hiddenSsid = wifiNetworkConfig.getHiddenSsid();
wifiCredentialBundle.putString(EXTRA_SSID, ssid);
wifiCredentialBundle.putString(EXTRA_PASSWORD, passwordExtra);
wifiCredentialBundle.putString(EXTRA_SECURITY_TYPE, securityType);
wifiCredentialBundle.putBoolean(EXTRA_HIDDEN_SSID, hiddenSsid);
intent.putExtra(EXTRA_WIFI_CREDENTIALS_BUNDLE, wifiCredentialBundle);
startActivity(intent);
}));
mQrCode = wifiNetworkConfig.getQrCode();
setQrCode();
}
@VisibleForTesting
ComponentName getNearbySharingComponent() {
String nearbyComponent = Settings.Secure.getString(
getContext().getContentResolver(),
Settings.Secure.NEARBY_SHARING_COMPONENT);
if (TextUtils.isEmpty(nearbyComponent)) {
nearbyComponent = getString(
com.android.internal.R.string.config_defaultNearbySharingComponent);
}
if (TextUtils.isEmpty(nearbyComponent)) {
return null;
}
return ComponentName.unflattenFromString(nearbyComponent);
}
private TargetInfo getNearbySharingTarget(Intent originalIntent) {
final ComponentName cn = getNearbySharingComponent();
if (cn == null) return null;
final Intent resolveIntent = new Intent(originalIntent);
resolveIntent.setComponent(cn);
PackageManager pm = getContext().getPackageManager();
final ResolveInfo resolveInfo = pm.resolveActivity(
resolveIntent, PackageManager.GET_META_DATA);
if (resolveInfo == null || resolveInfo.activityInfo == null) {
Log.e(TAG, "Device-specified nearby sharing component (" + cn
+ ") not available");
return null;
}
// Allow the nearby sharing component to provide a more appropriate icon and label
// for the chip.
CharSequence name = null;
Drawable icon = null;
final Bundle metaData = resolveInfo.activityInfo.metaData;
if (metaData != null) {
try {
final Resources pkgRes = pm.getResourcesForActivity(cn);
final int nameResId = metaData.getInt(CHIP_LABEL_METADATA_KEY);
name = pkgRes.getString(nameResId);
final int resId = metaData.getInt(CHIP_ICON_METADATA_KEY);
icon = pkgRes.getDrawable(resId);
} catch (Resources.NotFoundException ex) {
} catch (PackageManager.NameNotFoundException ex) {
}
}
if (TextUtils.isEmpty(name)) {
name = resolveInfo.loadLabel(pm);
}
if (icon == null) {
icon = resolveInfo.loadIcon(pm);
}
final DisplayResolveInfo dri = new DisplayResolveInfo(
originalIntent, resolveInfo, name, "", resolveIntent, null);
dri.setDisplayIcon(icon);
return dri;
}
private Button createActionButton(Drawable icon, CharSequence title, View.OnClickListener r) {
final Button b = (Button) LayoutInflater.from(getContext()).inflate(
com.android.internal.R.layout.chooser_action_button, null);
if (icon != null) {
final int size = getResources()
.getDimensionPixelSize(
com.android.internal.R.dimen.chooser_action_button_icon_size);
icon.setBounds(0, 0, size, size);
b.setCompoundDrawablesRelative(icon, null, null, null);
}
b.setText(title);
b.setOnClickListener(r);
return b;
}
private void addActionButton(ViewGroup parent, Button b) {
if (b == null) return;
final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
final int gap = getResources().getDimensionPixelSize(
com.android.internal.R.dimen.resolver_icon_margin) / 2;
lp.setMarginsRelative(gap, 0, gap, 0);
parent.addView(b, lp);
}
@VisibleForTesting
@Nullable
Button createNearbyButton(Intent originalIntent, View.OnClickListener r) {
final TargetInfo ti = getNearbySharingTarget(originalIntent);
if (ti == null) return null;
final Button button = createActionButton(ti.getDisplayIcon(getContext()),
ti.getDisplayLabel(), r);
button.setAllCaps(false);
return button;
}
private void setQrCode() {
try {
final int qrcodeSize = getContext().getResources().getDimensionPixelSize(

View File

@@ -149,7 +149,7 @@ public class WifiDppUtils {
return wifiConfiguration.preSharedKey;
}
private static String removeFirstAndLastDoubleQuotes(String str) {
static String removeFirstAndLastDoubleQuotes(String str) {
if (TextUtils.isEmpty(str)) {
return str;
}