Multiplication button for widget

This commit is contained in:
serso 2012-10-23 11:44:20 +04:00
parent 97c8b6b6d6
commit 8cda4424a3
2 changed files with 478 additions and 481 deletions

View File

@ -1,238 +1,241 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.RemoteViews;
import jscl.AngleUnit; import android.widget.Toast;
import jscl.NumeralBase; import jscl.AngleUnit;
import org.jetbrains.annotations.NotNull; import jscl.NumeralBase;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.AndroidUtils; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.view.NumeralBasesButton; import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.view.ColorButton; import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.view.drag.DragButton; import org.solovyev.android.view.ColorButton;
import org.solovyev.android.view.drag.DragDirection; import org.solovyev.android.view.drag.DragButton;
import org.solovyev.android.view.drag.SimpleOnDragListener; import org.solovyev.android.view.drag.DragDirection;
import org.solovyev.common.math.Point2d; import org.solovyev.android.view.drag.SimpleOnDragListener;
import org.solovyev.common.math.Point2d;
/**
* User: serso /**
* Date: 9/28/12 * User: serso
* Time: 12:06 AM * Date: 9/28/12
*/ * Time: 12:06 AM
public final class CalculatorButtons { */
public final class CalculatorButtons {
private CalculatorButtons () {
} private CalculatorButtons () {
}
public static void processButtons(boolean fixMagicFlames,
@NotNull CalculatorPreferences.Gui.Theme theme, public static void processButtons(boolean fixMagicFlames,
@NotNull View root) { @NotNull CalculatorPreferences.Gui.Theme theme,
if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { @NotNull View root) {
if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) {
if (fixMagicFlames) {
// for metro themes we should turn off magic flames if (fixMagicFlames) {
AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor<ColorButton>() { // for metro themes we should turn off magic flames
@Override AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor<ColorButton>() {
public void process(@NotNull ColorButton colorButton) { @Override
colorButton.setDrawMagicFlame(false); public void process(@NotNull ColorButton colorButton) {
} colorButton.setDrawMagicFlame(false);
}); }
} });
} }
} }
}
static void initMultiplicationButton(@NotNull View root) {
final View multiplicationButton = root.findViewById(R.id.multiplicationButton); static void initMultiplicationButton(@NotNull View root) {
if ( multiplicationButton instanceof Button) { final View multiplicationButton = root.findViewById(R.id.multiplicationButton);
((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); if ( multiplicationButton instanceof Button) {
} ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign());
} }
}
public static void initMultiplicationButton(@NotNull RemoteViews views) {
public static void toggleEqualsButton(@Nullable SharedPreferences preferences, views.setTextViewText(R.id.multiplicationButton, CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign());
@NotNull Activity activity) { }
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences;
public static void toggleEqualsButton(@Nullable SharedPreferences preferences,
final boolean large = AndroidUtils.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, activity.getResources().getConfiguration()); @NotNull Activity activity) {
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences;
if (!large) {
if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT final boolean large = AndroidUtils.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, activity.getResources().getConfiguration());
|| !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) {
if (!large) {
final DragButton equalsButton = (DragButton)activity.findViewById(R.id.equalsButton); if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT
if (equalsButton != null) { || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) {
if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) {
equalsButton.setVisibility(View.VISIBLE); final DragButton equalsButton = (DragButton)activity.findViewById(R.id.equalsButton);
} else { if (equalsButton != null) {
equalsButton.setVisibility(View.GONE); if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) {
} equalsButton.setVisibility(View.VISIBLE);
} } else {
} equalsButton.setVisibility(View.GONE);
} }
} }
}
@Nullable }
private static AndroidCalculatorDisplayView getCalculatorDisplayView() { }
return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView();
} @Nullable
private static AndroidCalculatorDisplayView getCalculatorDisplayView() {
/* return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView();
********************************************************************** }
*
* STATIC CLASSES /*
* **********************************************************************
********************************************************************** *
*/ * STATIC CLASSES
*
static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { **********************************************************************
@Override */
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
final boolean result; static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor {
@Override
if (dragDirection == DragDirection.left) { public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
getKeyboard().roundBracketsButtonPressed(); final boolean result;
result = true;
} else { if (dragDirection == DragDirection.left) {
result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); getKeyboard().roundBracketsButtonPressed();
} result = true;
} else {
return result; result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent);
} }
}
return result;
@NotNull }
private static CalculatorKeyboard getKeyboard() { }
return CalculatorLocatorImpl.getInstance().getKeyboard();
} @NotNull
private static CalculatorKeyboard getKeyboard() {
static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { return CalculatorLocatorImpl.getInstance().getKeyboard();
}
@NotNull
private Context context; static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor {
VarsDragProcessor(Context context) { @NotNull
this.context = context; private Context context;
}
VarsDragProcessor(Context context) {
@Override this.context = context;
public boolean processDragEvent(@NotNull DragDirection dragDirection, }
@NotNull DragButton dragButton,
@NotNull Point2d startPoint2d, @Override
@NotNull MotionEvent motionEvent) { public boolean processDragEvent(@NotNull DragDirection dragDirection,
boolean result = false; @NotNull DragButton dragButton,
@NotNull Point2d startPoint2d,
if (dragDirection == DragDirection.up) { @NotNull MotionEvent motionEvent) {
CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); boolean result = false;
result = true;
} if (dragDirection == DragDirection.up) {
CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay());
return result; result = true;
} }
}
return result;
static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { }
}
@NotNull
private final DigitButtonDragProcessor processor; static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor {
@NotNull @NotNull
private final Context context; private final DigitButtonDragProcessor processor;
AngleUnitsChanger(@NotNull Context context) { @NotNull
this.context = context; private final Context context;
this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard());
} AngleUnitsChanger(@NotNull Context context) {
this.context = context;
@Override this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard());
public boolean processDragEvent(@NotNull DragDirection dragDirection, }
@NotNull DragButton dragButton,
@NotNull Point2d startPoint2d, @Override
@NotNull MotionEvent motionEvent) { public boolean processDragEvent(@NotNull DragDirection dragDirection,
boolean result = false; @NotNull DragButton dragButton,
@NotNull Point2d startPoint2d,
if (dragButton instanceof AngleUnitsButton) { @NotNull MotionEvent motionEvent) {
if (dragDirection != DragDirection.left) { boolean result = false;
final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection);
if (directionText != null) { if (dragButton instanceof AngleUnitsButton) {
try { if (dragDirection != DragDirection.left) {
final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection);
final AngleUnit angleUnits = AngleUnit.valueOf(directionText); if (directionText != null) {
try {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final AngleUnit angleUnits = AngleUnit.valueOf(directionText);
final AngleUnit oldAngleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(preferences);
if (oldAngleUnits != angleUnits) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits);
final AngleUnit oldAngleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(preferences);
Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); if (oldAngleUnits != angleUnits) {
} AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits);
result = true; Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show();
} catch (IllegalArgumentException e) { }
Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText);
} result = true;
} } catch (IllegalArgumentException e) {
} else if (dragDirection == DragDirection.left) { Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText);
result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); }
} }
} } else if (dragDirection == DragDirection.left) {
result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent);
return result; }
} }
}
return result;
static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { }
}
@NotNull
private final Context context; static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor {
NumeralBasesChanger(@NotNull Context context) { @NotNull
this.context = context; private final Context context;
}
NumeralBasesChanger(@NotNull Context context) {
@Override this.context = context;
public boolean processDragEvent(@NotNull DragDirection dragDirection, }
@NotNull DragButton dragButton,
@NotNull Point2d startPoint2d, @Override
@NotNull MotionEvent motionEvent) { public boolean processDragEvent(@NotNull DragDirection dragDirection,
boolean result = false; @NotNull DragButton dragButton,
@NotNull Point2d startPoint2d,
if (dragButton instanceof NumeralBasesButton) { @NotNull MotionEvent motionEvent) {
final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); boolean result = false;
if (directionText != null) {
try { if (dragButton instanceof NumeralBasesButton) {
final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection);
final NumeralBase numeralBase = NumeralBase.valueOf(directionText); if (directionText != null) {
try {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final NumeralBase numeralBase = NumeralBase.valueOf(directionText);
final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences);
if (oldNumeralBase != numeralBase) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences);
Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); if (oldNumeralBase != numeralBase) {
} AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
result = true; Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show();
} catch (IllegalArgumentException e) { }
Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText);
} result = true;
} } catch (IllegalArgumentException e) {
} Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText);
}
return result; }
} }
}
} return result;
}
}
}

