From d8a97112bc675572afe26ea89e103a0d3e357931 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 4 Feb 2016 12:15:08 +0100 Subject: [PATCH] App.cast method introduced --- .../java/org/solovyev/android/calculator/App.java | 13 +++++++++++++ .../solovyev/android/calculator/AppComponent.java | 3 ++- .../solovyev/android/calculator/BaseActivity.java | 4 +++- .../android/calculator/BaseDialogFragment.java | 4 +++- .../solovyev/android/calculator/BaseFragment.java | 3 ++- .../org/solovyev/android/calculator/BaseUi.java | 3 ++- .../calculator/CalculatorEditorFragment.java | 4 +++- .../calculator/errors/FixableErrorsActivity.java | 11 +++++++---- .../onscreen/CalculatorOnscreenService.java | 5 +++-- 9 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/App.java b/app/src/main/java/org/solovyev/android/calculator/App.java index 3ab3a4f2..6e2c4ffe 100644 --- a/app/src/main/java/org/solovyev/android/calculator/App.java +++ b/app/src/main/java/org/solovyev/android/calculator/App.java @@ -363,4 +363,17 @@ public final class App { } return null; } + + @NonNull + public static CalculatorApplication cast(@NonNull Context context) { + if (context instanceof CalculatorApplication) { + return (CalculatorApplication) context; + } + final Context appContext = context.getApplicationContext(); + if (appContext instanceof CalculatorApplication) { + return (CalculatorApplication) appContext; + } + Check.shouldNotHappen(); + return (CalculatorApplication) application; + } } \ No newline at end of file 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 5b0fc552..85f4bff0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -1,6 +1,5 @@ package org.solovyev.android.calculator; -import dagger.Component; import org.solovyev.android.calculator.errors.FixableErrorFragment; import org.solovyev.android.calculator.errors.FixableErrorsActivity; import org.solovyev.android.calculator.functions.EditFunctionFragment; @@ -14,6 +13,8 @@ import org.solovyev.android.calculator.variables.VariablesFragment; import javax.inject.Singleton; +import dagger.Component; + @Singleton @Component(modules = AppModule.class) public interface AppComponent { diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java b/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java index 50c18e88..57892cf7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseActivity.java @@ -12,6 +12,8 @@ import org.solovyev.android.calculator.entities.Category; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import static org.solovyev.android.calculator.App.cast; + public class BaseActivity extends AppCompatActivity { @Nonnull @@ -38,7 +40,7 @@ public class BaseActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { ui.onPreCreate(this); super.onCreate(savedInstanceState); - inject(((CalculatorApplication) getApplication()).getComponent()); + inject(cast(getApplication()).getComponent()); ui.onCreate(this); } diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java b/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java index cb39cf76..bcc17355 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java @@ -17,6 +17,8 @@ import android.view.inputmethod.InputMethodManager; import javax.inject.Inject; +import static org.solovyev.android.calculator.App.cast; + public abstract class BaseDialogFragment extends DialogFragment { @Inject @@ -25,7 +27,7 @@ public abstract class BaseDialogFragment extends DialogFragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - inject(((CalculatorApplication) getActivity().getApplication()).getComponent()); + inject(cast(getActivity().getApplication()).getComponent()); } protected void inject(@NonNull AppComponent component) { diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java b/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java index 8b537952..c478c11a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseFragment.java @@ -8,6 +8,7 @@ import android.view.*; import javax.annotation.Nonnull; import static android.view.Menu.NONE; +import static org.solovyev.android.calculator.App.cast; public abstract class BaseFragment extends Fragment { @@ -21,7 +22,7 @@ public abstract class BaseFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - inject(((CalculatorApplication) getActivity().getApplication()).getComponent()); + inject(cast(getActivity().getApplication()).getComponent()); ui.onCreate(this); } diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java index 9ff49469..38d8238a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java @@ -51,6 +51,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; +import static org.solovyev.android.calculator.App.cast; import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple; import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile; import static org.solovyev.android.calculator.Engine.Preferences.angleUnit; @@ -129,7 +130,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan PreferredPreferences preferredPreferences; protected void onCreate(@Nonnull Activity activity) { - ((CalculatorApplication) activity.getApplication()).getComponent().inject(this); + cast(activity.getApplication()).getComponent().inject(this); layout = Preferences.Gui.layout.getPreferenceNoError(preferences); theme = Preferences.Gui.theme.getPreferenceNoError(preferences); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java index dcea4e87..9dbee527 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java @@ -40,6 +40,8 @@ import org.solovyev.android.menu.ListActivityMenu; import javax.annotation.Nonnull; import javax.inject.Inject; +import static org.solovyev.android.calculator.App.cast; + public class CalculatorEditorFragment extends Fragment { private FragmentUi fragmentUi; @@ -77,7 +79,7 @@ public class CalculatorEditorFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - ((CalculatorApplication) getActivity().getApplication()).getComponent().inject(this); + cast(getActivity().getApplication()).getComponent().inject(this); final Preferences.Gui.Layout layout = Preferences.Gui.getLayout(preferences); if (!layout.optimized) { diff --git a/app/src/main/java/org/solovyev/android/calculator/errors/FixableErrorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/errors/FixableErrorsActivity.java index 0a3c5a81..837192ff 100644 --- a/app/src/main/java/org/solovyev/android/calculator/errors/FixableErrorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/errors/FixableErrorsActivity.java @@ -29,16 +29,19 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; + import org.solovyev.android.Activities; -import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.Preferences; import org.solovyev.common.msg.Message; -import javax.annotation.Nonnull; -import javax.inject.Inject; import java.util.ArrayList; import java.util.List; +import javax.annotation.Nonnull; +import javax.inject.Inject; + +import static org.solovyev.android.calculator.App.cast; + public class FixableErrorsActivity extends AppCompatActivity { public static final String EXTRA_ERRORS = "errors"; @@ -81,7 +84,7 @@ public class FixableErrorsActivity extends AppCompatActivity { finish(); return; } - ((CalculatorApplication) getApplication()).getComponent().inject(this); + cast(getApplication()).getComponent().inject(this); if (state == null) { showNextError(); } 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 513515f7..11770e93 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 @@ -39,7 +39,6 @@ import com.squareup.otto.Subscribe; import org.solovyev.android.Check; import org.solovyev.android.Views; import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorApplication; import org.solovyev.android.calculator.Display; import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.Preferences; @@ -49,6 +48,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; +import static org.solovyev.android.calculator.App.cast; + public class CalculatorOnscreenService extends Service implements OnscreenViewListener, SharedPreferences.OnSharedPreferenceChangeListener { public static final Class INTENT_LISTENER_CLASS = CalculatorOnscreenBroadcastReceiver.class; @@ -126,7 +127,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi @Override public void onCreate() { super.onCreate(); - ((CalculatorApplication) getApplication()).getComponent().inject(this); + cast(getApplication()).getComponent().inject(this); } @Override