vars activity

This commit is contained in:
serso 2011-09-29 00:11:12 +04:00
parent d5170da0e8
commit 512e526713
12 changed files with 464 additions and 281 deletions

View File

@ -13,13 +13,13 @@
<intent-filter>
<action a:name="android.intent.action.MAIN" />
<category a:name="android.intent.category.LAUNCHER" />
<action a:name="org.solovyev.android.calculator.DragButtonPreferencesChanged"/>
<action a:name="org.solovyev.android.calculator.CalculatorActivity.insertText"/>
</intent-filter>
</activity>
<activity a:name=".CalculatorPreferencesActivity" a:label="@string/c_app_settings"/>
<activity a:name=".DragButtonCalibrationActivity" a:label="@string/c_prefs_drag_button_calibration"/>
<activity a:name=".AboutActivity" a:label="@string/c_about"/>
<activity a:name=".CalculatorVarsActivity" a:label="@string/c_vars_and_constants"/>
</application>
</manifest>

View File

@ -183,7 +183,11 @@
calc:textDown=")" style="@style/digit_button_style"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:text="" style="@style/control_button_style"/>
<org.solovyev.android.view.widgets.DirectionDragButton
a:id="@+id/varsButton"
a:text="@string/c_vars"
a:onClick="varsButtonClickHandler"
style="@style/control_button_style"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:text="" style="@style/control_button_style"/>

26
res/layout-land/var.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="match_parent"
a:layout_height="match_parent">
<LinearLayout
a:orientation="horizontal"
a:layout_width="match_parent"
a:layout_height="match_parent">
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/var_text"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
</TextView>
</LinearLayout>
</LinearLayout>

View File

@ -189,13 +189,18 @@
style="@style/control_button_style"
a:onClick="moveLeftButtonClickHandler"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:id="@+id/rightButton"
<org.solovyev.android.view.widgets.DirectionDragButton
a:id="@+id/rightButton"
calc:textUp="↠"
a:text="→"
style="@style/control_button_style"
a:onClick="moveRightButtonClickHandler"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:text="" style="@style/control_button_style"/>
<org.solovyev.android.view.widgets.DirectionDragButton
a:id="@+id/varsButton"
a:text="@string/c_vars"
a:onClick="varsButtonClickHandler"
style="@style/control_button_style"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:text="" style="@style/control_button_style"/>

26
res/layout-port/var.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="match_parent"
a:layout_height="match_parent">
<LinearLayout
a:orientation="horizontal"
a:layout_width="match_parent"
a:layout_height="match_parent">
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/var_text"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
</TextView>
</LinearLayout>
</LinearLayout>

View File

@ -7,6 +7,7 @@
<string name="c_settings">Settings</string>
<string name="c_help">Help</string>
<string name="c_about">About</string>
<string name="c_vars_and_constants">Variables And Constants</string>
<!--PREFERENCE ACTIVITY-->
<string name="c_prefs_main_category">Main settings</string>
<string name="c_prefs_drag_button_category">Drag buttons settings</string>
@ -28,6 +29,7 @@
<string name="c_redo">redo</string>
<string name="c_clear">clear</string>
<string name="c_paste">paste</string>
<string name="c_vars">vars</string>
<string name="c_calc_color_display_title">Color expressions</string>
<string name="p_calc_result_precision_title">Precision of result</string>

View File

@ -23,6 +23,7 @@ import org.solovyev.android.view.widgets.*;
import org.solovyev.common.BooleanMapper;
import org.solovyev.common.NumberMapper;
import org.solovyev.common.utils.Announcer;
import org.solovyev.common.utils.StringUtils;
import org.solovyev.common.utils.history.HistoryAction;
import java.lang.reflect.Field;
@ -30,6 +31,9 @@ import java.lang.reflect.Modifier;
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
public static final String INSERT_TEXT_INTENT = "org.solovyev.android.calculator.CalculatorActivity.insertText";
public static final String INSERT_TEXT_INTENT_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorActivity.insertText.extraString";
private static final int HVGA_WIDTH_PIXELS = 320;
@NotNull
@ -42,7 +46,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private CalculatorModel calculatorModel;
@NotNull
private BroadcastReceiver preferencesChangesReceiver;
private BroadcastReceiver insertTextReceiver;
/**
* Called when the activity is first created.
@ -61,7 +65,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
this.calculatorView = new CalculatorView(this, this.calculatorModel);
final DragButtonCalibrationActivity.Preferences dragPreferences = DragButtonCalibrationActivity.getPreferences(this);
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(this);
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(new DigitButtonDragProcessor(calculatorView), dragPreferences);
dpclRegister.addListener(onDragListener);
@ -94,17 +98,24 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
dpclRegister.addListener(toPositionOnDragListener);
preferencesChangesReceiver = new BroadcastReceiver() {
insertTextReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DragButtonCalibrationActivity.INTENT_ACTION.equals(intent.getAction())) {
dpclRegister.announce().onDragPreferencesChange(DragButtonCalibrationActivity.getPreferences(CalculatorActivity.this));
if (INSERT_TEXT_INTENT.equals(intent.getAction())) {
final String s = intent.getStringExtra(INSERT_TEXT_INTENT_EXTRA_STRING);
if (!StringUtils.isEmpty(s)) {
calculatorView.doTextOperation(new CalculatorView.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
editor.getText().insert(editor.getSelectionStart(), s);
}
});
}
}
}
};
registerReceiver(preferencesChangesReceiver, new IntentFilter(DragButtonCalibrationActivity.INTENT_ACTION));
registerReceiver(insertTextReceiver, new IntentFilter(INSERT_TEXT_INTENT));
final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this);
@ -114,7 +125,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Override
protected void onDestroy() {
unregisterReceiver(preferencesChangesReceiver);
unregisterReceiver(insertTextReceiver);
super.onDestroy();
}
@ -179,6 +190,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
calculatorView.processDigitButtonAction(((DirectionDragButton) v).getTextMiddle());
}
@SuppressWarnings({"UnusedDeclaration"})
public void varsButtonClickHandler(@NotNull View v) {
startActivity(new Intent(this, CalculatorVarsActivity.class));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
@ -239,8 +255,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
dpclRegister.announce().onDragPreferencesChange(DragButtonCalibrationActivity.getPreferences(CalculatorActivity.this));
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String s) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(CalculatorActivity.this));
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
this.calculatorModel.setNumberOfFractionDigits(integerNumberMapper.parseValue(sharedPreferences.getString(this.getString(R.string.p_calc_result_precision_key), this.getString(R.string.p_calc_result_precision))));

View File

@ -0,0 +1,53 @@
/*
* 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;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 9/28/11
* Time: 10:55 PM
*/
public class CalculatorVarsActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Dialog);
final List<VariableContainer> vars = new ArrayList<VariableContainer>();
vars.add(new VariableContainer("e", 2.71, true));
vars.add(new VariableContainer("π", 3.14, true));
setListAdapter(new ArrayAdapter<VariableContainer>(this, R.layout.var, R.id.var_text, vars));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Intent intent = new Intent(CalculatorActivity.INSERT_TEXT_INTENT);
intent.putExtra(CalculatorActivity.INSERT_TEXT_INTENT_EXTRA_STRING, vars.get(position).getId());
sendOrderedBroadcast(intent, null);
CalculatorVarsActivity.this.finish();
}
});
}
}

View File

