Widget changes

This commit is contained in:
Sergey Solovyev 2012-10-22 00:23:50 +04:00
parent 904a09ce76
commit 3a0ac43585
4 changed files with 56 additions and 34 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" android:versionCode="103" android:versionName="1.4.3-b1" package="org.solovyev.android.calculator"> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" android:versionCode="103" android:versionName="1.4.3-b1-SNAPSHOT" package="org.solovyev.android.calculator">
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>

View File

@ -48,6 +48,7 @@ public class CalculatorApplication extends android.app.Application {
@NotNull @NotNull
private static CalculatorApplication instance; private static CalculatorApplication instance;
@SuppressWarnings("FieldCanBeLocal")// in order not to be garbage collected
@NotNull @NotNull
private CalculatorWidgetHelper widgetHelper; private CalculatorWidgetHelper widgetHelper;

View File

@ -1,8 +1,5 @@
package org.solovyev.android.calculator.widget; package org.solovyev.android.calculator.widget;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
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.*;
@ -12,7 +9,7 @@ import org.solovyev.android.calculator.*;
* Date: 10/19/12 * Date: 10/19/12
* Time: 11:11 PM * Time: 11:11 PM
*/ */
public class CalculatorWidgetHelper extends BroadcastReceiver implements CalculatorEventListener { public class CalculatorWidgetHelper implements CalculatorEventListener {
private static final String TAG = "Calculator++ Widget Helper"; private static final String TAG = "Calculator++ Widget Helper";
@ -35,7 +32,7 @@ public class CalculatorWidgetHelper extends BroadcastReceiver implements Calcula
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed: " + newEditorState.getText()); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed: " + newEditorState.getText());
CalculatorWidgetProvider.onEditorStateChanged(CalculatorApplication.getInstance(), newEditorState); CalculatorWidgetProvider.onEditorStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newEditorState);
break; break;
case display_state_changed: case display_state_changed:
@ -44,22 +41,9 @@ public class CalculatorWidgetHelper extends BroadcastReceiver implements Calcula
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed: " + newDisplayState.getText()); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed: " + newDisplayState.getText());
CalculatorWidgetProvider.onDisplayStateChanged(CalculatorApplication.getInstance(), newDisplayState); CalculatorWidgetProvider.onDisplayStateChanged(CalculatorApplication.getInstance(), calculatorEventData, newDisplayState);
break; break;
} }
} }
} }
@Override
public void onReceive(Context context, Intent intent) {
if (CalculatorWidgetProvider.BUTTON_PRESSED_ACTION.equals(intent.getAction())) {
final int buttonId = intent.getIntExtra(CalculatorWidgetProvider.BUTTON_ID_EXTRA, 0);
//Toast.makeText(context, "Button id: " + buttonId, Toast.LENGTH_SHORT).show();
final WidgetButton button = WidgetButton.getById(buttonId);
if ( button != null ) {
button.onClick(context);
}
}
}
} }

View File

@ -10,10 +10,8 @@ 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.CalculatorDisplayViewState; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorEditorViewState; import org.solovyev.common.MutableObject;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R;
import java.io.Serializable; import java.io.Serializable;
@ -31,8 +29,10 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
* *
********************************************************************** **********************************************************************
*/ */
public static final String BUTTON_ID_EXTRA = "buttonId"; private static final String EVENT_ID_EXTRA = "eventId";
public static final String BUTTON_PRESSED_ACTION = "org.solovyev.calculator.widget.BUTTON_PRESSED";
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 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";
@ -53,6 +53,12 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
@Nullable @Nullable
private String cursorColor; private String cursorColor;
@NotNull
private final MutableObject<Long> lastDisplayEventId = new MutableObject<Long>(0L);
@NotNull
private final MutableObject<Long> lastEditorEventId = new MutableObject<Long>(0L);
/* /*
********************************************************************** **********************************************************************
* *
@ -132,17 +138,40 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
} else if (EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) { } else if (EDITOR_STATE_CHANGED_ACTION.equals(intent.getAction())) {
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast received!"); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast received!");
final Long eventId = intent.getLongExtra(EVENT_ID_EXTRA, 0L);
boolean updateEditor = false;
synchronized (lastEditorEventId) {
if (eventId > lastEditorEventId.getObject()) {
lastEditorEventId.setObject(eventId);
updateEditor = true;
}
}
if (updateEditor) {
final Serializable object = intent.getSerializableExtra(EDITOR_STATE_EXTRA); final Serializable object = intent.getSerializableExtra(EDITOR_STATE_EXTRA);
if (object instanceof CalculatorEditorViewState) { if (object instanceof CalculatorEditorViewState) {
updateWidget(context, (CalculatorEditorViewState) object, CalculatorLocatorImpl.getInstance().getDisplay().getViewState()); updateWidget(context, (CalculatorEditorViewState) object, CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
} }
}
} else if (DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { } else if (DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) {
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!"); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!");
final Long eventId = intent.getLongExtra(EVENT_ID_EXTRA, 0L);
boolean updateDisplay = false;
synchronized (lastDisplayEventId) {
if (eventId > lastDisplayEventId.getObject()) {
lastDisplayEventId.setObject(eventId);
updateDisplay = true;
}
}
if (updateDisplay) {
final Serializable object = intent.getSerializableExtra(DISPLAY_STATE_EXTRA); final Serializable object = intent.getSerializableExtra(DISPLAY_STATE_EXTRA);
if (object instanceof CalculatorDisplayViewState) { if (object instanceof CalculatorDisplayViewState) {
updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), (CalculatorDisplayViewState) object); updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), (CalculatorDisplayViewState) object);
} }
}
} else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) { } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) {
updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState()); updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
} }
@ -186,17 +215,25 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
********************************************************************** **********************************************************************
*/ */
public static void onEditorStateChanged(@NotNull Context context, @NotNull CalculatorEditorViewState editorViewState) { public static void onEditorStateChanged(@NotNull Context context,
@NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorEditorViewState editorViewState) {
final Intent intent = new Intent(EDITOR_STATE_CHANGED_ACTION); final Intent intent = new Intent(EDITOR_STATE_CHANGED_ACTION);
intent.setClass(context, CalculatorWidgetProvider.class); intent.setClass(context, CalculatorWidgetProvider.class);
intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId());
intent.putExtra(EDITOR_STATE_EXTRA, editorViewState); intent.putExtra(EDITOR_STATE_EXTRA, editorViewState);
context.sendBroadcast(intent); context.sendBroadcast(intent);
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast sent"); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Editor state changed broadcast sent");
} }
public static void onDisplayStateChanged(@NotNull Context context, @NotNull CalculatorDisplayViewState displayViewState) { public static void onDisplayStateChanged(@NotNull Context context,
@NotNull CalculatorEventData calculatorEventData,
@NotNull CalculatorDisplayViewState displayViewState) {
final Intent intent = new Intent(DISPLAY_STATE_CHANGED_ACTION); final Intent intent = new Intent(DISPLAY_STATE_CHANGED_ACTION);
intent.setClass(context, CalculatorWidgetProvider.class); intent.setClass(context, CalculatorWidgetProvider.class);
intent.putExtra(EVENT_ID_EXTRA, calculatorEventData.getEventId());
intent.putExtra(DISPLAY_STATE_EXTRA, displayViewState); intent.putExtra(DISPLAY_STATE_EXTRA, displayViewState);
context.sendBroadcast(intent); context.sendBroadcast(intent);
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast sent"); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast sent");