Merge branch 'keyboardReopen' of https://github.com/Drhaal/nextcloud-notes into Drhaal-keyboardReopen

This commit is contained in:
Stefan Niedermann 2021-06-17 16:20:21 +02:00
commit f95175e8fa
2 changed files with 63 additions and 10 deletions

View file

@ -16,6 +16,7 @@ import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.inputmethod.InputMethodManager;
import android.widget.ScrollView;
@ -29,6 +30,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.FragmentNoteEditBinding;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.model.ISyncCallback;
import it.niedermann.owncloud.notes.shared.util.DisplayUtils;
import static androidx.core.view.ViewCompat.isAttachedToWindow;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.getFontSizeFromPreferences;
@ -59,6 +61,7 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
}
};
private TextWatcher textWatcher;
private boolean keyboardShown = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -138,22 +141,17 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
public void onResume() {
super.onResume();
binding.editContent.addTextChangedListener(textWatcher);
if (keyboardShown) {
openSoftKeyboard();
}
}
@Override
protected void onNoteLoaded(Note note) {
super.onNoteLoaded(note);
if (TextUtils.isEmpty(note.getContent())) {
binding.editContent.post(() -> {
binding.editContent.requestFocus();
final InputMethodManager imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(binding.editContent, InputMethodManager.SHOW_IMPLICIT);
} else {
Log.e(TAG, InputMethodManager.class.getSimpleName() + " is null.");
}
});
openSoftKeyboard();
}
binding.editContent.setMarkdownString(note.getContent());
@ -166,11 +164,32 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
}
}
private void openSoftKeyboard() {
binding.editContent.postDelayed(() -> {
binding.editContent.requestFocus();
final InputMethodManager imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(binding.editContent, InputMethodManager.SHOW_IMPLICIT);
} else {
Log.e(TAG, InputMethodManager.class.getSimpleName() + " is null.");
}
//Without a small delay the keyboard does not show reliably
}, 100);
}
@Override
public void onPause() {
super.onPause();
binding.editContent.removeTextChangedListener(textWatcher);
cancelTimers();
final ViewGroup parentView = requireActivity().findViewById(android.R.id.content);
if(parentView != null && parentView.getChildCount() > 0){
keyboardShown = DisplayUtils.isSoftKeyboardVisible(parentView.getChildAt(0));
}else {
keyboardShown = false;
}
}
private void cancelTimers() {

View file

@ -3,15 +3,22 @@ package it.niedermann.owncloud.notes.shared.util;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.MetricAffectingSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowInsets;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.Collection;
import java.util.List;
@ -52,4 +59,31 @@ public class DisplayUtils {
}
return new NavigationItem.CategoryNavigationItem("category:" + counter.getCategory(), counter.getCategory(), counter.getTotalNotes(), icon, counter.getAccountId(), counter.getCategory());
}
/**
* Detect if the soft keyboard is open.
* On API prior to 30 we fall back to workaround which might be less reliable
*
* @param parentView View
* @return keyboardVisibility Boolean
*/
public static boolean isSoftKeyboardVisible(@NonNull View parentView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(parentView);
if(insets != null){
return insets.isVisible(WindowInsets.Type.ime());
}
}
//Fall Back to workaround
//Arbitrary keyboard height
final int defaultKeyboardHeightDP = 100;
final int EstimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
final Rect rect = new Rect();
int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, parentView.getResources().getDisplayMetrics());
parentView.getWindowVisibleDisplayFrame(rect);
int heightDiff = parentView.getRootView().getHeight() - (rect.bottom - rect.top);
return heightDiff >= estimatedKeyboardHeight;
}
}