Java源码示例:android.service.autofill.FillRequest

示例1
@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal,
        FillCallback callback) {
    Log.d(TAG, "onFillRequest()");

    // Find autofillable fields
    AssistStructure structure = getLatestAssistStructure(request);
    Map<String, AutofillId> fields = getAutofillableFields(structure);
    Log.d(TAG, "autofillable fields:" + fields);

    if (fields.isEmpty()) {
        toast("No autofill hints found");
        callback.onSuccess(null);
        return;
    }

    // Create the base response
    FillResponse.Builder response = new FillResponse.Builder();

    // 1.Add the dynamic datasets
    String packageName = getApplicationContext().getPackageName();
    for (int i = 1; i <= NUMBER_DATASETS; i++) {
        Dataset.Builder dataset = new Dataset.Builder();
        for (Entry<String, AutofillId> field : fields.entrySet()) {
            String hint = field.getKey();
            AutofillId id = field.getValue();
            String value = i + "-" + hint;
            // We're simple - our dataset values are hardcoded as "N-hint" (for example,
            // "1-username", "2-username") 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);
        }
        response.addDataset(dataset.build());
    }

    // 2.Add save info
    Collection<AutofillId> ids = fields.values();
    AutofillId[] requiredIds = new AutofillId[ids.size()];
    ids.toArray(requiredIds);
    response.setSaveInfo(
            // We're simple, so we're generic
            new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_GENERIC, requiredIds).build());

    // 3.Profit!
    callback.onSuccess(response.build());
}
 
示例2
/**
 * Helper method to get the {@link AssistStructure} associated with the latest request
 * in an autofill context.
 */
@NonNull
static AssistStructure getLatestAssistStructure(@NonNull FillRequest request) {
    List<FillContext> fillContexts = request.getFillContexts();
    return fillContexts.get(fillContexts.size() - 1).getStructure();
}
 
示例3
@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal,
        FillCallback callback) {
    Log.d(TAG, "onFillRequest()");

    // Find autofillable fields
    AssistStructure structure = getLatestAssistStructure(request);
    ArrayMap<String, AutofillId> fields = getAutofillableFields(structure);
    Log.d(TAG, "autofillable fields:" + fields);

    if (fields.isEmpty()) {
        toast("No autofill hints found");
        callback.onSuccess(null);
        return;
    }

    // Create response...
    FillResponse response;
    if (mAuthenticateResponses) {
        int size = fields.size();
        String[] hints = new String[size];
        AutofillId[] ids = new AutofillId[size];
        for (int i = 0; i < size; i++) {
            hints[i] = fields.keyAt(i);
            ids[i] = fields.valueAt(i);
        }

        IntentSender authentication = SimpleAuthActivity.newIntentSenderForResponse(this, hints,
                ids, mAuthenticateDatasets);
        RemoteViews presentation = newDatasetPresentation(getPackageName(),
                    "Tap to auth response");

        response = new FillResponse.Builder()
                .setAuthentication(ids, authentication, presentation).build();
    } else {
        response = createResponse(this, fields, mNumberDatasets,mAuthenticateDatasets);
    }

    // ... and return it
    callback.onSuccess(response);
}
 
示例4
@Override
public void onFillRequest(@NonNull FillRequest request,
        @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback) {
    List<FillContext> fillContexts = request.getFillContexts();
    List<AssistStructure> structures =
            fillContexts.stream().map(FillContext::getStructure).collect(toList());
    AssistStructure latestStructure = fillContexts.get(fillContexts.size() - 1).getStructure();
    ClientParser parser = new ClientParser(structures);

    // Check user's settings for authenticating Responses and Datasets.
    boolean responseAuth = mPreferences.isResponseAuth();
    boolean datasetAuth = mPreferences.isDatasetAuth();
    boolean manual = (request.getFlags() & FillRequest.FLAG_MANUAL_REQUEST) != 0;
    mLocalAutofillDataSource.getFieldTypeByAutofillHints(
            new DataCallback<HashMap<String, FieldTypeWithHeuristics>>() {
                @Override
                public void onLoaded(HashMap<String, FieldTypeWithHeuristics> fieldTypesByAutofillHint) {
                    DatasetAdapter datasetAdapter = new DatasetAdapter(parser);
                    ClientViewMetadataBuilder clientViewMetadataBuilder =
                            new ClientViewMetadataBuilder(parser, fieldTypesByAutofillHint);
                    mClientViewMetadata = clientViewMetadataBuilder.buildClientViewMetadata();
                    mResponseAdapter = new ResponseAdapter(MyAutofillService.this,
                            mClientViewMetadata, getPackageName(), datasetAdapter);
                    String packageName = latestStructure.getActivityComponent().getPackageName();
                    if (!mPackageVerificationRepository.putPackageSignatures(packageName)) {
                        callback.onFailure(getString(R.string.invalid_package_signature));
                        return;
                    }
                    if (logVerboseEnabled()) {
                        logv("onFillRequest(): clientState=%s",
                                bundleToString(request.getClientState()));
                        dumpStructure(latestStructure);
                    }
                    cancellationSignal.setOnCancelListener(() ->
                            logw("Cancel autofill not implemented in this sample.")
                    );
                    fetchDataAndGenerateResponse(fieldTypesByAutofillHint, responseAuth,
                            datasetAuth, manual, callback);
                }

                @Override
                public void onDataNotAvailable(String msg, Object... params) {

                }
            });
}
 
