From 5dcc8b979821960ce24d5eae3b55f0e9a43a3abb Mon Sep 17 00:00:00 2001 From: Eugene Susla Date: Tue, 26 Jan 2021 11:34:20 -0800 Subject: [PATCH] Don't crash on unknown CDM profile name We should support the case of new profiles added in PermissionController Test: manual Bug: 165951651 Change-Id: Ia647dfbd815785c2ed5a45d0416816489b9d4914 (cherry picked from commit 5bd46ed2b32c4e54c50ae34c93690df40ba098c4) --- .../android/companion/AssociationRequest.java | 17 ++++++----------- .../DeviceChooserActivity.java | 18 +++++++++++------- .../CompanionDeviceManagerService.java | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/core/java/android/companion/AssociationRequest.java b/core/java/android/companion/AssociationRequest.java index 083ce968d66fe..102c98ff9329c 100644 --- a/core/java/android/companion/AssociationRequest.java +++ b/core/java/android/companion/AssociationRequest.java @@ -55,6 +55,8 @@ import java.util.Objects; genBuilder = false) public final class AssociationRequest implements Parcelable { + private static final String LOG_TAG = AssociationRequest.class.getSimpleName(); + /** * Device profile: watch. * @@ -115,13 +117,6 @@ public final class AssociationRequest implements Parcelable { mDeviceProfilePrivilegesDescription = desc; } - private void onConstructed() { - if (mDeviceProfile != null - && !Objects.equals(mDeviceProfile, DEVICE_PROFILE_WATCH)) { - throw new IllegalArgumentException("Invalid device profile: " + mDeviceProfile); - } - } - /** @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public boolean isSingleDevice() { @@ -252,7 +247,7 @@ public final class AssociationRequest implements Parcelable { this.mCallingPackage = callingPackage; this.mDeviceProfilePrivilegesDescription = deviceProfilePrivilegesDescription; - onConstructed(); + // onConstructed(); // You can define this method to get a callback } /** @@ -386,7 +381,7 @@ public final class AssociationRequest implements Parcelable { this.mCallingPackage = callingPackage; this.mDeviceProfilePrivilegesDescription = deviceProfilePrivilegesDescription; - onConstructed(); + // onConstructed(); // You can define this method to get a callback } @DataClass.Generated.Member @@ -404,10 +399,10 @@ public final class AssociationRequest implements Parcelable { }; @DataClass.Generated( - time = 1610132130920L, + time = 1611692924843L, codegenVersion = "1.0.22", sourceFile = "frameworks/base/core/java/android/companion/AssociationRequest.java", - inputSignatures = "public static final java.lang.String DEVICE_PROFILE_WATCH\nprivate boolean mSingleDevice\nprivate @com.android.internal.util.DataClass.PluralOf(\"deviceFilter\") @android.annotation.NonNull java.util.List> mDeviceFilters\nprivate @android.annotation.Nullable @android.companion.AssociationRequest.DeviceProfile java.lang.String mDeviceProfile\nprivate @android.annotation.Nullable java.lang.String mCallingPackage\nprivate @android.annotation.Nullable java.lang.String mDeviceProfilePrivilegesDescription\npublic void setCallingPackage(java.lang.String)\npublic void setDeviceProfilePrivilegesDescription(java.lang.String)\nprivate void onConstructed()\npublic @android.compat.annotation.UnsupportedAppUsage boolean isSingleDevice()\npublic @android.annotation.NonNull @android.compat.annotation.UnsupportedAppUsage java.util.List> getDeviceFilters()\nclass AssociationRequest extends java.lang.Object implements [android.os.Parcelable]\nprivate boolean mSingleDevice\nprivate @android.annotation.Nullable java.util.ArrayList> mDeviceFilters\nprivate @android.annotation.Nullable java.lang.String mDeviceProfile\npublic @android.annotation.NonNull android.companion.AssociationRequest.Builder setSingleDevice(boolean)\npublic @android.annotation.NonNull android.companion.AssociationRequest.Builder addDeviceFilter(android.companion.DeviceFilter)\npublic @android.annotation.NonNull android.companion.AssociationRequest.Builder setDeviceProfile(java.lang.String)\npublic @android.annotation.NonNull @java.lang.Override android.companion.AssociationRequest build()\nclass Builder extends android.provider.OneTimeUseBuilder implements []\n@com.android.internal.util.DataClass(genToString=true, genEqualsHashCode=true, genHiddenGetters=true, genParcelable=true, genHiddenConstructor=true, genBuilder=false)") + inputSignatures = "private static final java.lang.String LOG_TAG\npublic static final java.lang.String DEVICE_PROFILE_WATCH\nprivate boolean mSingleDevice\nprivate @com.android.internal.util.DataClass.PluralOf(\"deviceFilter\") @android.annotation.NonNull java.util.List> mDeviceFilters\nprivate @android.annotation.Nullable @android.companion.AssociationRequest.DeviceProfile java.lang.String mDeviceProfile\nprivate @android.annotation.Nullable java.lang.String mCallingPackage\nprivate @android.annotation.Nullable java.lang.String mDeviceProfilePrivilegesDescription\npublic void setCallingPackage(java.lang.String)\npublic void setDeviceProfilePrivilegesDescription(java.lang.String)\npublic @android.compat.annotation.UnsupportedAppUsage boolean isSingleDevice()\npublic @android.annotation.NonNull @android.compat.annotation.UnsupportedAppUsage java.util.List> getDeviceFilters()\nclass AssociationRequest extends java.lang.Object implements [android.os.Parcelable]\nprivate boolean mSingleDevice\nprivate @android.annotation.Nullable java.util.ArrayList> mDeviceFilters\nprivate @android.annotation.Nullable java.lang.String mDeviceProfile\npublic @android.annotation.NonNull android.companion.AssociationRequest.Builder setSingleDevice(boolean)\npublic @android.annotation.NonNull android.companion.AssociationRequest.Builder addDeviceFilter(android.companion.DeviceFilter)\npublic @android.annotation.NonNull android.companion.AssociationRequest.Builder setDeviceProfile(java.lang.String)\npublic @android.annotation.NonNull @java.lang.Override android.companion.AssociationRequest build()\nclass Builder extends android.provider.OneTimeUseBuilder implements []\n@com.android.internal.util.DataClass(genToString=true, genEqualsHashCode=true, genHiddenGetters=true, genParcelable=true, genHiddenConstructor=true, genBuilder=false)") @Deprecated private void __metadata() {} diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java index 620c7ae96ebfe..72589e39d5559 100644 --- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java +++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceChooserActivity.java @@ -18,6 +18,7 @@ package com.android.companiondevicemanager; import static android.companion.BluetoothDeviceFilterUtils.getDeviceMacAddress; import static android.text.TextUtils.emptyIfNull; +import static android.text.TextUtils.isEmpty; import static android.text.TextUtils.withoutPrefix; import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; @@ -67,7 +68,13 @@ public class DeviceChooserActivity extends Activity { getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); String deviceProfile = getRequest().getDeviceProfile(); - String profileName = getDeviceProfileName(deviceProfile); + String profilePrivacyDisclaimer = emptyIfNull(getRequest() + .getDeviceProfilePrivilegesDescription()) + .replace("APP_NAME", getCallingAppName()); + boolean useDeviceProfile = deviceProfile != null && !isEmpty(profilePrivacyDisclaimer); + String profileName = useDeviceProfile + ? getDeviceProfileName(deviceProfile) + : getString(R.string.profile_name_generic); if (getRequest().isSingleDevice()) { setContentView(R.layout.device_confirmation); @@ -110,15 +117,12 @@ public class DeviceChooserActivity extends Activity { TextView profileSummary = findViewById(R.id.profile_summary); - if (deviceProfile != null) { - String privacyDisclaimer = emptyIfNull(getRequest() - .getDeviceProfilePrivilegesDescription()) - .replace("APP_NAME", getCallingAppName()); + if (useDeviceProfile) { profileSummary.setVisibility(View.VISIBLE); profileSummary.setText(getString(R.string.profile_summary, getCallingAppName(), profileName, - privacyDisclaimer)); + profilePrivacyDisclaimer)); } else { profileSummary.setVisibility(View.GONE); } @@ -142,7 +146,7 @@ public class DeviceChooserActivity extends Activity { return getString(R.string.profile_name_watch); } default: { - Log.wtf(LOG_TAG, + Log.w(LOG_TAG, "No localized profile name found for device profile: " + deviceProfile); return withoutPrefix("android.app.role.COMPANION_DEVICE_", deviceProfile) .toLowerCase() diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java index 04e08aeaf78ea..55e3ef2e427f2 100644 --- a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java +++ b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java @@ -926,7 +926,7 @@ public class CompanionDeviceManagerService extends SystemService implements Bind mPermissionControllerManager.getPrivilegesDescriptionStringForProfile( deviceProfile, FgThread.getExecutor(), desc -> { try { - result.complete(requireNonNull(desc)); + result.complete(desc); } catch (Exception e) { result.completeExceptionally(e); }