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

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

Implemented shell cmd to get field classification score.

Bug: 70939974
Test: adb shell cmd autofill get fc_score half kale

Change-Id: Idd5d79500e915f61920865f7b721eddfb580d319
parent bc055b0e
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.os.UserManagerInternal;
import android.provider.Settings;
import android.service.autofill.AutofillFieldClassificationService.Scores;
import android.service.autofill.FillEventHistory;
import android.service.autofill.UserData;
import android.util.LocalLog;
@@ -79,6 +80,7 @@ import com.android.server.autofill.ui.AutoFillUI;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@@ -444,7 +446,17 @@ public final class AutofillManagerService extends SystemService {
        }
    }

    // TODO(b/70291841): add command to get field classification score
    // Called by Shell command.
    public void getScore(@Nullable String algorithmName, @NonNull String value1,
            @NonNull String value2, @NonNull RemoteCallback callback) {
        mContext.enforceCallingPermission(MANAGE_AUTO_FILL, TAG);

        final FieldClassificationStrategy strategy =
                new FieldClassificationStrategy(mContext, UserHandle.USER_CURRENT);

        strategy.getScores(callback, algorithmName, null,
                Arrays.asList(AutofillValue.forText(value1)), new String[] { value2 });
    }

    private void setDebugLocked(boolean debug) {
        com.android.server.autofill.Helper.sDebug = debug;
+44 −12
Original line number Diff line number Diff line
@@ -16,11 +16,15 @@

package com.android.server.autofill;

import static android.service.autofill.AutofillFieldClassificationService.EXTRA_SCORES;

import static com.android.server.autofill.AutofillManagerService.RECEIVER_BUNDLE_EXTRA_SESSIONS;

import android.os.Bundle;
import android.os.RemoteCallback;
import android.os.ShellCommand;
import android.os.UserHandle;
import android.service.autofill.AutofillFieldClassificationService.Scores;
import android.view.autofill.AutofillManager;

import com.android.internal.os.IResultReceiver;
@@ -80,13 +84,16 @@ public final class AutofillManagerServiceShellCommand extends ShellCommand {
            pw.println("    Sets the maximum number of partitions per session.");
            pw.println("");
            pw.println("  list sessions [--user USER_ID]");
            pw.println("    List all pending sessions.");
            pw.println("    Lists all pending sessions.");
            pw.println("");
            pw.println("  destroy sessions [--user USER_ID]");
            pw.println("    Destroy all pending sessions.");
            pw.println("    Destroys all pending sessions.");
            pw.println("");
            pw.println("  reset");
            pw.println("    Reset all pending sessions and cached service connections.");
            pw.println("    Resets all pending sessions and cached service connections.");
            pw.println("");
            pw.println("  get fc_score [--algorithm ALGORITHM] value1 value2");
            pw.println("    Gets the field classification score for 2 fields.");
            pw.println("");
        }
    }
@@ -98,6 +105,8 @@ public final class AutofillManagerServiceShellCommand extends ShellCommand {
                return getLogLevel(pw);
            case "max_partitions":
                return getMaxPartitions(pw);
            case "fc_score":
                return getFieldClassificationScore(pw);
            default:
                pw.println("Invalid set: " + what);
                return -1;
@@ -164,6 +173,35 @@ public final class AutofillManagerServiceShellCommand extends ShellCommand {
        return 0;
    }

    private int getFieldClassificationScore(PrintWriter pw) {
        final String nextArg = getNextArgRequired();
        final String algorithm, value1;
        if ("--algorithm".equals(nextArg)) {
            algorithm = getNextArgRequired();
            value1 = getNextArgRequired();
        } else {
            algorithm = null;
            value1 = nextArg;
        }
        final String value2 = getNextArgRequired();

        final CountDownLatch latch = new CountDownLatch(1);
        mService.getScore(algorithm, value1, value2, new RemoteCallback((result) -> {
            final Scores scores = result.getParcelable(EXTRA_SCORES);
            if (scores == null) {
                pw.println("no score");
            } else {
                pw.print("algorithm: ");
                pw.print(scores.getAlgorithmName());
                pw.print(" score: ");
                pw.println(scores.getScores()[0][0]);
            }
            latch.countDown();
        }));

        return waitForLatch(pw, latch);
    }

    private int requestDestroy(PrintWriter pw) {
        if (!isNextArgSessions(pw)) {
            return -1;
@@ -210,19 +248,13 @@ public final class AutofillManagerServiceShellCommand extends ShellCommand {
        return true;
    }

    private boolean isNextArgLogLevel(PrintWriter pw, String cmd) {
        final String type = getNextArgRequired();
        if (!type.equals("log_level")) {
            pw.println("Error: invalid " + cmd + " type: " + type);
            return false;
        }
        return true;
    }

    private int requestSessionCommon(PrintWriter pw, CountDownLatch latch,
            Runnable command) {
        command.run();
        return waitForLatch(pw, latch);
    }

    private int waitForLatch(PrintWriter pw, CountDownLatch latch) {
        try {
            final boolean received = latch.await(5, TimeUnit.SECONDS);
            if (!received) {