Fragments

This commit is contained in:
serso 2012-09-25 13:57:34 +04:00
parent 5242d65e3a
commit cf322d04ee
6 changed files with 786 additions and 611 deletions

View File

@ -8,9 +8,9 @@
<LinearLayout <LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android" xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent" a:layout_width="match_parent"
a:layout_height="fill_parent" a:layout_height="match_parent"
a:id="@+id/ad_parent_view" a:id="@+id/main_layout"
a:orientation="vertical" a:orientation="vertical"
a:layout_gravity="center" a:layout_gravity="center"
a:background="#ff000000"> a:background="#ff000000">
@ -20,12 +20,14 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="0dp"/> a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp"> <LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_equals_button" <include layout="@layout/calc_equals_button"
a:layout_weight="1" a:layout_weight="1"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent"/> a:layout_height="match_parent"/>
<LinearLayout a:id="@+id/displayContainer" <LinearLayout a:id="@+id/displayContainer"
a:layout_weight="4" a:layout_weight="4"
@ -34,54 +36,9 @@
</LinearLayout> </LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp"> <LinearLayout a:id="@+id/keyboardContainer"
a:layout_weight="5"
a:layout_width="match_parent"
a:layout_height="0dp"/>
<include layout="@layout/calc_seven_digit_button"/>
<include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_digit_button"/>
<include layout="@layout/calc_multiplication_button"/>
<include layout="@layout/calc_clear_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_four_digit_button"/>
<include layout="@layout/calc_five_digit_button"/>
<include layout="@layout/calc_six_digit_button"/>
<include layout="@layout/calc_division_button"/>
<include layout="@layout/calc_erase_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_one_digit_button"/>
<include layout="@layout/calc_two_digit_button"/>
<include layout="@layout/calc_three_digit_button"/>
<include layout="@layout/calc_plus_button"/>
<include layout="@layout/calc_copy_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_paste_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
<include layout="@layout/calc_left_button"/>
<include layout="@layout/calc_right_button"/>
<include layout="@layout/calc_vars_button"/>
<include layout="@layout/calc_functions_button"/>
<include layout="@layout/calc_history_button"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical">
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_seven_digit_button"/>
<include layout="@layout/calc_eight_digit_button"/>
<include layout="@layout/calc_nine_digit_button"/>
<include layout="@layout/calc_multiplication_button"/>
<include layout="@layout/calc_clear_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_four_digit_button"/>
<include layout="@layout/calc_five_digit_button"/>
<include layout="@layout/calc_six_digit_button"/>
<include layout="@layout/calc_division_button"/>
<include layout="@layout/calc_erase_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_one_digit_button"/>
<include layout="@layout/calc_two_digit_button"/>
<include layout="@layout/calc_three_digit_button"/>
<include layout="@layout/calc_plus_button"/>
<include layout="@layout/calc_copy_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_round_brackets_button"/>
<include layout="@layout/calc_zero_digit_button"/>
<include layout="@layout/calc_dot_button"/>
<include layout="@layout/calc_subtraction_button"/>
<include layout="@layout/calc_paste_button"/>
</LinearLayout>
<LinearLayout a:layout_weight="1"
a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_left_button"/>
<include layout="@layout/calc_right_button"/>
<include layout="@layout/calc_vars_button"/>
<include layout="@layout/calc_functions_button"/>
<include layout="@layout/calc_history_button"/>
</LinearLayout>
</LinearLayout>

View File

