From 58b64b10177e709184ca9cff08bcc7e81c3d4093 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Fri, 28 Jun 2013 16:16:08 +0400 Subject: [PATCH] calculator receiver added --- .../calculator/CalculatorReceiver.java | 35 +++++++++++++++++++ .../AbstractCalculatorWidgetProvider.java | 18 ++-------- android-app/AndroidManifest.xml | 6 ++++ 3 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java diff --git a/android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java b/android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java new file mode 100644 index 00000000..22702083 --- /dev/null +++ b/android-app-core/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java @@ -0,0 +1,35 @@ +package org.solovyev.android.calculator; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import javax.annotation.Nonnull; + +public final class CalculatorReceiver extends BroadcastReceiver { + + public static final String ACTION_BUTTON_ID_EXTRA = "buttonId"; + public static final String ACTION_BUTTON_PRESSED = "org.solovyev.android.calculator.BUTTON_PRESSED"; + + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + + if (CalculatorReceiver.ACTION_BUTTON_PRESSED.equals(action)) { + final int buttonId = intent.getIntExtra(CalculatorReceiver.ACTION_BUTTON_ID_EXTRA, 0); + + final CalculatorButton button = CalculatorButton.getById(buttonId); + if (button != null) { + button.onClick(context); + } + } + } + + @Nonnull + public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) { + final Intent onButtonClickIntent = new Intent(context, CalculatorReceiver.class); + onButtonClickIntent.setAction(ACTION_BUTTON_PRESSED); + onButtonClickIntent.putExtra(ACTION_BUTTON_ID_EXTRA, button.getButtonId()); + return onButtonClickIntent; + } +} 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 46381be5..b5ee508b 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 @@ -38,6 +38,7 @@ 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; +import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent; /** * User: Solovyev_S @@ -46,9 +47,6 @@ import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITO */ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { - 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"; /* @@ -125,10 +123,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { 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(ACTION_BUTTON_PRESSED); - onButtonClickIntent.putExtra(ACTION_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(), newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT); if (pendingIntent != null) { views.setOnClickPendingIntent(button.getButtonId(), pendingIntent); } @@ -148,14 +143,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { super.onReceive(context, intent); 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(action)) { + if (ACTION_CONFIGURATION_CHANGED.equals(action)) { updateState(context); } else if (ACTION_EDITOR_STATE_CHANGED.equals(action)) { updateState(context); diff --git a/android-app/AndroidManifest.xml b/android-app/AndroidManifest.xml index de3b4e17..65c320d4 100644 --- a/android-app/AndroidManifest.xml +++ b/android-app/AndroidManifest.xml @@ -37,6 +37,12 @@ + + + + + +