calculator receiver added

This commit is contained in:
Sergey Solovyev 2013-06-28 16:16:08 +04:00
parent 5a94e633e7
commit 58b64b1017
3 changed files with 44 additions and 15 deletions

View File

@ -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;
}
}

View File

@ -38,6 +38,7 @@ import javax.annotation.Nullable;
import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; 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_DISPLAY_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED; import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
/** /**
* User: Solovyev_S * User: Solovyev_S
@ -46,9 +47,6 @@ import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITO
*/ */
abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { 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"; 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); final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
for (CalculatorButton button : CalculatorButton.values()) { for (CalculatorButton button : CalculatorButton.values()) {
final Intent onButtonClickIntent = new Intent(context, getComponentClass()); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, button.getButtonId(), newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT);
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) { if (pendingIntent != null) {
views.setOnClickPendingIntent(button.getButtonId(), pendingIntent); views.setOnClickPendingIntent(button.getButtonId(), pendingIntent);
} }
@ -148,14 +143,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider {
super.onReceive(context, intent); super.onReceive(context, intent);
final String action = intent.getAction(); final String action = intent.getAction();
if (ACTION_BUTTON_PRESSED.equals(action)) { if (ACTION_CONFIGURATION_CHANGED.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)) {
updateState(context); updateState(context);
} else if (ACTION_EDITOR_STATE_CHANGED.equals(action)) { } else if (ACTION_EDITOR_STATE_CHANGED.equals(action)) {
updateState(context); updateState(context);

View File

@ -37,6 +37,12 @@
</activity> </activity>
<receiver android:name=".CalculatorReceiver" android:exported="false">
<intent-filter>
<action android:name="org.solovyev.android.calculator.BUTTON_PRESSED"/>
</intent-filter>
</receiver>
<activity android:clearTaskOnLaunch="true" android:launchMode="singleTop" android:label="@string/c_app_name" <activity android:clearTaskOnLaunch="true" android:launchMode="singleTop" android:label="@string/c_app_name"
android:name=".CalculatorActivityMobile" android:windowSoftInputMode="adjustPan"/> android:name=".CalculatorActivityMobile" android:windowSoftInputMode="adjustPan"/>