From 5a94e633e7834a28ac92d9bb4b67e6d265431364 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Fri, 28 Jun 2013 15:59:57 +0400 Subject: [PATCH] fix for widget --- .../calculator/CalculatorBroadcaster.java | 46 ++++++ .../ParcelableCalculatorDisplayViewState.java | 131 ------------------ .../ParcelableCalculatorEditorViewState.java | 90 ------------ .../onscreen/CalculatorOnscreenService.java | 37 +++-- .../AbstractCalculatorWidgetProvider.java | 41 +++--- android-app/AndroidManifest.xml | 18 ++- .../calculator/CalculatorApplication.java | 5 + 7 files changed, 100 insertions(+), 268 deletions(-) create mode 100644 android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java delete mode 100644 android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorDisplayViewState.java delete mode 100644 android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorEditorViewState.java diff --git a/android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java b/android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java new file mode 100644 index 00000000..7a0a009a --- /dev/null +++ b/android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorBroadcaster.java @@ -0,0 +1,46 @@ +package org.solovyev.android.calculator; + +import android.content.Context; +import android.content.Intent; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public final class CalculatorBroadcaster implements CalculatorEventListener { + + public static final String ACTION_INIT = "org.solovyev.android.calculator.INIT"; + public static final String ACTION_EDITOR_STATE_CHANGED = "org.solovyev.android.calculator.EDITOR_STATE_CHANGED"; + public static final String ACTION_DISPLAY_STATE_CHANGED = "org.solovyev.android.calculator.DISPLAY_STATE_CHANGED"; + + @Nonnull + private final Context context; + + public CalculatorBroadcaster(@Nonnull Context context) { + this.context = context; + } + + @Override + public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { + switch (calculatorEventType) { + case editor_state_changed: + case editor_state_changed_light: + sendEditorStateChangedIntent(); + break; + case display_state_changed: + sendDisplayStateChanged(); + break; + } + } + + private void sendDisplayStateChanged() { + sendBroadcastIntent(ACTION_DISPLAY_STATE_CHANGED); + } + + private void sendEditorStateChangedIntent() { + sendBroadcastIntent(ACTION_EDITOR_STATE_CHANGED); + } + + private void sendBroadcastIntent(@Nonnull String action) { + context.sendBroadcast(new Intent(action)); + } +} diff --git a/android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorDisplayViewState.java b/android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorDisplayViewState.java deleted file mode 100644 index e8f6f305..00000000 --- a/android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorDisplayViewState.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import android.os.Parcel; -import android.os.Parcelable; -import jscl.math.Generic; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import org.solovyev.android.calculator.jscl.JsclOperation; - -/** - * User: serso - * Date: 11/18/12 - * Time: 1:40 PM - */ -public final class ParcelableCalculatorDisplayViewState implements CalculatorDisplayViewState, Parcelable { - - public static final Creator CREATOR = new Creator() { - @Override - public ParcelableCalculatorDisplayViewState createFromParcel(@Nonnull Parcel in) { - return ParcelableCalculatorDisplayViewState.fromParcel(in); - } - - @Override - public ParcelableCalculatorDisplayViewState[] newArray(int size) { - return new ParcelableCalculatorDisplayViewState[size]; - } - }; - - @Nonnull - private static ParcelableCalculatorDisplayViewState fromParcel(@Nonnull Parcel in) { - final int selection = in.readInt(); - final boolean valid = in.readInt() == 1; - final String stringResult = in.readString(); - final String errorMessage = in.readString(); - final JsclOperation operation = (JsclOperation) in.readSerializable(); - - CalculatorDisplayViewState calculatorDisplayViewState; - if (valid) { - calculatorDisplayViewState = CalculatorDisplayViewStateImpl.newValidState(operation, null, stringResult, selection); - } else { - calculatorDisplayViewState = CalculatorDisplayViewStateImpl.newErrorState(operation, errorMessage); - } - - return new ParcelableCalculatorDisplayViewState(calculatorDisplayViewState); - } - - @Nonnull - private CalculatorDisplayViewState viewState; - - public ParcelableCalculatorDisplayViewState(@Nonnull CalculatorDisplayViewState viewState) { - this.viewState = viewState; - } - - @Override - @Nonnull - public String getText() { - return viewState.getText(); - } - - @Override - public int getSelection() { - return viewState.getSelection(); - } - - @Override - @Nullable - public Generic getResult() { - return viewState.getResult(); - } - - @Override - public boolean isValid() { - return viewState.isValid(); - } - - @Override - @Nullable - public String getErrorMessage() { - return viewState.getErrorMessage(); - } - - @Override - @Nonnull - public JsclOperation getOperation() { - return viewState.getOperation(); - } - - @Override - @Nullable - public String getStringResult() { - return viewState.getStringResult(); - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(@Nonnull Parcel out, int flags) { - out.writeInt(viewState.getSelection()); - out.writeInt(viewState.isValid() ? 1 : 0); - out.writeString(viewState.getStringResult()); - out.writeString(viewState.getErrorMessage()); - out.writeSerializable(viewState.getOperation()); - } -} diff --git a/android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorEditorViewState.java b/android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorEditorViewState.java deleted file mode 100644 index 069a9a26..00000000 --- a/android-app-core/src/main/java/org/solovyev/android/calculator/ParcelableCalculatorEditorViewState.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import android.os.Parcel; -import android.os.Parcelable; - -import javax.annotation.Nonnull; - -/** - * User: serso - * Date: 11/18/12 - * Time: 1:40 PM - */ -public final class ParcelableCalculatorEditorViewState implements CalculatorEditorViewState, Parcelable { - - public static final Creator CREATOR = new Creator() { - @Override - public ParcelableCalculatorEditorViewState createFromParcel(@Nonnull Parcel in) { - return ParcelableCalculatorEditorViewState.fromParcel(in); - } - - @Override - public ParcelableCalculatorEditorViewState[] newArray(int size) { - return new ParcelableCalculatorEditorViewState[size]; - } - }; - - @Nonnull - private CalculatorEditorViewState viewState; - - public ParcelableCalculatorEditorViewState(@Nonnull CalculatorEditorViewState viewState) { - this.viewState = viewState; - } - - @Nonnull - private static ParcelableCalculatorEditorViewState fromParcel(@Nonnull Parcel in) { - final String text = in.readString(); - final int selection = in.readInt(); - return new ParcelableCalculatorEditorViewState(CalculatorEditorViewStateImpl.newInstance(text, selection)); - } - - @Override - @Nonnull - public String getText() { - return viewState.getText(); - } - - @Nonnull - @Override - public CharSequence getTextAsCharSequence() { - return getText(); - } - - @Override - public int getSelection() { - return viewState.getSelection(); - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(@Nonnull Parcel out, int flags) { - out.writeString(viewState.getText()); - out.writeInt(viewState.getSelection()); - } -} diff --git a/android-app-onscreen/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java b/android-app-onscreen/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java index fe020455..4a702af3 100644 --- a/android-app-onscreen/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java +++ b/android-app-onscreen/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java @@ -44,8 +44,8 @@ import javax.annotation.Nullable; */ public class CalculatorOnscreenService extends Service implements OnscreenViewListener, CalculatorEventListener { - private static final String INIT_ACTION = "org.solovyev.android.calculator.INIT"; - private static final String INIT_ACTION_CREATE_VIEW_EXTRA = "createView"; + private static final String SHOW_WINDOW_ACTION = "org.solovyev.android.calculator.onscreen.SHOW_WINDOW"; + private static final String SHOW_NOTIFICATION_ACTION = "org.solovyev.android.calculator.onscreen.SHOW_NOTIFICATION"; private static final int NOTIFICATION_ID = 9031988; // my birthday =) @@ -151,21 +151,21 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi private void handleStart(@Nullable Intent intent) { if (intent != null) { - if (isInitIntent(intent)) { - - boolean createView = intent.getBooleanExtra(INIT_ACTION_CREATE_VIEW_EXTRA, false); - if (createView) { - hideNotification(); - createView(); - } else { - showNotification(); - } + if (isShowWindowIntent(intent)) { + hideNotification(); + createView(); + } else if (isShowNotificationIntent(intent)) { + showNotification(); } } } - private boolean isInitIntent(@Nonnull Intent intent) { - return intent.getAction().equals(INIT_ACTION); + private boolean isShowNotificationIntent(@Nonnull Intent intent) { + return intent.getAction().equals(SHOW_NOTIFICATION_ACTION); + } + + private boolean isShowWindowIntent(@Nonnull Intent intent) { + return intent.getAction().equals(SHOW_WINDOW_ACTION); } private void hideNotification() { @@ -191,7 +191,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi builder.setContentText(getString(R.string.open_onscreen_calculator)); builder.setOngoing(true); - final Intent intent = createShowOnscreenViewIntent(this); + final Intent intent = createShowWindowIntent(this); builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -199,21 +199,20 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi } public static void showNotification(@Nonnull Context context) { - final Intent intent = new Intent(INIT_ACTION); + final Intent intent = new Intent(SHOW_NOTIFICATION_ACTION); intent.setClass(context, getIntentListenerClass()); context.sendBroadcast(intent); } public static void showOnscreenView(@Nonnull Context context) { - final Intent intent = createShowOnscreenViewIntent(context); + final Intent intent = createShowWindowIntent(context); context.sendBroadcast(intent); } @Nonnull - private static Intent createShowOnscreenViewIntent(@Nonnull Context context) { - final Intent intent = new Intent(INIT_ACTION); + private static Intent createShowWindowIntent(@Nonnull Context context) { + final Intent intent = new Intent(SHOW_WINDOW_ACTION); intent.setClass(context, getIntentListenerClass()); - intent.putExtra(INIT_ACTION_CREATE_VIEW_EXTRA, true); return intent; } diff --git a/android-app-widget/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java b/android-app-widget/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java index acb3cd03..46381be5 100644 --- a/android-app-widget/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java +++ b/android-app-widget/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java @@ -36,16 +36,18 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; +import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_DISPLAY_STATE_CHANGED; +import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED; /** * User: Solovyev_S * Date: 19.10.12 * Time: 16:18 */ -abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider{ +abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { - static final String BUTTON_ID_EXTRA = "buttonId"; - static final String BUTTON_PRESSED_ACTION = "org.solovyev.android.calculator.widget.BUTTON_PRESSED"; + static final String ACTION_BUTTON_ID_EXTRA = "buttonId"; + static final String ACTION_BUTTON_PRESSED = "org.solovyev.android.calculator.BUTTON_PRESSED"; private static final String TAG = "Calculator++ Widget"; @@ -99,18 +101,15 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider{ @Nonnull int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); - updateWidget(context, appWidgetManager, appWidgetIds, Locator.getInstance().getEditor().getViewState(), Locator.getInstance().getDisplay().getViewState()); + updateWidget(context, appWidgetManager, appWidgetIds); } - public void updateState(@Nonnull Context context, - @Nonnull CalculatorEditorViewState editorState, - @Nonnull CalculatorDisplayViewState displayState) { + public void updateState(@Nonnull Context context) { final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getComponentClass())); - updateWidget(context, appWidgetManager, appWidgetIds, editorState, displayState); + updateWidget(context, appWidgetManager, appWidgetIds); } - @Nonnull protected Class getComponentClass() { return this.getClass(); @@ -118,16 +117,17 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider{ private void updateWidget(@Nonnull Context context, @Nonnull AppWidgetManager appWidgetManager, - @Nonnull int[] appWidgetIds, - @Nonnull CalculatorEditorViewState editorState, - @Nonnull CalculatorDisplayViewState displayState) { + @Nonnull int[] appWidgetIds) { + final CalculatorEditorViewState editorState = Locator.getInstance().getEditor().getViewState(); + final CalculatorDisplayViewState displayState = Locator.getInstance().getDisplay().getViewState(); + for (int appWidgetId : appWidgetIds) { final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); for (CalculatorButton button : CalculatorButton.values()) { final Intent onButtonClickIntent = new Intent(context, getComponentClass()); - onButtonClickIntent.setAction(BUTTON_PRESSED_ACTION); - onButtonClickIntent.putExtra(BUTTON_ID_EXTRA, button.getButtonId()); + onButtonClickIntent.setAction(ACTION_BUTTON_PRESSED); + onButtonClickIntent.putExtra(ACTION_BUTTON_ID_EXTRA, button.getButtonId()); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, button.getButtonId(), onButtonClickIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (pendingIntent != null) { views.setOnClickPendingIntent(button.getButtonId(), pendingIntent); @@ -147,15 +147,20 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider{ public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); - if (BUTTON_PRESSED_ACTION.equals(intent.getAction())) { - final int buttonId = intent.getIntExtra(AbstractCalculatorWidgetProvider.BUTTON_ID_EXTRA, 0); + final String action = intent.getAction(); + if (ACTION_BUTTON_PRESSED.equals(action)) { + final int buttonId = intent.getIntExtra(ACTION_BUTTON_ID_EXTRA, 0); final CalculatorButton button = CalculatorButton.getById(buttonId); if (button != null) { button.onClick(context); } - } else if (ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) { - updateState(context, Locator.getInstance().getEditor().getViewState(), Locator.getInstance().getDisplay().getViewState()); + } else if (ACTION_CONFIGURATION_CHANGED.equals(action)) { + updateState(context); + } else if (ACTION_EDITOR_STATE_CHANGED.equals(action)) { + updateState(context); + } else if (ACTION_DISPLAY_STATE_CHANGED.equals(action)) { + updateState(context); } } diff --git a/android-app/AndroidManifest.xml b/android-app/AndroidManifest.xml index 8ea7a3d0..de3b4e17 100644 --- a/android-app/AndroidManifest.xml +++ b/android-app/AndroidManifest.xml @@ -109,18 +109,16 @@ - - - + + - - - + + @@ -134,7 +132,7 @@ - + @@ -148,7 +146,7 @@ - + @@ -162,7 +160,7 @@ - + @@ -176,7 +174,7 @@ - + diff --git a/android-app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/android-app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 38b3ee72..f03a4dd5 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -96,6 +96,9 @@ public class CalculatorApplication extends android.app.Application implements Sh @Nonnull protected final Handler uiHandler = new Handler(); + @Nonnull + private final CalculatorBroadcaster broadcaster = new CalculatorBroadcaster(this); + /* ********************************************************************** * @@ -154,6 +157,8 @@ public class CalculatorApplication extends android.app.Application implements Sh calculator.addCalculatorEventListener(listener); } + calculator.addCalculatorEventListener(broadcaster); + Locator.getInstance().getCalculator().init(); BillingDB.init(CalculatorApplication.this);