Buttons refactor
This commit is contained in:
parent
ba049e7f28
commit
b09b3c11a8
@ -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<CalculatorButton> 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<CalculatorButton> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user