Buttons refactor

This commit is contained in:
serso 2016-02-04 13:04:09 +01:00
parent ba049e7f28
commit b09b3c11a8
5 changed files with 32 additions and 40 deletions

View File

@ -24,6 +24,8 @@ package org.solovyev.android.calculator;
import android.util.SparseArray; import android.util.SparseArray;
import org.solovyev.android.Check;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -83,57 +85,45 @@ public enum CalculatorButton {
/*equals*/ /*equals*/
equals(R.id.cpp_button_equals, CalculatorSpecialButton.equals); equals(R.id.cpp_button_equals, CalculatorSpecialButton.equals);
@Nonnull @Nonnull
private static SparseArray<CalculatorButton> buttonsByIds = new SparseArray<>(); private static SparseArray<CalculatorButton> buttonsByIds = new SparseArray<>();
private final int buttonId; public final int id;
@Nonnull @Nonnull
public final String action; public final String action;
@Nullable @Nullable
public final String actionLong; public final String actionLong;
CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) { CalculatorButton(int id, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) {
this(buttonId, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode()); this(id, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode());
} }
CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton) { CalculatorButton(int id, @Nonnull CalculatorSpecialButton onClickButton) {
this(buttonId, onClickButton, null); this(id, onClickButton, null);
} }
CalculatorButton(int buttonId, @Nonnull String action, @Nullable String actionLong) { CalculatorButton(int id, @Nonnull String action, @Nullable String actionLong) {
this.buttonId = buttonId; this.id = id;
this.action = action; this.action = action;
this.actionLong = actionLong; this.actionLong = actionLong;
} }
CalculatorButton(int buttonId, @Nonnull String action) { CalculatorButton(int id, @Nonnull String action) {
this(buttonId, action, null); this(id, action, null);
} }
@Nullable @Nullable
public static CalculatorButton getById(int buttonId) { public static CalculatorButton getById(int buttonId) {
initButtonsByIdsMap(); initButtonsByIdsMap();
return buttonsByIds.get(buttonId); return buttonsByIds.get(buttonId);
} }
private static void initButtonsByIdsMap() { private static void initButtonsByIdsMap() {
Check.isMainThread();
if (buttonsByIds.size() == 0) { if (buttonsByIds.size() == 0) {
// if not initialized for (CalculatorButton button : values()) {
buttonsByIds.append(button.id, button);
final CalculatorButton[] calculatorButtons = values();
final SparseArray<CalculatorButton> localButtonsByIds = new SparseArray<>();
for (CalculatorButton calculatorButton : calculatorButtons) {
localButtonsByIds.append(calculatorButton.getButtonId(), calculatorButton);
} }
buttonsByIds = localButtonsByIds;
} }
} }
public int getButtonId() {
return buttonId;
}
} }

View File

@ -22,7 +22,7 @@ public final class CalculatorReceiver extends BroadcastReceiver {
public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) { public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) {
final Intent intent = new Intent(context, CalculatorReceiver.class); final Intent intent = new Intent(context, CalculatorReceiver.class);
intent.setAction(ACTION_BUTTON_PRESSED); intent.setAction(ACTION_BUTTON_PRESSED);
intent.putExtra(ACTION_BUTTON_ID_EXTRA, button.getButtonId()); intent.putExtra(ACTION_BUTTON_ID_EXTRA, button.id);
return intent; return intent;
} }

View File

@ -28,11 +28,6 @@ import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/**
* User: serso
* Date: 10/20/12
* Time: 2:05 PM
*/
public enum CalculatorSpecialButton { public enum CalculatorSpecialButton {
history("history") { history("history") {
@ -65,14 +60,12 @@ public enum CalculatorSpecialButton {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings, null);
} }
}, },
settings_detached("settings_detached") { settings_detached("settings_detached") {
@Override @Override
public void onClick(@Nonnull Keyboard keyboard) { public void onClick(@Nonnull Keyboard keyboard) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_detached, null);
} }
}, },
settings_widget("settings_widget") { settings_widget("settings_widget") {
@Override @Override
public void onClick(@Nonnull Keyboard keyboard) { public void onClick(@Nonnull Keyboard keyboard) {
@ -163,7 +156,6 @@ public enum CalculatorSpecialButton {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null);
} }
}, },
operators_detached("operators_detached") { operators_detached("operators_detached") {
@Override @Override
public void onClick(@Nonnull Keyboard keyboard) { public void onClick(@Nonnull Keyboard keyboard) {

View File

@ -153,7 +153,7 @@ public class CalculatorOnscreenView {
} }
for (final CalculatorButton widgetButton : CalculatorButton.values()) { for (final CalculatorButton widgetButton : CalculatorButton.values()) {
final View button = root.findViewById(widgetButton.getButtonId()); final View button = root.findViewById(widgetButton.id);
if (button == null) { if (button == null) {
continue; continue;
} }

View File

@ -40,19 +40,29 @@ import android.text.SpannedString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.Views; 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.Preferences.SimpleTheme;
import org.solovyev.android.calculator.R;
import java.util.EnumMap;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.EnumMap;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT; import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; import static android.content.Intent.ACTION_CONFIGURATION_CHANGED;
import static android.os.Build.VERSION_CODES.JELLY_BEAN; 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; import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
public class CalculatorWidget extends AppWidgetProvider { public class CalculatorWidget extends AppWidgetProvider {
@ -125,9 +135,9 @@ public class CalculatorWidget extends AppWidgetProvider {
final int buttonId; final int buttonId;
if (button == CalculatorButton.settings_widget) { if (button == CalculatorButton.settings_widget) {
// overriding default settings button behavior // overriding default settings button behavior
buttonId = CalculatorButton.settings.getButtonId(); buttonId = CalculatorButton.settings.id;
} else { } else {
buttonId = button.getButtonId(); buttonId = button.id;
} }
views.setOnClickPendingIntent(buttonId, intent); views.setOnClickPendingIntent(buttonId, intent);
} }
@ -254,7 +264,7 @@ public class CalculatorWidget extends AppWidgetProvider {
if (intent != null) { if (intent != null) {
return intent; 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) { if (intent == null) {
return null; return null;
} }