diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index 0350dd39..664fef5f 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -24,6 +24,7 @@ import android.view.ViewConfiguration; import android.view.ViewTreeObserver; import android.view.accessibility.AccessibilityManager; +import android.view.autofill.AutofillManager; import android.view.autofill.AutofillValue; import android.view.inputmethod.BaseInputConnection; import android.view.inputmethod.EditorInfo; @@ -105,6 +106,9 @@ public final class TerminalView extends View { boolean mShiftHeld; boolean mAltHeld; + /** The current AutoFill hint returned by {@link #getAutofillHints()} - null if no autofill desired. */ + private String mAutoFillHint; + private final boolean mAccessibilityEnabled; public TerminalView(Context context, AttributeSet attributes) { // NO_UCD (unused code) @@ -1020,6 +1024,13 @@ public void setTopRow(int mTopRow) { this.mTopRow = mTopRow; } + public void requestAutoFill(String autoFillHint) { + var autofillManager = getContext().getSystemService(AutofillManager.class); + if (autofillManager != null && autofillManager.isEnabled()) { + mAutoFillHint = autoFillHint; + autofillManager.requestAutofill(this); + } + } /** * Define functions required for AutoFill API @@ -1029,11 +1040,27 @@ public void autofill(AutofillValue value) { if (value.isText()) { mTermSession.write(value.getTextValue() + "\n"); } + mAutoFillHint = null; + var autofillManager = getContext().getSystemService(AutofillManager.class); + if (autofillManager != null) { + autofillManager.cancel(); + } } @Override public int getAutofillType() { - return AUTOFILL_TYPE_TEXT; + return (mAutoFillHint == null ? AUTOFILL_TYPE_NONE : AUTOFILL_TYPE_TEXT); + } + + @Override + public int getImportantForAutofill() { + return (mAutoFillHint == null ? IMPORTANT_FOR_AUTOFILL_NO : IMPORTANT_FOR_AUTOFILL_YES); + } + + @Nullable + @Override + public String[] getAutofillHints() { + return mAutoFillHint == null ? null : new String[]{mAutoFillHint}; } @Override diff --git a/termux-app/src/main/java/com/termux/app/TermuxActivity.java b/termux-app/src/main/java/com/termux/app/TermuxActivity.java index 70afeb39..31e5abc3 100644 --- a/termux-app/src/main/java/com/termux/app/TermuxActivity.java +++ b/termux-app/src/main/java/com/termux/app/TermuxActivity.java @@ -72,14 +72,15 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo private static final int CONTEXT_MENU_SELECT_URL_ID = 0; private static final int CONTEXT_MENU_SHARE_TRANSCRIPT_ID = 1; - private static final int CONTEXT_MENU_SHARE_SELECTED_TEXT = 10; - private static final int CONTEXT_MENU_AUTOFILL_ID = 2; - private static final int CONTEXT_MENU_RESET_TERMINAL_ID = 3; - private static final int CONTEXT_MENU_KILL_PROCESS_ID = 4; - private static final int CONTEXT_MENU_STYLING_ID = 5; - private static final int CONTEXT_MENU_TOGGLE_KEEP_SCREEN_ON = 6; - private static final int CONTEXT_MENU_FULLSCREEN_ID = 7; - private static final int CONTEXT_MENU_HELP_ID = 8; + private static final int CONTEXT_MENU_SHARE_SELECTED_TEXT = 2; + private static final int CONTEXT_MENU_AUTOFILL_USERNAME = 3; + private static final int CONTEXT_MENU_AUTOFILL_PASSWORD = 4; + private static final int CONTEXT_MENU_RESET_TERMINAL_ID = 5; + private static final int CONTEXT_MENU_KILL_PROCESS_ID = 6; + private static final int CONTEXT_MENU_STYLING_ID = 7; + private static final int CONTEXT_MENU_TOGGLE_KEEP_SCREEN_ON = 8; + private static final int CONTEXT_MENU_FULLSCREEN_ID = 9; + private static final int CONTEXT_MENU_HELP_ID = 10; private static final String ARG_TERMINAL_TOOLBAR_TEXT_INPUT = "terminal_toolbar_text_input"; private static final String ARG_ACTIVITY_RECREATED = "activity_recreated"; @@ -541,11 +542,8 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn TerminalSession currentSession = getCurrentSession(); if (currentSession == null) return; - boolean addAutoFillMenu = false; var autofillManager = getSystemService(AutofillManager.class); - if (autofillManager != null && autofillManager.isEnabled()) { - addAutoFillMenu = true; - } + boolean addAutoFillMenu = (autofillManager != null && autofillManager.isEnabled()); menu.add(Menu.NONE, CONTEXT_MENU_SELECT_URL_ID, Menu.NONE, R.string.action_select_url); menu.add(Menu.NONE, CONTEXT_MENU_SHARE_TRANSCRIPT_ID, Menu.NONE, R.string.action_share_transcript); @@ -553,8 +551,10 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn if (mTerminalView.getStoredSelectedText() != null) { menu.add(Menu.NONE, CONTEXT_MENU_SHARE_SELECTED_TEXT, Menu.NONE, R.string.action_share_selected_text); } - if (addAutoFillMenu) - menu.add(Menu.NONE, CONTEXT_MENU_AUTOFILL_ID, Menu.NONE, R.string.action_autofill_password); + if (addAutoFillMenu) { + menu.add(Menu.NONE, CONTEXT_MENU_AUTOFILL_USERNAME, Menu.NONE, R.string.action_autofill_username); + menu.add(Menu.NONE, CONTEXT_MENU_AUTOFILL_PASSWORD, Menu.NONE, R.string.action_autofill_password); + } menu.add(Menu.NONE, CONTEXT_MENU_RESET_TERMINAL_ID, Menu.NONE, R.string.action_reset_terminal); menu.add(Menu.NONE, CONTEXT_MENU_KILL_PROCESS_ID, Menu.NONE, getResources().getString(R.string.action_kill_process, getCurrentSession().getPid())).setEnabled(currentSession.isRunning()); menu.add(Menu.NONE, CONTEXT_MENU_STYLING_ID, Menu.NONE, R.string.action_style_terminal); @@ -586,8 +586,11 @@ public boolean onContextItemSelected(MenuItem item) { case CONTEXT_MENU_SHARE_SELECTED_TEXT: mTermuxTerminalViewClient.shareSelectedText(); return true; - case CONTEXT_MENU_AUTOFILL_ID: - requestAutoFill(); + case CONTEXT_MENU_AUTOFILL_USERNAME: + mTerminalView.requestAutoFill(View.AUTOFILL_HINT_USERNAME); + return true; + case CONTEXT_MENU_AUTOFILL_PASSWORD: + mTerminalView.requestAutoFill(View.AUTOFILL_HINT_PASSWORD); return true; case CONTEXT_MENU_RESET_TERMINAL_ID: if (session != null) { diff --git a/termux-app/src/main/res/layout/activity_termux.xml b/termux-app/src/main/res/layout/activity_termux.xml index 48c191d4..688cb023 100644 --- a/termux-app/src/main/res/layout/activity_termux.xml +++ b/termux-app/src/main/res/layout/activity_termux.xml @@ -23,7 +23,6 @@ android:focusableInTouchMode="true" android:scrollbarThumbVertical="@drawable/terminal_scroll_shape" android:scrollbars="vertical" - android:importantForAutofill="no" tools:ignore="UnusedAttribute" /> Share selected text Terminal Text Send selected text to: + Autofill username Autofill password Reset Terminal reset