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

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

Merge "[W] Return CharSquence and int for onFailure callback" into main

parents 92a4d8de abcecc27
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -9875,6 +9875,7 @@ package android.companion {
    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 @FlaggedApi("android.companion.association_failure_code") public static final int RESULT_SECURITY_ERROR = 4; // 0x4
    field public static final int RESULT_USER_REJECTED = 1; // 0x1
  }
@@ -9884,7 +9885,7 @@ package android.companion {
    method public void onAssociationPending(@NonNull android.content.IntentSender);
    method @Deprecated public void onDeviceFound(@NonNull android.content.IntentSender);
    method public abstract void onFailure(@Nullable CharSequence);
    method @FlaggedApi("android.companion.association_failure_code") public void onFailure(int);
    method @FlaggedApi("android.companion.association_failure_code") public void onFailure(int, @Nullable CharSequence);
  }
  public abstract class CompanionDeviceService extends android.app.Service {
+25 −23
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ 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 static java.util.Collections.unmodifiableMap;

import android.annotation.CallbackExecutor;
import android.annotation.FlaggedApi;
@@ -58,7 +57,6 @@ import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.util.ArrayMap;
import android.util.ExceptionUtils;
import android.util.Log;
import android.util.SparseArray;
@@ -78,7 +76,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
@@ -146,11 +143,18 @@ public final class CompanionDeviceManager {
    /**
     * The result code to propagate back to the user activity, indicates the internal error
     * in CompanionDeviceManager.
     * E.g. Missing necessary permissions or duplicate {@link AssociationRequest}s when create the
     * {@link AssociationInfo}.
     */
    public static final int RESULT_INTERNAL_ERROR = 3;

    /**
     * The result code to propagate back to the user activity and
     * {@link Callback#onFailure(int, CharSequence)}, indicates app is not allow to create the
     * association due to the security issue.
     * E.g. There are missing necessary permissions when creating association.
     */
    @FlaggedApi(Flags.FLAG_ASSOCIATION_FAILURE_CODE)
    public static final int RESULT_SECURITY_ERROR = 4;

    /**
     * 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
@@ -374,7 +378,6 @@ public final class CompanionDeviceManager {
         */
        public void onAssociationCreated(@NonNull AssociationInfo associationInfo) {}

        //TODO(b/331459560): Add deprecated and remove abstract after API cut for W.
        /**
         * Invoked if the association could not be created.
         *
@@ -385,11 +388,15 @@ public final class CompanionDeviceManager {
        /**
         * Invoked if the association could not be created.
         *
         * @param resultCode indicate the particular reason why the association
         * Please note that both {@link #onFailure(CharSequence error)} and this
         * API will be called if the association could not be created.
         *
         * @param errorCode indicate the particular error code why the association
         *                  could not be created.
         * @param error error message.
         */
        @FlaggedApi(Flags.FLAG_ASSOCIATION_FAILURE_CODE)
        public void onFailure(@ResultCode int resultCode) {}
        public void onFailure(@ResultCode int errorCode, @Nullable CharSequence error) {}
    }

    private final ICompanionDeviceManager mService;
@@ -1825,12 +1832,12 @@ public final class CompanionDeviceManager {
        }

        @Override
        public void onFailure(@ResultCode int resultCode) {
        public void onFailure(@ResultCode int errorCode, @Nullable CharSequence error) {
            if (Flags.associationFailureCode()) {
                execute(mCallback::onFailure, resultCode);
                execute(mCallback::onFailure, errorCode, error);
            }

            execute(mCallback::onFailure, RESULT_CODE_TO_REASON.get(resultCode));
            execute(mCallback::onFailure, error);
        }

        private <T> void execute(Consumer<T> callback, T arg) {
@@ -1840,6 +1847,12 @@ public final class CompanionDeviceManager {
                mHandler.post(() -> callback.accept(arg));
            }
        }

        private <T, U> void execute(BiConsumer<T, U> callback, T arg1, U arg2) {
            if (mExecutor != null) {
                mExecutor.execute(() -> callback.accept(arg1, arg2));
            }
        }
    }

    private static class OnAssociationsChangedListenerProxy
@@ -2014,15 +2027,4 @@ public final class CompanionDeviceManager {
            }
        }
    }

    private static final Map<Integer, String> RESULT_CODE_TO_REASON;
    static {
        final Map<Integer, String> map = new ArrayMap<>();
        map.put(RESULT_CANCELED, REASON_CANCELED);
        map.put(RESULT_USER_REJECTED, REASON_USER_REJECTED);
        map.put(RESULT_DISCOVERY_TIMEOUT, REASON_DISCOVERY_TIMEOUT);
        map.put(RESULT_INTERNAL_ERROR, REASON_INTERNAL_ERROR);

        RESULT_CODE_TO_REASON = unmodifiableMap(map);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -25,5 +25,5 @@ interface IAssociationRequestCallback {

    oneway void onAssociationCreated(in AssociationInfo associationInfo);

    oneway void onFailure(in int resultCode);
    oneway void onFailure(in int errorCode, in CharSequence error);
}
 No newline at end of file
+22 −13
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.companiondevicemanager;
import static android.companion.CompanionDeviceManager.RESULT_CANCELED;
import static android.companion.CompanionDeviceManager.RESULT_DISCOVERY_TIMEOUT;
import static android.companion.CompanionDeviceManager.RESULT_INTERNAL_ERROR;
import static android.companion.CompanionDeviceManager.RESULT_SECURITY_ERROR;
import static android.companion.CompanionDeviceManager.RESULT_USER_REJECTED;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

@@ -33,6 +34,7 @@ import static com.android.companiondevicemanager.CompanionDeviceResources.PROFIL
import static com.android.companiondevicemanager.CompanionDeviceResources.PROFILE_TITLES;
import static com.android.companiondevicemanager.CompanionDeviceResources.SUPPORTED_PROFILES;
import static com.android.companiondevicemanager.CompanionDeviceResources.SUPPORTED_SELF_MANAGED_PROFILES;
import static com.android.companiondevicemanager.Utils.RESULT_CODE_TO_REASON;
import static com.android.companiondevicemanager.Utils.getApplicationLabel;
import static com.android.companiondevicemanager.Utils.getHtmlFromResources;
import static com.android.companiondevicemanager.Utils.getIcon;
@@ -51,6 +53,7 @@ import android.companion.AssociatedDevice;
import android.companion.AssociationInfo;
import android.companion.AssociationRequest;
import android.companion.CompanionDeviceManager;
import android.companion.Flags;
import android.companion.IAssociationRequestCallback;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -231,7 +234,7 @@ public class CompanionAssociationActivity extends FragmentActivity implements
        boolean forCancelDialog = intent.getBooleanExtra(EXTRA_FORCE_CANCEL_CONFIRMATION, false);
        if (forCancelDialog) {
            Slog.i(TAG, "Cancelling the user confirmation");
            cancel(RESULT_CANCELED);
            cancel(RESULT_CANCELED, null);
            return;
        }

@@ -243,10 +246,15 @@ public class CompanionAssociationActivity extends FragmentActivity implements
        if (appCallback == null) {
            return;
        }
        Slog.e(TAG, "More than one AssociationRequests are processing.");

        try {
            appCallback.onFailure(RESULT_INTERNAL_ERROR);
            if (Flags.associationFailureCode()) {
                appCallback.onFailure(
                        RESULT_SECURITY_ERROR, "More than one AssociationRequests are processing.");
            } else {
                appCallback.onFailure(
                        RESULT_INTERNAL_ERROR, "More than one AssociationRequests are processing.");
            }
        } catch (RemoteException ignore) {
        }
    }
