add a AC (reset) button to the widget
- it does the same as long pressing the C in the app, however widgets cannot be long pressed
This commit is contained in:
parent
5d75720619
commit
66dc345c1b
|
@ -156,7 +156,7 @@ public class CalculatorImpl {
|
||||||
baseValue = Double.parseDouble(newValue);
|
baseValue = Double.parseDouble(newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleLongClear() {
|
public void handleReset() {
|
||||||
resetValues();
|
resetValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ public class Constants {
|
||||||
public static final String ROOT = "root";
|
public static final String ROOT = "root";
|
||||||
public static final String DECIMAL = "decimal";
|
public static final String DECIMAL = "decimal";
|
||||||
public static final String CLEAR = "clear";
|
public static final String CLEAR = "clear";
|
||||||
|
public static final String RESET = "reset";
|
||||||
|
|
||||||
public static final String ZERO = "zero";
|
public static final String ZERO = "zero";
|
||||||
public static final String ONE = "one";
|
public static final String ONE = "one";
|
||||||
|
|
|
@ -65,7 +65,7 @@ public class MainActivity extends AppCompatActivity implements Calculator {
|
||||||
|
|
||||||
@OnLongClick(R.id.btn_clear)
|
@OnLongClick(R.id.btn_clear)
|
||||||
public boolean clearLongClicked() {
|
public boolean clearLongClicked() {
|
||||||
calc.handleLongClear();
|
calc.handleReset();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import android.appwidget.AppWidgetManager;
|
||||||
import android.appwidget.AppWidgetProvider;
|
import android.appwidget.AppWidgetProvider;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.view.View;
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
||||||
|
@ -19,6 +20,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
||||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
widgetManager = appWidgetManager;
|
widgetManager = appWidgetManager;
|
||||||
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
|
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
|
||||||
|
remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE);
|
||||||
calc = new CalculatorImpl(this);
|
calc = new CalculatorImpl(this);
|
||||||
widgetIds = appWidgetIds;
|
widgetIds = appWidgetIds;
|
||||||
cxt = context;
|
cxt = context;
|
||||||
|
@ -45,6 +47,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
||||||
setupIntent(Constants.POWER, R.id.btn_power);
|
setupIntent(Constants.POWER, R.id.btn_power);
|
||||||
setupIntent(Constants.ROOT, R.id.btn_root);
|
setupIntent(Constants.ROOT, R.id.btn_root);
|
||||||
setupIntent(Constants.CLEAR, R.id.btn_clear);
|
setupIntent(Constants.CLEAR, R.id.btn_clear);
|
||||||
|
setupIntent(Constants.RESET, R.id.btn_reset);
|
||||||
|
|
||||||
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
|
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
|
||||||
}
|
}
|
||||||
|
@ -98,6 +101,9 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
||||||
case Constants.CLEAR:
|
case Constants.CLEAR:
|
||||||
calc.handleClear();
|
calc.handleClear();
|
||||||
break;
|
break;
|
||||||
|
case Constants.RESET:
|
||||||
|
calc.handleReset();
|
||||||
|
break;
|
||||||
case Constants.PLUS:
|
case Constants.PLUS:
|
||||||
case Constants.MINUS:
|
case Constants.MINUS:
|
||||||
case Constants.MULTIPLY:
|
case Constants.MULTIPLY:
|
||||||
|
|
|
@ -64,6 +64,15 @@
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:text="C"/>
|
android:text="C"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_reset"
|
||||||
|
style="@style/MyButtonStyle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="AC"
|
||||||
|
android:visibility="gone"/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
|
|
@ -102,7 +102,7 @@ public class MainActivityTest {
|
||||||
@Test
|
@Test
|
||||||
public void clearBtnLongClick_resetsEverything() {
|
public void clearBtnLongClick_resetsEverything() {
|
||||||
calcResult(-1.2, Constants.PLUS, 3.4);
|
calcResult(-1.2, Constants.PLUS, 3.4);
|
||||||
activity.getCalc().handleLongClear();
|
activity.getCalc().handleReset();
|
||||||
handleOperation(Constants.PLUS);
|
handleOperation(Constants.PLUS);
|
||||||
setDouble(3);
|
setDouble(3);
|
||||||
activity.getCalc().handleResult();
|
activity.getCalc().handleResult();
|
||||||
|
|
Loading…
Reference in New Issue