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);
|
||||
}
|
||||
|
@ -1,30 +1,45 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/12
|
||||
* Time: 10:14 PM
|
||||
*/
|
||||
public class CalculatorFragmentHelperImpl implements CalculatorFragmentHelper {
|
||||
|
||||
@Override
|
||||
public boolean isPane(@NotNull Fragment fragment) {
|
||||
return fragment.getActivity() instanceof CalculatorActivity;
|
||||
}
|
||||
|
||||
public void setPaneTitle(@NotNull Fragment fragment, int titleResId) {
|
||||
final TextView fragmentTitle = (TextView) fragment.getView().findViewById(R.id.fragmentTitle);
|
||||
if (fragmentTitle != null) {
|
||||
if (!isPane(fragment)) {
|
||||
fragmentTitle.setVisibility(View.GONE);
|
||||
} else {
|
||||
fragmentTitle.setText(fragment.getString(titleResId).toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/12
|
||||
* Time: 10:14 PM
|
||||
*/
|
||||
public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper implements CalculatorFragmentHelper {
|
||||
|
||||
@Override
|
||||
public boolean isPane(@NotNull Fragment fragment) {
|
||||
return fragment.getActivity() instanceof CalculatorActivity;
|
||||
}
|
||||
|
||||
public void setPaneTitle(@NotNull Fragment fragment, int titleResId) {
|
||||
final TextView fragmentTitle = (TextView) fragment.getView().findViewById(R.id.fragmentTitle);
|
||||
if (fragmentTitle != null) {
|
||||
if (!isPane(fragment)) {
|
||||
fragmentTitle.setVisibility(View.GONE);
|
||||
} else {
|
||||
fragmentTitle.setText(fragment.getString(titleResId).toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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,492 +1,119 @@
|
||||
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 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
|
||||
* Date: 25.09.12
|
||||
* Time: 12:25
|
||||
*/
|
||||
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();
|
||||
|
||||
@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) {
|
||||
return inflater.inflate(R.layout.calc_keyboard, container, false);
|
||||
}
|
||||
|
||||
@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);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
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) {
|
||||
// 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) {
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import com.actionbarsherlock.app.SherlockFragment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 25.09.12
|
||||
* Time: 12:25
|
||||
*/
|
||||
public class CalculatorKeyboardFragment extends SherlockFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@NotNull
|
||||
private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons();
|
||||
|
||||
@NotNull
|
||||
private CalculatorPreferences.Gui.Theme theme;
|
||||
|
||||
@NotNull
|
||||
private CalculatorFragmentHelper fragmentHelper;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
|
||||
|
||||
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
|
||||
fragmentHelper.onCreate(this);
|
||||
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.calc_keyboard, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View root, Bundle savedInstanceState) {
|
||||
super.onViewCreated(root, savedInstanceState);
|
||||
|
||||
fragmentHelper.processButtons(this, root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
fragmentHelper.onDestroy(this);
|
||||
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
|
||||
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
||||
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) {
|
||||
numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences);
|
||||
}
|
||||
|
||||
if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) {
|
||||
CalculatorButtons.toggleEqualsButton(preferences, this.getActivity(), theme, getView());
|
||||
}
|
||||
|
||||
if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) {
|
||||
CalculatorButtons.initMultiplicationButton(getView());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -1,321 +1,326 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math.edit;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
import com.actionbarsherlock.app.SherlockListFragment;
|
||||
import com.google.ads.AdView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.ads.AdsController;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.menu.AMenuBuilder;
|
||||
import org.solovyev.android.menu.LabeledMenuItem;
|
||||
import org.solovyev.android.menu.MenuImpl;
|
||||
import org.solovyev.common.equals.EqualsTool;
|
||||
import org.solovyev.common.filter.Filter;
|
||||
import org.solovyev.common.filter.FilterRule;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/21/11
|
||||
* Time: 9:24 PM
|
||||
*/
|
||||
public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category";
|
||||
|
||||
protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
private MathEntityArrayAdapter<T> adapter;
|
||||
|
||||
@Nullable
|
||||
private String category;
|
||||
|
||||
@Nullable
|
||||
private AdView adView;
|
||||
|
||||
@NotNull
|
||||
private CalculatorFragmentHelper fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
|
||||
|
||||
|
||||
protected int getLayoutResId() {
|
||||
return R.layout.math_entities;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final Bundle bundle = getArguments();
|
||||
if ( bundle != null ) {
|
||||
category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(getLayoutResId(), container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
this.fragmentHelper.setPaneTitle(this, getTitleResId());
|
||||
|
||||
final ListView lv = getListView();
|
||||
lv.setTextFilterEnabled(true);
|
||||
|
||||
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
public void onItemClick(final AdapterView<?> parent,
|
||||
final View view,
|
||||
final int position,
|
||||
final long id) {
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(((MathEntity) parent.getItemAtPosition(position)).getName());
|
||||
}
|
||||
});
|
||||
|
||||
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
@Override
|
||||
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final T item = (T) parent.getItemAtPosition(position);
|
||||
|
||||
final List<LabeledMenuItem<T>> menuItems = getMenuItemsOnLongClick(item);
|
||||
|
||||
if (!menuItems.isEmpty()) {
|
||||
final AMenuBuilder<LabeledMenuItem<T>, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems));
|
||||
menuBuilder.create(item).show();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view);
|
||||
}
|
||||
|
||||
protected abstract int getTitleResId();
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (this.adView != null) {
|
||||
this.adView.destroy();
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract List<LabeledMenuItem<T>> getMenuItemsOnLongClick(@NotNull T item);
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
adapter = new MathEntityArrayAdapter<T>(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory());
|
||||
setListAdapter(adapter);
|
||||
|
||||
sort();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<T> getMathEntitiesByCategory() {
|
||||
final List<T> result = getMathEntities();
|
||||
|
||||
new Filter<T>(new FilterRule<T>() {
|
||||
@Override
|
||||
public boolean isFiltered(T t) {
|
||||
return !isInCategory(t);
|
||||
}
|
||||
}).filter(result.iterator());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected boolean isInCategory(@Nullable T t) {
|
||||
return t != null && (category == null || EqualsTool.areEqual(getMathEntityCategory(t), category));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract MathEntityDescriptionGetter getDescriptionGetter();
|
||||
|
||||
@NotNull
|
||||
protected abstract List<T> getMathEntities();
|
||||
|
||||
@Nullable
|
||||
abstract String getMathEntityCategory(@NotNull T t);
|
||||
|
||||
protected void sort() {
|
||||
final MathEntityArrayAdapter<T> localAdapter = adapter;
|
||||
if (localAdapter != null) {
|
||||
localAdapter.sort(new Comparator<T>() {
|
||||
@Override
|
||||
public int compare(T function1, T function2) {
|
||||
return function1.getName().compareTo(function2.getName());
|
||||
}
|
||||
});
|
||||
|
||||
localAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> {
|
||||
|
||||
@NotNull
|
||||
private final MathEntityDescriptionGetter descriptionGetter;
|
||||
|
||||
private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter,
|
||||
@NotNull Context context,
|
||||
int resource,
|
||||
int textViewResourceId,
|
||||
@NotNull List<T> objects) {
|
||||
|
||||
super(context, resource, textViewResourceId, objects);
|
||||
this.descriptionGetter = descriptionGetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent);
|
||||
|
||||
final T mathEntity = getItem(position);
|
||||
|
||||
final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName());
|
||||
if (!StringUtils.isEmpty(mathEntityDescription)) {
|
||||
TextView description = (TextView) result.findViewById(R.id.math_entity_description);
|
||||
if (description == null) {
|
||||
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
|
||||
final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.math_entity, null);
|
||||
description = (TextView) itemView.findViewById(R.id.math_entity_description);
|
||||
itemView.removeView(description);
|
||||
result.addView(description);
|
||||
}
|
||||
description.setText(mathEntityDescription);
|
||||
} else {
|
||||
TextView description = (TextView) result.findViewById(R.id.math_entity_description);
|
||||
if (description != null) {
|
||||
result.removeView(description);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<?> mathRegistry;
|
||||
|
||||
public MathEntityDescriptionGetterImpl(@NotNull CalculatorMathRegistry<?> mathRegistry) {
|
||||
this.mathRegistry = mathRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription(@NotNull Context context, @NotNull String mathEntityName) {
|
||||
return this.mathRegistry.getDescription(mathEntityName);
|
||||
}
|
||||
}
|
||||
|
||||
protected static interface MathEntityDescriptionGetter {
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull Context context, @NotNull String mathEntityName);
|
||||
}
|
||||
|
||||
public void addToAdapter(@NotNull T mathEntity) {
|
||||
if (this.adapter != null) {
|
||||
this.adapter.add(mathEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromAdapter(@NotNull T mathEntity) {
|
||||
if (this.adapter != null) {
|
||||
this.adapter.remove(mathEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyAdapter() {
|
||||
if (this.adapter != null) {
|
||||
this.adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
static void createTab(@NotNull Context context,
|
||||
@NotNull TabHost tabHost,
|
||||
@NotNull String tabId,
|
||||
@NotNull String categoryId,
|
||||
int tabCaptionId,
|
||||
@NotNull Class<? extends Activity> activityClass,
|
||||
@Nullable Intent parentIntent) {
|
||||
|
||||
TabHost.TabSpec spec;
|
||||
|
||||
final Intent intent;
|
||||
if (parentIntent != null) {
|
||||
intent = new Intent(parentIntent);
|
||||
} else {
|
||||
intent = new Intent();
|
||||
}
|
||||
intent.setClass(context, activityClass);
|
||||
intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
|
||||
|
||||
// Initialize a TabSpec for each tab and add it to the TabHost
|
||||
spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent);
|
||||
|
||||
tabHost.addTab(spec);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Bundle createBundleFor(@NotNull String categoryId) {
|
||||
final Bundle result = new Bundle(1);
|
||||
result.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math.edit;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
import com.actionbarsherlock.app.SherlockListFragment;
|
||||
import com.google.ads.AdView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.ads.AdsController;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.menu.AMenuBuilder;
|
||||
import org.solovyev.android.menu.LabeledMenuItem;
|
||||
import org.solovyev.android.menu.MenuImpl;
|
||||
import org.solovyev.common.equals.EqualsTool;
|
||||
import org.solovyev.common.filter.Filter;
|
||||
import org.solovyev.common.filter.FilterRule;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/21/11
|
||||
* Time: 9:24 PM
|
||||
*/
|
||||
public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category";
|
||||
|
||||
protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
private MathEntityArrayAdapter<T> adapter;
|
||||
|
||||
@Nullable
|
||||
private String category;
|
||||
|
||||
@Nullable
|
||||
private AdView adView;
|
||||
|
||||
@NotNull
|
||||
private CalculatorFragmentHelper fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
|
||||
|
||||
|
||||
protected int getLayoutResId() {
|
||||
return R.layout.math_entities;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final Bundle bundle = getArguments();
|
||||
if ( bundle != null ) {
|
||||
category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING);
|
||||
}
|
||||
|
||||
fragmentHelper.onCreate(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(getLayoutResId(), container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
this.fragmentHelper.setPaneTitle(this, getTitleResId());
|
||||
|
||||
final ListView lv = getListView();
|
||||
lv.setTextFilterEnabled(true);
|
||||
|
||||
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
public void onItemClick(final AdapterView<?> parent,
|
||||
final View view,
|
||||
final int position,
|
||||
final long id) {
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(((MathEntity) parent.getItemAtPosition(position)).getName());
|
||||
}
|
||||
});
|
||||
|
||||
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
@Override
|
||||
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final T item = (T) parent.getItemAtPosition(position);
|
||||
|
||||
final List<LabeledMenuItem<T>> menuItems = getMenuItemsOnLongClick(item);
|
||||
|
||||
if (!menuItems.isEmpty()) {
|
||||
final AMenuBuilder<LabeledMenuItem<T>, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems));
|
||||
menuBuilder.create(item).show();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view);
|
||||
}
|
||||
|
||||
protected abstract int getTitleResId();
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (this.adView != null) {
|
||||
this.adView.destroy();
|
||||
}
|
||||
|
||||
fragmentHelper.onDestroy(this);
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract List<LabeledMenuItem<T>> getMenuItemsOnLongClick(@NotNull T item);
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
adapter = new MathEntityArrayAdapter<T>(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory());
|
||||
setListAdapter(adapter);
|
||||
|
||||
sort();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<T> getMathEntitiesByCategory() {
|
||||
final List<T> result = getMathEntities();
|
||||
|
||||
new Filter<T>(new FilterRule<T>() {
|
||||
@Override
|
||||
public boolean isFiltered(T t) {
|
||||
return !isInCategory(t);
|
||||
}
|
||||
}).filter(result.iterator());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected boolean isInCategory(@Nullable T t) {
|
||||
return t != null && (category == null || EqualsTool.areEqual(getMathEntityCategory(t), category));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract MathEntityDescriptionGetter getDescriptionGetter();
|
||||
|
||||
@NotNull
|
||||
protected abstract List<T> getMathEntities();
|
||||
|
||||
@Nullable
|
||||
abstract String getMathEntityCategory(@NotNull T t);
|
||||
|
||||
protected void sort() {
|
||||
final MathEntityArrayAdapter<T> localAdapter = adapter;
|
||||
if (localAdapter != null) {
|
||||
localAdapter.sort(new Comparator<T>() {
|
||||
@Override
|
||||
public int compare(T function1, T function2) {
|
||||
return function1.getName().compareTo(function2.getName());
|
||||
}
|
||||
});
|
||||
|
||||
localAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> {
|
||||
|
||||
@NotNull
|
||||
private final MathEntityDescriptionGetter descriptionGetter;
|
||||
|
||||
private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter,
|
||||
@NotNull Context context,
|
||||
int resource,
|
||||
int textViewResourceId,
|
||||
@NotNull List<T> objects) {
|
||||
|
||||
super(context, resource, textViewResourceId, objects);
|
||||
this.descriptionGetter = descriptionGetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent);
|
||||
|
||||
final T mathEntity = getItem(position);
|
||||
|
||||
final String mathEntityDescription = descriptionGetter.getDescription(getContext(), mathEntity.getName());
|
||||
if (!StringUtils.isEmpty(mathEntityDescription)) {
|
||||
TextView description = (TextView) result.findViewById(R.id.math_entity_description);
|
||||
if (description == null) {
|
||||
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
|
||||
final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.math_entity, null);
|
||||
description = (TextView) itemView.findViewById(R.id.math_entity_description);
|
||||
itemView.removeView(description);
|
||||
result.addView(description);
|
||||
}
|
||||
description.setText(mathEntityDescription);
|
||||
} else {
|
||||
TextView description = (TextView) result.findViewById(R.id.math_entity_description);
|
||||
if (description != null) {
|
||||
result.removeView(description);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
protected static class MathEntityDescriptionGetterImpl implements MathEntityDescriptionGetter {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<?> mathRegistry;
|
||||
|
||||
public MathEntityDescriptionGetterImpl(@NotNull CalculatorMathRegistry<?> mathRegistry) {
|
||||
this.mathRegistry = mathRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription(@NotNull Context context, @NotNull String mathEntityName) {
|
||||
return this.mathRegistry.getDescription(mathEntityName);
|
||||
}
|
||||
}
|
||||
|
||||
protected static interface MathEntityDescriptionGetter {
|
||||
|
||||
@Nullable
|
||||
String getDescription(@NotNull Context context, @NotNull String mathEntityName);
|
||||
}
|
||||
|
||||
public void addToAdapter(@NotNull T mathEntity) {
|
||||
if (this.adapter != null) {
|
||||
this.adapter.add(mathEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromAdapter(@NotNull T mathEntity) {
|
||||
if (this.adapter != null) {
|
||||
this.adapter.remove(mathEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyAdapter() {
|
||||
if (this.adapter != null) {
|
||||
this.adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
static void createTab(@NotNull Context context,
|
||||
@NotNull TabHost tabHost,
|
||||
@NotNull String tabId,
|
||||
@NotNull String categoryId,
|
||||
int tabCaptionId,
|
||||
@NotNull Class<? extends Activity> activityClass,
|
||||
@Nullable Intent parentIntent) {
|
||||
|
||||
TabHost.TabSpec spec;
|
||||
|
||||
final Intent intent;
|
||||
if (parentIntent != null) {
|
||||
intent = new Intent(parentIntent);
|
||||
} else {
|
||||
intent = new Intent();
|
||||
}
|
||||
intent.setClass(context, activityClass);
|
||||
intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
|
||||
|
||||
// Initialize a TabSpec for each tab and add it to the TabHost
|
||||
spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent);
|
||||
|
||||
tabHost.addTab(spec);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Bundle createBundleFor(@NotNull String categoryId) {
|
||||
final Bundle result = new Bundle(1);
|
||||
result.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -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