Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3b7b9894 authored by Evan Chen's avatar Evan Chen Committed by Android (Google) Code Review
Browse files

Merge "Add a new association request failure code" into tm-dev

parents ff33926e 8fedd479
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
@@ -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}
+29 −11
Original line number Diff line number Diff line
@@ -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;
@@ -89,9 +95,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";
@@ -209,7 +212,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()
        }
    }

@@ -293,7 +296,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);
        }
    }

@@ -336,10 +339,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()) {
@@ -353,14 +358,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) {
@@ -562,7 +580,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) {