From b09b3c11a8395657f38831ad2a2a1536b6ad5a63 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 4 Feb 2016 13:04:09 +0100 Subject: [PATCH] Buttons refactor --- .../android/calculator/CalculatorButton.java | 38 +++++++------------ .../calculator/CalculatorReceiver.java | 2 +- .../calculator/CalculatorSpecialButton.java | 8 ---- .../onscreen/CalculatorOnscreenView.java | 2 +- .../calculator/widget/CalculatorWidget.java | 22 ++++++++--- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java index a28a132e..de00d2a5 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java @@ -24,6 +24,8 @@ package org.solovyev.android.calculator; import android.util.SparseArray; +import org.solovyev.android.Check; + import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -83,57 +85,45 @@ public enum CalculatorButton { /*equals*/ equals(R.id.cpp_button_equals, CalculatorSpecialButton.equals); - @Nonnull private static SparseArray buttonsByIds = new SparseArray<>(); - private final int buttonId; + public final int id; @Nonnull public final String action; @Nullable public final String actionLong; - CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) { - this(buttonId, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode()); + CalculatorButton(int id, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) { + this(id, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode()); } - CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton) { - this(buttonId, onClickButton, null); + CalculatorButton(int id, @Nonnull CalculatorSpecialButton onClickButton) { + this(id, onClickButton, null); } - CalculatorButton(int buttonId, @Nonnull String action, @Nullable String actionLong) { - this.buttonId = buttonId; + CalculatorButton(int id, @Nonnull String action, @Nullable String actionLong) { + this.id = id; this.action = action; this.actionLong = actionLong; } - CalculatorButton(int buttonId, @Nonnull String action) { - this(buttonId, action, null); + CalculatorButton(int id, @Nonnull String action) { + this(id, action, null); } @Nullable public static CalculatorButton getById(int buttonId) { initButtonsByIdsMap(); - return buttonsByIds.get(buttonId); } private static void initButtonsByIdsMap() { + Check.isMainThread(); if (buttonsByIds.size() == 0) { - // if not initialized - - final CalculatorButton[] calculatorButtons = values(); - - final SparseArray localButtonsByIds = new SparseArray<>(); - for (CalculatorButton calculatorButton : calculatorButtons) { - localButtonsByIds.append(calculatorButton.getButtonId(), calculatorButton); + for (CalculatorButton button : values()) { + buttonsByIds.append(button.id, button); } - - buttonsByIds = localButtonsByIds; } } - - public int getButtonId() { - return buttonId; - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java index adf85052..f8288f40 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java @@ -22,7 +22,7 @@ public final class CalculatorReceiver extends BroadcastReceiver { public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) { final Intent intent = new Intent(context, CalculatorReceiver.class); intent.setAction(ACTION_BUTTON_PRESSED); - intent.putExtra(ACTION_BUTTON_ID_EXTRA, button.getButtonId()); + intent.putExtra(ACTION_BUTTON_ID_EXTRA, button.id); return intent; } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java index 4525ad57..be68503d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java @@ -28,11 +28,6 @@ import java.util.Map; import javax.annotation.Nonnull; import javax.annotation.Nullable; -/** - * User: serso - * Date: 10/20/12 - * Time: 2:05 PM - */ public enum CalculatorSpecialButton { history("history") { @@ -65,14 +60,12 @@ public enum CalculatorSpecialButton { Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null); } }, - settings_detached("settings_detached") { @Override public void onClick(@Nonnull Keyboard keyboard) { Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null); } }, - settings_widget("settings_widget") { @Override public void onClick(@Nonnull Keyboard keyboard) { @@ -163,7 +156,6 @@ public enum CalculatorSpecialButton { Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null); } }, - operators_detached("operators_detached") { @Override public void onClick(@Nonnull Keyboard keyboard) { diff --git a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java index f0ac2a74..b94af29a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java +++ b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java @@ -153,7 +153,7 @@ public class CalculatorOnscreenView { } for (final CalculatorButton widgetButton : CalculatorButton.values()) { - final View button = root.findViewById(widgetButton.getButtonId()); + final View button = root.findViewById(widgetButton.id); if (button == null) { continue; } diff --git a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java index 231ff3f2..81ed0a50 100644 --- a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java +++ b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java @@ -40,19 +40,29 @@ import android.text.SpannedString; import android.text.TextUtils; import android.text.style.StyleSpan; import android.widget.RemoteViews; + import org.solovyev.android.Check; import org.solovyev.android.Views; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.CalculatorButton; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.EditorState; +import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Preferences.SimpleTheme; +import org.solovyev.android.calculator.R; + +import java.util.EnumMap; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.EnumMap; import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT; import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; import static android.os.Build.VERSION_CODES.JELLY_BEAN; -import static org.solovyev.android.calculator.Broadcaster.*; +import static org.solovyev.android.calculator.Broadcaster.ACTION_DISPLAY_STATE_CHANGED; +import static org.solovyev.android.calculator.Broadcaster.ACTION_EDITOR_STATE_CHANGED; +import static org.solovyev.android.calculator.Broadcaster.ACTION_INIT; +import static org.solovyev.android.calculator.Broadcaster.ACTION_THEME_CHANGED; import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent; public class CalculatorWidget extends AppWidgetProvider { @@ -125,9 +135,9 @@ public class CalculatorWidget extends AppWidgetProvider { final int buttonId; if (button == CalculatorButton.settings_widget) { // overriding default settings button behavior - buttonId = CalculatorButton.settings.getButtonId(); + buttonId = CalculatorButton.settings.id; } else { - buttonId = button.getButtonId(); + buttonId = button.id; } views.setOnClickPendingIntent(buttonId, intent); } @@ -254,7 +264,7 @@ public class CalculatorWidget extends AppWidgetProvider { if (intent != null) { return intent; } - intent = PendingIntent.getBroadcast(context, button.getButtonId(), newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT); + intent = PendingIntent.getBroadcast(context, button.id, newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT); if (intent == null) { return null; }