From 030a93cce543b37d0c771423ab4d2842145d30d5 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 31 Jan 2016 10:35:10 +0100 Subject: [PATCH] Clipboard as a Bean --- .../calculator/CalculatorApplication.java | 1 - .../calculator/CalculatorClipboard.java | 41 ----------------- .../android/calculator/CalculatorLocator.java | 4 -- ...alculatorClipboard.java => Clipboard.java} | 32 ++++++------- .../calculator/DummyCalculatorClipboard.java | 46 ------------------- .../solovyev/android/calculator/Keyboard.java | 10 ++-- .../solovyev/android/calculator/Locator.java | 10 ---- .../calculator/AbstractCalculatorTest.java | 2 +- .../calculator/CalculatorTestUtils.java | 4 +- 9 files changed, 23 insertions(+), 127 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorClipboard.java rename app/src/main/java/org/solovyev/android/calculator/{AndroidCalculatorClipboard.java => Clipboard.java} (64%) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/DummyCalculatorClipboard.java diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index a926e4dc..669193c0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -121,7 +121,6 @@ public class CalculatorApplication extends android.app.Application implements Sh Locator.getInstance().init(calculator, engine, - new AndroidCalculatorClipboard(this), new AndroidCalculatorNotifier(this), errorReporter, new AndroidCalculatorPreferenceService(this), diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorClipboard.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorClipboard.java deleted file mode 100644 index b608c446..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorClipboard.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 9/22/12 - * Time: 1:34 PM - */ -public interface CalculatorClipboard { - - @Nullable - String getText(); - - void setText(@Nonnull CharSequence text); - - void setText(@Nonnull String text); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java index 7855599a..9694d8b9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java @@ -30,7 +30,6 @@ public interface CalculatorLocator { void init(@Nonnull Calculator calculator, @Nonnull Engine engine, - @Nonnull CalculatorClipboard clipboard, @Nonnull CalculatorNotifier notifier, @Nonnull ErrorReporter errorReporter, @Nonnull CalculatorPreferenceService preferenceService, @@ -46,9 +45,6 @@ public interface CalculatorLocator { @Nonnull Keyboard getKeyboard(); - @Nonnull - CalculatorClipboard getClipboard(); - @Nonnull CalculatorNotifier getNotifier(); diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorClipboard.java b/app/src/main/java/org/solovyev/android/calculator/Clipboard.java similarity index 64% rename from app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorClipboard.java rename to app/src/main/java/org/solovyev/android/calculator/Clipboard.java index dce12137..4666e9c3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorClipboard.java +++ b/app/src/main/java/org/solovyev/android/calculator/Clipboard.java @@ -27,40 +27,36 @@ import android.content.Context; import android.text.ClipboardManager; import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Singleton; -/** - * User: serso - * Date: 9/22/12 - * Time: 1:35 PM - */ -public class AndroidCalculatorClipboard implements CalculatorClipboard { - +@Singleton +public class Clipboard { + @SuppressWarnings("deprecation") @Nonnull - private final Context context; + private final ClipboardManager clipboard; - public AndroidCalculatorClipboard(@Nonnull Application application) { - this.context = application; + @SuppressWarnings("deprecation") + @Inject + public Clipboard(@Nonnull Application application) { + clipboard = (ClipboardManager) application.getSystemService(Context.CLIPBOARD_SERVICE); } - @Override + @SuppressWarnings("deprecation") + @Nonnull public String getText() { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); if (clipboard.hasText()) { return String.valueOf(clipboard.getText()); - } else { - return null; } + + return ""; } - @Override public void setText(@Nonnull CharSequence text) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(text); } - @Override public void setText(@Nonnull String text) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(text); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/DummyCalculatorClipboard.java b/app/src/main/java/org/solovyev/android/calculator/DummyCalculatorClipboard.java deleted file mode 100644 index b12f4db6..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/DummyCalculatorClipboard.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import javax.annotation.Nonnull; - -/** - * User: serso - * Date: 9/22/12 - * Time: 2:00 PM - */ -public class DummyCalculatorClipboard implements CalculatorClipboard { - - @Override - public String getText() { - return null; - } - - @Override - public void setText(@Nonnull CharSequence text) { - } - - @Override - public void setText(@Nonnull String text) { - } -} 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 7671a969..6dfa3d02 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Keyboard.java +++ b/app/src/main/java/org/solovyev/android/calculator/Keyboard.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator; +import android.text.TextUtils; import org.solovyev.android.calculator.math.MathType; import org.solovyev.common.text.Strings; @@ -38,9 +39,10 @@ public class Keyboard { @Inject Editor editor; - @Inject Display display; + @Inject + Clipboard clipboard; @Inject public Keyboard() { @@ -119,8 +121,8 @@ public class Keyboard { } public void pasteButtonPressed() { - final String text = Locator.getInstance().getClipboard().getText(); - if (text != null) { + final String text = clipboard.getText(); + if (!TextUtils.isEmpty(text)) { editor.insert(text); } } @@ -134,7 +136,7 @@ public class Keyboard { if (!displayState.valid) { return; } - Locator.getInstance().getClipboard().setText(displayState.text); + clipboard.setText(displayState.text); Locator.getInstance().getNotifier().showMessage(CalculatorMessage.newInfoMessage(CalculatorMessages.result_copied)); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Locator.java b/app/src/main/java/org/solovyev/android/calculator/Locator.java index 2fe51e7f..dd9753c4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Locator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Locator.java @@ -41,8 +41,6 @@ public class Locator implements CalculatorLocator { @Nonnull private ErrorReporter errorReporter = new SystemErrorReporter(); @Nonnull - private CalculatorClipboard calculatorClipboard = new DummyCalculatorClipboard(); - @Nonnull private CalculatorPreferenceService calculatorPreferenceService; @Nonnull @@ -59,7 +57,6 @@ public class Locator implements CalculatorLocator { @Override public void init(@Nonnull Calculator calculator, @Nonnull Engine engine, - @Nonnull CalculatorClipboard clipboard, @Nonnull CalculatorNotifier notifier, @Nonnull ErrorReporter errorReporter, @Nonnull CalculatorPreferenceService preferenceService, @@ -68,7 +65,6 @@ public class Locator implements CalculatorLocator { this.calculator = calculator; this.engine = engine; - this.calculatorClipboard = clipboard; this.calculatorNotifier = notifier; this.errorReporter = errorReporter; this.calculatorPreferenceService = preferenceService; @@ -99,12 +95,6 @@ public class Locator implements CalculatorLocator { instance.keyboard = keyboard; } - @Override - @Nonnull - public CalculatorClipboard getClipboard() { - return calculatorClipboard; - } - @Override @Nonnull public CalculatorNotifier getNotifier() { diff --git a/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java b/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java index 29462243..25e0c3bb 100644 --- a/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java @@ -36,7 +36,7 @@ import java.util.concurrent.Executor; public class AbstractCalculatorTest { protected void setUp() throws Exception { - Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); + Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); Locator.getInstance().getEngine().init(new Executor() { @Override public void execute(Runnable command) { diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index f3fb486f..8a2019ce 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -66,7 +66,7 @@ public class CalculatorTestUtils { public static void staticSetUp() throws Exception { App.init(new CalculatorApplication(), new Languages(new RoboSharedPreferences(new HashMap>(), "test", 0))); - Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); + Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); Locator.getInstance().getEngine().init(new Executor() { @Override public void execute(Runnable command) { @@ -81,7 +81,7 @@ public class CalculatorTestUtils { } public static void staticSetUp(@Nullable Context context) throws Exception { - Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); + Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); Locator.getInstance().getEngine().init(new Executor() { @Override public void execute(Runnable command) {