Merge "NFC support in work profile" am: c2647199ce am: 79675e89f2

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

Change-Id: I6526b8a3f83c72870a93777d37242f1fb191568f
This commit is contained in:
Jack Yu
2021-11-22 11:12:20 +00:00
committed by Automerger Merge Worker
3 changed files with 134 additions and 51 deletions

View File

@@ -20,6 +20,7 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.UserManager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@@ -127,7 +128,10 @@ public class NfcPaymentPreferenceController extends BasePreferenceController imp
public CharSequence getSummary() { public CharSequence getSummary() {
final PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp(); final PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
if (defaultApp != null) { if (defaultApp != null) {
return defaultApp.label; UserManager um = mContext.createContextAsUser(
defaultApp.userHandle, /*flags=*/0).getSystemService(UserManager.class);
return defaultApp.label + " (" + um.getUserName() + ")";
} else { } else {
return mContext.getText(R.string.nfc_payment_default_not_set); return mContext.getText(R.string.nfc_payment_default_not_set);
} }
@@ -218,12 +222,15 @@ public class NfcPaymentPreferenceController extends BasePreferenceController imp
} }
// Prevent checked callback getting called on recycled views // Prevent checked callback getting called on recycled views
UserManager um = mContext.createContextAsUser(
appInfo.userHandle, /*flags=*/0).getSystemService(UserManager.class);
holder.radioButton.setOnCheckedChangeListener(null); holder.radioButton.setOnCheckedChangeListener(null);
holder.radioButton.setChecked(appInfo.isDefault); holder.radioButton.setChecked(appInfo.isDefault);
holder.radioButton.setContentDescription(appInfo.label); holder.radioButton.setContentDescription(appInfo.label + " (" + um.getUserName() + ")");
holder.radioButton.setOnCheckedChangeListener(this); holder.radioButton.setOnCheckedChangeListener(this);
holder.radioButton.setTag(appInfo); holder.radioButton.setTag(appInfo);
holder.radioButton.setText(appInfo.label); holder.radioButton.setText(appInfo.label + " (" + um.getUserName() + ")");
return convertView; return convertView;
} }
@@ -245,7 +252,8 @@ public class NfcPaymentPreferenceController extends BasePreferenceController imp
private void makeDefault(PaymentAppInfo appInfo) { private void makeDefault(PaymentAppInfo appInfo) {
if (!appInfo.isDefault) { if (!appInfo.isDefault) {
mPaymentBackend.setDefaultPaymentApp(appInfo.componentName); mPaymentBackend.setDefaultPaymentApp(appInfo.componentName,
appInfo.userHandle.getIdentifier());
} }
final Dialog dialog = mPreference.getDialog(); final Dialog dialog = mPreference.getDialog();
if (dialog != null) { if (dialog != null) {

View File

@@ -16,11 +16,10 @@
package com.android.settings.nfc; package com.android.settings.nfc;
import android.app.ActivityManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.nfc.cardemulation.ApduServiceInfo; import android.nfc.cardemulation.ApduServiceInfo;
import android.nfc.cardemulation.CardEmulation; import android.nfc.cardemulation.CardEmulation;
@@ -50,6 +49,15 @@ public class PaymentBackend {
boolean isDefault; boolean isDefault;
public ComponentName componentName; public ComponentName componentName;
public ComponentName settingsComponent; public ComponentName settingsComponent;
public UserHandle userHandle;
}
/**
* ComponentName of the payment application and the userId that it belongs to.
*/
public static class PaymentInfo {
public ComponentName componentName;
public int userId;
} }
private final Context mContext; private final Context mContext;
@@ -80,40 +88,55 @@ public class PaymentBackend {
public void refresh() { public void refresh() {
PackageManager pm = mContext.getPackageManager(); PackageManager pm = mContext.getPackageManager();
List<ApduServiceInfo> serviceInfos = ArrayList<PaymentAppInfo> appInfosAllProfiles = new ArrayList<PaymentAppInfo>();
mCardEmuManager.getServices(CardEmulation.CATEGORY_PAYMENT);
ArrayList<PaymentAppInfo> appInfos = new ArrayList<PaymentAppInfo>();
if (serviceInfos == null) { UserManager um = mContext.createContextAsUser(
makeCallbacks(); UserHandle.of(ActivityManager.getCurrentUser()), /*flags=*/0)
return; .getSystemService(UserManager.class);
} List<UserHandle> userHandles = um.getEnabledProfiles();
ComponentName defaultAppName = getDefaultPaymentApp(); PaymentInfo defaultAppName = getDefaultPaymentApp();
PaymentAppInfo foundDefaultApp = null; PaymentAppInfo foundDefaultApp = null;
for (ApduServiceInfo service : serviceInfos) { for (UserHandle uh : userHandles) {
PaymentAppInfo appInfo = new PaymentAppInfo(); List<ApduServiceInfo> serviceInfosByProfile =
appInfo.label = service.loadLabel(pm); mCardEmuManager.getServices(CardEmulation.CATEGORY_PAYMENT, uh.getIdentifier());
if (appInfo.label == null) { if (serviceInfosByProfile == null) continue;
appInfo.label = service.loadAppLabel(pm);
ArrayList<PaymentAppInfo> appInfos = new ArrayList<PaymentAppInfo>();
for (ApduServiceInfo service : serviceInfosByProfile) {
PaymentAppInfo appInfo = new PaymentAppInfo();
appInfo.userHandle = uh;
appInfo.label = service.loadLabel(pm);
if (appInfo.label == null) {
appInfo.label = service.loadAppLabel(pm);
}
if (defaultAppName == null) {
appInfo.isDefault = false;
} else {
appInfo.isDefault =
service.getComponent().equals(defaultAppName.componentName)
&& defaultAppName.userId == uh.getIdentifier();
}
if (appInfo.isDefault) {
foundDefaultApp = appInfo;
}
appInfo.componentName = service.getComponent();
String settingsActivity = service.getSettingsActivityName();
if (settingsActivity != null) {
appInfo.settingsComponent = new ComponentName(
appInfo.componentName.getPackageName(),
settingsActivity);
} else {
appInfo.settingsComponent = null;
}
appInfo.description = service.getDescription();
appInfos.add(appInfo);
} }
appInfo.isDefault = service.getComponent().equals(defaultAppName); appInfosAllProfiles.addAll(appInfos);
if (appInfo.isDefault) {
foundDefaultApp = appInfo;
}
appInfo.componentName = service.getComponent();
String settingsActivity = service.getSettingsActivityName();
if (settingsActivity != null) {
appInfo.settingsComponent = new ComponentName(
appInfo.componentName.getPackageName(),
settingsActivity);
} else {
appInfo.settingsComponent = null;
}
appInfo.description = service.getDescription();
appInfos.add(appInfo);
} }
mAppInfos = appInfos; mAppInfos = appInfosAllProfiles;
mDefaultAppInfo = foundDefaultApp; mDefaultAppInfo = foundDefaultApp;
makeCallbacks(); makeCallbacks();
} }
@@ -150,13 +173,36 @@ public class PaymentBackend {
} }
void setForegroundMode(boolean foreground) { void setForegroundMode(boolean foreground) {
Settings.Secure.putIntForUser(mContext.getContentResolver(), UserManager um = mContext.createContextAsUser(
Settings.Secure.NFC_PAYMENT_FOREGROUND, foreground ? 1 : 0, UserHandle.myUserId()); UserHandle.of(UserHandle.myUserId()), /*flags=*/0)
.getSystemService(UserManager.class);
List<UserHandle> userHandles = um.getEnabledProfiles();
for (UserHandle uh : userHandles) {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NFC_PAYMENT_FOREGROUND, foreground ? 1 : 0, uh.getIdentifier());
}
} }
ComponentName getDefaultPaymentApp() { PaymentInfo getDefaultPaymentApp() {
UserManager um = mContext.createContextAsUser(
UserHandle.of(ActivityManager.getCurrentUser()), /*flags=*/0)
.getSystemService(UserManager.class);
List<UserHandle> userHandles = um.getEnabledProfiles();
for (UserHandle uh : userHandles) {
ComponentName defaultApp = getDefaultPaymentApp(uh.getIdentifier());
if (defaultApp != null) {
PaymentInfo appInfo = new PaymentInfo();
appInfo.userId = uh.getIdentifier();
appInfo.componentName = defaultApp;
return appInfo;
}
}
return null;
}
ComponentName getDefaultPaymentApp(int userId) {
String componentString = Settings.Secure.getStringForUser(mContext.getContentResolver(), String componentString = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT, UserHandle.myUserId()); Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT, userId);
if (componentString != null) { if (componentString != null) {
return ComponentName.unflattenFromString(componentString); return ComponentName.unflattenFromString(componentString);
} else { } else {
@@ -165,9 +211,29 @@ public class PaymentBackend {
} }
public void setDefaultPaymentApp(ComponentName app) { public void setDefaultPaymentApp(ComponentName app) {
Settings.Secure.putStringForUser(mContext.getContentResolver(), setDefaultPaymentApp(app, UserHandle.myUserId());
Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT, }
app != null ? app.flattenToString() : null, UserHandle.myUserId());
/**
* Set Nfc default payment application
*/
public void setDefaultPaymentApp(ComponentName app, int userId) {
UserManager um = mContext.createContextAsUser(
UserHandle.of(ActivityManager.getCurrentUser()), /*flags=*/0)
.getSystemService(UserManager.class);
List<UserHandle> userHandles = um.getEnabledProfiles();
for (UserHandle uh : userHandles) {
if (uh.getIdentifier() == userId) {
Settings.Secure.putStringForUser(mContext.getContentResolver(),
Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
app != null ? app.flattenToString() : null, uh.getIdentifier());
} else {
Settings.Secure.putStringForUser(mContext.getContentResolver(),
Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
null, uh.getIdentifier());
}
}
refresh(); refresh();
} }

View File

@@ -21,12 +21,14 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.nfc.cardemulation.CardEmulation; import android.nfc.cardemulation.CardEmulation;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log; import android.util.Log;
import com.android.internal.app.AlertActivity; import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController; import com.android.internal.app.AlertController;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo; import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
import com.android.settings.nfc.PaymentBackend.PaymentInfo;
import java.util.List; import java.util.List;
@@ -39,7 +41,7 @@ public final class PaymentDefaultDialog extends AlertActivity implements
private static final int PAYMENT_APP_MAX_CAPTION_LENGTH = 40; private static final int PAYMENT_APP_MAX_CAPTION_LENGTH = 40;
private PaymentBackend mBackend; private PaymentBackend mBackend;
private ComponentName mNewDefault; private PaymentInfo mNewDefault;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -56,9 +58,10 @@ public final class PaymentDefaultDialog extends AlertActivity implements
ComponentName component = intent.getParcelableExtra( ComponentName component = intent.getParcelableExtra(
CardEmulation.EXTRA_SERVICE_COMPONENT); CardEmulation.EXTRA_SERVICE_COMPONENT);
String category = intent.getStringExtra(CardEmulation.EXTRA_CATEGORY); String category = intent.getStringExtra(CardEmulation.EXTRA_CATEGORY);
int userId = intent.getIntExtra(CardEmulation.EXTRA_USERID, UserHandle.myUserId());
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
if (!buildDialog(component, category)) { if (!buildDialog(component, category, userId)) {
finish(); finish();
} }
@@ -68,7 +71,7 @@ public final class PaymentDefaultDialog extends AlertActivity implements
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case BUTTON_POSITIVE: case BUTTON_POSITIVE:
mBackend.setDefaultPaymentApp(mNewDefault); mBackend.setDefaultPaymentApp(mNewDefault.componentName, mNewDefault.userId);
setResult(RESULT_OK); setResult(RESULT_OK);
break; break;
case BUTTON_NEGATIVE: case BUTTON_NEGATIVE:
@@ -76,7 +79,7 @@ public final class PaymentDefaultDialog extends AlertActivity implements
} }
} }
private boolean buildDialog(ComponentName component, String category) { private boolean buildDialog(ComponentName component, String category, int userId) {
if (component == null || category == null) { if (component == null || category == null) {
Log.e(TAG, "Component or category are null"); Log.e(TAG, "Component or category are null");
return false; return false;
@@ -93,10 +96,12 @@ public final class PaymentDefaultDialog extends AlertActivity implements
List<PaymentAppInfo> services = mBackend.getPaymentAppInfos(); List<PaymentAppInfo> services = mBackend.getPaymentAppInfos();
for (PaymentAppInfo service : services) { for (PaymentAppInfo service : services) {
if (component.equals(service.componentName)) { // check if userId matches
if (component.equals(service.componentName)
&& service.userHandle.getIdentifier() == userId) {
requestedPaymentApp = service; requestedPaymentApp = service;
} }
if (service.isDefault) { if (service.isDefault && service.userHandle.getIdentifier() == userId) {
defaultPaymentApp = service; defaultPaymentApp = service;
} }
} }
@@ -107,13 +112,17 @@ public final class PaymentDefaultDialog extends AlertActivity implements
} }
// Get current mode and default component // Get current mode and default component
ComponentName defaultComponent = mBackend.getDefaultPaymentApp(); PaymentInfo defaultComponent = mBackend.getDefaultPaymentApp();
if (defaultComponent != null && defaultComponent.equals(component)) { if (defaultComponent != null && defaultComponent.componentName.equals(component)
&& defaultComponent.userId == userId) {
Log.e(TAG, "Component " + component + " is already default."); Log.e(TAG, "Component " + component + " is already default.");
return false; return false;
} }
mNewDefault = component; mNewDefault = new PaymentInfo();
mNewDefault.componentName = component;
mNewDefault.userId = userId;
// Compose dialog; get // Compose dialog; get
final AlertController.AlertParams p = mAlertParams; final AlertController.AlertParams p = mAlertParams;
if (defaultPaymentApp == null) { if (defaultPaymentApp == null) {