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

Commit e4ac740e authored by Felipe Leme's avatar Felipe Leme
Browse files

Implemented getAlgorithm() and getDefaultAlgorithm() using manifest metadata.

Bug: 70939974
Test: atest CtsAutoFillServiceTestCases:FieldsClassificationTest

Change-Id: I8b7028c0acfef164b84821a9e4c99817acc838f8
parent bf2d3fa9
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -3862,8 +3862,6 @@ package android.service.autofill {

  public abstract class AutofillFieldClassificationService extends android.app.Service {
    method public android.os.IBinder onBind(android.content.Intent);
    method public java.util.List<java.lang.String> onGetAvailableAlgorithms();
    method public java.lang.String onGetDefaultAlgorithm();
    method public android.service.autofill.AutofillFieldClassificationService.Scores onGetScores(java.lang.String, android.os.Bundle, java.util.List<android.view.autofill.AutofillValue>, java.util.List<java.lang.String>);
    field public static final java.lang.String SERVICE_INTERFACE = "android.service.autofill.AutofillFieldClassificationService";
  }
+1 −53
Original line number Diff line number Diff line
@@ -15,9 +15,6 @@
 */
package android.service.autofill;

import static android.view.autofill.AutofillManager.EXTRA_AVAILABLE_ALGORITHMS;
import static android.view.autofill.AutofillManager.EXTRA_DEFAULT_ALGORITHM;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
@@ -58,9 +55,7 @@ public abstract class AutofillFieldClassificationService extends Service {

    private static final String TAG = "AutofillFieldClassificationService";

    private static final int MSG_GET_AVAILABLE_ALGORITHMS = 1;
    private static final int MSG_GET_DEFAULT_ALGORITHM = 2;
    private static final int MSG_GET_SCORES = 3;
    private static final int MSG_GET_SCORES = 1;

    /**
     * The {@link Intent} action that must be declared as handled by a service
@@ -79,21 +74,6 @@ public abstract class AutofillFieldClassificationService extends Service {
        final Bundle data = new Bundle();
        final RemoteCallback callback;
        switch (action) {
            case MSG_GET_AVAILABLE_ALGORITHMS:
                callback = (RemoteCallback) msg.obj;
                final List<String> availableAlgorithms = onGetAvailableAlgorithms();
                String[] asArray = null;
                if (availableAlgorithms != null) {
                    asArray = new String[availableAlgorithms.size()];
                    availableAlgorithms.toArray(asArray);
                }
                data.putStringArray(EXTRA_AVAILABLE_ALGORITHMS, asArray);
                break;
            case MSG_GET_DEFAULT_ALGORITHM:
                callback = (RemoteCallback) msg.obj;
                final String defaultAlgorithm = onGetDefaultAlgorithm();
                data.putString(EXTRA_DEFAULT_ALGORITHM, defaultAlgorithm);
                break;
            case MSG_GET_SCORES:
                final SomeArgs args = (SomeArgs) msg.obj;
                callback = (RemoteCallback) args.arg1;
@@ -133,27 +113,6 @@ public abstract class AutofillFieldClassificationService extends Service {
        return mWrapper;
    }

    /**
     * Gets the name of all available algorithms.
     *
     * @throws UnsupportedOperationException if not implemented by service.
     */
    // TODO(b/70939974): rename to onGetAvailableAlgorithms if not removed
    @NonNull
    public List<String> onGetAvailableAlgorithms() {
        throw new UnsupportedOperationException("Must be implemented by external service");
    }

    /**
     * Gets the default algorithm that's used when an algorithm is not specified or is invalid.
     *
     * @throws UnsupportedOperationException if not implemented by service.
     */
    @NonNull
    public String onGetDefaultAlgorithm() {
        throw new UnsupportedOperationException("Must be implemented by external service");
    }

    /**
     * Calculates field classification scores in a batch.
     *
@@ -179,17 +138,6 @@ public abstract class AutofillFieldClassificationService extends Service {

    private final class AutofillFieldClassificationServiceWrapper
            extends IAutofillFieldClassificationService.Stub {

        @Override
        public void getAvailableAlgorithms(RemoteCallback callback) throws RemoteException {
            mHandlerCaller.obtainMessageO(MSG_GET_AVAILABLE_ALGORITHMS, callback).sendToTarget();
        }

        @Override
        public void getDefaultAlgorithm(RemoteCallback callback) throws RemoteException {
            mHandlerCaller.obtainMessageO(MSG_GET_DEFAULT_ALGORITHM, callback).sendToTarget();
        }

        @Override
        public void getScores(RemoteCallback callback, String algorithmName, Bundle algorithmArgs,
                List<AutofillValue> actualValues, String[] userDataValues)
+1 −3
Original line number Diff line number Diff line
@@ -27,8 +27,6 @@ import java.util.List;
 * @hide
 */
oneway interface IAutofillFieldClassificationService {
    void getAvailableAlgorithms(in RemoteCallback callback);
    void getDefaultAlgorithm(in RemoteCallback callback);
    void getScores(in RemoteCallback callback, String algorithmName, in Bundle algorithmArgs,
                   in List<AutofillValue> actualValues, in String[] userDataValues);
}
+4 −73
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import android.metrics.LogMaker;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.service.autofill.AutofillService;
import android.service.autofill.FillEventHistory;
@@ -58,8 +57,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

// TODO: use java.lang.ref.Cleaner once Android supports Java 9
import sun.misc.Cleaner;
@@ -177,11 +174,6 @@ public final class AutofillManager {
    public static final String EXTRA_RESTORE_SESSION_TOKEN =
            "android.view.autofill.extra.RESTORE_SESSION_TOKEN";

    /** @hide */
    public static final String EXTRA_AVAILABLE_ALGORITHMS = "available_algorithms";
    /** @hide */
    public static final String EXTRA_DEFAULT_ALGORITHM = "default_algorithm";

    private static final String SESSION_ID_TAG = "android:sessionId";
    private static final String STATE_TAG = "android:state";
    private static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData";
@@ -1174,22 +1166,10 @@ public final class AutofillManager {
     * and it's ignored if the caller currently doesn't have an enabled autofill service for
     * the user.
     */
    // TODO(b/70939974): refactor this method to be "purely" sync by getting the info from the
    // the ExtService manifest (instead of calling the service)
    @Nullable
    public String getDefaultFieldClassificationAlgorithm() {
        final SyncRemoteCallbackListener<String> listener =
                new SyncRemoteCallbackListener<String>() {

            @Override
            String getResult(Bundle result) {
                return result == null ? null : result.getString(EXTRA_DEFAULT_ALGORITHM);
            }
        };

        try {
            mService.getDefaultFieldClassificationAlgorithm(new RemoteCallback(listener));
            return listener.getResult(FC_SERVICE_TIMEOUT);
            return mService.getDefaultFieldClassificationAlgorithm();
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
            return null;
@@ -1204,29 +1184,12 @@ public final class AutofillManager {
     * and it returns an empty list if the caller currently doesn't have an enabled autofill service
     * for the user.
     */
    // TODO(b/70939974): refactor this method to be "purely" sync by getting the info from the
    // the ExtService manifest (instead of calling the service)
    @NonNull
    public List<String> getAvailableFieldClassificationAlgorithms() {
        final SyncRemoteCallbackListener<List<String>> listener =
                new SyncRemoteCallbackListener<List<String>>() {

            @Override
            List<String> getResult(Bundle result) {
                List<String> algorithms = null;
                if (result != null) {
                    final String[] asArray = result.getStringArray(EXTRA_AVAILABLE_ALGORITHMS);
                    if (asArray != null) {
                        algorithms = Arrays.asList(asArray);
                    }
                }
                return algorithms != null ? algorithms : Collections.emptyList();
            }
        };

        final String[] algorithms;
        try {
            mService.getAvailableFieldClassificationAlgorithms(new RemoteCallback(listener));
            return listener.getResult(FC_SERVICE_TIMEOUT);
            algorithms = mService.getAvailableFieldClassificationAlgorithms();
            return algorithms != null ? Arrays.asList(algorithms) : Collections.emptyList();
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
            return null;
@@ -2322,36 +2285,4 @@ public final class AutofillManager {
            }
        }
    }

    private abstract static class SyncRemoteCallbackListener<T>
            implements RemoteCallback.OnResultListener {

        private final CountDownLatch mLatch = new CountDownLatch(1);
        private T mResult;

        @Override
        public void onResult(Bundle result) {
            if (sVerbose) Log.w(TAG, "SyncRemoteCallbackListener.onResult(): " + result);
            mResult = getResult(result);
            mLatch.countDown();
        }

        T getResult(int timeoutMs) {
            T result = null;
            try {
                if (mLatch.await(timeoutMs, TimeUnit.MILLISECONDS)) {
                    result = mResult;
                } else {
                    Log.w(TAG, "SyncRemoteCallbackListener not called in " + timeoutMs + "ms");
                }
            } catch (InterruptedException e) {
                Log.w(TAG, "SyncRemoteCallbackListener interrupted: " + e);
                Thread.currentThread().interrupt();
            }
            if (sVerbose) Log.w(TAG, "SyncRemoteCallbackListener: returning " + result);
            return result;
        }

        abstract T getResult(Bundle result);
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -59,6 +59,6 @@ interface IAutoFillManager {
    void setUserData(in UserData userData);
    boolean isFieldClassificationEnabled();
    ComponentName getAutofillServiceComponentName();
    void getAvailableFieldClassificationAlgorithms(in RemoteCallback callback);
    void getDefaultFieldClassificationAlgorithm(in RemoteCallback callback);
    String[] getAvailableFieldClassificationAlgorithms();
    String getDefaultFieldClassificationAlgorithm();
}
Loading