Add a new association request failure code

1. Add the new association request failure codes
   RESULT_USER_REJECTED, RESULT_DISCOVERY_TIMEOUT
   RESULT_INTERNAL_ERROR

2. Add the new association failure reason:
   REASON_CANCELED, REASON_DISCOVERY_TIMEOUT,
   REASON_USER_REJECTED

The change helps to identify user's action that
the association request explicitly declined by the user

Test: atest CtsCompanionDeviceManagerCoreTestCases
      atest CtsCompanionDeviceManagerUiAutomationTestCases
      atest CtsOsTestCases:CompanionDeviceManagerTest

Bug: 223706865
Fix: 224559263
Change-Id: I50fe40780a977c5bbe0aac5b67e1925ea75058bb
This commit is contained in:
Evan Chen
2022-03-15 23:46:34 +00:00
parent 8ea0eb7019
commit 8fedd4796e
2 changed files with 79 additions and 11 deletions

View File

@@ -75,6 +75,56 @@ public final class CompanionDeviceManager {
private static final boolean DEBUG = false;
private static final String LOG_TAG = "CompanionDeviceManager";
/**
* The result code to propagate back to the originating activity, indicates the association
* dialog is explicitly declined by the users.
*
* @hide
*/
public static final int RESULT_USER_REJECTED = 1;
/**
* The result code to propagate back to the originating 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
* 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.
*
* @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.
*
* @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.
*
* @hide
*/
public static final String REASON_CANCELED = "canceled";
/**
* A device, returned in the activity result of the {@link IntentSender} received in
* {@link Callback#onDeviceFound}

View File

@@ -20,6 +20,12 @@ import static android.companion.AssociationRequest.DEVICE_PROFILE_APP_STREAMING;
import static android.companion.AssociationRequest.DEVICE_PROFILE_AUTOMOTIVE_PROJECTION;
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_USER_REJECTED;
import static android.companion.CompanionDeviceManager.RESULT_DISCOVERY_TIMEOUT;
import static android.companion.CompanionDeviceManager.RESULT_INTERNAL_ERROR;
import static android.companion.CompanionDeviceManager.RESULT_USER_REJECTED;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
import static com.android.companiondevicemanager.CompanionDeviceDiscoveryService.DiscoveryState;
@@ -88,9 +94,6 @@ public class CompanionDeviceActivity extends FragmentActivity implements
private static final String FRAGMENT_DIALOG_TAG = "fragment_dialog";
// Activity result: Internal Error.
private static final int RESULT_INTERNAL_ERROR = 2;
// AssociationRequestsProcessor -> UI
private static final int RESULT_CODE_ASSOCIATION_CREATED = 0;
private static final String EXTRA_ASSOCIATION = "association";
@@ -208,7 +211,7 @@ public class CompanionDeviceActivity extends FragmentActivity implements
// TODO: handle config changes without cancelling.
if (!isDone()) {
cancel(false); // will finish()
cancel(/* discoveryTimeOut */ false, /* userRejected */ false); // will finish()
}
}
@@ -290,7 +293,7 @@ public class CompanionDeviceActivity extends FragmentActivity implements
private void onDiscoveryStateChanged(DiscoveryState newState) {
if (newState == FINISHED_TIMEOUT
&& CompanionDeviceDiscoveryService.getScanResult().getValue().isEmpty()) {
cancel(true);
cancel(/* discoveryTimeOut */ true, /* userRejected */ false);
}
}
@@ -333,10 +336,12 @@ public class CompanionDeviceActivity extends FragmentActivity implements
setResultAndFinish(association, RESULT_OK);
}
private void cancel(boolean discoveryTimeout) {
private void cancel(boolean discoveryTimeout, boolean userRejected) {
if (DEBUG) {
Log.i(TAG, "cancel(), discoveryTimeout=" + discoveryTimeout,
new Exception("Stack Trace Dump"));
Log.i(TAG, "cancel(), discoveryTimeout="
+ discoveryTimeout
+ ", userRejected="
+ userRejected, new Exception("Stack Trace Dump"));
}
if (isDone()) {
@@ -350,14 +355,27 @@ public class CompanionDeviceActivity extends FragmentActivity implements
CompanionDeviceDiscoveryService.stop(this);
}
final String cancelReason;
final int resultCode;
if (userRejected) {
cancelReason = REASON_USER_REJECTED;
resultCode = RESULT_USER_REJECTED;
} else if (discoveryTimeout) {
cancelReason = REASON_DISCOVERY_TIMEOUT;
resultCode = RESULT_DISCOVERY_TIMEOUT;
} else {
cancelReason = REASON_CANCELED;
resultCode = RESULT_CANCELED;
}
// First send callback to the app directly...
try {
mAppCallback.onFailure(discoveryTimeout ? "Timeout." : "Cancelled.");
mAppCallback.onFailure(cancelReason);
} catch (RemoteException ignore) {
}
// ... then set result and finish ("sending" onActivityResult()).
setResultAndFinish(null, RESULT_CANCELED);
setResultAndFinish(null, resultCode);
}
private void setResultAndFinish(@Nullable AssociationInfo association, int resultCode) {
@@ -555,7 +573,7 @@ public class CompanionDeviceActivity extends FragmentActivity implements
// Disable the button, to prevent more clicks.
v.setEnabled(false);
cancel(false);
cancel(/* discoveryTimeout */ false, /* userRejected */ true);
}
private void onShowHelperDialog(View view) {