示例5
int resolveViewAutofillFlags(Context context, int fillRequestFlags) {
    return (fillRequestFlags & FillRequest.FLAG_MANUAL_REQUEST) != 0
                || context.isAutofillCompatibilityEnabled()
            ? View.AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS : 0;
}
 
示例6
@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal,
        FillCallback callback) {
    Log.d(TAG, "onFillRequest()");

    // Find autofillable fields
    AssistStructure structure = getLatestAssistStructure(request);
    Map<String, AutofillId> fields = getAutofillableFields(structure);
    Log.d(TAG, "autofillable fields:" + fields);

    if (fields.isEmpty()) {
        toast("No autofill hints found");
        callback.onSuccess(null);
        return;
    }

    // Create the base response
    FillResponse.Builder response = new FillResponse.Builder();

    // 1.Add the dynamic datasets
    String packageName = getApplicationContext().getPackageName();
    for (int i = 1; i <= NUMBER_DATASETS; i++) {
        Dataset.Builder dataset = new Dataset.Builder();
        for (Entry<String, AutofillId> field : fields.entrySet()) {
            String hint = field.getKey();
            AutofillId id = field.getValue();
            String value = i + "-" + hint;
            // We're simple - our dataset values are hardcoded as "N-hint" (for example,
            // "1-username", "2-username") 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);
        }
        response.addDataset(dataset.build());
    }

    // 2.Add save info
    Collection<AutofillId> ids = fields.values();
    AutofillId[] requiredIds = new AutofillId[ids.size()];
    ids.toArray(requiredIds);
    response.setSaveInfo(
            // We're simple, so we're generic
            new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_GENERIC, requiredIds).build());

    // 3.Profit!
    callback.onSuccess(response.build());
}
 
示例7
/**
 * Helper method to get the {@link AssistStructure} associated with the latest request
 * in an autofill context.
 */
@NonNull
static AssistStructure getLatestAssistStructure(@NonNull FillRequest request) {
    List<FillContext> fillContexts = request.getFillContexts();
    return fillContexts.get(fillContexts.size() - 1).getStructure();
}
 
示例8
@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal,
        FillCallback callback) {
    Log.d(TAG, "onFillRequest()");

    // Find autofillable fields
    AssistStructure structure = getLatestAssistStructure(request);
    ArrayMap<String, AutofillId> fields = getAutofillableFields(structure);
    Log.d(TAG, "autofillable fields:" + fields);

    if (fields.isEmpty()) {
        toast("No autofill hints found");
        callback.onSuccess(null);
        return;
    }

    // Create response...
    FillResponse response;
    if (mAuthenticateResponses) {
        int size = fields.size();
        String[] hints = new String[size];
        AutofillId[] ids = new AutofillId[size];
        for (int i = 0; i < size; i++) {
            hints[i] = fields.keyAt(i);
            ids[i] = fields.valueAt(i);
        }

        IntentSender authentication = SimpleAuthActivity.newIntentSenderForResponse(this, hints,
                ids, mAuthenticateDatasets);
        RemoteViews presentation = newDatasetPresentation(getPackageName(),
                    "Tap to auth response");

        response = new FillResponse.Builder()
                .setAuthentication(ids, authentication, presentation).build();
    } else {
        response = createResponse(this, fields, mNumberDatasets,mAuthenticateDatasets);
    }

    // ... and return it
    callback.onSuccess(response);
}
 
示例9
@Override
public void onFillRequest(@NonNull FillRequest request,
        @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback) {
    List<FillContext> fillContexts = request.getFillContexts();
    List<AssistStructure> structures =
            fillContexts.stream().map(FillContext::getStructure).collect(toList());
    AssistStructure latestStructure = fillContexts.get(fillContexts.size() - 1).getStructure();
    ClientParser parser = new ClientParser(structures);

    // Check user's settings for authenticating Responses and Datasets.
    boolean responseAuth = mPreferences.isResponseAuth();
    boolean datasetAuth = mPreferences.isDatasetAuth();
    boolean manual = (request.getFlags() & FillRequest.FLAG_MANUAL_REQUEST) != 0;
    mLocalAutofillDataSource.getFieldTypeByAutofillHints(
            new DataCallback<HashMap<String, FieldTypeWithHeuristics>>() {
                @Override
                public void onLoaded(HashMap<String, FieldTypeWithHeuristics> fieldTypesByAutofillHint) {
                    DatasetAdapter datasetAdapter = new DatasetAdapter(parser);
                    ClientViewMetadataBuilder clientViewMetadataBuilder =
                            new ClientViewMetadataBuilder(parser, fieldTypesByAutofillHint);
                    mClientViewMetadata = clientViewMetadataBuilder.buildClientViewMetadata();
                    mResponseAdapter = new ResponseAdapter(MyAutofillService.this,
                            mClientViewMetadata, getPackageName(), datasetAdapter);
                    String packageName = latestStructure.getActivityComponent().getPackageName();
                    if (!mPackageVerificationRepository.putPackageSignatures(packageName)) {
                        callback.onFailure(getString(R.string.invalid_package_signature));
                        return;
                    }
                    if (logVerboseEnabled()) {
                        logv("onFillRequest(): clientState=%s",
                                bundleToString(request.getClientState()));
                        dumpStructure(latestStructure);
                    }
                    cancellationSignal.setOnCancelListener(() ->
                            logw("Cancel autofill not implemented in this sample.")
                    );
                    fetchDataAndGenerateResponse(fieldTypesByAutofillHint, responseAuth,
                            datasetAuth, manual, callback);
                }

                @Override
                public void onDataNotAvailable(String msg, Object... params) {

                }
            });
}