Calculation fixable messages

This commit is contained in:
Sergey Solovyev 2012-11-17 23:53:56 +04:00
parent 1ab714b044
commit ef11120244
14 changed files with 192 additions and 59 deletions

View File

@ -20,6 +20,20 @@ public enum CalculatorFixableError {
public void fix() {
CalculatorLocatorImpl.getInstance().getPreferenceService().setAngleUnits(AngleUnit.rad);
}
},
preferred_numeral_base() {
@Override
public void fix() {
CalculatorLocatorImpl.getInstance().getPreferenceService().setPreferredNumeralBase();
}
},
preferred_angle_units() {
@Override
public void fix() {
CalculatorLocatorImpl.getInstance().getPreferenceService().setPreferredAngleUnits();
}
};
@NotNull

View File

@ -163,6 +163,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull String expression,
@Nullable MessageRegistry mr) {
CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false);
PreparedExpression preparedExpression = null;
try {

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
/**
@ -10,5 +11,11 @@ import org.jetbrains.annotations.NotNull;
*/
public interface CalculatorPreferenceService {
void setPreferredAngleUnits();
void setAngleUnits(@NotNull AngleUnit angleUnit);
void setPreferredNumeralBase();
void setNumeralBase(@NotNull NumeralBase numeralBase);
void checkPreferredPreferences(boolean force);
}

View File

@ -8,13 +8,15 @@
<TextView a:id="@+id/calculation_messages_text_view"
a:layout_width="0dp"
a:layout_height="wrap_content"
a:layout_weight="5"/>
a:layout_weight="5"
a:layout_gravity="top|left"/>
<Button
a:id="@+id/fix_button"
a:layout_width="0dp"
a:layout_height="wrap_content"
a:layout_weight="1"
a:layout_gravity="top|right"
a:text="@string/fix" />
</LinearLayout>

View File

@ -257,4 +257,12 @@
</string>
<string name="fix">Fix</string>
<string name="p_preferred_numeral_base_title">Preferred numeral base</string>
<string name="p_preferred_numeral_base_summary">Defines the preferred numeral base</string>
<string name="preferred_numeral_base_message">Preferred numeral base \'%1$s\' differs from current numeral base \'%2$s\'. Forgot to change back?</string>
<string name="p_preferred_angle_units_title">Preferred angle units</string>
<string name="p_preferred_angle_units_summary">Defines the preferred angle units</string>
<string name="preferred_angle_units_message">Preferred angle units \'%1$s\' differs from current angle units \'%2$s\'. Forgot to change back?</string>
</resources>

View File

@ -32,12 +32,26 @@
a:summary="@string/c_calc_grouping_separator_summary"
a:entryValues="@array/p_grouping_separator_values"/>
<ListPreference
a:key="preferred_angle_units"
a:title="@string/p_preferred_angle_units_title"
a:summary="@string/p_preferred_angle_units_summary"
a:entries="@array/p_angle_units_names"
a:entryValues="@array/p_angle_units" />
<ListPreference a:key="@string/p_calc_angle_units_key"
a:title="@string/c_calc_angle_units"
a:entries="@array/p_angle_units_names"
a:summary="@string/c_angle_units_summary"
a:entryValues="@array/p_angle_units"/>
<ListPreference
a:key="preferred_numeral_base"
a:title="@string/p_preferred_numeral_base_title"
a:summary="@string/p_preferred_numeral_base_summary"
a:entries="@array/p_numeral_bases_names"
a:entryValues="@array/p_numeral_bases" />
<ListPreference a:key="@string/p_calc_numeral_bases_key"
a:title="@string/c_calc_numeral_bases"
a:entries="@array/p_numeral_bases_names"

View File

@ -4,11 +4,15 @@ import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.common.msg.MessageType;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 11/17/12
@ -16,6 +20,9 @@ import org.solovyev.common.msg.MessageType;
*/
public class AndroidCalculatorPreferenceService implements CalculatorPreferenceService {
// ont hour
private static final Long PREFERRED_PREFS_INTERVAL_TIME = 1000L * 60L * 60L;
@NotNull
private final Application application;
@ -23,6 +30,46 @@ public class AndroidCalculatorPreferenceService implements CalculatorPreferenceS
this.application = application;
}
@Override
public void checkPreferredPreferences(boolean force) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
final Long currentTime = System.currentTimeMillis();
if ( force || isTimeForCheck(currentTime, preferences)) {
final NumeralBase preferredNumeralBase = CalculatorPreferences.Calculations.preferredNumeralBase.getPreference(preferences);
final NumeralBase numeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences);
final AngleUnit preferredAngleUnits = CalculatorPreferences.Calculations.preferredAngleUnits.getPreference(preferences);
final AngleUnit angleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(preferences);
final List<CalculatorFixableMessage> messages = new ArrayList<CalculatorFixableMessage>(2);
if ( numeralBase != preferredNumeralBase ) {
messages.add(new CalculatorFixableMessage(application.getString(R.string.preferred_numeral_base_message, preferredNumeralBase.name(), numeralBase.name()), MessageType.warning, CalculatorFixableError.preferred_numeral_base));
}
if ( angleUnits != preferredAngleUnits ) {
messages.add(new CalculatorFixableMessage(application.getString(R.string.preferred_angle_units_message, preferredAngleUnits.name(), angleUnits.name()), MessageType.warning, CalculatorFixableError.preferred_angle_units));
}
CalculatorMessagesDialog.showDialog(messages, application);
CalculatorPreferences.Calculations.lastPreferredPreferencesCheck.putPreference(preferences, currentTime);
}
}
private boolean isTimeForCheck(@NotNull Long currentTime, @NotNull SharedPreferences preferences) {
final Long lastPreferredPreferencesCheckTime = CalculatorPreferences.Calculations.lastPreferredPreferencesCheck.getPreference(preferences);
return currentTime - lastPreferredPreferencesCheckTime > PREFERRED_PREFS_INTERVAL_TIME;
}
@Override
public void setPreferredAngleUnits() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
setAngleUnits(CalculatorPreferences.Calculations.preferredAngleUnits.getPreference(preferences));
}
@Override
public void setAngleUnits(@NotNull AngleUnit angleUnit) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
@ -30,4 +77,18 @@ public class AndroidCalculatorPreferenceService implements CalculatorPreferenceS
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(new AndroidMessage(R.string.c_angle_units_changed_to, MessageType.info, application, angleUnit.name()));
}
@Override
public void setPreferredNumeralBase() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
setNumeralBase(CalculatorPreferences.Calculations.preferredNumeralBase.getPreference(preferences));
}
@Override
public void setNumeralBase(@NotNull NumeralBase numeralBase) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(new AndroidMessage(R.string.c_numeral_base_changed_to, MessageType.info, application, numeralBase.name()));
}
}