@ -12,22 +12,14 @@ import android.app.FragmentTransaction;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.*; import android.view.*;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import jscl.AngleUnit;
import jscl.NumeralBase;
import net.robotmedia.billing.BillingController; import net.robotmedia.billing.BillingController;
import net.robotmedia.billing.IBillingObserver; import net.robotmedia.billing.IBillingObserver;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -35,29 +27,16 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.FontSizeAdjuster; import org.solovyev.android.FontSizeAdjuster;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; 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.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.CalculatorAdditionalTitle; 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.ActivityMenu;
import org.solovyev.android.menu.LayoutActivityMenu; import org.solovyev.android.menu.LayoutActivityMenu;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import org.solovyev.android.view.ColorButton; 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.equals.EqualsTool;
import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.math.Point2d;
import org.solovyev.common.text.StringUtils; 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 { public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
@NotNull @NotNull
@ -68,23 +47,14 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Nullable @Nullable
private IBillingObserver billingObserver; private IBillingObserver billingObserver;
@NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
@NotNull @NotNull
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
@NotNull @NotNull
private CalculatorPreferences.Gui.Layout layout; private CalculatorPreferences.Gui.Layout layout;
@Nullable
private Vibrator vibrator;
private boolean useBackAsPrev; private boolean useBackAsPrev;
@NotNull
private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons();
@NotNull @NotNull
private ActivityMenu<Menu, MenuItem> menu = LayoutActivityMenu.newInstance(R.menu.main_menu, CalculatorMenu.class); private ActivityMenu<Menu, MenuItem> 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); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
CalculatorPreferences.setDefaultValues(preferences);
setTheme(preferences); setTheme(preferences);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setLayout(preferences); setLayout(preferences);
CalculatorKeyboardFragment.fixThemeParameters(findViewById(R.id.main_layout), true, this, theme);
final FragmentManager fragmentManager = getFragmentManager(); final FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@ -118,7 +88,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
fragmentTransaction.add(R.id.displayContainer, displayFragment); fragmentTransaction.add(R.id.displayContainer, displayFragment);
fragmentTransaction.commit(); fragmentTransaction.commit();
if (customTitleSupported) { fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorKeyboardFragment keyboardFragment = new CalculatorKeyboardFragment();
fragmentTransaction.add(R.id.keyboardContainer, keyboardFragment);
fragmentTransaction.commit();
if (customTitleSupported) {
try { try {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);
final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text); 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 // init billing controller
BillingController.checkBillingSupported(this); 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<CalculatorHistoryState>(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); toggleOrientationChange(preferences);
// todo serso: continue CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, theme, findViewById(R.id.main_layout));
//toggleEqualsButton(preferences);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
} }
@ -235,202 +131,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()); return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator());
} }
private void fixThemeParameters(boolean fixMagicFlames) { private synchronized void setLayout(@NotNull SharedPreferences preferences) {
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<ColorButton>() {
@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<Integer> dragButtonIds = new ArrayList<Integer>();
final List<Integer> buttonIds = new ArrayList<Integer>();
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) {
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
setContentView(layout.getLayoutId()); setContentView(layout.getLayoutId());
@ -512,89 +213,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
return result; 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 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { if (keyCode == KeyEvent.KEYCODE_BACK) {
@ -606,6 +224,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
return super.onKeyDown(keyCode, event); return super.onKeyDown(keyCode, event);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void equalsButtonClickHandler(@NotNull View v) {
getCalculator().evaluate();
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
@ -655,61 +277,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { 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) ) { if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) {
useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); 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) ) { if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) {
toggleOrientationChange(preferences); 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) { private void toggleOrientationChange(@Nullable SharedPreferences preferences) {
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences;
if (CalculatorPreferences.Gui.autoOrientation.getPreference(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 { @SuppressWarnings({"UnusedDeclaration"})
@Override public void elementaryButtonClickHandler(@NotNull View v) {
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { throw new UnsupportedOperationException("Not implemented yet!");
final boolean result; }
if ( dragDirection == DragDirection.left ) { @SuppressWarnings({"UnusedDeclaration"})
getKeyboard().roundBracketsButtonPressed(); public void historyButtonClickHandler(@NotNull View v) {
result = true; CalculatorActivityLauncher.showHistory(this);
} else { }
result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent);
} @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;
}
}
} }

View File

@ -1,99 +1,105 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.content.SharedPreferences;
import android.text.method.LinkMovementMethod; import android.net.Uri;
import android.view.LayoutInflater; import android.preference.PreferenceManager;
import android.view.View; import android.text.method.LinkMovementMethod;
import android.widget.TextView; import android.view.LayoutInflater;
import net.robotmedia.billing.BillingController; import android.view.View;
import org.jetbrains.annotations.NotNull; import android.widget.TextView;
import org.solovyev.android.ads.AdsController; import net.robotmedia.billing.BillingController;
import org.solovyev.android.calculator.history.AndroidCalculatorHistory; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; 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 * User: serso
*/ * Date: 12/1/11
public class CalculatorApplication extends android.app.Application { * Time: 1:21 PM
*/
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&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; public class CalculatorApplication extends android.app.Application {
public static final String AD_FREE_PRODUCT_ID = "ad_free"; 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&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted";
public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free";
public static final String AD_FREE_PRODUCT_ID = "ad_free";
public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free";
public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php";
public static final String ADMOB_USER_ID = "a14f02cf9c80cbc";
@NotNull public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php";
private static CalculatorApplication instance;
@NotNull
public CalculatorApplication() { private static CalculatorApplication instance;
instance = this;
} public CalculatorApplication() {
instance = this;
@NotNull }
public static CalculatorApplication getInstance() {
return instance; @NotNull
} public static CalculatorApplication getInstance() {
return instance;
@Override }
public void onCreate() {
super.onCreate(); @Override
public void onCreate() {
final AndroidCalculator calculator = new AndroidCalculator(); super.onCreate();
CalculatorLocatorImpl.getInstance().init(calculator, final AndroidCalculator calculator = new AndroidCalculator();
new AndroidCalculatorEngine(this),
new AndroidCalculatorClipboard(this), CalculatorLocatorImpl.getInstance().init(calculator,
new AndroidCalculatorNotifier(this), new AndroidCalculatorEngine(this),
new AndroidCalculatorHistory(this, calculator)); new AndroidCalculatorClipboard(this),
new AndroidCalculatorNotifier(this),
CalculatorLocatorImpl.getInstance().getCalculator().init(); new AndroidCalculatorHistory(this, calculator));
AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { CalculatorLocatorImpl.getInstance().getCalculator().init();
@Override AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() {
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 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(); @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 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final View view = layoutInflater.inflate(R.layout.donate, null);
CalculatorPreferences.setDefaultValues(preferences);
final TextView donate = (TextView) view.findViewById(R.id.donateText); }
donate.setMovementMethod(LinkMovementMethod.getInstance());
public static void showDonationDialog(@NotNull final Context context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context) final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
.setCancelable(true) final View view = layoutInflater.inflate(R.layout.donate, null);
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() { final TextView donate = (TextView) view.findViewById(R.id.donateText);
@Override donate.setMovementMethod(LinkMovementMethod.getInstance());
public void onClick(DialogInterface dialog, int which) {
final Intent i = new Intent(Intent.ACTION_VIEW); final AlertDialog.Builder builder = new AlertDialog.Builder(context)
i.setData(Uri.parse(paypalDonateUrl)); .setCancelable(true)
context.startActivity(i); .setNegativeButton(R.string.c_cancel, null)
} .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() {
}) @Override
.setView(view); public void onClick(DialogInterface dialog, int which) {
final Intent i = new Intent(Intent.ACTION_VIEW);
builder.create().show(); i.setData(Uri.parse(paypalDonateUrl));
} context.startActivity(i);
}
public static void registerOnRemoteStackTrace() { })
//Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL)); .setView(view);
}
} builder.create().show();
}
public static void registerOnRemoteStackTrace() {
//Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL));
}
}

View File

@ -15,7 +15,9 @@ public class CalculatorDisplayFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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 @Override

View File

@ -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<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(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<CalculatorHistoryState>(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<ColorButton>() {
@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<Integer> dragButtonIds = new ArrayList<Integer>();
final List<Integer> buttonIds = new ArrayList<Integer>();
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;
}
}
}