Search in sources :

Example 21 with AssistStructure

use of android.app.assist.AssistStructure in project passman-android by nextcloud.

the class CredentialAutofillService method getLikelyDomain.

@NonNull
static WebDomainResult getLikelyDomain(ArrayList<AssistStructure> assistStructures) {
    WebDomainResult res = new WebDomainResult();
    for (AssistStructure assistStructure : assistStructures) {
        int nodes = assistStructure.getWindowNodeCount();
        for (int i = 0; i < nodes; i++) {
            AssistStructure.ViewNode viewNode = assistStructure.getWindowNodeAt(i).getRootViewNode();
            getNodeDomain(viewNode, res);
        }
    }
    Log.d(TAG, "Returning, found :" + String.valueOf(res.allDomains.size()) + " domains.");
    return res;
}
Also used : AssistStructure(android.app.assist.AssistStructure) ViewNode(android.app.assist.AssistStructure.ViewNode) NonNull(androidx.annotation.NonNull)

Example 22 with AssistStructure

use of android.app.assist.AssistStructure in project passman-android by nextcloud.

the class CredentialAutofillService method onSaveRequest.

@Override
public void onSaveRequest(SaveRequest request, SaveCallback callback) {
    Log.d(TAG, "onSaveRequest()");
    List<FillContext> fillContexts = request.getFillContexts();
    final AssistStructure latestStructure = fillContexts.get(fillContexts.size() - 1).getStructure();
    final String requesterPackageName = latestStructure.getActivityComponent().getPackageName();
    String requesterDomainName = null;
    String requesterApplicationLabel = null;
    // Find autofillable fields
    ArrayList<AssistStructure> structures = new ArrayList<>();
    for (FillContext fc : request.getFillContexts()) {
        structures.add(fc.getStructure());
    }
    CredentialAutofillService.WebDomainResult domain = getLikelyDomain(structures);
    if (domain.firstDomain != null) {
        requesterDomainName = domain.firstDomain;
    } else {
        requesterDomainName = "";
    }
    AutofillFieldCollection fields = getAutofillableFields(latestStructure, true);
    // We don't have any fields to work with
    if (fields.isEmpty()) {
        Log.d(TAG, "No autofillable fields for: " + requesterPackageName);
        callback.onSuccess();
        return;
    }
    SingleTon ton = SingleTon.getTon();
    final Vault v = getAutofillVault(ton);
    if (v == null) {
        Toast.makeText(getApplicationContext(), getString(R.string.autofill_noactivevault), Toast.LENGTH_SHORT).show();
        Log.d(TAG, getString(R.string.autofill_noactivevault));
        callback.onSuccess();
        return;
    }
    if (!v.is_unlocked()) {
        Toast.makeText(getApplicationContext(), getString(R.string.autofill_vaultlocked), Toast.LENGTH_SHORT).show();
        Log.d(TAG, getString(R.string.autofill_vaultlocked));
        callback.onSuccess();
        return;
    }
    try {
        ApplicationInfo requesterAppInfo = getPackageManager().getApplicationInfo(requesterPackageName, 0);
        requesterApplicationLabel = getPackageManager().getApplicationLabel(requesterAppInfo).toString();
    } catch (Exception ex) {
        Log.d(TAG, "Couldn't read application label for: " + requesterPackageName);
    }
    if (TextUtils.isEmpty(requesterApplicationLabel)) {
        requesterApplicationLabel = requesterPackageName;
    }
    Log.d(TAG, "onSaveRequest(): Application: " + requesterApplicationLabel);
    if (!requesterDomainName.equals("")) {
        String parsedDomain = getDomainName(requesterDomainName);
        if (parsedDomain.equals("")) {
            parsedDomain = requesterDomainName;
        }
        requesterApplicationLabel += " - " + parsedDomain;
    }
    AutofillField bestUsername = fields.getRequiredId(View.AUTOFILL_HINT_USERNAME);
    AutofillField bestEmail = fields.getRequiredId(View.AUTOFILL_HINT_EMAIL_ADDRESS);
    AutofillField bestPassword = fields.getRequiredId(View.AUTOFILL_HINT_PASSWORD);
    String username = AutofillField.toStringValue(bestUsername);
    String email = AutofillField.toStringValue(bestEmail);
    String password = AutofillField.toStringValue(bestPassword);
    if (email == null || email.equals("true") || email.equals("false")) {
        email = "";
    }
    if (username == null || username.equals("true") || username.equals("false")) {
        username = "";
    }
    String customFieldString = "";
    try {
        JSONArray customFields = new JSONArray();
        JSONObject customField = new JSONObject();
        customField.put("label", "androidCredPackageName");
        customField.put("value", requesterPackageName);
        customField.put("secret", false);
        customField.put("field_type", "text");
        customFields.put(customField);
        customFieldString = customFields.toString();
    } catch (JSONException e) {
        Log.e(TAG, "onSaveRequest(), error creating customField");
    }
    Log.d(TAG, "onSaveRequest(), building Credential");
    Credential newCred = new Credential();
    newCred.setVault(v);
    newCred.setDescription(getString(R.string.autofill_createdbyautofillservice));
    newCred.setEmail(email);
    newCred.setLabel(requesterApplicationLabel);
    newCred.setCustomFields(customFieldString);
    newCred.setUsername(username);
    newCred.setPassword(password);
    newCred.setFiles((new JSONArray()).toString());
    newCred.setTags((new JSONArray()).toString());
    newCred.setOtp((new JSONObject()).toString());
    newCred.setUrl(requesterDomainName);
    newCred.setCompromised(false);
    Log.d(TAG, "onSaveRequest(), saving Credential");
    final AsyncHttpResponseHandler responseHandler = new AutofillCredentialSaveResponseHandler(getAutofillVault(ton), getBaseContext(), getApplicationContext(), ton, TAG);
    newCred.save(getApplicationContext(), responseHandler);
    Log.d(TAG, "onSaveRequest() finished");
    callback.onSuccess();
}
Also used : Credential(es.wolfi.passman.API.Credential) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) JSONArray(org.json.JSONArray) AssistStructure(android.app.assist.AssistStructure) JSONException(org.json.JSONException) AsyncHttpResponseHandler(com.loopj.android.http.AsyncHttpResponseHandler) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) SingleTon(es.wolfi.app.passman.SingleTon) AutofillCredentialSaveResponseHandler(es.wolfi.app.ResponseHandlers.AutofillCredentialSaveResponseHandler) FillContext(android.service.autofill.FillContext) Vault(es.wolfi.passman.API.Vault)

