diff --git a/core/api/current.txt b/core/api/current.txt index e7194df0f97d5..8782236a8ea84 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -9085,6 +9085,11 @@ package android.companion { method @RequiresPermission(android.Manifest.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE) public void stopObservingDevicePresence(@NonNull String) throws android.companion.DeviceNotAssociatedException; field public static final String EXTRA_ASSOCIATION = "android.companion.extra.ASSOCIATION"; field @Deprecated public static final String EXTRA_DEVICE = "android.companion.extra.DEVICE"; + field public static final int RESULT_CANCELED = 0; // 0x0 + field public static final int RESULT_DISCOVERY_TIMEOUT = 2; // 0x2 + field public static final int RESULT_INTERNAL_ERROR = 3; // 0x3 + field public static final int RESULT_OK = -1; // 0xffffffff + field public static final int RESULT_USER_REJECTED = 1; // 0x1 } public abstract static class CompanionDeviceManager.Callback { diff --git a/core/java/android/companion/CompanionDeviceManager.java b/core/java/android/companion/CompanionDeviceManager.java index 90973c307a205..0d890a4496107 100644 --- a/core/java/android/companion/CompanionDeviceManager.java +++ b/core/java/android/companion/CompanionDeviceManager.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.REQUEST_COMPANION_PROFILE_AUTOMOTIVE_P import static android.Manifest.permission.REQUEST_COMPANION_PROFILE_COMPUTER; import static android.Manifest.permission.REQUEST_COMPANION_PROFILE_WATCH; +import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; @@ -56,6 +57,8 @@ import libcore.io.IoUtils; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -84,50 +87,77 @@ public final class CompanionDeviceManager { private static final boolean DEBUG = false; private static final String LOG_TAG = "CDM_CompanionDeviceManager"; + /** @hide */ + @IntDef(prefix = {"RESULT_"}, value = { + RESULT_OK, + RESULT_CANCELED, + RESULT_USER_REJECTED, + RESULT_DISCOVERY_TIMEOUT, + RESULT_INTERNAL_ERROR + }) + @Retention(RetentionPolicy.SOURCE) + public @interface ResultCode {} + /** - * The result code to propagate back to the originating activity, indicates the association - * dialog is explicitly declined by the users. - * - * @hide + * The result code to propagate back to the user activity, indicates the association + * is created successfully. + */ + public static final int RESULT_OK = -1; + + /** + * The result code to propagate back to the user activity, indicates if the association dialog + * is implicitly cancelled. + * E.g. phone is locked, switch to another app or press outside the dialog. + */ + public static final int RESULT_CANCELED = 0; + + /** + * The result code to propagate back to the user activity, indicates the association dialog + * is explicitly declined by the users. */ public static final int RESULT_USER_REJECTED = 1; /** - * The result code to propagate back to the originating activity, indicates the association + * The result code to propagate back to the user activity, indicates the association * dialog is dismissed if there's no device found after 20 seconds. - * - * @hide */ public static final int RESULT_DISCOVERY_TIMEOUT = 2; /** - * The result code to propagate back to the originating activity, indicates the internal error + * The result code to propagate back to the user activity, indicates the internal error * in CompanionDeviceManager. - * - * @hide */ public static final int RESULT_INTERNAL_ERROR = 3; /** - * Requesting applications will receive the String in {@link Callback#onFailure} if the - * association dialog is explicitly declined by the users. e.g. press the Don't allow button. + * Requesting applications will receive the String in {@link Callback#onFailure} if the + * association dialog is explicitly declined by the users. E.g. press the Don't allow + * button. * * @hide */ public static final String REASON_USER_REJECTED = "user_rejected"; /** - * Requesting applications will receive the String in {@link Callback#onFailure} if there's - * no device found after 20 seconds. + * Requesting applications will receive the String in {@link Callback#onFailure} if there's + * no devices found after 20 seconds. * * @hide */ public static final String REASON_DISCOVERY_TIMEOUT = "discovery_timeout"; /** - * Requesting applications will receive the String in {@link Callback#onFailure} if the - * association dialog is in-explicitly declined by the users. e.g. phone is locked, switch to - * another app or press outside the dialog. + * Requesting applications will receive the String in {@link Callback#onFailure} if there's + * an internal error. + * + * @hide + */ + public static final String REASON_INTERNAL_ERROR = "internal_error"; + + /** + * Requesting applications will receive the String in {@link Callback#onFailure} if the + * association dialog is implicitly cancelled. E.g. phone is locked, switch to + * another app or press outside the dialog. * * @hide */ diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java index ae4046064a20e..723c59ae1b9cf 100644 --- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java +++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java @@ -22,6 +22,7 @@ import static android.companion.AssociationRequest.DEVICE_PROFILE_COMPUTER; import static android.companion.AssociationRequest.DEVICE_PROFILE_WATCH; import static android.companion.CompanionDeviceManager.REASON_CANCELED; import static android.companion.CompanionDeviceManager.REASON_DISCOVERY_TIMEOUT; +import static android.companion.CompanionDeviceManager.REASON_INTERNAL_ERROR; import static android.companion.CompanionDeviceManager.REASON_USER_REJECTED; import static android.companion.CompanionDeviceManager.RESULT_DISCOVERY_TIMEOUT; import static android.companion.CompanionDeviceManager.RESULT_INTERNAL_ERROR; @@ -214,7 +215,8 @@ public class CompanionDeviceActivity extends FragmentActivity implements Log.i(TAG, "Cancelling the user confirmation"); - cancel(false, false); + cancel(/* discoveryTimeOut */ false, + /* userRejected */ false, /* internalError */ false); return; } @@ -241,7 +243,8 @@ public class CompanionDeviceActivity extends FragmentActivity implements // TODO: handle config changes without cancelling. if (!isDone()) { - cancel(/* discoveryTimeOut */ false, /* userRejected */ false); // will finish() + cancel(/* discoveryTimeOut */ false, + /* userRejected */ false, /* internalError */ false); // will finish() } } @@ -326,7 +329,8 @@ public class CompanionDeviceActivity extends FragmentActivity implements private void onDiscoveryStateChanged(DiscoveryState newState) { if (newState == FINISHED_TIMEOUT && CompanionDeviceDiscoveryService.getScanResult().getValue().isEmpty()) { - cancel(/* discoveryTimeOut */ true, /* userRejected */ false); + cancel(/* discoveryTimeOut */ true, + /* userRejected */ false, /* internalError */ false); } } @@ -364,12 +368,14 @@ public class CompanionDeviceActivity extends FragmentActivity implements mCdmServiceReceiver.send(RESULT_CODE_ASSOCIATION_APPROVED, data); } - private void cancel(boolean discoveryTimeout, boolean userRejected) { + private void cancel(boolean discoveryTimeout, boolean userRejected, boolean internalError) { if (DEBUG) { Log.i(TAG, "cancel(), discoveryTimeout=" + discoveryTimeout + ", userRejected=" - + userRejected, new Exception("Stack Trace Dump")); + + userRejected + + ", internalError=" + + internalError, new Exception("Stack Trace Dump")); } if (isDone()) { @@ -391,9 +397,12 @@ public class CompanionDeviceActivity extends FragmentActivity implements } else if (discoveryTimeout) { cancelReason = REASON_DISCOVERY_TIMEOUT; resultCode = RESULT_DISCOVERY_TIMEOUT; + } else if (internalError) { + cancelReason = REASON_INTERNAL_ERROR; + resultCode = RESULT_INTERNAL_ERROR; } else { cancelReason = REASON_CANCELED; - resultCode = RESULT_CANCELED; + resultCode = CompanionDeviceManager.RESULT_CANCELED; } // First send callback to the app directly... @@ -449,7 +458,8 @@ public class CompanionDeviceActivity extends FragmentActivity implements } } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Package u" + userId + "/" + packageName + " not found."); - setResultAndFinish(null, RESULT_INTERNAL_ERROR); + cancel(/* discoveryTimeout */ false, + /* userRejected */ false, /* internalError */ true); return; } @@ -627,7 +637,7 @@ public class CompanionDeviceActivity extends FragmentActivity implements // Disable the button, to prevent more clicks. v.setEnabled(false); - cancel(/* discoveryTimeout */ false, /* userRejected */ true); + cancel(/* discoveryTimeout */ false, /* userRejected */ true, /* internalError */ false); } private void onShowHelperDialog(View view) { @@ -652,7 +662,7 @@ public class CompanionDeviceActivity extends FragmentActivity implements final AssociationInfo association = data.getParcelable( EXTRA_ASSOCIATION, AssociationInfo.class); requireNonNull(association); - setResultAndFinish(association, RESULT_OK); + setResultAndFinish(association, CompanionDeviceManager.RESULT_OK); } else { setResultAndFinish(null, resultCode); } @@ -661,7 +671,7 @@ public class CompanionDeviceActivity extends FragmentActivity implements @Override public void onShowHelperDialogFailed() { - setResultAndFinish(null, RESULT_INTERNAL_ERROR); + cancel(/* discoveryTimeout */ false, /* userRejected */ false, /* internalError */ true); } @Override diff --git a/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java b/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java index a614b723e11cf..b04f3c57c2c14 100644 --- a/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java +++ b/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java @@ -20,6 +20,8 @@ import static android.app.PendingIntent.FLAG_CANCEL_CURRENT; import static android.app.PendingIntent.FLAG_IMMUTABLE; import static android.app.PendingIntent.FLAG_ONE_SHOT; import static android.companion.CompanionDeviceManager.COMPANION_DEVICE_DISCOVERY_PACKAGE_NAME; +import static android.companion.CompanionDeviceManager.REASON_INTERNAL_ERROR; +import static android.companion.CompanionDeviceManager.RESULT_INTERNAL_ERROR; import static android.content.ComponentName.createRelative; import static com.android.server.companion.CompanionDeviceManagerService.DEBUG; @@ -40,7 +42,6 @@ import android.app.PendingIntent; import android.companion.AssociatedDevice; import android.companion.AssociationInfo; import android.companion.AssociationRequest; -import android.companion.CompanionDeviceManager; import android.companion.IAssociationRequestCallback; import android.content.ComponentName; import android.content.Context; @@ -348,8 +349,7 @@ class AssociationRequestsProcessor { // Send the association back via the app's callback if (callback != null) { try { - // TODO: update to INTERNAL_ERROR once it's added. - callback.onFailure(CompanionDeviceManager.REASON_CANCELED); + callback.onFailure(REASON_INTERNAL_ERROR); } catch (RemoteException ignore) { } } @@ -358,7 +358,7 @@ class AssociationRequestsProcessor { // back to the app via Activity.setResult(). if (resultReceiver != null) { final Bundle data = new Bundle(); - resultReceiver.send(CompanionDeviceManager.RESULT_INTERNAL_ERROR, data); + resultReceiver.send(RESULT_INTERNAL_ERROR, data); } } }