Minor code improvements

Signed-off-by: Stefan Niedermann <info@niedermann.it>
This commit is contained in:
Stefan Niedermann 2021-06-17 16:30:59 +02:00
parent f95175e8fa
commit 35605e4a0a
2 changed files with 7 additions and 23 deletions

View file

@ -16,7 +16,6 @@ import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ScrollView; import android.widget.ScrollView;
@ -184,10 +183,10 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
binding.editContent.removeTextChangedListener(textWatcher); binding.editContent.removeTextChangedListener(textWatcher);
cancelTimers(); cancelTimers();
final ViewGroup parentView = requireActivity().findViewById(android.R.id.content); final ViewGroup parentView = requireActivity().findViewById(android.R.id.content);
if(parentView != null && parentView.getChildCount() > 0){ if (parentView != null && parentView.getChildCount() > 0) {
keyboardShown = DisplayUtils.isSoftKeyboardVisible(parentView.getChildAt(0)); keyboardShown = DisplayUtils.isSoftKeyboardVisible(parentView.getChildAt(0));
}else { } else {
keyboardShown = false; keyboardShown = false;
} }
} }

View file

@ -2,34 +2,21 @@ package it.niedermann.owncloud.notes.shared.util;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Build; 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.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.WindowInsets; import android.view.WindowInsets;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import it.niedermann.android.util.ColorUtil;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.main.navigation.NavigationAdapter; import it.niedermann.owncloud.notes.main.navigation.NavigationAdapter;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem; import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount; import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount;
@ -69,21 +56,19 @@ public class DisplayUtils {
*/ */
public static boolean isSoftKeyboardVisible(@NonNull View parentView) { public static boolean isSoftKeyboardVisible(@NonNull View parentView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(parentView); final WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(parentView);
if(insets != null){ if (insets != null) {
return insets.isVisible(WindowInsets.Type.ime()); return insets.isVisible(WindowInsets.Type.ime());
} }
} }
//Fall Back to workaround
//Arbitrary keyboard height //Arbitrary keyboard height
final int defaultKeyboardHeightDP = 100; final int defaultKeyboardHeightDP = 100;
final int EstimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0); final int EstimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
final Rect rect = new Rect(); final Rect rect = new Rect();
final int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, parentView.getResources().getDisplayMetrics());
int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, parentView.getResources().getDisplayMetrics());
parentView.getWindowVisibleDisplayFrame(rect); parentView.getWindowVisibleDisplayFrame(rect);
int heightDiff = parentView.getRootView().getHeight() - (rect.bottom - rect.top); final int heightDiff = parentView.getRootView().getHeight() - (rect.bottom - rect.top);
return heightDiff >= estimatedKeyboardHeight; return heightDiff >= estimatedKeyboardHeight;
} }
} }