From c9b39068aeebc020e5fe06f9383a3ee9097bb92e Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 25 Jan 2016 11:06:32 +0100 Subject: [PATCH] Android 2.3 fixes --- app/build.gradle | 2 +- .../android/calculator/EditorView.java | 2 +- .../function/EditFunctionFragment.java | 44 +++++++++++++--- .../calculator/view/EditTextCompat.java | 52 +++++++++++++++---- app/src/main/res/values/styles.xml | 2 +- 5 files changed, 80 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b7f63e75..3baf9c56 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,7 +41,7 @@ android { zipAlignEnabled true } debug { - minifyEnabled false + minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-debug.cfg' } } diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorView.java b/app/src/main/java/org/solovyev/android/calculator/EditorView.java index 93d33628..e0ff304f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorView.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -65,7 +65,7 @@ public class EditorView extends EditTextCompat { private void init() { addTextChangedListener(new MyTextWatcher()); - setShowSoftInputOnFocusCompat(false); + dontShowSoftInputOnFocusCompat(); // changes should only be reported after the view has been set up completely, i.e. now reportChanges = true; } diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index ed7913c0..72e0a189 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -36,25 +36,50 @@ import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextUtils; -import android.view.*; +import android.view.ContextMenu; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; -import butterknife.Bind; -import butterknife.ButterKnife; -import jscl.math.function.CustomFunction; -import jscl.math.function.Function; + import org.solovyev.android.Check; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.AppComponent; +import org.solovyev.android.calculator.BaseDialogFragment; +import org.solovyev.android.calculator.Calculator; +import org.solovyev.android.calculator.CalculatorEventType; +import org.solovyev.android.calculator.FunctionsRegistry; +import org.solovyev.android.calculator.KeyboardUi; +import org.solovyev.android.calculator.KeyboardWindow; +import org.solovyev.android.calculator.Locator; +import org.solovyev.android.calculator.ParseException; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.VarsRegistry; import org.solovyev.android.calculator.math.edit.FunctionsActivity; import org.solovyev.android.calculator.math.edit.VarEditorSaver; import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.common.math.MathRegistry; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; -import java.util.*; + +import butterknife.Bind; +import butterknife.ButterKnife; +import jscl.math.function.CustomFunction; +import jscl.math.function.Function; import static org.solovyev.android.calculator.function.CppFunction.NO_ID; @@ -390,7 +415,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC bodyView.setOnClickListener(this); bodyView.setOnFocusChangeListener(this); bodyView.setOnKeyListener(this); - bodyView.setShowSoftInputOnFocusCompat(false); + bodyView.dontShowSoftInputOnFocusCompat(); descriptionView.setOnFocusChangeListener(this); return view; @@ -468,6 +493,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { final int id = v.getId(); if (id == R.id.function_body) { + menu.clear(); addEntities(menu, getNamesSorted(constantsRegistry), MENU_CONSTANT); unregisterForContextMenu(bodyView); } @@ -490,6 +516,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { final int id = v.getId(); if (id == R.id.function_body) { + menu.clear(); addEntities(menu, getNamesSorted(functionsRegistry), MENU_FUNCTION); unregisterForContextMenu(bodyView); } @@ -511,6 +538,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { final int id = v.getId(); if (id == R.id.function_body) { + menu.clear(); // can't use sub-menus as AlertDialog doesn't support them menu.add(MENU_CATEGORY, MENU_CONSTANT, Menu.NONE, R.string.c_vars_and_constants).setOnMenuItemClickListener(KeyboardUser.this); menu.add(MENU_CATEGORY, MENU_FUNCTION, Menu.NONE, R.string.c_functions).setOnMenuItemClickListener(KeyboardUser.this); diff --git a/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java b/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java index 776a749c..ed7da7fe 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java @@ -3,18 +3,22 @@ package org.solovyev.android.calculator.view; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; +import android.text.InputType; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; + import org.solovyev.android.Check; -import javax.annotation.Nullable; import java.lang.reflect.Method; +import javax.annotation.Nullable; + public class EditTextCompat extends EditText { @Nullable - private Method setShowSoftInputOnFocusMethod; + private static Method setShowSoftInputOnFocusMethod; + private static boolean setShowSoftInputOnFocusMethodChecked; public EditTextCompat(Context context) { super(context); @@ -33,25 +37,51 @@ public class EditTextCompat extends EditText { super(context, attrs, defStyleAttr, defStyleRes); } - public void setShowSoftInputOnFocusCompat(boolean show) { + public void dontShowSoftInputOnFocusCompat() { Check.isMainThread(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setShowSoftInputOnFocus(show); + setShowSoftInputOnFocus(false); } else { - setShowSoftInputOnFocusPreLollipop(show); + dontShowSoftInputOnFocusPreLollipop(); } } - private void setShowSoftInputOnFocusPreLollipop(boolean show) { + private void dontShowSoftInputOnFocusPreLollipop() { + final Method method = getSetShowSoftInputOnFocusMethod(); + if (method == null) { + disableSoftInputFromAppearing(); + return; + } try { - if (setShowSoftInputOnFocusMethod == null) { - setShowSoftInputOnFocusMethod = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class); - setShowSoftInputOnFocusMethod.setAccessible(true); - } - setShowSoftInputOnFocusMethod.invoke(this, show); + method.invoke(this, false); } catch (Exception e) { Log.w("EditTextCompat", e.getMessage(), e); } } + + @Nullable + private Method getSetShowSoftInputOnFocusMethod() { + if (setShowSoftInputOnFocusMethodChecked) { + return setShowSoftInputOnFocusMethod; + } + setShowSoftInputOnFocusMethodChecked = true; + try { + setShowSoftInputOnFocusMethod = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class); + setShowSoftInputOnFocusMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + Log.w("EditTextCompat", e.getMessage(), e); + } + return setShowSoftInputOnFocusMethod; + } + + public void disableSoftInputFromAppearing() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + setRawInputType(InputType.TYPE_CLASS_TEXT); + setTextIsSelectable(true); + } else { + setRawInputType(InputType.TYPE_NULL); + setFocusable(true); + } + } } diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 6c560169..5c608e2e 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -269,7 +269,7 @@ wrap_content match_parent center_vertical|left - ?android:attr/selectableItemBackground + ?attr/selectableItemBackground