Buttons
This commit is contained in:
parent
8882cb32ee
commit
c8440e18a5
@ -0,0 +1,225 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryState;
|
||||
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.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: serso
|
||||
* Date: 9/28/12
|
||||
* Time: 12:12 AM
|
||||
*/
|
||||
public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@NotNull
|
||||
private CalculatorPreferences.Gui.Layout layout;
|
||||
|
||||
@Nullable
|
||||
private Vibrator vibrator;
|
||||
|
||||
@NotNull
|
||||
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
|
||||
|
||||
@NotNull
|
||||
private String logTag = "CalculatorActivity";
|
||||
|
||||
protected AbstractCalculatorHelper() {
|
||||
}
|
||||
|
||||
protected AbstractCalculatorHelper(@NotNull String logTag) {
|
||||
this.logTag = logTag;
|
||||
}
|
||||
|
||||
protected void onCreate(@NotNull Activity activity) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
|
||||
vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE);
|
||||
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
|
||||
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
public void logDebug(@NotNull String message) {
|
||||
Log.d(logTag, message);
|
||||
}
|
||||
|
||||
public void processButtons(@NotNull final Activity activity, @NotNull View root) {
|
||||
dpclRegister.clear();
|
||||
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, activity);
|
||||
|
||||
setOnDragListeners(root, dragPreferences, preferences);
|
||||
|
||||
final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences);
|
||||
final DragButton historyButton = getButton(root, R.id.historyButton);
|
||||
if (historyButton != null) {
|
||||
historyButton.setOnDragListener(historyOnDragListener);
|
||||
}
|
||||
|
||||
final DragButton subtractionButton = getButton(root, R.id.subtractionButton);
|
||||
if (subtractionButton != null) {
|
||||
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(activity);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences);
|
||||
|
||||
final DragButton rightButton = getButton(root, R.id.rightButton);
|
||||
if (rightButton != null) {
|
||||
rightButton.setOnDragListener(toPositionOnDragListener);
|
||||
}
|
||||
|
||||
final DragButton leftButton = getButton(root, R.id.leftButton);
|
||||
if (leftButton != null) {
|
||||
leftButton.setOnDragListener(toPositionOnDragListener);
|
||||
}
|
||||
|
||||
final DragButton equalsButton = getButton(root, R.id.equalsButton);
|
||||
if (equalsButton != null) {
|
||||
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton);
|
||||
if (angleUnitsButton != null) {
|
||||
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.AngleUnitsChanger(activity), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton);
|
||||
if (clearButton != null) {
|
||||
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.NumeralBasesChanger(activity), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final DragButton varsButton = getButton(root, R.id.varsButton);
|
||||
if (varsButton != null) {
|
||||
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.VarsDragProcessor(activity), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton);
|
||||
if (roundBracketsButton != null) {
|
||||
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.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);
|
||||
}
|
||||
}
|
||||
|
||||
private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) {
|
||||
final View v = getButton(root, id);
|
||||
if (v instanceof DirectionDragButton ) {
|
||||
final DirectionDragButton button = (DirectionDragButton)v;
|
||||
for (DragDirection dragDirection : dragDirections) {
|
||||
button.showDirectionText(showDirectionText, dragDirection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Calculator getCalculator() {
|
||||
return CalculatorLocatorImpl.getInstance().getCalculator();
|
||||
}
|
||||
|
||||
|
||||
private 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>();
|
||||
|
||||
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);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(R.id.class.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Integer dragButtonId : dragButtonIds) {
|
||||
final DragButton button = getButton(root, dragButtonId);
|
||||
if (button != null) {
|
||||
button.setOnDragListener(onDragListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private CalculatorKeyboard getKeyboard() {
|
||||
return CalculatorLocatorImpl.getInstance().getKeyboard();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private <T extends DragButton> T getButton(@NotNull View root, int buttonId) {
|
||||
return (T) root.findViewById(buttonId);
|
||||
}
|
||||
|
||||
@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, CalculatorApplication.getInstance()));
|
||||
}
|
||||
}
|
||||
|
||||
public void onDestroy(@NotNull Activity activity) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
|
||||
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
}
|
@ -84,7 +84,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
|
||||
getSupportActionBar().hide();
|
||||
}
|
||||
|
||||
CalculatorKeyboardFragment.fixThemeParameters(true, activityHelper.getTheme(), this.getWindow().getDecorView());
|
||||
CalculatorButtons.processButtons(true, activityHelper.getTheme(), this.getWindow().getDecorView());
|
||||
|
||||
FragmentUtils.createFragment(this, CalculatorEditorFragment.class, R.id.editorContainer, "editor");
|
||||
FragmentUtils.createFragment(this, CalculatorDisplayFragment.class, R.id.displayContainer, "display");
|
||||
@ -113,7 +113,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
|
||||
|
||||
toggleOrientationChange(preferences);
|
||||
|
||||
CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, activityHelper.getTheme(), findViewById(R.id.main_layout));
|
||||
CalculatorButtons.toggleEqualsButton(preferences, this, activityHelper.getTheme(), findViewById(R.id.main_layout));
|
||||
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
@ -210,13 +210,6 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
|
||||
getCalculator().evaluate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
||||
activityHelper.onPause(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
@ -3,6 +3,7 @@ package org.solovyev.android.calculator;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.View;
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@ -29,6 +30,7 @@ public interface CalculatorActivityHelper {
|
||||
void onResume(@NotNull Activity activity);
|
||||
|
||||
void onDestroy(@NotNull SherlockFragmentActivity activity);
|
||||
void onDestroy(@NotNull Activity activity);
|
||||
|
||||
void addTab(@NotNull SherlockFragmentActivity activity,
|
||||
@NotNull String tag,
|
||||
@ -40,5 +42,5 @@ public interface CalculatorActivityHelper {
|
||||
|
||||
void logDebug(@NotNull String message);
|
||||
|
||||
void onPause(@NotNull SherlockFragmentActivity activity);
|
||||
void processButtons(@NotNull Activity activity, @NotNull View root);
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import com.actionbarsherlock.app.ActionBar;
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -21,7 +20,7 @@ import java.util.List;
|
||||
* Date: 9/25/12
|
||||
* Time: 10:32 PM
|
||||
*/
|
||||
public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
|
||||
public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper implements CalculatorActivityHelper {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
@ -51,12 +50,10 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
|
||||
private CalculatorPreferences.Gui.Theme theme;
|
||||
private int navPosition = 0;
|
||||
|
||||
@NotNull
|
||||
private String logTag = "CalculatorActivity";
|
||||
|
||||
public CalculatorActivityHelperImpl(int layoutId, @NotNull String logTag) {
|
||||
super(logTag);
|
||||
this.layoutId = layoutId;
|
||||
this.logTag = logTag;
|
||||
}
|
||||
|
||||
public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) {
|
||||
@ -66,7 +63,7 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
|
||||
|
||||
@Override
|
||||
public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) {
|
||||
Log.d(logTag + ": helper", "onCreate");
|
||||
super.onCreate(activity);
|
||||
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
|
||||
@ -99,11 +96,6 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logDebug(@NotNull String message) {
|
||||
Log.d(logTag, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) {
|
||||
onSaveInstanceState((Activity) activity, outState);
|
||||
@ -126,10 +118,7 @@ public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
|
||||
|
||||
@Override
|
||||
public void onDestroy(@NotNull SherlockFragmentActivity activity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(@NotNull SherlockFragmentActivity activity) {
|
||||
super.onDestroy(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,244 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
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.model.AndroidCalculatorEngine;
|
||||
import org.solovyev.android.calculator.view.AngleUnitsButton;
|
||||
import org.solovyev.android.calculator.view.NumeralBasesButton;
|
||||
import org.solovyev.android.view.ColorButton;
|
||||
import org.solovyev.android.view.drag.DragButton;
|
||||
import org.solovyev.android.view.drag.DragDirection;
|
||||
import org.solovyev.android.view.drag.SimpleOnDragListener;
|
||||
import org.solovyev.common.math.Point2d;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/28/12
|
||||
* Time: 12:06 AM
|
||||
*/
|
||||
public final class CalculatorButtons {
|
||||
|
||||
private CalculatorButtons () {
|
||||
}
|
||||
|
||||
|
||||
public static void processButtons(boolean fixMagicFlames,
|
||||
@NotNull CalculatorPreferences.Gui.Theme theme,
|
||||
@NotNull View root) {
|
||||
if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) {
|
||||
|
||||
if (fixMagicFlames) {
|
||||
// for metro themes we should turn off magic flames
|
||||
AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor<ColorButton>() {
|
||||
@Override
|
||||
public void process(@NotNull ColorButton colorButton) {
|
||||
colorButton.setDrawMagicFlame(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void initMultiplicationButton(@NotNull View root) {
|
||||
final View multiplicationButton = root.findViewById(R.id.multiplicationButton);
|
||||
if ( multiplicationButton instanceof Button) {
|
||||
((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign());
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
processButtons(false, theme, root);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static AndroidCalculatorDisplayView getCalculatorDisplayView() {
|
||||
return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView();
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC CLASSES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static CalculatorKeyboard getKeyboard() {
|
||||
return CalculatorLocatorImpl.getInstance().getKeyboard();
|
||||
}
|
||||
|
||||
static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor {
|
||||
|
||||
@NotNull
|
||||
private Context context;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor {
|
||||
|
||||
@NotNull
|
||||
private final DigitButtonDragProcessor processor;
|
||||
|
||||
@NotNull
|
||||
private final Context context;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor {
|
||||
|
||||
@NotNull
|
||||
private final Context context;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.View;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@ -13,4 +14,10 @@ public interface CalculatorFragmentHelper {
|
||||
boolean isPane(@NotNull Fragment fragment);
|
||||
|
||||
void setPaneTitle(@NotNull Fragment fragment, int titleResId);
|
||||
|
||||
void processButtons(@NotNull Fragment fragment, @NotNull View root);
|
||||
|
||||
void onCreate(@NotNull Fragment fragment);
|
||||
|
||||
void onDestroy(@NotNull Fragment fragment);
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
* Date: 9/26/12
|
||||
* Time: 10:14 PM
|
||||
*/
|
||||
public class CalculatorFragmentHelperImpl implements CalculatorFragmentHelper {
|
||||
public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper implements CalculatorFragmentHelper {
|
||||
|
||||
@Override
|
||||
public boolean isPane(@NotNull Fragment fragment) {
|
||||
@ -27,4 +27,19 @@ public class CalculatorFragmentHelperImpl implements CalculatorFragmentHelper {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processButtons(@NotNull Fragment fragment, @NotNull View root) {
|
||||
super.processButtons(fragment.getActivity(), root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@NotNull Fragment fragment) {
|
||||
super.onCreate(fragment.getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(@NotNull Fragment fragment) {
|
||||
super.onDestroy(fragment.getActivity());
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,15 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Activity;
|
||||
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.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.view.*;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import com.actionbarsherlock.app.SherlockFragment;
|
||||
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
|
||||
@ -42,12 +18,6 @@ import java.util.List;
|
||||
*/
|
||||
public class CalculatorKeyboardFragment extends SherlockFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Nullable
|
||||
private Vibrator vibrator;
|
||||
|
||||
@NotNull
|
||||
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
|
||||
|
||||
@NotNull
|
||||
private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons();
|
||||
|
||||
@ -55,19 +25,19 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
|
||||
private CalculatorPreferences.Gui.Theme theme;
|
||||
|
||||
@NotNull
|
||||
private CalculatorPreferences.Gui.Layout layout;
|
||||
private CalculatorFragmentHelper fragmentHelper;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
vibrator = (Vibrator) this.getActivity().getSystemService(Activity.VIBRATOR_SERVICE);
|
||||
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
|
||||
|
||||
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
|
||||
fragmentHelper.onCreate(this);
|
||||
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
|
||||
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
|
||||
|
||||
}
|
||||
@ -81,139 +51,21 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
|
||||
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);
|
||||
final DragButton historyButton = getButton(root, R.id.historyButton);
|
||||
if (historyButton != null) {
|
||||
historyButton.setOnDragListener(historyOnDragListener);
|
||||
}
|
||||
|
||||
final DragButton subtractionButton = getButton(root, R.id.subtractionButton);
|
||||
if (subtractionButton != null) {
|
||||
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());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
|
||||
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences);
|
||||
|
||||
final DragButton rightButton = getButton(root, R.id.rightButton);
|
||||
if (rightButton != null) {
|
||||
rightButton.setOnDragListener(toPositionOnDragListener);
|
||||
}
|
||||
|
||||
final DragButton leftButton = getButton(root, R.id.leftButton);
|
||||
if (leftButton != null) {
|
||||
leftButton.setOnDragListener(toPositionOnDragListener);
|
||||
}
|
||||
|
||||
final DragButton equalsButton = getButton(root, R.id.equalsButton);
|
||||
if (equalsButton != null) {
|
||||
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton);
|
||||
if (angleUnitsButton != null) {
|
||||
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton);
|
||||
if (clearButton != null) {
|
||||
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final DragButton varsButton = getButton(root, R.id.varsButton);
|
||||
if (varsButton != null) {
|
||||
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences));
|
||||
}
|
||||
|
||||
final DragButton roundBracketsButton = getButton(root, 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(true, theme, this.getView());
|
||||
|
||||
toggleEqualsButton(preferences, this.getActivity(), theme, root);
|
||||
|
||||
initMultiplicationButton();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private <T extends DragButton> T getButton(@NotNull View root, int buttonId) {
|
||||
return (T) root.findViewById(buttonId);
|
||||
fragmentHelper.processButtons(this, root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
fragmentHelper.onDestroy(this);
|
||||
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
|
||||
|
||||
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
||||
|
||||
}
|
||||
|
||||
public static void fixThemeParameters(boolean fixMagicFlames,
|
||||
@NotNull CalculatorPreferences.Gui.Theme theme,
|
||||
@NotNull View root) {
|
||||
if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) {
|
||||
|
||||
if (fixMagicFlames) {
|
||||
// for metro themes we should turn off magic flames
|
||||
AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor<ColorButton>() {
|
||||
@Override
|
||||
public void process(@NotNull ColorButton colorButton) {
|
||||
colorButton.setDrawMagicFlame(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
/* 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
|
||||
|
||||
@ -233,107 +85,18 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
|
||||
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) {
|
||||
final DragButton button = getButton(root, dragButtonId);
|
||||
if (button != null) {
|
||||
button.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());
|
||||
CalculatorButtons.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 = getButton(root, 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(false, theme, root);
|
||||
CalculatorButtons.initMultiplicationButton(getView());
|
||||
}
|
||||
}
|
||||
|
||||
@ -352,141 +115,5 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
|
||||
*/
|
||||
public class NumeralBaseButtons {
|
||||
|
||||
private synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull NumeralBase currentNumeralBase) {
|
||||
public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull NumeralBase currentNumeralBase) {
|
||||
for (NumeralBase numeralBase : NumeralBase.values()) {
|
||||
if ( currentNumeralBase != numeralBase ) {
|
||||
AndroidNumeralBase.valueOf(numeralBase).toggleButtons(false, activity);
|
||||
|
@ -92,6 +92,7 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
|
||||
fragmentHelper.onCreate(this);
|
||||
|
||||
logDebug("onCreate");
|
||||
}
|
||||
@ -175,6 +176,9 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
|
||||
if ( this.adView != null ) {
|
||||
this.adView.destroy();
|
||||
}
|
||||
|
||||
fragmentHelper.onDestroy(this);
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,8 @@ public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
activityHelper.onDestroy(this);
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
|
@ -87,6 +87,8 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
|
||||
if ( bundle != null ) {
|
||||
category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING);
|
||||
}
|
||||
|
||||
fragmentHelper.onCreate(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -139,6 +141,9 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
|
||||
if (this.adView != null) {
|
||||
this.adView.destroy();
|
||||
}
|
||||
|
||||
fragmentHelper.onDestroy(this);
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,8 @@ public class CalculatorFunctionsFragmentActivity extends SherlockFragmentActivit
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
this.activityHelper.onDestroy(this);
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
|
@ -70,6 +70,8 @@ public class CalculatorVarsFragmentActivity extends SherlockFragmentActivity imp
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
this.activityHelper.onDestroy(this);
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user