From 0b5bc2b62132742c28a0e5b020972a543a29366a Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 23 Jan 2016 21:13:56 +0100 Subject: [PATCH] Categories removed --- .../android/calculator/ActivityUi.java | 4 +- .../calculator/AndroidFunctionCategory.java | 61 ------------------- .../calculator/AndroidOperatorCategory.java | 60 ------------------ .../calculator/AndroidVarCategory.java | 58 ------------------ .../solovyev/android/calculator/Category.java | 16 +++++ .../android/calculator/EditorView.java | 35 +---------- .../solovyev/android/calculator/Engine.java | 27 ++++---- .../android/calculator/EntitiesRegistry.java | 2 +- .../android/calculator/FunctionCategory.java | 29 +++++---- .../android/calculator/FunctionsRegistry.java | 27 +++----- .../android/calculator/KeyboardWindow.java | 6 +- .../android/calculator/OperatorCategory.java | 39 ++++++------ .../android/calculator/OperatorsRegistry.java | 4 +- .../calculator/PostfixFunctionsRegistry.java | 4 +- .../android/calculator/VarCategory.java | 29 +++++---- .../android/calculator/VarsRegistry.java | 4 +- .../function/EditFunctionFragment.java | 25 ++++---- .../math/edit/BaseEntitiesFragment.java | 27 ++++---- .../edit/CalculatorFunctionsActivity.java | 23 +++---- .../edit/CalculatorOperatorsActivity.java | 8 +-- .../math/edit/CalculatorVarsActivity.java | 11 +--- .../math/edit/FunctionsFragment.java | 10 +-- .../math/edit/OperatorsFragment.java | 22 +++---- .../calculator/math/edit/VarsFragment.java | 2 +- .../calculator/view/EditTextCompat.java | 57 +++++++++++++++++ .../res/layout/fragment_function_edit.xml | 3 +- 26 files changed, 204 insertions(+), 389 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/AndroidOperatorCategory.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/AndroidVarCategory.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/Category.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java diff --git a/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java b/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java index 9f1c05f5..cc13194d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java @@ -34,13 +34,12 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; -import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; - import org.solovyev.android.Activities; +import org.solovyev.android.Check; import org.solovyev.android.Views; import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Languages; @@ -227,6 +226,7 @@ public class ActivityUi extends BaseUi { int captionResId, int parentViewId) { final ActionBar actionBar = activity.getSupportActionBar(); + Check.isNotNull(actionBar); final ActionBar.Tab tab = actionBar.newTab(); tab.setTag(tag); diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java b/app/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java deleted file mode 100644 index 7c32e597..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidFunctionCategory.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 10/7/12 - * Time: 7:17 PM - */ -public enum AndroidFunctionCategory { - - trigonometric(R.string.c_fun_category_trig), - hyperbolic_trigonometric(R.string.c_fun_category_hyper_trig), - comparison(R.string.c_fun_category_comparison), - my(R.string.c_fun_category_my), - common(R.string.c_fun_category_common); - - private final int captionId; - - AndroidFunctionCategory(int captionId) { - this.captionId = captionId; - } - - @Nullable - public static AndroidFunctionCategory valueOf(@Nonnull FunctionCategory functionCategory) { - for (AndroidFunctionCategory androidFunctionCategory : values()) { - if (androidFunctionCategory.name().equals(functionCategory.name())) { - return androidFunctionCategory; - } - } - - return null; - } - - public int getCaptionId() { - return captionId; - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidOperatorCategory.java b/app/src/main/java/org/solovyev/android/calculator/AndroidOperatorCategory.java deleted file mode 100644 index bb96cd2d..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidOperatorCategory.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 10/7/12 - * Time: 7:41 PM - */ -public enum AndroidOperatorCategory { - - derivatives(R.string.derivatives), - other(R.string.other), - my(R.string.c_fun_category_my), - common(R.string.c_fun_category_common); - - private final int captionId; - - AndroidOperatorCategory(int captionId) { - this.captionId = captionId; - } - - @Nullable - public static AndroidOperatorCategory valueOf(@Nonnull OperatorCategory operatorCategory) { - for (AndroidOperatorCategory androidOperatorCategory : values()) { - if (androidOperatorCategory.name().equals(operatorCategory.name())) { - return androidOperatorCategory; - } - } - - return null; - } - - public int getCaptionId() { - return captionId; - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidVarCategory.java b/app/src/main/java/org/solovyev/android/calculator/AndroidVarCategory.java deleted file mode 100644 index 1cab9df3..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidVarCategory.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 10/7/12 - * Time: 7:56 PM - */ -public enum AndroidVarCategory { - - system(R.string.c_var_system), - my(R.string.c_var_my); - - private final int captionId; - - AndroidVarCategory(int captionId) { - this.captionId = captionId; - } - - @Nullable - public static AndroidVarCategory valueOf(@Nonnull VarCategory varCategory) { - for (AndroidVarCategory androidVarCategory : values()) { - if (androidVarCategory.name().equals(varCategory.name())) { - return androidVarCategory; - } - } - - return null; - } - - public int getCaptionId() { - return captionId; - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/Category.java b/app/src/main/java/org/solovyev/android/calculator/Category.java new file mode 100644 index 00000000..aa3f78f7 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/Category.java @@ -0,0 +1,16 @@ +package org.solovyev.android.calculator; + +import android.support.annotation.StringRes; + +import javax.annotation.Nonnull; + +public interface Category { + + int ordinal(); + + @Nonnull + String name(); + + @StringRes + int title(); +} 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 bcced567..93d33628 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorView.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -28,27 +28,18 @@ import android.os.Build; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; -import android.util.Log; import android.view.ContextMenu; -import android.widget.EditText; - import org.solovyev.android.Check; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; - -import java.lang.reflect.Method; +import org.solovyev.android.calculator.view.EditTextCompat; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class EditorView extends EditText { - - @Nonnull - private static final String TAG = App.subTag("EditorView"); +public class EditorView extends EditTextCompat { private boolean reportChanges; @Nullable - private Method setShowSoftInputOnFocusMethod; - @Nullable private Editor editor; public EditorView(Context context) { @@ -123,28 +114,6 @@ public class EditorView extends EditText { editor.setSelection(start); } - public void setShowSoftInputOnFocusCompat(boolean show) { - Check.isMainThread(); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setShowSoftInputOnFocus(show); - } else { - setShowSoftInputOnFocusPreLollipop(show); - } - } - - private void setShowSoftInputOnFocusPreLollipop(boolean show) { - try { - if (setShowSoftInputOnFocusMethod == null) { - setShowSoftInputOnFocusMethod = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class); - setShowSoftInputOnFocusMethod.setAccessible(true); - } - setShowSoftInputOnFocusMethod.invoke(this, show); - } catch (Exception e) { - Log.w(TAG, e.getMessage(), e); - } - } - private class MyTextWatcher implements TextWatcher { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { diff --git a/app/src/main/java/org/solovyev/android/calculator/Engine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java index 258a699d..34fede7c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -23,9 +23,12 @@ package org.solovyev.android.calculator; import android.content.SharedPreferences; - import com.squareup.otto.Bus; - +import jscl.AngleUnit; +import jscl.JsclMathEngine; +import jscl.MathEngine; +import jscl.NumeralBase; +import jscl.math.operator.Operator; import org.solovyev.android.Check; import org.solovyev.android.calculator.model.EntityDao; import org.solovyev.android.calculator.model.Vars; @@ -37,29 +40,21 @@ import org.solovyev.common.text.EnumMapper; import org.solovyev.common.text.NumberMapper; import org.solovyev.common.text.Strings; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Singleton; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; -import javax.annotation.Nonnull; -import javax.inject.Inject; -import javax.inject.Singleton; - -import jscl.AngleUnit; -import jscl.JsclMathEngine; -import jscl.MathEngine; -import jscl.NumeralBase; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; - @Singleton public class Engine implements SharedPreferences.OnSharedPreferenceChangeListener { @Nonnull private final MathEngine mathEngine; @Nonnull - private final EntitiesRegistry varsRegistry; + private final VarsRegistry varsRegistry; @Nonnull private final EntitiesRegistry operatorsRegistry; @@ -76,7 +71,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene @Nonnull private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue(); - public Engine(@Nonnull MathEngine mathEngine, @Nonnull EntitiesRegistry varsRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull EntitiesRegistry operatorsRegistry, @Nonnull EntitiesRegistry postfixFunctionsRegistry) { + public Engine(@Nonnull MathEngine mathEngine, @Nonnull VarsRegistry varsRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull EntitiesRegistry operatorsRegistry, @Nonnull EntitiesRegistry postfixFunctionsRegistry) { this.mathEngine = mathEngine; this.varsRegistry = varsRegistry; this.functionsRegistry = functionsRegistry; @@ -111,7 +106,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene } @Nonnull - public EntitiesRegistry getVarsRegistry() { + public VarsRegistry getVarsRegistry() { return varsRegistry; } diff --git a/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java index 95971203..becd2116 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java @@ -34,7 +34,7 @@ public interface EntitiesRegistry extends MathRegistry String getDescription(@Nonnull String name); @Nullable - String getCategory(@Nonnull E entity); + Category getCategory(@Nonnull E entity); void init(); diff --git a/app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java b/app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java index 8b28eb70..87dd3b01 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator; +import android.support.annotation.StringRes; import jscl.math.function.ArcTrigonometric; import jscl.math.function.Comparison; import jscl.math.function.Function; @@ -31,21 +32,16 @@ import org.solovyev.common.collections.Collections; import javax.annotation.Nonnull; import java.util.*; -/** - * User: serso - * Date: 10/7/12 - * Time: 7:15 PM - */ -public enum FunctionCategory { +public enum FunctionCategory implements Category { - trigonometric(100) { + trigonometric(100, R.string.c_fun_category_trig) { @Override public boolean isInCategory(@Nonnull Function function) { return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function); } }, - hyperbolic_trigonometric(300) { + hyperbolic_trigonometric(300, R.string.c_fun_category_hyper_trig) { private final Set names = new HashSet<>(Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth")); @@ -55,21 +51,21 @@ public enum FunctionCategory { } }, - comparison(200) { + comparison(200, R.string.c_fun_category_comparison) { @Override public boolean isInCategory(@Nonnull Function function) { return function instanceof Comparison; } }, - my(0) { + my(0, R.string.c_fun_category_my) { @Override public boolean isInCategory(@Nonnull Function function) { return !function.isSystem(); } }, - common(50) { + common(50, R.string.c_fun_category_common) { @Override public boolean isInCategory(@Nonnull Function function) { for (FunctionCategory category : values()) { @@ -84,10 +80,12 @@ public enum FunctionCategory { } }; - private final int tabOrder; + public final int tabOrder; + public final int title; - FunctionCategory(int tabOrder) { + FunctionCategory(int tabOrder, @StringRes int title) { this.tabOrder = tabOrder; + this.title = title; } @Nonnull @@ -104,5 +102,10 @@ public enum FunctionCategory { return result; } + @Override + public int title() { + return title; + } + public abstract boolean isInCategory(@Nonnull Function function); } diff --git a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java index 553a59d7..57fb8209 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java @@ -26,9 +26,10 @@ import android.app.Application; import android.content.SharedPreferences; import android.os.Handler; import android.support.annotation.NonNull; - import com.squareup.otto.Bus; - +import jscl.JsclMathEngine; +import jscl.math.function.CustomFunction; +import jscl.math.function.Function; import org.json.JSONArray; import org.json.JSONException; import org.simpleframework.xml.Serializer; @@ -42,25 +43,15 @@ import org.solovyev.android.io.FileSaver; import org.solovyev.common.JBuilder; import org.solovyev.common.text.Strings; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; - import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; - -import jscl.JsclMathEngine; -import jscl.math.function.CustomFunction; -import jscl.math.function.Function; +import java.io.File; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.Executor; import static android.text.TextUtils.isEmpty; @@ -186,10 +177,10 @@ public class FunctionsRegistry extends BaseEntitiesRegistry parameterNames) { + final EditText editor = user.getEditor(); if (isShown()) { + hideIme(editor); return; } this.dialog = dialog; moveDialog(Gravity.TOP); - final EditText editor = user.getEditor(); hideIme(editor); final Context context = editor.getContext(); final LinearLayout view = new LinearLayout(context); diff --git a/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java b/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java index ab79309b..1970ff92 100644 --- a/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java @@ -22,49 +22,38 @@ package org.solovyev.android.calculator; +import android.support.annotation.StringRes; +import jscl.math.operator.*; import org.solovyev.common.collections.Collections; +import javax.annotation.Nonnull; import java.util.Comparator; import java.util.List; -import javax.annotation.Nonnull; +public enum OperatorCategory implements Category { -import jscl.math.operator.Derivative; -import jscl.math.operator.IndefiniteIntegral; -import jscl.math.operator.Integral; -import jscl.math.operator.Operator; -import jscl.math.operator.Product; -import jscl.math.operator.Sum; - -/** - * User: serso - * Date: 10/7/12 - * Time: 7:40 PM - */ -public enum OperatorCategory { - - derivatives(100) { + derivatives(100, R.string.derivatives) { @Override public boolean isInCategory(@Nonnull Operator operator) { return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral; } }, - other(200) { + other(200, R.string.other) { @Override public boolean isInCategory(@Nonnull Operator operator) { return operator instanceof Sum || operator instanceof Product; } }, - my(0) { + my(0, R.string.c_fun_category_my) { @Override public boolean isInCategory(@Nonnull Operator operator) { return !operator.isSystem(); } }, - common(50) { + common(50, R.string.c_fun_category_common) { @Override public boolean isInCategory(@Nonnull Operator operator) { for (OperatorCategory category : values()) { @@ -80,9 +69,12 @@ public enum OperatorCategory { }; private final int tabOrder; + @StringRes + private final int title; - OperatorCategory(int tabOrder) { + OperatorCategory(int tabOrder, @StringRes int title) { this.tabOrder = tabOrder; + this.title = title; } @Nonnull @@ -102,5 +94,12 @@ public enum OperatorCategory { return result; } + + public abstract boolean isInCategory(@Nonnull Operator operator); + + @Override + public int title() { + return title; + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java index 38aa7f66..f60ddf7a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java @@ -57,10 +57,10 @@ public class OperatorsRegistry extends BaseEntitiesRegistry { } @Override - public String getCategory(@Nonnull IConstant var) { + public Category getCategory(@Nonnull IConstant var) { for (VarCategory category : VarCategory.values()) { if (category.isInCategory(var)) { - return category.name(); + return category; } } 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 fb7cbc4c..63770664 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 @@ -44,12 +44,12 @@ import butterknife.Bind; import butterknife.ButterKnife; import jscl.math.function.CustomFunction; import jscl.math.function.Function; -import jscl.math.function.IConstant; import org.solovyev.android.Check; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; import org.solovyev.android.calculator.math.edit.FunctionsFragment; import org.solovyev.android.calculator.math.edit.VarEditorSaver; +import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.common.math.MathRegistry; import javax.annotation.Nonnull; @@ -67,9 +67,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC private static final int MENU_CATEGORY = Menu.FIRST + 2; @NonNull - private final MathRegistry functionsRegistry = Locator.getInstance().getEngine().getFunctionsRegistry(); - @NonNull - private final MathRegistry constantsRegistry = Locator.getInstance().getEngine().getVarsRegistry(); + private final VarsRegistry constantsRegistry = Locator.getInstance().getEngine().getVarsRegistry(); @NonNull private final KeyboardWindow keyboardWindow = new KeyboardWindow(); @NonNull @@ -83,13 +81,13 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC @Bind(R.id.function_body_label) TextInputLayout bodyLabel; @Bind(R.id.function_body) - EditText bodyView; + EditTextCompat bodyView; @Bind(R.id.function_description) EditText descriptionView; @Inject Calculator calculator; @Inject - FunctionsRegistry registry; + FunctionsRegistry functionsRegistry; @Nullable private CppFunction function; @@ -175,6 +173,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC } }); if (!isNewFunction()) { + Check.isNotNull(function); final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); neutral.setOnClickListener(new View.OnClickListener() { @Override @@ -191,7 +190,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC return; } final CustomFunction entity = function.toCustomFunctionBuilder().create(); - registry.remove(entity); + functionsRegistry.remove(entity); calculator.fireCalculatorEvent(CalculatorEventType.function_removed, entity, this); dismiss(); } @@ -293,8 +292,8 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC .withId(isNewFunction() ? NO_ID : function.id) .withParameters(collectParameters()) .withDescription(descriptionView.getText().toString()).build(); - final Function oldFunction = isNewFunction() ? null : registry.getById(function.id); - registry.add(newFunction.toCustomFunctionBuilder(), oldFunction); + final Function oldFunction = isNewFunction() ? null : functionsRegistry.getById(function.id); + functionsRegistry.add(newFunction.toCustomFunctionBuilder(), oldFunction); return true; } catch (RuntimeException e) { setError(bodyLabel, e.getLocalizedMessage()); @@ -312,7 +311,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC setError(nameLabel, getString(R.string.function_name_is_not_valid)); return false; } - final Function existingFunction = registry.get(name); + final Function existingFunction = functionsRegistry.get(name); if (existingFunction != null) { if (!existingFunction.isIdDefined()) { Check.shouldNotHappen(); @@ -385,6 +384,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC bodyView.setOnClickListener(this); bodyView.setOnFocusChangeListener(this); bodyView.setOnKeyListener(this); + bodyView.setShowSoftInputOnFocusCompat(false); descriptionView.setOnFocusChangeListener(this); return view; @@ -564,9 +564,10 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC bodyView.post(new Runnable() { @Override public void run() { - if (item.getItemId() == MENU_FUNCTION) { + final int itemId = item.getItemId(); + if (itemId == MENU_FUNCTION) { showFunctions(bodyView); - } else { + } else if (itemId == MENU_CONSTANT) { showConstants(bodyView); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java index 086d9519..279d89b2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java @@ -31,34 +31,27 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.ClipboardManager; import android.text.TextUtils; -import android.view.ContextMenu; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; +import android.view.*; import android.widget.TextView; - +import butterknife.Bind; +import butterknife.ButterKnife; import com.melnykov.fab.FloatingActionButton; - import org.solovyev.android.Check; import org.solovyev.android.calculator.BaseFragment; import org.solovyev.android.calculator.CalculatorFragmentType; +import org.solovyev.android.calculator.Category; import org.solovyev.android.calculator.R; import org.solovyev.android.views.llm.DividerItemDecoration; import org.solovyev.common.math.MathEntity; import org.solovyev.common.text.Strings; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import butterknife.Bind; -import butterknife.ButterKnife; - public abstract class BaseEntitiesFragment extends BaseFragment { @@ -140,14 +133,18 @@ public abstract class BaseEntitiesFragment extends BaseFra } private boolean isInCategory(@NonNull E entity, @NonNull String category) { - return TextUtils.equals(getCategory(entity), category); + final Category entityCategory = getCategory(entity); + if (entityCategory == null) { + return false; + } + return TextUtils.equals(entityCategory.name(), category); } @Nonnull protected abstract List getEntities(); @Nullable - abstract String getCategory(@Nonnull E e); + abstract Category getCategory(@Nonnull E e); protected EntitiesAdapter getAdapter() { return adapter; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java index b59b0640..d4d6b99e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java @@ -23,9 +23,6 @@ package org.solovyev.android.calculator.math.edit; import android.os.Bundle; -import android.util.Log; - -import org.solovyev.android.calculator.AndroidFunctionCategory; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.FunctionCategory; @@ -48,22 +45,16 @@ public class CalculatorFunctionsActivity extends BaseActivity { final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions; for (FunctionCategory category : FunctionCategory.getCategoriesByTabOrder()) { - final AndroidFunctionCategory androidCategory = AndroidFunctionCategory.valueOf(category); - if (androidCategory != null) { + final Bundle fragmentParameters; - final Bundle fragmentParameters; - - if (category == FunctionCategory.my && bundle != null) { - BaseEntitiesFragment.putCategory(bundle, category.name()); - fragmentParameters = bundle; - } else { - fragmentParameters = BaseEntitiesFragment.createBundleFor(category.name()); - } - - ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidCategory.getCaptionId(), R.id.main_layout); + if (category == FunctionCategory.my && bundle != null) { + BaseEntitiesFragment.putCategory(bundle, category.name()); + fragmentParameters = bundle; } else { - Log.e(CalculatorFunctionsActivity.class.getSimpleName(), "Unable to find android function category for " + category); + fragmentParameters = BaseEntitiesFragment.createBundleFor(category.name()); } + + ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, category.title, R.id.main_layout); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java index bb5bbdcc..f1401b53 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java @@ -24,7 +24,6 @@ package org.solovyev.android.calculator.math.edit; import android.os.Bundle; -import org.solovyev.android.calculator.AndroidOperatorCategory; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.CalculatorEventListener; @@ -49,12 +48,7 @@ public class CalculatorOperatorsActivity extends BaseActivity implements Calcula final CalculatorFragmentType fragmentType = CalculatorFragmentType.operators; for (OperatorCategory category : OperatorCategory.getCategoriesByTabOrder()) { - final AndroidOperatorCategory androidCategory = AndroidOperatorCategory.valueOf(category); - if (androidCategory != null) { - ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), BaseEntitiesFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout); - } else { - ui.logError("Unable to find android operator category for " + category); - } + ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), BaseEntitiesFragment.createBundleFor(category.name()), category.title(), R.id.main_layout); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index 36372465..71c2a39c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -25,7 +25,6 @@ package org.solovyev.android.calculator.math.edit; import android.content.Intent; import android.os.Bundle; -import org.solovyev.android.calculator.AndroidVarCategory; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.CalculatorEventListener; @@ -68,15 +67,7 @@ public class CalculatorVarsActivity extends BaseActivity implements CalculatorEv } else { fragmentParameters = BaseEntitiesFragment.createBundleFor(category.name()); } - - - final AndroidVarCategory androidVarCategory = AndroidVarCategory.valueOf(category); - - if (androidVarCategory != null) { - ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidVarCategory.getCaptionId(), R.id.main_layout); - } else { - ui.logError("Unable to find android var category for " + category); - } + ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, category.title(), R.id.main_layout); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java index 71eb8eeb..a2988a23 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java @@ -34,13 +34,7 @@ import android.view.ViewGroup; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; -import org.solovyev.android.calculator.AppComponent; -import org.solovyev.android.calculator.Calculator; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.EntitiesRegistry; -import org.solovyev.android.calculator.FunctionsRegistry; -import org.solovyev.android.calculator.Keyboard; -import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.function.CppFunction; import org.solovyev.android.calculator.function.EditFunctionFragment; import org.solovyev.common.text.Strings; @@ -165,7 +159,7 @@ public class FunctionsFragment extends BaseEntitiesFragment { } @Override - protected String getCategory(@Nonnull Function function) { + protected Category getCategory(@Nonnull Function function) { return registry.getCategory(function); } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java index 600dde92..765b1c9e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java @@ -23,24 +23,16 @@ package org.solovyev.android.calculator.math.edit; import android.support.annotation.NonNull; -import android.text.TextUtils; import android.view.ContextMenu; import android.view.MenuItem; - -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.EntitiesRegistry; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.R; +import jscl.math.operator.Operator; +import org.solovyev.android.calculator.*; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.math.operator.Operator; +import java.util.ArrayList; +import java.util.List; public class OperatorsFragment extends BaseEntitiesFragment { @@ -70,9 +62,9 @@ public class OperatorsFragment extends BaseEntitiesFragment { } @Override - protected String getCategory(@Nonnull Operator operator) { - final String result = operatorsRegistry.getCategory(operator); - if (!TextUtils.isEmpty(result)) { + protected Category getCategory(@Nonnull Operator operator) { + final Category result = operatorsRegistry.getCategory(operator); + if (result != null) { return result; } return postfixFunctionsRegistry.getCategory(operator); diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java index 3ac0d2d8..eefad260 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java @@ -120,7 +120,7 @@ public class VarsFragment extends BaseEntitiesFragmentimplements Calc } @Override - protected String getCategory(@Nonnull IConstant var) { + protected Category getCategory(@Nonnull IConstant var) { return registry.getCategory(var); } 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 new file mode 100644 index 00000000..776a749c --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java @@ -0,0 +1,57 @@ +package org.solovyev.android.calculator.view; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build; +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; + +public class EditTextCompat extends EditText { + + @Nullable + private Method setShowSoftInputOnFocusMethod; + + public EditTextCompat(Context context) { + super(context); + } + + public EditTextCompat(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public EditTextCompat(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public EditTextCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public void setShowSoftInputOnFocusCompat(boolean show) { + Check.isMainThread(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setShowSoftInputOnFocus(show); + } else { + setShowSoftInputOnFocusPreLollipop(show); + } + } + + private void setShowSoftInputOnFocusPreLollipop(boolean show) { + try { + if (setShowSoftInputOnFocusMethod == null) { + setShowSoftInputOnFocusMethod = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class); + setShowSoftInputOnFocusMethod.setAccessible(true); + } + setShowSoftInputOnFocusMethod.invoke(this, show); + } catch (Exception e) { + Log.w("EditTextCompat", e.getMessage(), e); + } + } +} diff --git a/app/src/main/res/layout/fragment_function_edit.xml b/app/src/main/res/layout/fragment_function_edit.xml index 714ed30c..42f4555a 100644 --- a/app/src/main/res/layout/fragment_function_edit.xml +++ b/app/src/main/res/layout/fragment_function_edit.xml @@ -55,11 +55,12 @@ a:layout_width="match_parent" a:layout_height="wrap_content"> -