View File

@ -163,7 +163,7 @@ public class CalculatorActivityLauncher {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if ( CalculatorPreferences.Calculations.showCalculationMessagesDialog.getPreference(prefs) ) {
CalculatorMessagesDialog.showDialog(messages, context);
CalculatorMessagesDialog.showDialogForMessages(messages, context);
}
}
}

View File

@ -126,6 +126,8 @@ public class CalculatorApplication extends android.app.Application {
CalculatorLocatorImpl.getInstance().getLogger().debug(TAG, "Application started!");
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Application started!");
CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(true);
}
private void setTheme(@NotNull SharedPreferences preferences) {

View File

@ -10,7 +10,6 @@ import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
@ -19,7 +18,6 @@ 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.drag.DirectionDragButton;
import org.solovyev.android.view.drag.DragButton;
import org.solovyev.android.view.drag.DragDirection;
import org.solovyev.android.view.drag.SimpleOnDragListener;
@ -217,9 +215,7 @@ public final class CalculatorButtons {
final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences);
if (oldNumeralBase != numeralBase) {
AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show();
CalculatorLocatorImpl.getInstance().getPreferenceService().setNumeralBase(numeralBase);
}
result = true;

View File

@ -12,27 +12,27 @@ import org.solovyev.common.msg.MessageType;
* Date: 11/17/12
* Time: 6:54 PM
*/
public class CalculationMessage implements Parcelable {
public class CalculatorFixableMessage implements Parcelable {
public static final Creator<CalculationMessage> CREATOR = new Creator<CalculationMessage>() {
public static final Creator<CalculatorFixableMessage> CREATOR = new Creator<CalculatorFixableMessage>() {
@Override
public CalculationMessage createFromParcel(@NotNull Parcel in) {
return CalculationMessage.fromParcel(in);
public CalculatorFixableMessage createFromParcel(@NotNull Parcel in) {
return CalculatorFixableMessage.fromParcel(in);
}
@Override
public CalculationMessage[] newArray(int size) {
return new CalculationMessage[size];
public CalculatorFixableMessage[] newArray(int size) {
return new CalculatorFixableMessage[size];
}
};
@NotNull
private static CalculationMessage fromParcel(@NotNull Parcel in) {
private static CalculatorFixableMessage fromParcel(@NotNull Parcel in) {
final String message = in.readString();
final MessageType messageType = (MessageType) in.readSerializable();
final CalculatorFixableError fixableError = (CalculatorFixableError) in.readSerializable();
return new CalculationMessage(message, messageType, fixableError);
return new CalculatorFixableMessage(message, messageType, fixableError);
}
@NotNull
@ -44,13 +44,13 @@ public class CalculationMessage implements Parcelable {
@Nullable
private final CalculatorFixableError fixableError;
public CalculationMessage(@NotNull Message message) {
public CalculatorFixableMessage(@NotNull Message message) {
this.message = message.getLocalizedMessage();
this.messageType = message.getMessageType();
this.fixableError = CalculatorFixableError.getErrorByMessageCode(message.getMessageCode());
}
public CalculationMessage(@NotNull String message,
public CalculatorFixableMessage(@NotNull String message,
@NotNull MessageType messageType,
@Nullable CalculatorFixableError fixableError) {
this.message = message;

View File

@ -31,7 +31,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
private static final String INPUT = "input";
@NotNull
private Input input = new Input(Collections.<CalculationMessage>emptyList());
private Input input = new Input(Collections.<CalculatorFixableMessage>emptyList());
public CalculatorMessagesDialog() {
}
@ -40,7 +40,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculation_messages_dialog);
setContentView(R.layout.calculator_messages_dialog);
final Intent intent = getIntent();
if (intent != null) {
@ -77,9 +77,9 @@ public class CalculatorMessagesDialog extends SherlockActivity {
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final List<CalculationMessage> messages = input.getMessages();
for (final CalculationMessage message : messages) {
final View view = layoutInflater.inflate(R.layout.calculation_messages_dialog_message, null);
final List<CalculatorFixableMessage> messages = input.getMessages();
for (final CalculatorFixableMessage message : messages) {
final View view = layoutInflater.inflate(R.layout.calculator_messages_dialog_message, null);
final TextView calculationMessagesTextView = (TextView) view.findViewById(R.id.calculation_messages_text_view);
calculationMessagesTextView.setText(message.getMessage());
@ -91,26 +91,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
fixButton.setOnClickListener(null);
} else {
fixButton.setVisibility(View.VISIBLE);
fixButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final List<CalculationMessage> filteredMessages = new ArrayList<CalculationMessage>(messages.size() - 1);
for (CalculationMessage calculationMessage : messages) {
if ( calculationMessage.getFixableError() == null || calculationMessage.getFixableError() != message.getFixableError() ) {
filteredMessages.add(message);
}
}
fixableError.fix();
if (!filteredMessages.isEmpty()) {
CalculatorMessagesDialog.this.input = new Input(filteredMessages);
onInputChanged();
} else {
CalculatorMessagesDialog.this.finish();
}
}
});
fixButton.setOnClickListener(new FixErrorOnClickListener(messages, message));
}
viewGroup.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
@ -132,7 +113,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
**********************************************************************
*/
public static void showDialog(@NotNull List<Message> messages, @NotNull Context context) {
public static void showDialogForMessages(@NotNull List<Message> messages, @NotNull Context context) {
if (!messages.isEmpty()) {
final Intent intent = new Intent(context, CalculatorMessagesDialog.class);
@ -143,6 +124,17 @@ public class CalculatorMessagesDialog extends SherlockActivity {
}
}
public static void showDialog(@NotNull List<CalculatorFixableMessage> messages, @NotNull Context context) {
if (!messages.isEmpty()) {
final Intent intent = new Intent(context, CalculatorMessagesDialog.class);
intent.putExtra(INPUT, new Input(messages));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
private static final class Input implements Parcelable {
public static final Creator<Input> CREATOR = new Creator<Input>() {
@ -159,21 +151,21 @@ public class CalculatorMessagesDialog extends SherlockActivity {
@NotNull
private static Input fromParcel(@NotNull Parcel in) {
final List<CalculationMessage> messages = new ArrayList<CalculationMessage>();
in.readTypedList(messages, CalculationMessage.CREATOR);
final List<CalculatorFixableMessage> messages = new ArrayList<CalculatorFixableMessage>();
in.readTypedList(messages, CalculatorFixableMessage.CREATOR);
return new Input(messages);
}
@NotNull
private List<CalculationMessage> messages = new ArrayList<CalculationMessage>();
private List<CalculatorFixableMessage> messages = new ArrayList<CalculatorFixableMessage>();
private Input(@NotNull List<CalculationMessage> messages) {
private Input(@NotNull List<CalculatorFixableMessage> messages) {
this.messages = messages;
}
@NotNull
public List<CalculationMessage> getMessages() {
public List<CalculatorFixableMessage> getMessages() {
return messages;
}
@ -189,13 +181,49 @@ public class CalculatorMessagesDialog extends SherlockActivity {
@NotNull
public static Input fromMessages(@NotNull List<Message> messages) {
final List<CalculationMessage> stringMessages = new ArrayList<CalculationMessage>(messages.size());
final List<CalculatorFixableMessage> stringMessages = new ArrayList<CalculatorFixableMessage>(messages.size());
for (Message message : messages) {
stringMessages.add(new CalculationMessage(message));
stringMessages.add(new CalculatorFixableMessage(message));
}
return new Input(stringMessages);
}
}
private class FixErrorOnClickListener implements View.OnClickListener {
@NotNull
private final List<CalculatorFixableMessage> messages;
@NotNull
private final CalculatorFixableMessage currentMessage;
public FixErrorOnClickListener(@NotNull List<CalculatorFixableMessage> messages,
@NotNull CalculatorFixableMessage message) {
this.messages = messages;
this.currentMessage = message;
}
@Override
public void onClick(View v) {
final List<CalculatorFixableMessage> filteredMessages = new ArrayList<CalculatorFixableMessage>(messages.size() - 1);
for (CalculatorFixableMessage message : messages) {
if ( message.getFixableError() == null ) {
filteredMessages.add(message);
} else if ( message.getFixableError() != currentMessage.getFixableError() ) {
filteredMessages.add(message);
}
}
currentMessage.getFixableError().fix();
if (!filteredMessages.isEmpty()) {
CalculatorMessagesDialog.this.input = new Input(filteredMessages);
onInputChanged();
} else {
CalculatorMessagesDialog.this.finish();
}
}
}
}

View File

@ -8,10 +8,7 @@ import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.plot.GraphLineColor;
import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference;
import org.solovyev.android.prefs.*;
import org.solovyev.android.view.VibratorContainer;
import java.text.DecimalFormatSymbols;
@ -36,8 +33,9 @@ public final class CalculatorPreferences {
public static final Preference<Boolean> calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true);
public static final Preference<Boolean> showCalculationMessagesDialog = new BooleanPreference("show_calculation_messages_dialog", true);
public static final Preference<NumeralBase> preferredNumeralBase = StringPreference.newInstance("preferred_numeral_base", NumeralBase.dec, NumeralBase.class);
public static final Preference<AngleUnit> preferredAngleUnit = StringPreference.newInstance("preferred_angle_units", AngleUnit.deg, AngleUnit.class);
public static final Preference<NumeralBase> preferredNumeralBase = StringPreference.newInstance("preferred_numeral_base", AndroidCalculatorEngine.Preferences.numeralBase.getDefaultValue(), NumeralBase.class);
public static final Preference<AngleUnit> preferredAngleUnits = StringPreference.newInstance("preferred_angle_units", AndroidCalculatorEngine.Preferences.angleUnit.getDefaultValue(), AngleUnit.class);
public static final Preference<Long> lastPreferredPreferencesCheck = new LongPreference("preferred_preferences_check_time", 0L);
}
@ -183,8 +181,9 @@ public final class CalculatorPreferences {
applyDefaultPreference(preferences, Graph.lineColorReal);
applyDefaultPreference(preferences, History.showIntermediateCalculations);
applyDefaultPreference(preferences, Calculations.calculateOnFly);
applyDefaultPreference(preferences, Calculations.preferredAngleUnit);
applyDefaultPreference(preferences, Calculations.preferredAngleUnits);
applyDefaultPreference(preferences, Calculations.preferredNumeralBase);
applyDefaultPreference(preferences, Calculations.lastPreferredPreferencesCheck);
// renew value after each application start