diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 09274ddd..18406e71 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ - + + + + + + + + + diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Calculator.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Calculator.java index 94bb2772..84345f89 100644 --- a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Calculator.java +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Calculator.java @@ -1,8 +1,6 @@ package calculator.simplemobiletools.com.simple_calculator; public interface Calculator { - String getDisplayedNumber(); - void setValue(String value); void setValueDouble(double d); diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/CalculatorImpl.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/CalculatorImpl.java index ee17b175..693e2e3a 100644 --- a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/CalculatorImpl.java +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/CalculatorImpl.java @@ -1,8 +1,7 @@ package calculator.simplemobiletools.com.simple_calculator; -import android.view.View; - public class CalculatorImpl { + private String displayedValue; private double baseValue; private double secondValue; private boolean resetValue; @@ -17,7 +16,7 @@ public class CalculatorImpl { private void resetValueIfNeeded() { if (resetValue) - callback.setValue("0"); + setValue("0"); resetValue = false; } @@ -28,7 +27,13 @@ public class CalculatorImpl { resetValue = false; lastKey = ""; lastOperation = ""; - callback.setValue("0"); + displayedValue = ""; + setValue("0"); + } + + public void setValue(String value) { + callback.setValue(value); + displayedValue = value; } public void setLastKey(String lastKey) { @@ -36,9 +41,9 @@ public class CalculatorImpl { } public void addDigit(int number) { - final String currentValue = callback.getDisplayedNumber(); + final String currentValue = getDisplayedNumber(); final String newValue = removeLeadingZero(currentValue + number); - callback.setValue(newValue); + setValue(newValue); } private String removeLeadingZero(String str) { @@ -47,12 +52,12 @@ public class CalculatorImpl { } private void updateResult(double value) { - callback.setValue(Formatter.doubleToString(value)); + setValue(Formatter.doubleToString(value)); baseValue = value; } public String getDisplayedNumber() { - return callback.getDisplayedNumber(); + return displayedValue; } public double getDisplayedNumberAsDouble() { @@ -147,7 +152,7 @@ public class CalculatorImpl { if (newValue.equals("-0")) newValue = "0"; - callback.setValue(newValue); + setValue(newValue); baseValue = Double.parseDouble(newValue); } @@ -171,23 +176,23 @@ public class CalculatorImpl { String value = getDisplayedNumber(); if (!value.contains(".")) value += "."; - callback.setValue(value); + setValue(value); } public void zeroClicked() { String value = getDisplayedNumber(); if (!value.equals("0")) value += "0"; - callback.setValue(value); + setValue(value); } - public void numpadClicked(View view) { + public void numpadClicked(int id) { if (lastKey.equals(Constants.EQUALS)) lastOperation = Constants.EQUALS; lastKey = Constants.DIGIT; resetValueIfNeeded(); - switch (view.getId()) { + switch (id) { case R.id.btn_decimal: decimalClicked(); break; diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Constants.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Constants.java index f9d1ff9c..96d74b98 100644 --- a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Constants.java +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Constants.java @@ -10,4 +10,17 @@ public class Constants { public static final String MODULO = "modulo"; public static final String POWER = "power"; public static final String ROOT = "root"; + public static final String DECIMAL = "decimal"; + public static final String CLEAR = "clear"; + + public static final String ZERO = "zero"; + public static final String ONE = "one"; + public static final String TWO = "two"; + public static final String THREE = "three"; + public static final String FOUR = "four"; + public static final String FIVE = "five"; + public static final String SIX = "six"; + public static final String SEVEN = "seven"; + public static final String EIGHT = "eight"; + public static final String NINE = "nine"; } diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java index 5904d1c9..84f12dc2 100644 --- a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java @@ -23,11 +23,6 @@ public class MainActivity extends AppCompatActivity implements Calculator { calc = new CalculatorImpl(this); } - @Override - public String getDisplayedNumber() { - return result.getText().toString(); - } - @OnClick(R.id.btn_plus) public void plusClicked() { calc.handleOperation(Constants.PLUS); @@ -82,11 +77,11 @@ public class MainActivity extends AppCompatActivity implements Calculator { @OnClick({R.id.btn_decimal, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9}) public void numpadClick(View view) { - numpadClicked(view); + numpadClicked(view.getId()); } - public void numpadClicked(View view) { - calc.numpadClicked(view); + public void numpadClicked(int id) { + calc.numpadClicked(id); } @Override @@ -97,7 +92,7 @@ public class MainActivity extends AppCompatActivity implements Calculator { // used only by Robolectric @Override public void setValueDouble(double d) { - setValue(Formatter.doubleToString(d)); + calc.setValue(Formatter.doubleToString(d)); calc.setLastKey(Constants.DIGIT); } diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MyWidgetProvider.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MyWidgetProvider.java new file mode 100644 index 00000000..8d7357e2 --- /dev/null +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MyWidgetProvider.java @@ -0,0 +1,125 @@ +package calculator.simplemobiletools.com.simple_calculator; + +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.Context; +import android.content.Intent; +import android.widget.RemoteViews; + +public class MyWidgetProvider extends AppWidgetProvider implements Calculator { + private static RemoteViews remoteViews; + private static CalculatorImpl calc; + private static AppWidgetManager widgetManager; + private static int[] widgetIds; + private static Intent intent; + private static Context cxt; + + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + widgetManager = appWidgetManager; + remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main); + calc = new CalculatorImpl(this); + widgetIds = appWidgetIds; + cxt = context; + + intent = new Intent(context, MyWidgetProvider.class); + setupIntent(Constants.DECIMAL, R.id.btn_decimal); + setupIntent(Constants.ZERO, R.id.btn_0); + setupIntent(Constants.ONE, R.id.btn_1); + setupIntent(Constants.TWO, R.id.btn_2); + setupIntent(Constants.THREE, R.id.btn_3); + setupIntent(Constants.FOUR, R.id.btn_4); + setupIntent(Constants.FIVE, R.id.btn_5); + setupIntent(Constants.SIX, R.id.btn_6); + setupIntent(Constants.SEVEN, R.id.btn_7); + setupIntent(Constants.EIGHT, R.id.btn_8); + setupIntent(Constants.NINE, R.id.btn_9); + + setupIntent(Constants.EQUALS, R.id.btn_equals); + setupIntent(Constants.PLUS, R.id.btn_plus); + setupIntent(Constants.MINUS, R.id.btn_minus); + setupIntent(Constants.MULTIPLY, R.id.btn_multiply); + setupIntent(Constants.DIVIDE, R.id.btn_divide); + setupIntent(Constants.MODULO, R.id.btn_modulo); + setupIntent(Constants.POWER, R.id.btn_power); + setupIntent(Constants.ROOT, R.id.btn_root); + setupIntent(Constants.CLEAR, R.id.btn_clear); + + appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); + } + + private void setupIntent(String action, int id) { + intent.setAction(action); + PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0); + remoteViews.setOnClickPendingIntent(id, pendingIntent); + } + + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + switch (action) { + case Constants.DECIMAL: + calc.numpadClicked(R.id.btn_decimal); + break; + case Constants.ZERO: + calc.numpadClicked(R.id.btn_0); + break; + case Constants.ONE: + calc.numpadClicked(R.id.btn_1); + break; + case Constants.TWO: + calc.numpadClicked(R.id.btn_2); + break; + case Constants.THREE: + calc.numpadClicked(R.id.btn_3); + break; + case Constants.FOUR: + calc.numpadClicked(R.id.btn_4); + break; + case Constants.FIVE: + calc.numpadClicked(R.id.btn_5); + break; + case Constants.SIX: + calc.numpadClicked(R.id.btn_6); + break; + case Constants.SEVEN: + calc.numpadClicked(R.id.btn_7); + break; + case Constants.EIGHT: + calc.numpadClicked(R.id.btn_8); + break; + case Constants.NINE: + calc.numpadClicked(R.id.btn_9); + break; + case Constants.EQUALS: + calc.handleEquals(); + break; + case Constants.CLEAR: + calc.handleClear(); + break; + case Constants.PLUS: + case Constants.MINUS: + case Constants.MULTIPLY: + case Constants.DIVIDE: + case Constants.MODULO: + case Constants.POWER: + case Constants.ROOT: + calc.handleOperation(action); + break; + default: + super.onReceive(context, intent); + } + } + + @Override + public void setValue(String value) { + remoteViews.setTextViewText(R.id.result, value); + widgetManager.updateAppWidget(widgetIds, remoteViews); + } + + @Override + public void setValueDouble(double d) { + + } +} diff --git a/app/src/main/res/xml/widget_info.xml b/app/src/main/res/xml/widget_info.xml new file mode 100644 index 00000000..ef721625 --- /dev/null +++ b/app/src/main/res/xml/widget_info.xml @@ -0,0 +1,6 @@ + + + diff --git a/app/src/test/java/calculator/simplemobiletools/com/simple_calculator/MainActivityTest.java b/app/src/test/java/calculator/simplemobiletools/com/simple_calculator/MainActivityTest.java index e14b44bf..c314d616 100644 --- a/app/src/test/java/calculator/simplemobiletools/com/simple_calculator/MainActivityTest.java +++ b/app/src/test/java/calculator/simplemobiletools/com/simple_calculator/MainActivityTest.java @@ -161,6 +161,6 @@ public class MainActivityTest { } private String getDisplayedNumber() { - return activity.getDisplayedNumber(); + return activity.getCalc().getDisplayedNumber(); } }