Skip to content

Commit

Permalink
Added|Fixed: Do not show AutoFill UI on Termux start and add support …
Browse files Browse the repository at this point in the history
…for usernames

Merge termux/termux-app#4125 and termux/termux-app#4165

Co-authored-by: @AChep <mail@artemchep.com>
Co-authored-by: @agnostic-apollo  <agnosticapollo@gmail.com>
  • Loading branch information
3 people committed Oct 21, 2024
1 parent d325985 commit 5b0c558
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
29 changes: 28 additions & 1 deletion terminal-view/src/main/java/com/termux/view/TerminalView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 19 additions & 16 deletions termux-app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -541,20 +542,19 @@ 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);

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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion termux-app/src/main/res/layout/activity_termux.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
android:focusableInTouchMode="true"
android:scrollbarThumbVertical="@drawable/terminal_scroll_shape"
android:scrollbars="vertical"
android:importantForAutofill="no"
tools:ignore="UnusedAttribute" />

<LinearLayout
Expand Down
1 change: 1 addition & 0 deletions termux-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<string name="action_share_selected_text">Share selected text</string>
<string name="title_share_selected_text">Terminal Text</string>
<string name="title_share_selected_text_with">Send selected text to:</string>
<string name="action_autofill_username">Autofill username</string>
<string name="action_autofill_password">Autofill password</string>
<string name="action_reset_terminal">Reset</string>
<string name="msg_terminal_reset">Terminal reset</string>
Expand Down

0 comments on commit 5b0c558

Please sign in to comment.