@ -6,8 +6,6 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
@ -43,14 +41,6 @@ public class DragButtonCalibrationActivity extends Activity {
private static final float DEFAULT_VALUE = -999;
private static final int MIN_HISTORY_FOR_CALIBRATION = 10;
public static final String INTENT_ACTION = "org.solovyev.android.calculator.DragButtonPreferencesChanged";
public static enum PreferenceType {
angle,
distance,
duration
}
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -163,13 +153,11 @@ public class DragButtonCalibrationActivity extends Activity {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = settings.edit();
setPreferences(angleStatData, editor, PreferenceType.angle);
setPreferences(distanceStatData, editor, PreferenceType.distance);
setPreferences(timeStatData, editor, PreferenceType.duration);
setPreferences(angleStatData, editor, SimpleOnDragListener.PreferenceType.angle);
setPreferences(distanceStatData, editor, SimpleOnDragListener.PreferenceType.distance);
setPreferences(timeStatData, editor, SimpleOnDragListener.PreferenceType.duration);
editor.commit();
sendOrderedBroadcast(new Intent(INTENT_ACTION), null);
}
}
@ -177,167 +165,15 @@ public class DragButtonCalibrationActivity extends Activity {
return super.onKeyDown(keyCode, event);
}
private void setPreferences(@NotNull Map<DragDirection, MathUtils.StatData> statData, @NotNull SharedPreferences.Editor editor, @NotNull PreferenceType preferenceType) {
private void setPreferences(@NotNull Map<DragDirection, MathUtils.StatData> statData, @NotNull SharedPreferences.Editor editor, @NotNull SimpleOnDragListener.PreferenceType preferenceType) {
final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);
for (Map.Entry<DragDirection, MathUtils.StatData> entry : statData.entrySet()) {
final float min = (float) entry.getValue().getMean() - 2 * (float) entry.getValue().getStandardDeviation();
final float max = (float) entry.getValue().getMean() + 2 * (float) entry.getValue().getStandardDeviation();
editor.putString(getPreferenceId(preferenceType, entry.getKey()), mapper.formatValue(transformInterval(preferenceType, entry.getKey(), new IntervalImpl<Float>(Math.max(0, min), max))));
editor.putString(SimpleOnDragListener.getPreferenceId(preferenceType, entry.getKey()), mapper.formatValue(SimpleOnDragListener.transformInterval(preferenceType, entry.getKey(), new IntervalImpl<Float>(Math.max(0, min), max))));
}
}
// todo serso: currently we do not use direction
public static String getPreferenceId(@NotNull PreferenceType preferenceType, @NotNull DragDirection direction) {
return "org.solovyev.android.calculator.DragButtonCalibrationActivity" + "_" + preferenceType.name() /*+ "_" + direction.name()*/;
}
@NotNull
public static Preferences getPreferences(@NotNull Context context) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);
final Preferences result = new Preferences();
for (PreferenceType preferenceType : PreferenceType.values()) {
for (DragDirection dragDirection : DragDirection.values()) {
final String preferenceId = getPreferenceId(preferenceType, dragDirection);
final String defaultValue;
switch (preferenceType) {
case angle:
defaultValue = context.getResources().getString(R.string.p_drag_angle);
break;
case distance:
defaultValue = context.getResources().getString(R.string.p_drag_distance);
break;
case duration:
defaultValue = context.getResources().getString(R.string.p_drag_duration);
break;
default:
defaultValue = null;
Log.e(DragButtonCalibrationActivity.class.getName(), "New preference type added: default preferences should be defined. Preference id: " + preferenceId);
}
final String value = preferences.getString(preferenceId, defaultValue);
if (defaultValue != null) {
final Interval<Float> intervalPref = mapper.parseValue(value);
assert intervalPref != null;
transformInterval(preferenceType, dragDirection, intervalPref);
Log.d(DragButtonCalibrationActivity.class.getName(), "Preference loaded. Id: " + preferenceId + ", value: " + intervalPref.toString());
final DragPreference directionPreference = new DragPreference(dragDirection, intervalPref);
Preference preference = result.getPreferencesMap().get(preferenceType);
if (preference == null) {
preference = new Preference(preferenceType);
result.getPreferencesMap().put(preferenceType, preference);
}
preference.getDirectionPreferences().put(dragDirection, directionPreference);
}
}
}
return result;
}
@NotNull
private static Interval<Float> transformInterval(@NotNull PreferenceType preferenceType, @NotNull DragDirection dragDirection, @NotNull Interval<Float> interval) {
if (preferenceType == PreferenceType.angle) {
final Float leftBorder = interval.getLeftBorder();
final Float rightBorder = interval.getRightBorder();
if (dragDirection == DragDirection.up) {
interval.setLeftBorder(180f - rightBorder);
interval.setRightBorder(180f - leftBorder);
} else if (dragDirection == DragDirection.left || dragDirection == DragDirection.right) {
interval.setLeftBorder(90f - rightBorder / 2);
interval.setRightBorder(90f + leftBorder / 2);
}
}
return interval;
}
public static class DragPreference {
@NotNull
private DragDirection direction;
@NotNull
private Interval<Float> interval;
public DragPreference(@NotNull DragDirection direction, @NotNull Interval<Float> interval) {
this.direction = direction;
this.interval = interval;
}
@NotNull
public DragDirection getDirection() {
return direction;
}
public void setDirection(@NotNull DragDirection direction) {
this.direction = direction;
}
@NotNull
public Interval<Float> getInterval() {
return interval;
}
public void setInterval(@NotNull Interval<Float> interval) {
this.interval = interval;
}
}
public static class Preference {
@NotNull
private PreferenceType preferenceType;
@NotNull
private Map<DragDirection, DragPreference> directionPreferences = new HashMap<DragDirection, DragPreference>();
public Preference(@NotNull PreferenceType preferenceType) {
this.preferenceType = preferenceType;
}
@NotNull
public PreferenceType getPreferenceType() {
return preferenceType;
}
public void setPreferenceType(@NotNull PreferenceType preferenceType) {
this.preferenceType = preferenceType;
}
@NotNull
public Map<DragDirection, DragPreference> getDirectionPreferences() {
return directionPreferences;
}
public void setDirectionPreferences(@NotNull Map<DragDirection, DragPreference> directionPreferences) {
this.directionPreferences = directionPreferences;
}
}
public static class Preferences {
private final Map<PreferenceType, Preference> preferencesMap = new HashMap<PreferenceType, Preference>();
public Map<PreferenceType, Preference> getPreferencesMap() {
return preferencesMap;
}
}
private void logStatData(@NotNull Map<DragDirection, MathUtils.StatData> statData) {
for (Map.Entry<DragDirection, MathUtils.StatData> entry : statData.entrySet()) {

View File

@ -0,0 +1,51 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.definitions.Identity;
/**
* User: serso
* Date: 9/28/11
* Time: 11:22 PM
*/
public class VariableContainer extends Identity<String>{
@NotNull
private Double value;
private boolean system;
public VariableContainer(@NotNull String id, @NotNull Double value, boolean system) {
super(id);
this.value = value;
this.system = system;
}
@NotNull
public Double getValue() {
return value;
}
public void setValue(@NotNull Double value) {
this.value = value;
}
public boolean isSystem() {
return system;
}
public void setSystem(boolean system) {
this.system = system;
}
@Override
public String toString() {
return getId() + " = " + value;
}
}

View File

@ -6,7 +6,6 @@
package org.solovyev.android.view.widgets;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.DragButtonCalibrationActivity;
import java.util.EventListener;
@ -17,5 +16,5 @@ import java.util.EventListener;
*/
public interface DragPreferencesChangeListener extends EventListener{
void onDragPreferencesChange(@NotNull DragButtonCalibrationActivity.Preferences preferences );
void onDragPreferencesChange(@NotNull SimpleOnDragListener.Preferences preferences );
}

View File

@ -5,14 +5,20 @@
package org.solovyev.android.view.widgets;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MotionEvent;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.DragButtonCalibrationActivity;
import org.solovyev.android.calculator.R;
import org.solovyev.common.NumberIntervalMapper;
import org.solovyev.common.utils.Interval;
import org.solovyev.common.utils.Mapper;
import org.solovyev.common.utils.MathUtils;
import org.solovyev.common.utils.Point2d;
import java.util.HashMap;
import java.util.Map;
public class SimpleOnDragListener implements OnDragListener, DragPreferencesChangeListener {
@ -24,13 +30,13 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
private DragProcessor dragProcessor;
@NotNull
private DragButtonCalibrationActivity.Preferences preferences;
private Preferences preferences;
public SimpleOnDragListener(@NotNull DragButtonCalibrationActivity.Preferences preferences) {
public SimpleOnDragListener(@NotNull Preferences preferences) {
this.preferences = preferences;
}
public SimpleOnDragListener(@NotNull DragProcessor dragProcessor, @NotNull DragButtonCalibrationActivity.Preferences preferences) {
public SimpleOnDragListener(@NotNull DragProcessor dragProcessor, @NotNull Preferences preferences) {
this.dragProcessor = dragProcessor;
this.preferences = preferences;
}
@ -51,16 +57,16 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
final double angle = Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, axis), endPoint));
final double duration = motionEvent.getEventTime() - motionEvent.getDownTime();
final DragButtonCalibrationActivity.Preference distancePreferences = preferences.getPreferencesMap().get(DragButtonCalibrationActivity.PreferenceType.distance);
final DragButtonCalibrationActivity.Preference anglePreferences = preferences.getPreferencesMap().get(DragButtonCalibrationActivity.PreferenceType.angle);
final Preference distancePreferences = preferences.getPreferencesMap().get(PreferenceType.distance);
final Preference anglePreferences = preferences.getPreferencesMap().get(PreferenceType.angle);
DragDirection direction = null;
for (Map.Entry<DragDirection, DragButtonCalibrationActivity.DragPreference> directionEntry : distancePreferences.getDirectionPreferences().entrySet()) {
for (Map.Entry<DragDirection, DragPreference> directionEntry : distancePreferences.getDirectionPreferences().entrySet()) {
Log.d(String.valueOf(dragButton.getId()), "Trying direction interval: " + directionEntry.getValue().getInterval());
if (isInInterval(directionEntry.getValue().getInterval(), distance)) {
for (Map.Entry<DragDirection, DragButtonCalibrationActivity.DragPreference> angleEntry : anglePreferences.getDirectionPreferences().entrySet()) {
for (Map.Entry<DragDirection, DragPreference> angleEntry : anglePreferences.getDirectionPreferences().entrySet()) {
Log.d(String.valueOf(dragButton.getId()), "Trying angle interval: " + angleEntry.getValue().getInterval());
@ -80,9 +86,9 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
}
if (direction != null) {
final DragButtonCalibrationActivity.Preference durationPreferences = preferences.getPreferencesMap().get(DragButtonCalibrationActivity.PreferenceType.duration);
final Preference durationPreferences = preferences.getPreferencesMap().get(PreferenceType.duration);
final DragButtonCalibrationActivity.DragPreference durationDragPreferences = durationPreferences.getDirectionPreferences().get(direction);
final DragPreference durationDragPreferences = durationPreferences.getDirectionPreferences().get(direction);
Log.d(String.valueOf(dragButton.getId()), "Trying time interval: " + durationDragPreferences.getInterval());
if (isInInterval(durationDragPreferences.getInterval(), (float) duration)) {
@ -125,7 +131,7 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
}
@Override
public void onDragPreferencesChange(@NotNull DragButtonCalibrationActivity.Preferences preferences) {
public void onDragPreferencesChange(@NotNull Preferences preferences) {
this.preferences = preferences;
}
@ -133,4 +139,163 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent);
}
// todo serso: currently we do not use direction
public static String getPreferenceId(@NotNull PreferenceType preferenceType, @NotNull DragDirection direction) {
return "org.solovyev.android.calculator.DragButtonCalibrationActivity" + "_" + preferenceType.name() /*+ "_" + direction.name()*/;
}
@NotNull
public static Preferences getPreferences(@NotNull Context context) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);
final Preferences result = new Preferences();
for (PreferenceType preferenceType : PreferenceType.values()) {
for (DragDirection dragDirection : DragDirection.values()) {
final String preferenceId = getPreferenceId(preferenceType, dragDirection);
final String defaultValue;
switch (preferenceType) {
case angle:
defaultValue = context.getResources().getString(R.string.p_drag_angle);
break;
case distance:
defaultValue = context.getResources().getString(R.string.p_drag_distance);
break;
case duration:
defaultValue = context.getResources().getString(R.string.p_drag_duration);
break;
default:
defaultValue = null;
Log.e(SimpleOnDragListener.class.getName(), "New preference type added: default preferences should be defined. Preference id: " + preferenceId);
}
final String value = preferences.getString(preferenceId, defaultValue);
if (defaultValue != null) {
final Interval<Float> intervalPref = mapper.parseValue(value);
assert intervalPref != null;
transformInterval(preferenceType, dragDirection, intervalPref);
Log.d(SimpleOnDragListener.class.getName(), "Preference loaded. Id: " + preferenceId + ", value: " + intervalPref.toString());
final DragPreference directionPreference = new DragPreference(dragDirection, intervalPref);
Preference preference = result.getPreferencesMap().get(preferenceType);
if (preference == null) {
preference = new Preference(preferenceType);
result.getPreferencesMap().put(preferenceType, preference);
}
preference.getDirectionPreferences().put(dragDirection, directionPreference);
}
}
}
return result;
}
@NotNull
public static Interval<Float> transformInterval(@NotNull PreferenceType preferenceType, @NotNull DragDirection dragDirection, @NotNull Interval<Float> interval) {
if (preferenceType == PreferenceType.angle) {
final Float leftBorder = interval.getLeftBorder();
final Float rightBorder = interval.getRightBorder();
if (dragDirection == DragDirection.up) {
interval.setLeftBorder(180f - rightBorder);
interval.setRightBorder(180f - leftBorder);
} else if (dragDirection == DragDirection.left || dragDirection == DragDirection.right) {
interval.setLeftBorder(90f - rightBorder / 2);
interval.setRightBorder(90f + leftBorder / 2);
}
}
return interval;
}
public static enum PreferenceType {
angle,
distance,
duration
}
public static class DragPreference {
@NotNull
private DragDirection direction;
@NotNull
private Interval<Float> interval;
public DragPreference(@NotNull DragDirection direction, @NotNull Interval<Float> interval) {
this.direction = direction;
this.interval = interval;
}
@NotNull
public DragDirection getDirection() {
return direction;
}
public void setDirection(@NotNull DragDirection direction) {
this.direction = direction;
}
@NotNull
public Interval<Float> getInterval() {
return interval;
}
public void setInterval(@NotNull Interval<Float> interval) {
this.interval = interval;
}
}
public static class Preference {
@NotNull
private PreferenceType preferenceType;
@NotNull
private Map<DragDirection, DragPreference> directionPreferences = new HashMap<DragDirection, DragPreference>();
public Preference(@NotNull PreferenceType preferenceType) {
this.preferenceType = preferenceType;
}
@NotNull
public PreferenceType getPreferenceType() {
return preferenceType;
}
public void setPreferenceType(@NotNull PreferenceType preferenceType) {
this.preferenceType = preferenceType;
}
@NotNull
public Map<DragDirection, DragPreference> getDirectionPreferences() {
return directionPreferences;
}
public void setDirectionPreferences(@NotNull Map<DragDirection, DragPreference> directionPreferences) {
this.directionPreferences = directionPreferences;
}
}
public static class Preferences {
private final Map<PreferenceType, Preference> preferencesMap = new HashMap<PreferenceType, Preference>();
public Map<PreferenceType, Preference> getPreferencesMap() {
return preferencesMap;
}
}
}