View File

@ -1,243 +1,237 @@
package org.solovyev.android.calculator.widget; package org.solovyev.android.calculator.widget;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.Html; import android.text.Html;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.common.MutableObject; import org.solovyev.common.MutableObject;
import java.io.Serializable; import java.io.Serializable;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 19.10.12 * Date: 19.10.12
* Time: 16:18 * Time: 16:18
*/ */
public class CalculatorWidgetProvider extends AppWidgetProvider { public class CalculatorWidgetProvider extends AppWidgetProvider {
/* /*
********************************************************************** **********************************************************************
* *
* CONSTANTS * CONSTANTS
* *
********************************************************************** **********************************************************************
*/ */
private static final String EVENT_ID_EXTRA = "eventId"; private static final String EVENT_ID_EXTRA = "eventId";
private static final String BUTTON_ID_EXTRA = "buttonId"; private static final String BUTTON_ID_EXTRA = "buttonId";
private static final String BUTTON_PRESSED_ACTION = "org.solovyev.calculator.widget.BUTTON_PRESSED"; private static final String BUTTON_PRESSED_ACTION = "org.solovyev.calculator.widget.BUTTON_PRESSED";
private static final String EDITOR_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.EDITOR_STATE_CHANGED"; private static final String EDITOR_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.EDITOR_STATE_CHANGED";
private static final String EDITOR_STATE_EXTRA = "editorState"; private static final String EDITOR_STATE_EXTRA = "editorState";
private static final String DISPLAY_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.DISPLAY_STATE_CHANGED"; private static final String DISPLAY_STATE_CHANGED_ACTION = "org.solovyev.calculator.widget.DISPLAY_STATE_CHANGED";
private static final String DISPLAY_STATE_EXTRA = "displayState"; private static final String DISPLAY_STATE_EXTRA = "displayState";
private static final String TAG = "Calculator++ Widget"; private static final String TAG = "Calculator++ Widget";
/* /*
********************************************************************** **********************************************************************
* *
* FIELDS * FIELDS
* *
********************************************************************** **********************************************************************
*/ */
@Nullable @Nullable
private String cursorColor; private String cursorColor;
@NotNull @NotNull
private final MutableObject<Long> lastDisplayEventId = new MutableObject<Long>(0L); private final MutableObject<Long> lastDisplayEventId = new MutableObject<Long>(0L);
@NotNull @NotNull
private final MutableObject<Long> lastEditorEventId = new MutableObject<Long>(0L); private final MutableObject<Long> lastEditorEventId = new MutableObject<Long>(0L);
/* /*
********************************************************************** **********************************************************************
* *
* METHODS * METHODS
* *
********************************************************************** **********************************************************************
*/ */
@Override @Override
public void onEnabled(Context context) { public void onEnabled(Context context) {
super.onEnabled(context); super.onEnabled(context);
getCursorColor(context); getCursorColor(context);
} }
@NotNull @NotNull
private String getCursorColor(@NotNull Context context) { private String getCursorColor(@NotNull Context context) {
if (cursorColor == null) { if (cursorColor == null) {
cursorColor = Integer.toHexString(context.getResources().getColor(R.color.widget_cursor_color)).substring(2); cursorColor = Integer.toHexString(context.getResources().getColor(R.color.widget_cursor_color)).substring(2);
} }
return cursorColor; return cursorColor;
} }
@Override @Override
public void onUpdate(@NotNull Context context, public void onUpdate(@NotNull Context context,
@NotNull AppWidgetManager appWidgetManager, @NotNull AppWidgetManager appWidgetManager,
@NotNull int[] appWidgetIds) { @NotNull int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds); super.onUpdate(context, appWidgetManager, appWidgetIds);
updateWidget(context, appWidgetManager, appWidgetIds, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState()); updateWidget(context, appWidgetManager, appWidgetIds, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
} }
private void updateWidget(@NotNull Context context, private void updateWidget(@NotNull Context context,
@NotNull CalculatorEditorViewState editorState, @NotNull CalculatorEditorViewState editorState,
@NotNull CalculatorDisplayViewState displayState) { @NotNull CalculatorDisplayViewState displayState) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CalculatorWidgetProvider.class)); final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CalculatorWidgetProvider.class));
updateWidget(context, appWidgetManager, appWidgetIds, editorState, displayState); updateWidget(context, appWidgetManager, appWidgetIds, editorState, displayState);
} }
private void updateWidget(@NotNull Context context, private void updateWidget(@NotNull Context context,
@NotNull AppWidgetManager appWidgetManager, @NotNull AppWidgetManager appWidgetManager,
@NotNull int[] appWidgetIds, @NotNull int[] appWidgetIds,
@NotNull CalculatorEditorViewState editorState, @NotNull CalculatorEditorViewState editorState,
@NotNull CalculatorDisplayViewState displayState) { @NotNull CalculatorDisplayViewState displayState) {
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
for (WidgetButton button : WidgetButton.values()) { for (WidgetButton button : WidgetButton.values()) {
final Intent onButtonClickIntent = new Intent(context, CalculatorWidgetProvider.class); final Intent onButtonClickIntent = new Intent(context, CalculatorWidgetProvider.class);
onButtonClickIntent.setAction(BUTTON_PRESSED_ACTION); onButtonClickIntent.setAction(BUTTON_PRESSED_ACTION);
onButtonClickIntent.putExtra(BUTTON_ID_EXTRA, button.getButtonId()); onButtonClickIntent.putExtra(BUTTON_ID_EXTRA, button.getButtonId());
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, button.getButtonId(), onButtonClickIntent, PendingIntent.FLAG_UPDATE_CURRENT); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, button.getButtonId(), onButtonClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (pendingIntent != null) { if (pendingIntent != null) {
views.setOnClickPendingIntent(button.getButtonId(), pendingIntent); views.setOnClickPendingIntent(button.getButtonId(), pendingIntent);
} }
} }
updateEditorState(context, views, editorState); CalculatorButtons.initMultiplicationButton(views);
updateDisplayState(context, views, displayState);
updateEditorState(context, views, editorState);
appWidgetManager.updateAppWidget(appWidgetId, views); updateDisplayState(context, views, displayState);
}
} appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override }
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent); @Override
public void onReceive(Context context, Intent intent) {
if (CalculatorWidgetProvider.BUTTON_PRESSED_ACTION.equals(intent.getAction())) { super.onReceive(context, intent);
final int buttonId = intent.getIntExtra(CalculatorWidgetProvider.BUTTON_ID_EXTRA, 0);
if (CalculatorWidgetProvider.BUTTON_PRESSED_ACTION.equals(intent.getAction())) {
final WidgetButton button = WidgetButton.getById(buttonId); final int buttonId = intent.getIntExtra(CalculatorWidgetProvider.BUTTON_ID_EXTRA, 0);
if (button != null) {
button.onClick(context); final WidgetButton button = WidgetButton.getById(buttonId);
} if (button != null) {
} else if (EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) { button.onClick(context);
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast received!"); }
} else if (EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) {
final Long eventId = intent.getLongExtra(EVENT_ID_EXTRA, 0L); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast received!");
boolean updateEditor = false; final Long eventId = intent.getLongExtra(EVENT_ID_EXTRA, 0L);
synchronized (lastEditorEventId) {
if (eventId > lastEditorEventId.getObject()) { boolean updateEditor = false;
lastEditorEventId.setObject(eventId); synchronized (lastEditorEventId) {
updateEditor = true; if (eventId > lastEditorEventId.getObject()) {
} lastEditorEventId.setObject(eventId);
} updateEditor = true;
}
if (updateEditor) { }
final Serializable object = intent.getSerializableExtra(EDITOR_STATE_EXTRA);
if (object instanceof CalculatorEditorViewState) { if (updateEditor) {
updateWidget(context, (CalculatorEditorViewState) object, CalculatorLocatorImpl.getInstance().getDisplay().getViewState()); final Serializable object = intent.getSerializableExtra(EDITOR_STATE_EXTRA);
} if (object instanceof CalculatorEditorViewState) {
} updateWidget(context, (CalculatorEditorViewState) object, CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
} else if (DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { }
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!"); }
} else if (DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) {
final Long eventId = intent.getLongExtra(EVENT_ID_EXTRA, 0L); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!");
boolean updateDisplay = false;
synchronized (lastDisplayEventId) { final Long eventId = intent.getLongExtra(EVENT_ID_EXTRA, 0L);
if (eventId > lastDisplayEventId.getObject()) { boolean updateDisplay = false;
lastDisplayEventId.setObject(eventId); synchronized (lastDisplayEventId) {
updateDisplay = true; if (eventId > lastDisplayEventId.getObject()) {
} lastDisplayEventId.setObject(eventId);
} updateDisplay = true;
}
if (updateDisplay) { }
final Serializable object = intent.getSerializableExtra(DISPLAY_STATE_EXTRA);
if (object instanceof CalculatorDisplayViewState) { if (updateDisplay) {
updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), (CalculatorDisplayViewState) object); final Serializable object = intent.getSerializableExtra(DISPLAY_STATE_EXTRA);
} if (object instanceof CalculatorDisplayViewState) {
} updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), (CalculatorDisplayViewState) object);
} else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) { }
updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState()); }
} } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) {
} updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
}
private void updateDisplayState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorDisplayViewState displayState) { }
if (displayState.isValid()) {
setText(views, R.id.calculatorDisplay, displayState.getText()); private void updateDisplayState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorDisplayViewState displayState) {
setTextColor(views, R.id.calculatorDisplay, context.getResources().getColor(R.color.default_text_color)); if (displayState.isValid()) {
} else { views.setTextViewText(R.id.calculatorDisplay, displayState.getText());
setTextColor(views, R.id.calculatorDisplay, context.getResources().getColor(R.color.display_error_text_color)); views.setTextColor(R.id.calculatorDisplay, context.getResources().getColor(R.color.default_text_color));
} } else {
} views.setTextColor(R.id.calculatorDisplay, context.getResources().getColor(R.color.display_error_text_color));
}
private void setText(@NotNull RemoteViews views, int textViewId, @Nullable CharSequence text) { }
views.setTextViewText(textViewId, text);
} private void updateEditorState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorEditorViewState editorState) {
String text = editorState.getText();
private void setTextColor(@NotNull RemoteViews views, int textViewId, int textColor) {
views.setTextColor(textViewId, textColor); CharSequence newText = text;
} int selection = editorState.getSelection();
if (selection >= 0 && selection <= text.length()) {
private void updateEditorState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorEditorViewState editorState) { // inject cursor
String text = editorState.getText(); newText = Html.fromHtml(text.substring(0, selection) + "<font color=\"#" + getCursorColor(context) + "\">|</font>" + text.substring(selection));
}
CharSequence newText = text; CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text);
int selection = editorState.getSelection(); views.setTextViewText(R.id.calculatorEditor, newText);
if (selection >= 0 && selection <= text.length()) { }
// inject cursor
newText = Html.fromHtml(text.substring(0, selection) + "<font color=\"#" + getCursorColor(context) + "\">|</font>" + text.substring(selection)); /*
} **********************************************************************
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text); *
setText(views, R.id.calculatorEditor, newText); * STATIC
} *
**********************************************************************
/* */
**********************************************************************
* public static void onEditorStateChanged(@NotNull Context context,
* STATIC @NotNull CalculatorEventData calculatorEventData,
* @NotNull CalculatorEditorViewState editorViewState) {
**********************************************************************
*/ final Intent intent = new Intent(EDITOR_STATE_CHANGED_ACTION);
intent.setClass(context, CalculatorWidgetProvider.class);
public static void onEditorStateChanged(@NotNull Context context, intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId());
@NotNull CalculatorEventData calculatorEventData, intent.putExtra(EDITOR_STATE_EXTRA, editorViewState);
@NotNull CalculatorEditorViewState editorViewState) { context.sendBroadcast(intent);
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast sent");
final Intent intent = new Intent(EDITOR_STATE_CHANGED_ACTION); }
intent.setClass(context, CalculatorWidgetProvider.class);
intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId()); public static void onDisplayStateChanged(@NotNull Context context,
intent.putExtra(EDITOR_STATE_EXTRA, editorViewState); @NotNull CalculatorEventData calculatorEventData,
context.sendBroadcast(intent); @NotNull CalculatorDisplayViewState displayViewState) {
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast sent");
} final Intent intent = new Intent(DISPLAY_STATE_CHANGED_ACTION);
intent.setClass(context, CalculatorWidgetProvider.class);
public static void onDisplayStateChanged(@NotNull Context context, intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId());
@NotNull CalculatorEventData calculatorEventData, intent.putExtra(DISPLAY_STATE_EXTRA, displayViewState);
@NotNull CalculatorDisplayViewState displayViewState) { context.sendBroadcast(intent);
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast sent");
final Intent intent = new Intent(DISPLAY_STATE_CHANGED_ACTION); }
intent.setClass(context, CalculatorWidgetProvider.class);
intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId());
intent.putExtra(DISPLAY_STATE_EXTRA, displayViewState); }
context.sendBroadcast(intent);
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast sent");
}
}