From ba049e7f280f9da8275a6bd64a65bbb21752dd2a Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 4 Feb 2016 12:59:06 +0100 Subject: [PATCH] Avoid using Locator.getKeyboard --- .../android/calculator/AppComponent.java | 1 + .../android/calculator/CalculatorButton.java | 30 +++-------- .../calculator/CalculatorReceiver.java | 10 +++- .../solovyev/android/calculator/Keyboard.java | 26 +++++----- .../onscreen/CalculatorOnscreenService.java | 19 +++++-- .../onscreen/CalculatorOnscreenView.java | 50 +++++++++++++------ 6 files changed, 79 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java index 85f4bff0..1a165145 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -33,4 +33,5 @@ public interface AppComponent { void inject(OperatorsFragment fragment); void inject(CalculatorActivity activity); void inject(FixableErrorsActivity activity); + void inject(CalculatorReceiver receiver); } 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 bc9e1af8..a28a132e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java @@ -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.vars_detached; -/** - * User: serso - * Date: 10/20/12 - * Time: 12:05 AM - */ public enum CalculatorButton { /*digits*/ @@ -93,9 +88,9 @@ public enum CalculatorButton { private static SparseArray buttonsByIds = new SparseArray<>(); private final int buttonId; @Nonnull - private final String onClickText; + public final String action; @Nullable - private final String onLongClickText; + public final String actionLong; CalculatorButton(int buttonId, @Nonnull CalculatorSpecialButton onClickButton, @Nullable CalculatorSpecialButton onLongClickButton) { this(buttonId, onClickButton.getActionCode(), onLongClickButton == null ? null : onLongClickButton.getActionCode()); @@ -105,15 +100,15 @@ public enum CalculatorButton { 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.onClickText = onClickText; - this.onLongClickText = onLongClickText; + this.action = action; + this.actionLong = actionLong; } - CalculatorButton(int buttonId, @Nonnull String onClickText) { - this(buttonId, onClickText, null); + CalculatorButton(int buttonId, @Nonnull String action) { + this(buttonId, action, null); } @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() { 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 aaba484f..adf85052 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorReceiver.java @@ -6,12 +6,18 @@ import android.content.Intent; import android.text.TextUtils; import javax.annotation.Nonnull; +import javax.inject.Inject; + +import static org.solovyev.android.calculator.App.cast; 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"; + @Inject + Keyboard keyboard; + @Nonnull public static Intent newButtonClickedIntent(@Nonnull Context context, @Nonnull CalculatorButton button) { final Intent intent = new Intent(context, CalculatorReceiver.class); @@ -27,12 +33,14 @@ public final class CalculatorReceiver extends BroadcastReceiver { return; } + cast(context).getComponent().inject(this); + final int buttonId = intent.getIntExtra(ACTION_BUTTON_ID_EXTRA, 0); final CalculatorButton button = CalculatorButton.getById(buttonId); if (button == null) { return; } - button.onClick(); + keyboard.buttonPressed(button.action); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java index a582de63..05024d62 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java +++ b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java @@ -23,16 +23,18 @@ package org.solovyev.android.calculator; import android.text.TextUtils; + import com.squareup.otto.Bus; -import dagger.Lazy; + import org.solovyev.android.calculator.math.MathType; -import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; +import dagger.Lazy; + @Singleton public class Keyboard { @@ -51,17 +53,17 @@ public class Keyboard { } public boolean buttonPressed(@Nullable final String text) { - App.getGa().onButtonPressed(text); - if (!Strings.isEmpty(text)) { - // process special buttons - boolean processed = processSpecialButtons(text); - - if (!processed) { - processText(prepareText(text)); - } - return true; + if (TextUtils.isEmpty(text)) { + return false; } - return false; + App.getGa().onButtonPressed(text); + // process special buttons + boolean processed = processSpecialButtons(text); + + if (!processed) { + processText(prepareText(text)); + } + return true; } private void processText(@Nonnull String text) { diff --git a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java index 11770e93..aea37965 100644 --- a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java +++ b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java @@ -41,6 +41,7 @@ import org.solovyev.android.Views; import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.Display; import org.solovyev.android.calculator.Editor; +import org.solovyev.android.calculator.Keyboard; import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.R; @@ -61,6 +62,14 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi @Inject Bus bus; + @Inject + Editor editor; + @Inject + Display display; + @Inject + SharedPreferences preferences; + @Inject + Keyboard keyboard; @Nonnull private static Class getIntentListenerClass() { @@ -111,13 +120,13 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi final int width = Math.min(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.updateEditorState(App.getEditor().getState()); - view.updateDisplayState(App.getDisplay().getState()); + view.updateEditorState(editor.getState()); + view.updateDisplayState(display.getState()); bus.register(this); - App.getPreferences().registerOnSharedPreferenceChangeListener(this); + preferences.registerOnSharedPreferenceChangeListener(this); } private int getHeight(int width) { @@ -133,7 +142,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi @Override public void onDestroy() { if (view != null) { - App.getPreferences().unregisterOnSharedPreferenceChangeListener(this); + preferences.unregisterOnSharedPreferenceChangeListener(this); bus.unregister(this); view.hide(); view = null; 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 9ceb62e2..f0ac2a74 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 @@ -28,16 +28,30 @@ import android.content.res.Resources; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import android.util.DisplayMetrics; 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 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 java.util.Locale; + import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Locale; public class CalculatorOnscreenView { private static final String TAG = CalculatorOnscreenView.class.getSimpleName(); @@ -63,6 +77,8 @@ public class CalculatorOnscreenView { private CalculatorOnscreenViewState state = CalculatorOnscreenViewState.createDefault(); @Nullable private OnscreenViewListener viewListener; + @Nonnull + private Keyboard keyboard; private boolean minimized; private boolean attached; @@ -76,24 +92,26 @@ public class CalculatorOnscreenView { public static CalculatorOnscreenView create(@Nonnull Context context, @Nonnull CalculatorOnscreenViewState state, - @Nullable OnscreenViewListener viewListener) { - final CalculatorOnscreenView result = new CalculatorOnscreenView(); + @Nullable OnscreenViewListener viewListener, + @NonNull SharedPreferences preferences, + @NonNull Keyboard keyboard) { + final CalculatorOnscreenView view = new CalculatorOnscreenView(); - final SharedPreferences p = App.getPreferences(); - final Preferences.SimpleTheme theme = Preferences.Onscreen.theme.getPreferenceNoError(p); - final Preferences.Gui.Theme appTheme = Preferences.Gui.theme.getPreferenceNoError(p); - result.root = View.inflate(context, theme.getOnscreenLayout(appTheme), null); - result.context = context; - result.viewListener = viewListener; + final Preferences.SimpleTheme theme = Preferences.Onscreen.theme.getPreferenceNoError(preferences); + final Preferences.Gui.Theme appTheme = Preferences.Gui.theme.getPreferenceNoError(preferences); + view.root = View.inflate(context, theme.getOnscreenLayout(appTheme), null); + view.context = context; + view.viewListener = viewListener; + view.keyboard = keyboard; final CalculatorOnscreenViewState persistedState = readState(context); if (persistedState != null) { - result.state = persistedState; + view.state = persistedState; } else { - result.state = state; + view.state = state; } - return result; + return view; } public static void persistState(@Nonnull Context context, @Nonnull CalculatorOnscreenViewState state) { @@ -142,7 +160,7 @@ public class CalculatorOnscreenView { button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (widgetButton.onClick()) { + if (keyboard.buttonPressed(widgetButton.action)) { v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); } if (widgetButton == CalculatorButton.app) { @@ -153,7 +171,7 @@ public class CalculatorOnscreenView { button.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - if (widgetButton.onLongClick()) { + if (keyboard.buttonPressed(widgetButton.actionLong)) { v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); } return true;