diff --git a/res/layout/calc_vars_button.xml b/res/layout/calc_vars_button.xml index f23111df..12e69256 100644 --- a/res/layout/calc_vars_button.xml +++ b/res/layout/calc_vars_button.xml @@ -7,7 +7,10 @@ --> Меньше - возвращает 1, если первый аргумент меньше второго, иначе 0. Больше - возвращает 1, если первый аргумент больше второго, иначе 0. + Невозможно создать пустую константу! + Результат не валиден! + diff --git a/res/values/strings.xml b/res/values/strings.xml index a814e7d9..dd5fa161 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -141,4 +141,7 @@ Lesser function - gives 1 if first argument is less than second, 0 otherwise. Greater function - gives 1 if first argument is greater than second, 0 otherwise. + Unable to create empty constant! + Current result is not valid! + diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index c1a4669a..94e1cb6f 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -31,6 +31,7 @@ import org.solovyev.android.view.FontSizeAdjuster; import org.solovyev.android.view.widgets.*; import org.solovyev.common.BooleanMapper; import org.solovyev.common.utils.Announcer; +import org.solovyev.common.utils.Point2d; import org.solovyev.common.utils.history.HistoryAction; import java.lang.reflect.Field; @@ -42,8 +43,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh private static final int HVGA_WIDTH_PIXELS = 320; - private static final long ON_CLICK_VIBRATION_TIME = 100; - @NotNull private final Announcer dpclRegister = new Announcer(DragPreferencesChangeListener.class); @@ -107,11 +106,35 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh equalsButton.setOnDragListener(evalOnDragListener); } + final DragButton varsButton = (DragButton) findViewById(R.id.varsButton); + if (varsButton != null) { + final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences); + varsButton.setOnDragListener(varsOnDragListener); + } + CalculatorEngine.instance.reset(this, preferences); preferences.registerOnSharedPreferenceChangeListener(this); } + private class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragDirection == DragDirection.up) { + CalculatorActivityLauncher.createVar(CalculatorActivity.this, CalculatorActivity.this.calculatorModel); + result = true; + } + + return result; + } + } + private void setDefaultValues(@NotNull SharedPreferences preferences) { if (!preferences.contains(CalculatorEngine.GROUPING_SEPARATOR_P_KEY)) { final Locale locale = Locale.getDefault(); @@ -246,7 +269,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @SuppressWarnings({"UnusedDeclaration"}) public void historyButtonClickHandler(@NotNull View v) { - this.showHistory(); + CalculatorActivityLauncher.showHistory(this); } @SuppressWarnings({"UnusedDeclaration"}) @@ -307,12 +330,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @SuppressWarnings({"UnusedDeclaration"}) public void functionsButtonClickHandler(@NotNull View v) { - startActivity(new Intent(this, CalculatorFunctionsActivity.class)); + CalculatorActivityLauncher.showFunctions(this); } @SuppressWarnings({"UnusedDeclaration"}) public void varsButtonClickHandler(@NotNull View v) { - startActivity(new Intent(this, CalculatorVarsActivity.class)); + CalculatorActivityLauncher.showVars(this); } private final static String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; @@ -364,15 +387,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh switch (item.getItemId()) { case R.id.main_menu_item_settings: - showSettings(); + CalculatorActivityLauncher.showSettings(this); result = true; break; case R.id.main_menu_item_history: - showHistory(); + CalculatorActivityLauncher.showHistory(this); result = true; break; case R.id.main_menu_item_about: - showAbout(); + CalculatorActivityLauncher.showAbout(this); result = true; break; case R.id.main_menu_item_exit: @@ -386,18 +409,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh return result; } - private void showHistory() { - startActivity(new Intent(this, CalculatorHistoryActivity.class)); - } - - private void showSettings() { - startActivity(new Intent(this, CalculatorPreferencesActivity.class)); - } - - private void showAbout() { - startActivity(new Intent(this, AboutActivity.class)); - } - /** * The font sizes in the layout files are specified for a HVGA display. * Adjust the font sizes accordingly if we are running on a different diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java new file mode 100644 index 00000000..536e0498 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -0,0 +1,56 @@ +package org.solovyev.android.calculator; + +import android.content.Context; +import android.content.Intent; +import android.view.View; +import android.widget.Toast; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.utils.StringUtils; + +/** + * User: serso + * Date: 11/2/11 + * Time: 2:18 PM + */ +public class CalculatorActivityLauncher { + + public static void showHistory(@NotNull final Context context) { + context.startActivity(new Intent(context, CalculatorHistoryActivity.class)); + } + + public static void showSettings(@NotNull final Context context) { + context.startActivity(new Intent(context, CalculatorPreferencesActivity.class)); + } + + public static void showAbout(@NotNull final Context context) { + context.startActivity(new Intent(context, AboutActivity.class)); + } + + public static void showFunctions(@NotNull final Context context) { + context.startActivity(new Intent(context, CalculatorFunctionsActivity.class)); + } + + public static void showVars(@NotNull final Context context) { + context.startActivity(new Intent(context, CalculatorVarsActivity.class)); + } + + public static void createVar(@NotNull final Context context, @NotNull CalculatorModel calculatorModel) { + if (calculatorModel.getDisplay().isValid() ) { + final String varValue = calculatorModel.getDisplay().getText().toString(); + if (!StringUtils.isEmpty(varValue)) { + if (CalculatorVarsActivity.isValid(varValue)) { + final Intent intent = new Intent(context, CalculatorVarsActivity.class); + intent.putExtra(CalculatorVarsActivity.CREATE_VAR_EXTRA_STRING, varValue); + context.startActivity(intent); + } else { + Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(context, R.string.c_empty_var_error, Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show(); + } + } +} diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java index 6e61f444..8d354f52 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java @@ -352,4 +352,9 @@ public enum CalculatorModel implements CursorControl, HistoryControl acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё".toCharArray())); @NotNull @@ -78,6 +81,14 @@ public class CalculatorVarsActivity extends ListActivity { } }); + final Intent intent = getIntent(); + if ( intent != null ) { + final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING); + if (!StringUtils.isEmpty(varValue)) { + createEditVariableDialog(null, null, varValue, null); + } + } + } @SuppressWarnings({"UnusedDeclaration"}) @@ -209,12 +220,7 @@ public class CalculatorVarsActivity extends ListActivity { error = null; } else { // value is not empty => must be a number - boolean correctDouble = true; - try { - Double.valueOf(value); - } catch (NumberFormatException e) { - correctDouble = false; - } + boolean correctDouble = isValid(value); if (correctDouble) { varBuilder.setName(name); @@ -253,6 +259,16 @@ public class CalculatorVarsActivity extends ListActivity { } } + public static boolean isValid(@NotNull String value) { + boolean valid = true; + try { + Double.valueOf(value); + } catch (NumberFormatException e) { + valid = false; + } + return valid; + } + private class VarsArrayAdapter extends ArrayAdapter { private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List objects) {