aboutsummaryrefslogtreecommitdiff
path: root/input
diff options
context:
space:
mode:
authorFelipe Leme <felipeal@google.com>2018-03-20 14:42:15 -0700
committerFelipe Leme <felipeal@google.com>2018-04-18 10:25:09 -0700
commite44363c82c9187230ba85cdc1beb3d2aba9595e6 (patch)
treef9520a6504e42ccf510d0ca3fa61a96b0221fdbf /input
parent8793dbbdc92d8086e6de7215530eea4a8bb4adce (diff)
downloadandroid-e44363c82c9187230ba85cdc1beb3d2aba9595e6.tar.gz
Added another simple service, this one uses some rudimentary heuristics.pie-dev
Also improved services so they just try to autofill fields set with autofill type text. Bug: 75285224 Test: ./gradlew :afservice:installDebug Change-Id: I299f89784aaf15d6ddc05e4d4f5fdf6866854df2
Diffstat (limited to 'input')
-rw-r--r--input/autofill/AutofillFramework/afservice/src/main/AndroidManifest.xml12
-rw-r--r--input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicHeuristicsService.java105
-rw-r--r--input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicService.java94
3 files changed, 181 insertions, 30 deletions
diff --git a/input/autofill/AutofillFramework/afservice/src/main/AndroidManifest.xml b/input/autofill/AutofillFramework/afservice/src/main/AndroidManifest.xml
index 83ac4ee7..b02a591d 100644
--- a/input/autofill/AutofillFramework/afservice/src/main/AndroidManifest.xml
+++ b/input/autofill/AutofillFramework/afservice/src/main/AndroidManifest.xml
@@ -28,13 +28,23 @@
<service
android:name=".simple.BasicService"
- android:label="Basic Service"
+ android:label="Basic Autofill Service"
android:permission="android.permission.BIND_AUTOFILL_SERVICE">
<intent-filter>
<action android:name="android.service.autofill.AutofillService" />
</intent-filter>
</service>
+ <service
+ android:name=".simple.BasicHeuristicsService"
+ android:label="Basic Heuristics Autofill Service"
+ android:permission="android.permission.BIND_AUTOFILL_SERVICE">
+
+ <intent-filter>
+ <action android:name="android.service.autofill.AutofillService" />
+ </intent-filter>
+ </service>
+
<activity
android:name=".AuthActivity"
android:taskAffinity=".AuthActivity"
diff --git a/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicHeuristicsService.java b/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicHeuristicsService.java
new file mode 100644
index 00000000..843440c9
--- /dev/null
+++ b/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicHeuristicsService.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.autofill.service.simple;
+
+import android.app.assist.AssistStructure.ViewNode;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.View;
+
+import com.example.android.autofill.service.MyAutofillService;
+
+/**
+ * A basic service that uses some rudimentary heuristics to identify fields that are not explicitly
+ * marked with autofill hints.
+ *
+ * <p>The goal of this class is to provide a simple autofill service implementation that is easy
+ * to understand and extend, but it should <strong>not</strong> be used as-is on real apps because
+ * it lacks fundamental security requirements such as data partitioning and package verification
+ * &mdashthese requirements are fullfilled by {@link MyAutofillService}. *
+ */
+public class BasicHeuristicsService extends BasicService {
+
+ private static final String TAG = "BasicHeuristicsService";
+
+ @Override
+ @Nullable
+ protected String getHint(@NonNull ViewNode node) {
+
+ // First try the explicit autofill hints...
+
+ String hint = super.getHint(node);
+ if (hint != null) return hint;
+
+ // Then try some rudimentary heuristics based on other node properties
+
+ String viewHint = node.getHint();
+ hint = inferHint(viewHint);
+ if (hint != null) {
+ Log.d(TAG, "Found hint using view hint(" + viewHint + "): " + hint);
+ return hint;
+ } else if (!TextUtils.isEmpty(viewHint)) {
+ Log.v(TAG, "No hint using view hint: " + viewHint);
+ }
+
+ String resourceId = node.getIdEntry();
+ hint = inferHint(resourceId);
+ if (hint != null) {
+ Log.d(TAG, "Found hint using resourceId(" + resourceId + "): " + hint);
+ return hint;
+ } else if (!TextUtils.isEmpty(resourceId)) {
+ Log.v(TAG, "No hint using resourceId: " + resourceId);
+ }
+
+ CharSequence text = node.getText();
+ CharSequence className = node.getClassName();
+ if (text != null && className != null && className.toString().contains("EditText")) {
+ hint = inferHint(text.toString());
+ if (hint != null) {
+ // NODE: text should not be logged, as it could contain PII
+ Log.d(TAG, "Found hint using text(" + text + "): " + hint);
+ return hint;
+ }
+ } else if (!TextUtils.isEmpty(text)) {
+ // NODE: text should not be logged, as it could contain PII
+ Log.v(TAG, "No hint using text: " + text + " and class " + className);
+ }
+ return null;
+ }
+
+ /**
+ * Uses heuristics to infer an autofill hint from a {@code string}.
+ *
+ * @return standard autofill hint, or {@code null} when it could not be inferred.
+ */
+ @Nullable
+ protected String inferHint(@Nullable String string) {
+ if (string == null) return null;
+
+ string = string.toLowerCase();
+ if (string.contains("password")) return View.AUTOFILL_HINT_PASSWORD;
+ if (string.contains("username")
+ || (string.contains("login") && string.contains("id")))
+ return View.AUTOFILL_HINT_USERNAME;
+ if (string.contains("email")) return View.AUTOFILL_HINT_EMAIL_ADDRESS;
+ if (string.contains("name")) return View.AUTOFILL_HINT_NAME;
+ if (string.contains("phone")) return View.AUTOFILL_HINT_PHONE;
+
+ return null;
+ }
+}
diff --git a/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicService.java b/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicService.java
index af8a9afa..92d6436f 100644
--- a/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicService.java
+++ b/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/simple/BasicService.java
@@ -28,13 +28,16 @@ import android.service.autofill.SaveCallback;
import android.service.autofill.SaveInfo;
import android.service.autofill.SaveRequest;
import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.util.Log;
+import android.view.View;
import android.view.autofill.AutofillId;
import android.view.autofill.AutofillValue;
import android.widget.RemoteViews;
import android.widget.Toast;
+import com.example.android.autofill.service.MyAutofillService;
import com.example.android.autofill.service.R;
import java.util.Collection;
@@ -46,27 +49,18 @@ import java.util.Map.Entry;
* A very basic {@link AutofillService} implementation that only shows dynamic-generated datasets
* and don't persist the saved data.
*
- * <p>This class has 2 goals:
- * <ul>
- * <li>Provide a simple service that app developers can use to see how their apps behave with
- * autofill.
- * <li>Explain the basic autofill workflow / provide a service that can be easily modified.
- * </ul>
- */
-/*
- * TODO list:
- * - improve documentation above, explaining
- * - no-goals, security limitations, etc..
- * - toast usage
- * - how dynamic datasets are created
- * - how save works
- * - use strings instead of hardcoded values
- * - use different icons for different services
+ * <p>The goal of this class is to provide a simple autofill service implementation that is easy
+ * to understand and extend, but it should <strong>not</strong> be used as-is on real apps because
+ * it lacks fundamental security requirements such as data partitioning and package verification
+ * &mdashthese requirements are fullfilled by {@link MyAutofillService}.
*/
public class BasicService extends AutofillService {
private static final String TAG = "BasicService";
+ /**
+ * Number of datasets sent on each request - we're simple, that value is hardcoded in our DNA!
+ */
private static final int NUMBER_DATASETS = 4;
@Override
@@ -84,7 +78,6 @@ public class BasicService extends AutofillService {
callback.onSuccess(null);
return;
}
- Log.d(TAG, "autofill hints: " + fields);
// Create the base response
FillResponse.Builder response = new FillResponse.Builder();
@@ -97,6 +90,9 @@ public class BasicService extends AutofillService {
String hint = field.getKey();
AutofillId id = field.getValue();
String value = hint + i;
+ // We're simple - our dataset values are hardcoded as "hintN" (for example,
+ // "username1", "username2") and they're displayed as such, except if they're a
+ // password
String displayValue = hint.contains("password") ? "password for #" + i : value;
RemoteViews presentation = newDatasetPresentation(packageName, displayValue);
dataset.setValue(id, AutofillValue.forText(value), presentation);
@@ -123,7 +119,13 @@ public class BasicService extends AutofillService {
callback.onSuccess();
}
- // TODO: document
+ /**
+ * Parses the {@link AssistStructure} representing the activity being autofilled, and returns a
+ * map of autofillable fields (represented by their autofill ids) mapped by the hint associate
+ * with them.
+ *
+ * <p>An autofillable field is a {@link ViewNode} whose {@link #getHint(ViewNode)} metho
+ */
@NonNull
private Map<String, AutofillId> getAutofillableFields(@NonNull AssistStructure structure) {
Map<String, AutofillId> fields = new ArrayMap<>();
@@ -135,16 +137,25 @@ public class BasicService extends AutofillService {
return fields;
}
- // TODO: document
+ /**
+ * Adds any autofillable view from the {@link ViewNode} and its descendants to the map.
+ */
private void addAutofillableFields(@NonNull Map<String, AutofillId> fields,
@NonNull ViewNode node) {
- String[] hints = node.getAutofillHints();
- if (hints != null) {
- AutofillId id = node.getAutofillId();
- // We're simple, we only care about the first hint
- String hint = hints[0].toLowerCase();
- Log.v(TAG, "Found hint " + hint + " on " + id);
- fields.put(hint, id);
+ int type = node.getAutofillType();
+ // We're simple, we just autofill text fields.
+ if (type == View.AUTOFILL_TYPE_TEXT) {
+ String hint = getHint(node);
+ if (hint != null) {
+ AutofillId id = node.getAutofillId();
+ if (!fields.containsKey(hint)) {
+ Log.v(TAG, "Setting hint " + hint + " on " + id);
+ fields.put(hint, id);
+ } else {
+ Log.v(TAG, "Ignoring hint " + hint + " on " + id
+ + " because it was already set");
+ }
+ }
}
int childrenSize = node.getChildCount();
for (int i = 0; i < childrenSize; i++) {
@@ -152,14 +163,37 @@ public class BasicService extends AutofillService {
}
}
- // TODO: move to common code
+ /**
+ * Gets the autofill hint associated with the given node.
+ *
+ * <p>By default it just return the first entry on the node's
+ * {@link ViewNode#getAutofillHints() autofillHints} (when available), but subclasses could
+ * extend it to use heuristics when the app developer didn't explicitly provide these hints.
+ *
+ */
+ @Nullable
+ protected String getHint(@NonNull ViewNode node) {
+ String[] hints = node.getAutofillHints();
+ if (hints == null) return null;
+
+ // We're simple, we only care about the first hint
+ String hint = hints[0].toLowerCase();
+ return hint;
+ }
+
+ /**
+ * Helper method to get the {@link AssistStructure} associated with the latest request
+ * in an autofill context.
+ */
@NonNull
private static AssistStructure getLatestAssistStructure(@NonNull FillRequest request) {
List<FillContext> fillContexts = request.getFillContexts();
return fillContexts.get(fillContexts.size() - 1).getStructure();
}
- // TODO: move to common code
+ /**
+ * Helper method to create a dataset presentation with the given text.
+ */
@NonNull
private static RemoteViews newDatasetPresentation(@NonNull String packageName,
@NonNull CharSequence text) {
@@ -170,7 +204,9 @@ public class BasicService extends AutofillService {
return presentation;
}
- // TODO: move to common code
+ /**
+ * Displays a toast with the given message.
+ */
private void toast(@NonNull CharSequence message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}