add a basic home screen widget
This commit is contained in:
parent
1969300620
commit
4e1c556e8e
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest package="calculator.simplemobiletools.com.simple_calculator"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<manifest
|
||||
package="calculator.simplemobiletools.com.simple_calculator"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
@ -15,6 +16,19 @@
|
|||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".MyWidgetProvider"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="Simple Calculator">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget_info"/>
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package calculator.simplemobiletools.com.simple_calculator;
|
||||
|
||||
public interface Calculator {
|
||||
String getDisplayedNumber();
|
||||
|
||||
void setValue(String value);
|
||||
|
||||
void setValueDouble(double d);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:initialLayout="@layout/activity_main"
|
||||
android:minHeight="250dp"
|
||||
android:minWidth="250dp">
|
||||
</appwidget-provider>
|
|
@ -161,6 +161,6 @@ public class MainActivityTest {
|
|||
}
|
||||
|
||||
private String getDisplayedNumber() {
|
||||
return activity.getDisplayedNumber();
|
||||
return activity.getCalc().getDisplayedNumber();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue