Skip to content

Commit

Permalink
Improvements to keeping keyboard open after dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Sep 25, 2023
1 parent 03aaafc commit 7b1db5b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
android:parentActivityName=".activity.MainActivity"
android:taskAffinity=".activity.DocumentActivity"
android:theme="@style/AppTheme.Unified.StartupFlash"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
android:windowSoftInputMode="stateHidden|adjustResize"
tools:targetApi="lollipop">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,9 @@ public void setViewModeVisibility(boolean show) {
_format.getActions().recreateActionButtons(_textActionsBar, show ? ActionButtonBase.ActionItem.DisplayMode.VIEW : ActionButtonBase.ActionItem.DisplayMode.EDIT);
if (show) {
updateViewModeText();
_cu.showSoftKeyboard(activity, false, _hlEditor);
_cu.showIme(activity, false, _hlEditor);
_hlEditor.clearFocus();
_hlEditor.postDelayed(() -> _cu.showSoftKeyboard(activity, false, _hlEditor), 300);
_hlEditor.postDelayed(() -> _cu.showIme(activity, false, _hlEditor), 300);
fadeInOut(_webView, _primaryScrollView);
} else {
_webViewClient.setRestoreScrollY(_webView.getScrollY());
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/net/gsantner/markor/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
Expand All @@ -43,6 +44,7 @@
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserFragment;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserListAdapter;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserOptions;
import net.gsantner.opoc.util.GsContextUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -372,19 +374,18 @@ public String getPosTitle(final int pos) {
return null;
}

public void onViewPagerPageSelected(int pos) {
public void onViewPagerPageSelected(final int pos) {
_bottomNav.getMenu().getItem(pos).setChecked(true);

if (pos == tabIdToPos(R.id.nav_notebook)) {
_fab.show();
_cu.showIme(this, false);
} else {
_fab.hide();
}
setTitle(getPosTitle(pos));

if (pos != tabIdToPos(R.id.nav_notebook)) {
restoreDefaultToolbar();
}

setTitle(getPosTitle(pos));
}

private GsFileBrowserOptions.Options _filesystemDialogOptions = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.Document;
import net.gsantner.opoc.util.GsCollectionUtils;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;
import net.gsantner.opoc.wrapper.GsCallback;

Expand Down Expand Up @@ -204,11 +205,11 @@ public boolean onActionLongClick(final @StringRes int action) {

switch (action) {
case R.string.abid_todotxt_add_context: {
MarkorDialogFactory.showSttKeySearchDialog(getActivity(), _hlEditor, R.string.browse_by_context, true, true, TextViewUtils.isImeOpen(_hlEditor), TodoTxtFilter.TYPE.CONTEXT);
MarkorDialogFactory.showSttKeySearchDialog(getActivity(), _hlEditor, R.string.browse_by_context, true, true, GsContextUtils.isImeOpen(_hlEditor), TodoTxtFilter.TYPE.CONTEXT);
return true;
}
case R.string.abid_todotxt_add_project: {
MarkorDialogFactory.showSttKeySearchDialog(getActivity(), _hlEditor, R.string.browse_by_project, true, true, TextViewUtils.isImeOpen(_hlEditor), TodoTxtFilter.TYPE.PROJECT);
MarkorDialogFactory.showSttKeySearchDialog(getActivity(), _hlEditor, R.string.browse_by_project, true, true, GsContextUtils.isImeOpen(_hlEditor), TodoTxtFilter.TYPE.PROJECT);
return true;
}
case R.string.abid_todotxt_sort_todo: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import android.content.Context;
import android.text.Editable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

Expand Down Expand Up @@ -159,7 +157,7 @@ public static void showInsertImageOrLinkDialog(
}
};

final Boolean restoreIme = TextViewUtils.isImeOpen(edit);
final Boolean restoreIme = GsContextUtils.isImeOpen(edit);

final AlertDialog dialog =
builder.setView(view)
Expand Down Expand Up @@ -217,7 +215,7 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {

dialog.setOnDismissListener(d -> {
LocalBroadcastManager.getInstance(activity).unregisterReceiver(br);
edit.postDelayed(() -> GsContextUtils.instance.showSoftKeyboard(activity, restoreIme, edit), 250);
edit.postDelayed(() -> GsContextUtils.instance.showIme(activity, restoreIme, edit), 250);
});

// Get picture from camera
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import android.text.TextUtils;
import android.util.Pair;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
Expand Down Expand Up @@ -235,7 +234,7 @@ public static void showSttFilteringDialog(final Activity activity, final EditTex
final DialogOptions dopt = new DialogOptions();
baseConf(activity, dopt);

final Boolean showIme = TextViewUtils.isImeOpen(text);
final Boolean showIme = GsContextUtils.isImeOpen(text);

final List<String> options = new ArrayList<>();
final List<Integer> icons = new ArrayList<>();
Expand Down Expand Up @@ -489,11 +488,11 @@ public static void showSttSearchDialog(final Activity activity, final EditText t

// Restore the keyboard when dialog closes if keyboard is open when dialog was called
private static void addRestoreKeyboard(final Activity activity, final DialogOptions options, final EditText edit) {
addRestoreKeyboard(activity, options, edit, TextViewUtils.isImeOpen(edit));
addRestoreKeyboard(activity, options, edit, GsContextUtils.isImeOpen(edit));
}

private static void addRestoreKeyboard(final Activity activity, final DialogOptions options, final EditText edit, final Boolean restore) {
options.dismissCallback = (d) -> edit.postDelayed(() -> GsContextUtils.instance.showSoftKeyboard(activity, restore, edit), 250);
options.dismissCallback = (d) -> edit.postDelayed(() -> GsContextUtils.instance.showIme(activity, restore, edit), 250);
}

/**
Expand Down Expand Up @@ -818,7 +817,7 @@ public static void showInsertSnippetDialog(final Activity activity, @Nullable fi
baseConf(activity, dopt);

final Map<String, File> texts = getSnippets(as());
final Boolean showIme = edit != null ? TextViewUtils.isImeOpen(edit) : null;
final Boolean showIme = edit != null ? GsContextUtils.isImeOpen(edit) : null;

final List<String> data = new ArrayList<>(texts.keySet());
dopt.data = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
#########################################################*/
package net.gsantner.markor.frontend.textview;

import android.animation.ObjectAnimator;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
Expand All @@ -22,7 +20,6 @@
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowInsets;
import android.widget.EditText;
import android.widget.TextView;

Expand Down Expand Up @@ -725,12 +722,4 @@ public static boolean isViewVisible(final View view) {
final Rect screen = new Rect(0, 0, metrics.widthPixels, metrics.heightPixels);
return isGlobalVisible && Rect.intersects(actualPosition, screen);
}

// Check if keyboard open. Only available after android 11 :(
public static Boolean isImeOpen(final View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return view.getRootWindowInsets().isVisible(WindowInsets.Type.ime());
}
return null; // Uncertain
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void onViewCreated(View root, @Nullable Bundle savedInstanceState) {
_searchEdit.setTextColor(rcolor(_dopt.primaryTextColor));
_searchEdit.setHintTextColor(rcolor(_dopt.secondaryTextColor));
_searchEdit.setOnFocusChangeListener((v, isFocussed) -> {
GsContextUtils.instance.showSoftKeyboard(getActivity(), isFocussed, _searchEdit);
GsContextUtils.instance.showIme(getActivity(), isFocussed, _searchEdit);
});

root.setBackgroundColor(rcolor(_dopt.backgroundColor));
Expand Down Expand Up @@ -210,7 +210,7 @@ public void onClicked(View view) {
@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
GsContextUtils.instance.showSoftKeyboard(getActivity(), false, _searchEdit);
GsContextUtils.instance.showIme(getActivity(), false, _searchEdit);
}

private void showNewDirDialog() {
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/net/gsantner/opoc/util/GsContextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.MimeTypeMap;
Expand Down Expand Up @@ -149,7 +150,6 @@
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
Expand Down Expand Up @@ -2499,7 +2499,16 @@ public void showSnackBar(final Activity context, @StringRes int stringResId, boo
.show();
}

public <T extends GsContextUtils> T showSoftKeyboard(final Activity activity, final Boolean show, final View... view) {

// Check if keyboard open. Only available after android 11 :(
public static Boolean isImeOpen(final View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return view.getRootWindowInsets().isVisible(WindowInsets.Type.ime());
}
return null; // Uncertain
}

public <T extends GsContextUtils> T showIme(final Activity activity, final Boolean show, final View... view) {
if (activity != null && show != null) {
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
final View focus = (view != null && view.length > 0) ? view[0] : activity.getCurrentFocus();
Expand Down

0 comments on commit 7b1db5b

Please sign in to comment.