Avoid using Locator.getKeyboard

This commit is contained in:
serso 2016-02-04 12:59:06 +01:00
parent d8a97112bc
commit ba049e7f28
6 changed files with 79 additions and 57 deletions

View File

@ -33,4 +33,5 @@ public interface AppComponent {
void inject(OperatorsFragment fragment); void inject(OperatorsFragment fragment);
void inject(CalculatorActivity activity); void inject(CalculatorActivity activity);
void inject(FixableErrorsActivity activity); void inject(FixableErrorsActivity activity);
void inject(CalculatorReceiver receiver);
} }

View File

@ -36,11 +36,6 @@ import static org.solovyev.android.calculator.CalculatorSpecialButton.operators_
import static org.solovyev.android.calculator.CalculatorSpecialButton.settings_detached; import static org.solovyev.android.calculator.CalculatorSpecialButton.settings_detached;
import static org.solovyev.android.calculator.CalculatorSpecialButton.vars_detached; import static org.solovyev.android.calculator.CalculatorSpecialButton.vars_detached;
/**
* User: serso
* Date: 10/20/12
* Time: 12:05 AM
*/
public enum CalculatorButton { public enum CalculatorButton {
/*digits*/ /*digits*/
@ -93,9 +88,9 @@ public enum CalculatorButton {
private static SparseArray<CalculatorButton> buttonsByIds = new SparseArray<>(); private static SparseArray<CalculatorButton> buttonsByIds = new SparseArray<>();
private final int buttonId; private final int buttonId;
@Nonnull @Nonnull
private final String onClickText; public final String action;
@Nullable @Nullable
private final String onLongClickText; public final String actionLong;
CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) { CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) {
this(buttonId, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode()); this(buttonId, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode());
@ -105,15 +100,15 @@ public enum CalculatorButton {
this(buttonId, onClickButton, null); this(buttonId, onClickButton, null);
} }
CalculatorButton(int buttonId, @Nonnull String onClickText, @Nullable String onLongClickText) { CalculatorButton(int buttonId, @Nonnull String action, @Nullable String actionLong) {
this.buttonId = buttonId; this.buttonId = buttonId;
this.onClickText = onClickText; this.action = action;
this.onLongClickText = onLongClickText; this.actionLong = actionLong;
} }
CalculatorButton(int buttonId, @Nonnull String onClickText) { CalculatorButton(int buttonId, @Nonnull String action) {
this(buttonId, onClickText, null); this(buttonId, action, null);
} }
@Nullable @Nullable
@ -138,17 +133,6 @@ public enum CalculatorButton {
} }
} }
public boolean onLongClick() {
if (onLongClickText != null) {
return Locator.getInstance().getKeyboard().buttonPressed(onLongClickText);
}
return false;
}
public boolean onClick() {
return Locator.getInstance().getKeyboard().buttonPressed(onClickText);
}
public int getButtonId() { public int getButtonId() {
return buttonId; return buttonId;
} }

View File

@ -6,12 +6,18 @@ import android.content.Intent;
import android.text.TextUtils; import android.text.TextUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public final class CalculatorReceiver extends BroadcastReceiver { public final class CalculatorReceiver extends BroadcastReceiver {
public static final String ACTION_BUTTON_ID_EXTRA = "buttonId"; public static final String ACTION_BUTTON_ID_EXTRA = "buttonId";
public static final String ACTION_BUTTON_PRESSED = "org.solovyev.android.calculator.BUTTON_PRESSED"; public static final String ACTION_BUTTON_PRESSED = "org.solovyev.android.calculator.BUTTON_PRESSED";
@Inject
Keyboard keyboard;
@Nonnull @Nonnull
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);
@ -27,12 +33,14 @@ public final class CalculatorReceiver extends BroadcastReceiver {
return; return;
} }
cast(context).getComponent().inject(this);
final int buttonId = intent.getIntExtra(ACTION_BUTTON_ID_EXTRA, 0); final int buttonId = intent.getIntExtra(ACTION_BUTTON_ID_EXTRA, 0);
final CalculatorButton button = CalculatorButton.getById(buttonId); final CalculatorButton button = CalculatorButton.getById(buttonId);
if (button == null) { if (button == null) {
return; return;
} }
button.onClick(); keyboard.buttonPressed(button.action);
} }
} }

View File