Example 23 with AssistStructure

use of android.app.assist.AssistStructure in project AndroidComponentPlugin by androidmalin.

the class ActivityThread method handleRequestAssistContextExtras.

public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
    // Filling for autofill has a few differences:
    // - it does not need an AssistContent
    // - it does not call onProvideAssistData()
    // - it needs an IAutoFillCallback
    boolean forAutofill = cmd.requestType == ActivityManager.ASSIST_CONTEXT_AUTOFILL;
    // TODO: decide if lastSessionId logic applies to autofill sessions
    if (mLastSessionId != cmd.sessionId) {
        // Clear the existing structures
        mLastSessionId = cmd.sessionId;
        for (int i = mLastAssistStructures.size() - 1; i >= 0; i--) {
            AssistStructure structure = mLastAssistStructures.get(i).get();
            if (structure != null) {
                structure.clearSendChannel();
            }
            mLastAssistStructures.remove(i);
        }
    }
    Bundle data = new Bundle();
    AssistStructure structure = null;
    AssistContent content = forAutofill ? null : new AssistContent();
    final long startTime = SystemClock.uptimeMillis();
    ActivityClientRecord r = mActivities.get(cmd.activityToken);
    Uri referrer = null;
    if (r != null) {
        if (!forAutofill) {
            r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
            r.activity.onProvideAssistData(data);
            referrer = r.activity.onProvideReferrer();
        }
        if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL || forAutofill) {
            structure = new AssistStructure(r.activity, forAutofill, cmd.flags);
            Intent activityIntent = r.activity.getIntent();
            boolean notSecure = r.window == null || (r.window.getAttributes().flags & WindowManager.LayoutParams.FLAG_SECURE) == 0;
            if (activityIntent != null && notSecure) {
                if (!forAutofill) {
                    Intent intent = new Intent(activityIntent);
                    intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
                    intent.removeUnsafeExtras();
                    content.setDefaultIntent(intent);
                }
            } else {
                if (!forAutofill) {
                    content.setDefaultIntent(new Intent());
                }
            }
            if (!forAutofill) {
                r.activity.onProvideAssistContent(content);
            }
        }
    }
    if (structure == null) {
        structure = new AssistStructure();
    }
    // TODO: decide if lastSessionId logic applies to autofill sessions
    structure.setAcquisitionStartTime(startTime);
    structure.setAcquisitionEndTime(SystemClock.uptimeMillis());
    mLastAssistStructures.add(new WeakReference<>(structure));
    IActivityTaskManager mgr = ActivityTaskManager.getService();
    try {
        mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
Also used : AssistContent(android.app.assist.AssistContent) Bundle(android.os.Bundle) PersistableBundle(android.os.PersistableBundle) AssistStructure(android.app.assist.AssistStructure) ReferrerIntent(com.android.internal.content.ReferrerIntent) Intent(android.content.Intent) RemoteException(android.os.RemoteException) Uri(android.net.Uri)

Aggregations

AssistStructure (android.app.assist.AssistStructure)23 Intent (android.content.Intent)11 Bundle (android.os.Bundle)11 AssistContent (android.app.assist.AssistContent)10 RemoteException (android.os.RemoteException)10 Uri (android.net.Uri)6 PersistableBundle (android.os.PersistableBundle)6 ReferrerIntent (com.android.internal.content.ReferrerIntent)6 FillResponse (android.service.autofill.FillResponse)5 ClipData (android.content.ClipData)4 Matrix (android.graphics.Matrix)4 Paint (android.graphics.Paint)4 FillContext (android.service.autofill.FillContext)4 AutofillId (android.view.autofill.AutofillId)4 ArrayList (java.util.ArrayList)3 IntentSender (android.content.IntentSender)2 Dataset (android.service.autofill.Dataset)2 SaveInfo (android.service.autofill.SaveInfo)2 RemoteViews (android.widget.RemoteViews)2 SingleTon (es.wolfi.app.passman.SingleTon)2