@@ -257,7 +265,7 @@ public class CompanionAssociationActivity extends FragmentActivity implements

        // TODO: handle config changes without cancelling.
        if (!isDone()) {
            cancel(RESULT_CANCELED); // will finish()
            cancel(RESULT_CANCELED, null); // will finish()
        }
    }

@@ -331,7 +339,7 @@ public class CompanionAssociationActivity extends FragmentActivity implements
                && CompanionDeviceDiscoveryService.getScanResult().getValue().isEmpty()) {
            synchronized (LOCK) {
                if (sDiscoveryStarted) {
                    cancel(RESULT_DISCOVERY_TIMEOUT);
                    cancel(RESULT_DISCOVERY_TIMEOUT, null);
                }
            }
        }
@@ -371,7 +379,7 @@ public class CompanionAssociationActivity extends FragmentActivity implements
        mCdmServiceReceiver.send(RESULT_CODE_ASSOCIATION_APPROVED, data);
    }

    private void cancel(int failureCode) {
    private void cancel(int errorCode, @Nullable CharSequence error) {
        if (isDone()) {
            Slog.w(TAG, "Already done: " + (mApproved ? "Approved" : "Cancelled"));
            return;
@@ -385,13 +393,14 @@ public class CompanionAssociationActivity extends FragmentActivity implements

        // First send callback to the app directly...
        try {
            Slog.i(TAG, "Sending onFailure to app due to failureCode=" + failureCode);
            mAppCallback.onFailure(failureCode);
            CharSequence errorMessage = error != null
                    ? error : RESULT_CODE_TO_REASON.get(errorCode);
            mAppCallback.onFailure(errorCode, errorMessage);
        } catch (RemoteException ignore) {
        }

        // ... then set result and finish ("sending" onActivityResult()).
        setResultAndFinish(null, failureCode);
        setResultAndFinish(null, errorCode);
    }

    private void setResultAndFinish(@Nullable AssociationInfo association, int resultCode) {
@@ -436,7 +445,7 @@ public class CompanionAssociationActivity extends FragmentActivity implements
            }
        } catch (PackageManager.NameNotFoundException e) {
            Slog.e(TAG, "Package u" + userId + "/" + packageName + " not found.");
            cancel(RESULT_INTERNAL_ERROR);
            cancel(RESULT_INTERNAL_ERROR, e.getMessage());
            return;
        }

@@ -625,7 +634,7 @@ public class CompanionAssociationActivity extends FragmentActivity implements
        // Disable the button, to prevent more clicks.
        v.setEnabled(false);

        cancel(RESULT_USER_REJECTED);
        cancel(RESULT_USER_REJECTED, null);
    }

    private void onShowHelperDialog(View view) {
@@ -755,8 +764,8 @@ public class CompanionAssociationActivity extends FragmentActivity implements
            };

    @Override
    public void onShowHelperDialogFailed() {
        cancel(RESULT_INTERNAL_ERROR);
    public void onShowHelperDialogFailed(CharSequence errorMessage) {
        cancel(RESULT_INTERNAL_ERROR, errorMessage);
    }

    @Override
+2 −2
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ public class CompanionVendorHelperDialogFragment extends DialogFragment {
    private Button mButton;

    interface CompanionVendorHelperDialogListener {
        void onShowHelperDialogFailed();
        void onShowHelperDialogFailed(CharSequence error);
        void onHelperDialogDismissed();
    }

@@ -110,7 +110,7 @@ public class CompanionVendorHelperDialogFragment extends DialogFragment {
            appLabel = getApplicationLabel(getContext(), packageName, userId);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Package u" + userId + "/" + packageName + " not found.");
            mListener.onShowHelperDialogFailed();
            mListener.onShowHelperDialogFailed(e.getMessage());
            return;
        }

Loading