@ -23,16 +23,18 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.text.TextUtils; import android.text.TextUtils;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import dagger.Lazy;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import dagger.Lazy;
@Singleton @Singleton
public class Keyboard { public class Keyboard {
@ -51,8 +53,10 @@ public class Keyboard {
} }
public boolean buttonPressed(@Nullable final String text) { public boolean buttonPressed(@Nullable final String text) {
if (TextUtils.isEmpty(text)) {
return false;
}
App.getGa().onButtonPressed(text); App.getGa().onButtonPressed(text);
if (!Strings.isEmpty(text)) {
// process special buttons // process special buttons
boolean processed = processSpecialButtons(text); boolean processed = processSpecialButtons(text);
@ -61,8 +65,6 @@ public class Keyboard {
} }
return true; return true;
} }
return false;
}
private void processText(@Nonnull String text) { private void processText(@Nonnull String text) {
int cursorPositionOffset = 0; int cursorPositionOffset = 0;

View File

@ -41,6 +41,7 @@ import org.solovyev.android.Views;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Display; import org.solovyev.android.calculator.Display;
import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
@ -61,6 +62,14 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi
@Inject @Inject
Bus bus; Bus bus;
@Inject
Editor editor;
@Inject
Display display;
@Inject
SharedPreferences preferences;
@Inject
Keyboard keyboard;
@Nonnull @Nonnull
private static Class<?> getIntentListenerClass() { private static Class<?> getIntentListenerClass() {
@ -111,13 +120,13 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi
final int width = Math.min(width0, height0); final int width = Math.min(width0, height0);
final int height = Math.max(width0, height0); final int height = Math.max(width0, height0);
view = CalculatorOnscreenView.create(this, CalculatorOnscreenViewState.create(width, height, -1, -1), this); view = CalculatorOnscreenView.create(this, CalculatorOnscreenViewState.create(width, height, -1, -1), this, preferences, keyboard);
view.show(); view.show();
view.updateEditorState(App.getEditor().getState()); view.updateEditorState(editor.getState());
view.updateDisplayState(App.getDisplay().getState()); view.updateDisplayState(display.getState());
bus.register(this); bus.register(this);
App.getPreferences().registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
} }
private int getHeight(int width) { private int getHeight(int width) {
@ -133,7 +142,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi
@Override @Override
public void onDestroy() { public void onDestroy() {
if (view != null) { if (view != null) {
App.getPreferences().unregisterOnSharedPreferenceChangeListener(this); preferences.unregisterOnSharedPreferenceChangeListener(this);
bus.unregister(this); bus.unregister(this);
view.hide(); view.hide();
view = null; view = null;

View File

@ -28,16 +28,30 @@ import android.content.res.Resources;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.*; import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorButton;
import org.solovyev.android.calculator.DisplayState;
import org.solovyev.android.calculator.DisplayView;
import org.solovyev.android.calculator.EditorState;
import org.solovyev.android.calculator.EditorView;
import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import java.util.Locale;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Locale;
public class CalculatorOnscreenView { public class CalculatorOnscreenView {
private static final String TAG = CalculatorOnscreenView.class.getSimpleName(); private static final String TAG = CalculatorOnscreenView.class.getSimpleName();
@ -63,6 +77,8 @@ public class CalculatorOnscreenView {
private CalculatorOnscreenViewState state = CalculatorOnscreenViewState.createDefault(); private CalculatorOnscreenViewState state = CalculatorOnscreenViewState.createDefault();
@Nullable @Nullable
private OnscreenViewListener viewListener; private OnscreenViewListener viewListener;
@Nonnull
private Keyboard keyboard;
private boolean minimized; private boolean minimized;
private boolean attached; private boolean attached;
@ -76,24 +92,26 @@ public class CalculatorOnscreenView {
public static CalculatorOnscreenView create(@Nonnull Context context, public static CalculatorOnscreenView create(@Nonnull Context context,
@Nonnull CalculatorOnscreenViewState state, @Nonnull CalculatorOnscreenViewState state,
@Nullable OnscreenViewListener viewListener) { @Nullable OnscreenViewListener viewListener,
final CalculatorOnscreenView result = new CalculatorOnscreenView(); @NonNull SharedPreferences preferences,
@NonNull Keyboard keyboard) {
final CalculatorOnscreenView view = new CalculatorOnscreenView();
final SharedPreferences p = App.getPreferences(); final Preferences.SimpleTheme theme = Preferences.Onscreen.theme.getPreferenceNoError(preferences);
final Preferences.SimpleTheme theme = Preferences.Onscreen.theme.getPreferenceNoError(p); final Preferences.Gui.Theme appTheme = Preferences.Gui.theme.getPreferenceNoError(preferences);
final Preferences.Gui.Theme appTheme = Preferences.Gui.theme.getPreferenceNoError(p); view.root = View.inflate(context, theme.getOnscreenLayout(appTheme), null);
result.root = View.inflate(context, theme.getOnscreenLayout(appTheme), null); view.context = context;
result.context = context; view.viewListener = viewListener;
result.viewListener = viewListener; view.keyboard = keyboard;
final CalculatorOnscreenViewState persistedState = readState(context); final CalculatorOnscreenViewState persistedState = readState(context);
if (persistedState != null) { if (persistedState != null) {
result.state = persistedState; view.state = persistedState;
} else { } else {
result.state = state; view.state = state;
} }
return result; return view;
} }
public static void persistState(@Nonnull Context context, @Nonnull CalculatorOnscreenViewState state) { public static void persistState(@Nonnull Context context, @Nonnull CalculatorOnscreenViewState state) {
@ -142,7 +160,7 @@ public class CalculatorOnscreenView {
button.setOnClickListener(new View.OnClickListener() { button.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (widgetButton.onClick()) { if (keyboard.buttonPressed(widgetButton.action)) {
v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
} }
if (widgetButton == CalculatorButton.app) { if (widgetButton == CalculatorButton.app) {
@ -153,7 +171,7 @@ public class CalculatorOnscreenView {
button.setOnLongClickListener(new View.OnLongClickListener() { button.setOnLongClickListener(new View.OnLongClickListener() {
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
if (widgetButton.onLongClick()) { if (keyboard.buttonPressed(widgetButton.actionLong)) {
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
} }
return true; return true;