From cf322d04ee846acddbb550b2f40361ac16efea34 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 25 Sep 2012 13:57:34 +0400 Subject: [PATCH] Fragments --- .../res/layout-port/main_calculator.xml | 65 +- calculatorpp/res/layout/calc_keyboard.xml | 69 +++ .../calculator/CalculatorActivity.java | 558 ++++-------------- .../calculator/CalculatorApplication.java | 204 +++---- .../calculator/CalculatorDisplayFragment.java | 4 +- .../CalculatorKeyboardFragment.java | 497 ++++++++++++++++ 6 files changed, 786 insertions(+), 611 deletions(-) create mode 100644 calculatorpp/res/layout/calc_keyboard.xml create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java diff --git a/calculatorpp/res/layout-port/main_calculator.xml b/calculatorpp/res/layout-port/main_calculator.xml index 8cc49b13..5cc2d545 100644 --- a/calculatorpp/res/layout-port/main_calculator.xml +++ b/calculatorpp/res/layout-port/main_calculator.xml @@ -8,9 +8,9 @@ @@ -20,12 +20,14 @@ a:layout_width="match_parent" a:layout_height="0dp"/> - + + a:layout_height="match_parent"/> - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/calculatorpp/res/layout/calc_keyboard.xml b/calculatorpp/res/layout/calc_keyboard.xml new file mode 100644 index 00000000..9653cb2a --- /dev/null +++ b/calculatorpp/res/layout/calc_keyboard.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 989d3dde..25204a40 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -12,22 +12,14 @@ import android.app.FragmentTransaction; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; -import android.content.res.Configuration; import android.os.Bundle; -import android.os.Vibrator; import android.preference.PreferenceManager; import android.text.Html; import android.text.method.LinkMovementMethod; -import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; import android.view.*; -import android.widget.Button; -import android.widget.LinearLayout; import android.widget.TextView; -import android.widget.Toast; -import jscl.AngleUnit; -import jscl.NumeralBase; import net.robotmedia.billing.BillingController; import net.robotmedia.billing.IBillingObserver; import org.jetbrains.annotations.NotNull; @@ -35,29 +27,16 @@ import org.jetbrains.annotations.Nullable; import org.solovyev.android.AndroidUtils; import org.solovyev.android.FontSizeAdjuster; import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; -import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.model.AndroidCalculatorEngine; -import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.CalculatorAdditionalTitle; -import org.solovyev.android.calculator.view.NumeralBasesButton; -import org.solovyev.android.calculator.view.OnDragListenerVibrator; -import org.solovyev.android.history.HistoryDragProcessor; import org.solovyev.android.menu.ActivityMenu; import org.solovyev.android.menu.LayoutActivityMenu; import org.solovyev.android.prefs.Preference; import org.solovyev.android.view.ColorButton; -import org.solovyev.android.view.drag.*; -import org.solovyev.common.Announcer; import org.solovyev.common.equals.EqualsTool; import org.solovyev.common.history.HistoryAction; -import org.solovyev.common.math.Point2d; import org.solovyev.common.text.StringUtils; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { @NotNull @@ -68,23 +47,14 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @Nullable private IBillingObserver billingObserver; - @NotNull - private final Announcer dpclRegister = new Announcer(DragPreferencesChangeListener.class); - @NotNull private CalculatorPreferences.Gui.Theme theme; @NotNull private CalculatorPreferences.Gui.Layout layout; - @Nullable - private Vibrator vibrator; - private boolean useBackAsPrev; - @NotNull - private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); - @NotNull private ActivityMenu menu = LayoutActivityMenu.newInstance(R.menu.main_menu, CalculatorMenu.class); @@ -100,12 +70,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - CalculatorPreferences.setDefaultValues(preferences); - setTheme(preferences); super.onCreate(savedInstanceState); setLayout(preferences); + CalculatorKeyboardFragment.fixThemeParameters(findViewById(R.id.main_layout), true, this, theme); + final FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); @@ -118,7 +88,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh fragmentTransaction.add(R.id.displayContainer, displayFragment); fragmentTransaction.commit(); - if (customTitleSupported) { + fragmentTransaction = fragmentManager.beginTransaction(); + final CalculatorKeyboardFragment keyboardFragment = new CalculatorKeyboardFragment(); + fragmentTransaction.add(R.id.keyboardContainer, keyboardFragment); + fragmentTransaction.commit(); + + if (customTitleSupported) { try { getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text); @@ -139,88 +114,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh // init billing controller BillingController.checkBillingSupported(this); - vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE); - - dpclRegister.clear(); - - final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this); - - setOnDragListeners(dragPreferences, preferences); - - final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor(getCalculator()), dragPreferences), vibrator, preferences); - ((DragButton) findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener); - - ((DragButton) findViewById(R.id.subtractionButton)).setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() { - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { - if (dragDirection == DragDirection.down) { - operatorsButtonClickHandler(dragButton); - return true; - } - return false; - } - }, dragPreferences), vibrator, preferences)); - - - final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences); - ((DragButton) findViewById(R.id.rightButton)).setOnDragListener(toPositionOnDragListener); - ((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener); - - final DragButton equalsButton = (DragButton) findViewById(R.id.equalsButton); - if (equalsButton != null) { - equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences)); - } - - final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) findViewById(R.id.sixDigitButton); - if (angleUnitsButton != null) { - angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(), dragPreferences), vibrator, preferences)); - } - - final NumeralBasesButton clearButton = (NumeralBasesButton) findViewById(R.id.clearButton); - if (clearButton != null) { - clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(), dragPreferences), vibrator, preferences)); - } - - final DragButton varsButton = (DragButton) findViewById(R.id.varsButton); - if (varsButton != null) { - varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences)); - } - - final DragButton roundBracketsButton = (DragButton) findViewById(R.id.roundBracketsButton); - if ( roundBracketsButton != null ) { - roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); - } - - initMultiplicationButton(); - - fixThemeParameters(true); - - if (layout == CalculatorPreferences.Gui.Layout.simple) { - toggleButtonDirectionText(R.id.oneDigitButton, false, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(R.id.twoDigitButton, false, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(R.id.threeDigitButton, false, DragDirection.up, DragDirection.down); - - toggleButtonDirectionText(R.id.sixDigitButton, false, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); - toggleButtonDirectionText(R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); - - toggleButtonDirectionText(R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down); - - toggleButtonDirectionText(R.id.fourDigitButton, false, DragDirection.down); - toggleButtonDirectionText(R.id.fiveDigitButton, false, DragDirection.down); - - toggleButtonDirectionText(R.id.nineDigitButton, false, DragDirection.left); - - toggleButtonDirectionText(R.id.multiplicationButton, false, DragDirection.left); - toggleButtonDirectionText(R.id.plusButton, false, DragDirection.down, DragDirection.up); - } - - numeralBaseButtons.toggleNumericDigits(this, preferences); - toggleOrientationChange(preferences); - // todo serso: continue - //toggleEqualsButton(preferences); + CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, theme, findViewById(R.id.main_layout)); preferences.registerOnSharedPreferenceChangeListener(this); } @@ -235,202 +131,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); } - private void fixThemeParameters(boolean fixMagicFlames) { - if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { - - if (fixMagicFlames) { - // for metro themes we should turn off magic flames - AndroidUtils.processViewsOfType(this.getWindow().getDecorView(), ColorButton.class, new AndroidUtils.ViewProcessor() { - @Override - public void process(@NotNull ColorButton colorButton) { - colorButton.setDrawMagicFlame(false); - } - }); - } - - fixMargins(2, 2); - } else { - fixMargins(1, 1); - } - } - - private void toggleButtonDirectionText(int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) { - final View v = findViewById(id); - if (v instanceof DirectionDragButton ) { - final DirectionDragButton button = (DirectionDragButton)v; - for (DragDirection dragDirection : dragDirections) { - button.showDirectionText(showDirectionText, dragDirection); - } - } - } - - private void fixMargins(int marginLeft, int marginBottom) { - // sad but true - - final View equalsButton = findViewById(R.id.equalsButton); - final View rightButton = findViewById(R.id.rightButton); - final View leftButton = findViewById(R.id.leftButton); - final View clearButton = findViewById(R.id.clearButton); - final View eraseButton = findViewById(R.id.eraseButton); - - int orientation = AndroidUtils.getScreenOrientation(this); - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - setMarginsForView(equalsButton, marginLeft, marginBottom); - // todo serso: continue - //setMarginsForView(getCalculatorDisplayView(), marginLeft, marginBottom); - } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { - setMarginsForView(leftButton, marginLeft, marginBottom); - setMarginsForView(eraseButton, marginLeft, marginBottom); - setMarginsForView(clearButton, marginLeft, marginBottom); - setMarginsForView(rightButton, marginLeft, marginBottom); - // magic magic magic - // todo serso: continue - //setMarginsForView(getCalculatorDisplayView(), 3 * marginLeft, marginBottom); - } - } - - private void setMarginsForView(@NotNull View view, int marginLeft, int marginBottom) { - // IMPORTANT: this is workaround for probably android bug - // currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually - - final DisplayMetrics dm = getResources().getDisplayMetrics(); - if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { - final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams(); - final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight); - newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom)); - view.setLayoutParams(newParams); - } - } - - private class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { - - private final DigitButtonDragProcessor processor = new DigitButtonDragProcessor(getKeyboard()); - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragButton instanceof AngleUnitsButton) { - if (dragDirection != DragDirection.left ) { - final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); - if ( directionText != null ) { - try { - - final AngleUnit angleUnits = AngleUnit.valueOf(directionText); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this); - - AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); - - Toast.makeText(CalculatorActivity.this, CalculatorActivity.this.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); - - result = true; - } catch (IllegalArgumentException e) { - Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); - } - } - } else if ( dragDirection == DragDirection.left ) { - result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); - } - } - - return result; - } - } - - private class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if ( dragButton instanceof NumeralBasesButton ) { - final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); - if ( directionText != null ) { - try { - - final NumeralBase numeralBase = NumeralBase.valueOf(directionText); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this); - AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); - - Toast.makeText(CalculatorActivity.this, CalculatorActivity.this.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); - - result = true; - } catch (IllegalArgumentException e) { - Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); - } - } - } - - return result; - } - } - - - private class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragDirection == DragDirection.up) { - CalculatorActivityLauncher.createVar(CalculatorActivity.this, CalculatorLocatorImpl.getInstance().getDisplay()); - result = true; - } - - return result; - } - } - - private synchronized void setOnDragListeners(@NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { - final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences); - - final List dragButtonIds = new ArrayList(); - final List buttonIds = new ArrayList(); - - for (Field field : R.id.class.getDeclaredFields()) { - int modifiers = field.getModifiers(); - if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { - try { - int viewId = field.getInt(R.id.class); - final View view = this.findViewById(viewId); - if (view instanceof DragButton) { - dragButtonIds.add(viewId); - } - if (view instanceof Button) { - buttonIds.add(viewId); - } - } catch (IllegalAccessException e) { - Log.e(R.id.class.getName(), e.getMessage()); - } - } - } - - for (Integer dragButtonId : dragButtonIds) { - ((DragButton) findViewById(dragButtonId)).setOnDragListener(onDragListener); - } - } - - @NotNull - private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor, - @NotNull SimpleOnDragListener.Preferences dragPreferences) { - final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences); - dpclRegister.addListener(onDragListener); - return onDragListener; - } - - - private synchronized void setLayout(@NotNull SharedPreferences preferences) { + private synchronized void setLayout(@NotNull SharedPreferences preferences) { layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); setContentView(layout.getLayoutId()); @@ -512,89 +213,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh return result; } - @SuppressWarnings({"UnusedDeclaration"}) - public void elementaryButtonClickHandler(@NotNull View v) { - throw new UnsupportedOperationException("Not implemented yet!"); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void equalsButtonClickHandler(@NotNull View v) { - getCalculator().evaluate(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void historyButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showHistory(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void eraseButtonClickHandler(@NotNull View v) { - CalculatorLocatorImpl.getInstance().getEditor().erase(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void simplifyButtonClickHandler(@NotNull View v) { - throw new UnsupportedOperationException("Not implemented yet!"); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void moveLeftButtonClickHandler(@NotNull View v) { - getKeyboard().moveCursorLeft(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void moveRightButtonClickHandler(@NotNull View v) { - getKeyboard().moveCursorRight(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void pasteButtonClickHandler(@NotNull View v) { - getKeyboard().pasteButtonPressed(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void copyButtonClickHandler(@NotNull View v) { - getKeyboard().copyButtonPressed(); - } - - @NotNull - private static CalculatorKeyboard getKeyboard() { - return CalculatorLocatorImpl.getInstance().getKeyboard(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void clearButtonClickHandler(@NotNull View v) { - getKeyboard().clearButtonPressed(); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void digitButtonClickHandler(@NotNull View v) { - Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); - if (((ColorButton) v).isShowText()) { - getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString()); - } - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void functionsButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showFunctions(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void operatorsButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showOperators(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void varsButtonClickHandler(@NotNull View v) { - CalculatorActivityLauncher.showVars(this); - } - - @SuppressWarnings({"UnusedDeclaration"}) - public void donateButtonClickHandler(@NotNull View v) { - CalculatorApplication.showDonationDialog(this); - } - @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { @@ -606,6 +224,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh return super.onKeyDown(keyCode, event); } + @SuppressWarnings({"UnusedDeclaration"}) + public void equalsButtonClickHandler(@NotNull View v) { + getCalculator().evaluate(); + } @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -655,61 +277,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @Override public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { - if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { - dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this)); - } - if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) { useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); } - if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { - numeralBaseButtons.toggleNumericDigits(this, preferences); - } - - if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { - initMultiplicationButton(); - } - if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) { toggleOrientationChange(preferences); } - - if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { - toggleEqualsButton(preferences); - } } - private void toggleEqualsButton(@Nullable SharedPreferences preferences) { - preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; - - - if (AndroidUtils.getScreenOrientation(this) == Configuration.ORIENTATION_PORTRAIT || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { - final Display display = this.getWindowManager().getDefaultDisplay(); - - final DragButton button = (DragButton)findViewById(R.id.equalsButton); - if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { - button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1f)); - if (display.getWidth() <= 480) { - // mobile phones - getCalculatorDisplayView().setBackgroundDrawable(null); - } - } else { - button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 0f)); - if (display.getWidth() <= 480) { - // mobile phones - getCalculatorDisplayView().setBackgroundDrawable(this.getResources().getDrawable(R.drawable.equals9)); - } - } - fixThemeParameters(false); - } - } - - @NotNull - private AndroidCalculatorDisplayView getCalculatorDisplayView() { - return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); - } - private void toggleOrientationChange(@Nullable SharedPreferences preferences) { preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { @@ -719,26 +295,94 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } } - private void initMultiplicationButton() { - final View multiplicationButton = findViewById(R.id.multiplicationButton); - if ( multiplicationButton instanceof Button) { - ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); - } - } + /* + ********************************************************************** + * + * BUTTON HANDLERS + * + ********************************************************************** + */ - private static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { - final boolean result; + @SuppressWarnings({"UnusedDeclaration"}) + public void elementaryButtonClickHandler(@NotNull View v) { + throw new UnsupportedOperationException("Not implemented yet!"); + } - if ( dragDirection == DragDirection.left ) { - getKeyboard().roundBracketsButtonPressed(); - result = true; - } else { - result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); - } + @SuppressWarnings({"UnusedDeclaration"}) + public void historyButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showHistory(this); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void eraseButtonClickHandler(@NotNull View v) { + CalculatorLocatorImpl.getInstance().getEditor().erase(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void simplifyButtonClickHandler(@NotNull View v) { + throw new UnsupportedOperationException("Not implemented yet!"); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void moveLeftButtonClickHandler(@NotNull View v) { + getKeyboard().moveCursorLeft(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void moveRightButtonClickHandler(@NotNull View v) { + getKeyboard().moveCursorRight(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void pasteButtonClickHandler(@NotNull View v) { + getKeyboard().pasteButtonPressed(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void copyButtonClickHandler(@NotNull View v) { + getKeyboard().copyButtonPressed(); + } + + @NotNull + private static CalculatorKeyboard getKeyboard() { + return CalculatorLocatorImpl.getInstance().getKeyboard(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void clearButtonClickHandler(@NotNull View v) { + getKeyboard().clearButtonPressed(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void digitButtonClickHandler(@NotNull View v) { + Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); + if (((ColorButton) v).isShowText()) { + getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString()); + } + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void functionsButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showFunctions(this); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void operatorsButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showOperators(this); + } + + public static void operatorsButtonClickHandler(@NotNull Activity activity, @NotNull View view) { + CalculatorActivityLauncher.showOperators(activity); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void varsButtonClickHandler(@NotNull View v) { + CalculatorActivityLauncher.showVars(this); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void donateButtonClickHandler(@NotNull View v) { + CalculatorApplication.showDonationDialog(this); + } - return result; - } - } } \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 120db3ad..037394a3 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -1,99 +1,105 @@ -package org.solovyev.android.calculator; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; -import android.text.method.LinkMovementMethod; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.TextView; -import net.robotmedia.billing.BillingController; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.ads.AdsController; -import org.solovyev.android.calculator.history.AndroidCalculatorHistory; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; - -/** - * User: serso - * Date: 12/1/11 - * Time: 1:21 PM - */ -public class CalculatorApplication extends android.app.Application { - - private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; - - public static final String AD_FREE_PRODUCT_ID = "ad_free"; - public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; - - public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; - public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php"; - - @NotNull - private static CalculatorApplication instance; - - public CalculatorApplication() { - instance = this; - } - - @NotNull - public static CalculatorApplication getInstance() { - return instance; - } - - @Override - public void onCreate() { - super.onCreate(); - - final AndroidCalculator calculator = new AndroidCalculator(); - - CalculatorLocatorImpl.getInstance().init(calculator, - new AndroidCalculatorEngine(this), - new AndroidCalculatorClipboard(this), - new AndroidCalculatorNotifier(this), - new AndroidCalculatorHistory(this, calculator)); - - CalculatorLocatorImpl.getInstance().getCalculator().init(); - - AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { - - @Override - public byte[] getObfuscationSalt() { - return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82}; - } - - @Override - public String getPublicKey() { - return CalculatorSecurity.getPK(); - } - }); - } - - public static void showDonationDialog(@NotNull final Context context) { - final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); - final View view = layoutInflater.inflate(R.layout.donate, null); - - final TextView donate = (TextView) view.findViewById(R.id.donateText); - donate.setMovementMethod(LinkMovementMethod.getInstance()); - - final AlertDialog.Builder builder = new AlertDialog.Builder(context) - .setCancelable(true) - .setNegativeButton(R.string.c_cancel, null) - .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - final Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse(paypalDonateUrl)); - context.startActivity(i); - } - }) - .setView(view); - - builder.create().show(); - } - - public static void registerOnRemoteStackTrace() { - //Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL)); - } -} +package org.solovyev.android.calculator; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.net.Uri; +import android.preference.PreferenceManager; +import android.text.method.LinkMovementMethod; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; +import net.robotmedia.billing.BillingController; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.ads.AdsController; +import org.solovyev.android.calculator.history.AndroidCalculatorHistory; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine; + +/** + * User: serso + * Date: 12/1/11 + * Time: 1:21 PM + */ +public class CalculatorApplication extends android.app.Application { + + private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; + + public static final String AD_FREE_PRODUCT_ID = "ad_free"; + public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; + + public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; + public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php"; + + @NotNull + private static CalculatorApplication instance; + + public CalculatorApplication() { + instance = this; + } + + @NotNull + public static CalculatorApplication getInstance() { + return instance; + } + + @Override + public void onCreate() { + super.onCreate(); + + final AndroidCalculator calculator = new AndroidCalculator(); + + CalculatorLocatorImpl.getInstance().init(calculator, + new AndroidCalculatorEngine(this), + new AndroidCalculatorClipboard(this), + new AndroidCalculatorNotifier(this), + new AndroidCalculatorHistory(this, calculator)); + + CalculatorLocatorImpl.getInstance().getCalculator().init(); + + AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { + + @Override + public byte[] getObfuscationSalt() { + return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82}; + } + + @Override + public String getPublicKey() { + return CalculatorSecurity.getPK(); + } + }); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + + CalculatorPreferences.setDefaultValues(preferences); + } + + public static void showDonationDialog(@NotNull final Context context) { + final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); + final View view = layoutInflater.inflate(R.layout.donate, null); + + final TextView donate = (TextView) view.findViewById(R.id.donateText); + donate.setMovementMethod(LinkMovementMethod.getInstance()); + + final AlertDialog.Builder builder = new AlertDialog.Builder(context) + .setCancelable(true) + .setNegativeButton(R.string.c_cancel, null) + .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + final Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(paypalDonateUrl)); + context.startActivity(i); + } + }) + .setView(view); + + builder.create().show(); + } + + public static void registerOnRemoteStackTrace() { + //Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL)); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java index 9dfa9a40..5f4040d8 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorDisplayFragment.java @@ -15,7 +15,9 @@ public class CalculatorDisplayFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.calc_display, null); + final View view = inflater.inflate(R.layout.calc_display, null); + view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + return view; } @Override diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java new file mode 100644 index 00000000..710955bf --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java @@ -0,0 +1,497 @@ +package org.solovyev.android.calculator; + +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.os.Bundle; +import android.os.Vibrator; +import android.preference.PreferenceManager; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.*; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.Toast; +import jscl.AngleUnit; +import jscl.NumeralBase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; +import org.solovyev.android.calculator.history.CalculatorHistoryState; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine; +import org.solovyev.android.calculator.view.AngleUnitsButton; +import org.solovyev.android.calculator.view.NumeralBasesButton; +import org.solovyev.android.calculator.view.OnDragListenerVibrator; +import org.solovyev.android.history.HistoryDragProcessor; +import org.solovyev.android.view.ColorButton; +import org.solovyev.android.view.drag.*; +import org.solovyev.common.Announcer; +import org.solovyev.common.math.Point2d; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +/** + * User: Solovyev_S + * Date: 25.09.12 + * Time: 12:25 + */ +public class CalculatorKeyboardFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener { + + @Nullable + private Vibrator vibrator; + + @NotNull + private final Announcer dpclRegister = new Announcer(DragPreferencesChangeListener.class); + + @NotNull + private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); + + @NotNull + private CalculatorPreferences.Gui.Theme theme; + + @NotNull + private CalculatorPreferences.Gui.Layout layout; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + vibrator = (Vibrator) this.getActivity().getSystemService(Activity.VIBRATOR_SERVICE); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + + preferences.registerOnSharedPreferenceChangeListener(this); + + layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); + theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); + + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.calc_keyboard, null); + view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + return view; + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + dpclRegister.clear(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this.getActivity()); + + setOnDragListeners(root, dragPreferences, preferences); + + final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor(getCalculator()), dragPreferences), vibrator, preferences); + ((DragButton) root.findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener); + + ((DragButton) root.findViewById(R.id.subtractionButton)).setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() { + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { + if (dragDirection == DragDirection.down) { + CalculatorActivity.operatorsButtonClickHandler(getActivity(), dragButton); + return true; + } + return false; + } + }, dragPreferences), vibrator, preferences)); + + + final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences); + ((DragButton) root.findViewById(R.id.rightButton)).setOnDragListener(toPositionOnDragListener); + ((DragButton) root.findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener); + + final DragButton equalsButton = (DragButton) root.findViewById(R.id.equalsButton); + if (equalsButton != null) { + equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences)); + } + + final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) root.findViewById(R.id.sixDigitButton); + if (angleUnitsButton != null) { + angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences)); + } + + final NumeralBasesButton clearButton = (NumeralBasesButton) root.findViewById(R.id.clearButton); + if (clearButton != null) { + clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences)); + } + + final DragButton varsButton = (DragButton) root.findViewById(R.id.varsButton); + if (varsButton != null) { + varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences)); + } + + final DragButton roundBracketsButton = (DragButton) root.findViewById(R.id.roundBracketsButton); + if (roundBracketsButton != null) { + roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); + } + + if (layout == CalculatorPreferences.Gui.Layout.simple) { + toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down); + + toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); + toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); + + toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down); + + toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down); + toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down); + + toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left); + + toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left); + toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up); + } + + numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); + + fixThemeParameters(root, true, this.getActivity(), theme); + + toggleEqualsButton(preferences, this.getActivity(), theme, root); + + initMultiplicationButton(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); + + preferences.unregisterOnSharedPreferenceChangeListener(this); + } + + public static void fixThemeParameters(@NotNull View root, boolean fixMagicFlames, @NotNull Activity activity, @NotNull CalculatorPreferences.Gui.Theme theme) { + if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { + + if (fixMagicFlames) { + // for metro themes we should turn off magic flames + AndroidUtils.processViewsOfType(activity.getWindow().getDecorView(), ColorButton.class, new AndroidUtils.ViewProcessor() { + @Override + public void process(@NotNull ColorButton colorButton) { + colorButton.setDrawMagicFlame(false); + } + }); + } + + fixMargins(root, 2, 2, activity); + } else { + fixMargins(root, 1, 1, activity); + } + } + + public static void fixMargins(@NotNull View root, int marginLeft, int marginBottom, @NotNull Activity activity) { + // sad but true + + final View equalsButton = root.findViewById(R.id.equalsButton); + final View rightButton = root.findViewById(R.id.rightButton); + final View leftButton = root.findViewById(R.id.leftButton); + final View clearButton = root.findViewById(R.id.clearButton); + final View eraseButton = root.findViewById(R.id.eraseButton); + + int orientation = AndroidUtils.getScreenOrientation(activity); + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + setMarginsForView(equalsButton, marginLeft, marginBottom, activity); + setMarginsForView(getCalculatorDisplayView(), marginLeft, marginBottom, activity); + } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + setMarginsForView(leftButton, marginLeft, marginBottom, activity); + setMarginsForView(eraseButton, marginLeft, marginBottom, activity); + setMarginsForView(clearButton, marginLeft, marginBottom, activity); + setMarginsForView(rightButton, marginLeft, marginBottom, activity); + // magic magic magic + setMarginsForView(getCalculatorDisplayView(), 3 * marginLeft, marginBottom, activity); + } + } + + private void initMultiplicationButton() { + final View multiplicationButton = getView().findViewById(R.id.multiplicationButton); + if ( multiplicationButton instanceof Button) { + ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); + } + } + + private static void setMarginsForView(@Nullable View view, int marginLeft, int marginBottom, @NotNull Context context) { + // IMPORTANT: this is workaround for probably android bug + // currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually + + if (view != null) { + final DisplayMetrics dm = context.getResources().getDisplayMetrics(); + if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { + final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams(); + final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight); + newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom)); + view.setLayoutParams(newParams); + } + } + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + } + + private synchronized void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { + final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences); + + final List dragButtonIds = new ArrayList(); + final List buttonIds = new ArrayList(); + + for (Field field : R.id.class.getDeclaredFields()) { + int modifiers = field.getModifiers(); + if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { + try { + int viewId = field.getInt(R.id.class); + final View view = root.findViewById(viewId); + if (view instanceof DragButton) { + dragButtonIds.add(viewId); + } + if (view instanceof Button) { + buttonIds.add(viewId); + } + } catch (IllegalAccessException e) { + Log.e(R.id.class.getName(), e.getMessage()); + } + } + } + + for (Integer dragButtonId : dragButtonIds) { + ((DragButton) root.findViewById(dragButtonId)).setOnDragListener(onDragListener); + } + } + + + @NotNull + private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor, + @NotNull SimpleOnDragListener.Preferences dragPreferences) { + final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences); + dpclRegister.addListener(onDragListener); + return onDragListener; + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { + if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { + dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this.getActivity())); + } + + if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { + numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); + } + + if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { + toggleEqualsButton(preferences, this.getActivity(), theme, getView()); + } + + if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { + initMultiplicationButton(); + } + } + + private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) { + final View v = root.findViewById(id); + if (v instanceof DirectionDragButton ) { + final DirectionDragButton button = (DirectionDragButton)v; + for (DragDirection dragDirection : dragDirections) { + button.showDirectionText(showDirectionText, dragDirection); + } + } + } + + public static void toggleEqualsButton(@Nullable SharedPreferences preferences, + @NotNull Activity activity, + @NotNull CalculatorPreferences.Gui.Theme theme, + @NotNull View root) { + preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; + + if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { + final Display display = activity.getWindowManager().getDefaultDisplay(); + + final DragButton button = (DragButton)activity.findViewById(R.id.equalsButton); + if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { + button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1f)); + if (display.getWidth() <= 480) { + // mobile phones + final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); + if (calculatorDisplayView != null) { + calculatorDisplayView.setBackgroundDrawable(null); + } + } + } else { + button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 0f)); + if (display.getWidth() <= 480) { + // mobile phones + final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); + if (calculatorDisplayView != null) { + calculatorDisplayView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.equals9)); + } + } + } + + fixThemeParameters(root, false, activity, theme); + } + } + + + @Nullable + private static AndroidCalculatorDisplayView getCalculatorDisplayView() { + return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); + } + + @NotNull + private Calculator getCalculator() { + return CalculatorLocatorImpl.getInstance().getCalculator(); + } + + @NotNull + private static CalculatorKeyboard getKeyboard() { + return CalculatorLocatorImpl.getInstance().getKeyboard(); + } + + /* + ********************************************************************** + * + * STATIC CLASSES + * + ********************************************************************** + */ + + private static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { + final boolean result; + + if (dragDirection == DragDirection.left) { + getKeyboard().roundBracketsButtonPressed(); + result = true; + } else { + result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); + } + + return result; + } + } + + private static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { + + @NotNull + private Context context; + + private VarsDragProcessor(Context context) { + this.context = context; + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragDirection == DragDirection.up) { + CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); + result = true; + } + + return result; + } + } + + private static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { + + @NotNull + private final DigitButtonDragProcessor processor; + + @NotNull + private final Context context; + + private AngleUnitsChanger(@NotNull Context context) { + this.context = context; + this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard()); + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof AngleUnitsButton) { + if (dragDirection != DragDirection.left) { + final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); + if (directionText != null) { + try { + + final AngleUnit angleUnits = AngleUnit.valueOf(directionText); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); + + Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); + + result = true; + } catch (IllegalArgumentException e) { + Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); + } + } + } else if (dragDirection == DragDirection.left) { + result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); + } + } + + return result; + } + } + + private static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { + + @NotNull + private final Context context; + + private NumeralBasesChanger(@NotNull Context context) { + this.context = context; + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof NumeralBasesButton) { + final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); + if (directionText != null) { + try { + + final NumeralBase numeralBase = NumeralBase.valueOf(directionText); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); + + Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); + + result = true; + } catch (IllegalArgumentException e) { + Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); + } + } + } + + return result; + } + } +} +