This commit is contained in:
serso 2016-03-03 17:06:27 +01:00
parent 001888973a
commit ba69cc7096
25 changed files with 129 additions and 323 deletions

View File

@ -88,15 +88,10 @@ public final class ActivityLauncher {
context.startActivity(intent);
}
@Nonnull
private static Notifier getNotifier() {
return ((CalculatorApplication) App.getApplication()).notifier;
}
public void plotDisplayedExpression() {
final DisplayState state = display.get().getState();
if (!state.valid) {
getNotifier().showMessage(R.string.not_valid_result, MessageType.error);
notifier.showMessage(R.string.not_valid_result, MessageType.error);
return;
}
plot(state.getResult());

View File

@ -41,114 +41,51 @@ import android.support.v7.view.ContextThemeWrapper;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import com.squareup.otto.Bus;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.floating.FloatingCalculatorService;
import org.solovyev.android.calculator.ga.Ga;
import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.view.ScreenMetrics;
import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.wizard.Wizards;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This class aggregates several useful in any Android application interfaces and provides access to {@link android.app.Application} object from a static context.
* NOTE: use this class only if you don't use and dependency injection library (if you use any you can directly set interfaces through it). <br/>
* <p/>
* Before first usage this class must be initialized by calling {@link App#init(android.app.Application)} method (for example, from {@link android.app.Application#onCreate()})
*/
public final class App {
public static final String TAG = "C++";
@Nonnull
private static Languages languages;
@Nonnull
private static volatile Application application;
private static Application application;
@Nonnull
private static SharedPreferences preferences;
@Nonnull
private static volatile Ga ga;
@Nonnull
private static volatile ScreenMetrics screenMetrics;
@Nonnull
private static Wizards wizards;
@Nonnull
private static Editor editor;
@Nonnull
private static Bus bus;
@Nonnull
private static Display display;
private App() {
throw new AssertionError();
}
public static void init(@Nonnull CalculatorApplication application,
@Nonnull Languages languages) {
public static void init(@Nonnull CalculatorApplication application) {
App.application = application;
App.preferences = PreferenceManager.getDefaultSharedPreferences(application);
App.ga = new Ga(application, preferences);
App.screenMetrics = new ScreenMetrics(application);
App.languages = languages;
App.languages.init();
App.wizards = new CalculatorWizards(application);
App.editor = application.editor;
App.display = application.display;
App.bus = application.bus;
}
/**
* @param <A> real type of application
* @return application instance which was provided in {@link App#init} method
*/
@SuppressWarnings("unchecked")
@Nonnull
public static <A extends Application> A getApplication() {
return (A) application;
}
@Nonnull
public static Wizards getWizards() {
return wizards;
}
@Nonnull
public static Ga getGa() {
return ga;
}
@Nonnull
public static SharedPreferences getPreferences() {
return preferences;
}
@Nonnull
public static Preferences.Gui.Theme getTheme() {
return Preferences.Gui.getTheme(getPreferences());
return Preferences.Gui.getTheme(preferences);
}
@Nonnull
public static Preferences.SimpleTheme getWidgetTheme() {
return Preferences.Widget.getTheme(getPreferences());
return Preferences.Widget.getTheme(preferences);
}
@Nonnull
public static Preferences.Gui.Theme getThemeFor(@Nonnull Context context) {
if (isFloatingCalculator(context)) {
final SharedPreferences p = getPreferences();
final SharedPreferences p = preferences;
final Preferences.SimpleTheme onscreenTheme = Preferences.Onscreen.getTheme(p);
final Preferences.Gui.Theme appTheme = Preferences.Gui.getTheme(p);
return onscreenTheme.resolveThemeFor(appTheme).getAppTheme();
@ -165,16 +102,6 @@ public final class App {
return context;
}
@Nonnull
public static Languages getLanguages() {
return languages;
}
@Nonnull
public static ScreenMetrics getScreenMetrics() {
return screenMetrics;
}
public static void showDialog(@Nonnull DialogFragment dialogFragment,
@Nonnull String fragmentTag,
@Nonnull FragmentManager fm) {
@ -201,21 +128,6 @@ public final class App {
return spannable.toString();
}
@Nonnull
public static Editor getEditor() {
return editor;
}
@Nonnull
public static Display getDisplay() {
return display;
}
@Nonnull
public static Bus getBus() {
return bus;
}
private static final AtomicInteger sNextViewId = new AtomicInteger(1);
public static int generateViewId() {
@ -391,14 +303,6 @@ public final class App {
pm.setComponentEnabledSetting(new ComponentName(context, componentClass), componentState, PackageManager.DONT_KILL_APP);
}
public static boolean isComponentEnabled(@Nonnull Context context,
@Nonnull Class<? extends Context> componentClass) {
final PackageManager pm = context.getPackageManager();
int componentEnabledSetting = pm.getComponentEnabledSetting(new ComponentName(context, componentClass));
return componentEnabledSetting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || componentEnabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}
public static int toPixels(@Nonnull DisplayMetrics dm, float dps) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dps, dm);
}

View File

@ -1,8 +1,10 @@
package org.solovyev.android.calculator;
import dagger.Component;
import org.solovyev.android.calculator.converter.ConverterFragment;
import org.solovyev.android.calculator.errors.FixableErrorFragment;
import org.solovyev.android.calculator.errors.FixableErrorsActivity;
import org.solovyev.android.calculator.floating.FloatingCalculatorBroadcastReceiver;
import org.solovyev.android.calculator.floating.FloatingCalculatorService;
import org.solovyev.android.calculator.floating.FloatingCalculatorView;
import org.solovyev.android.calculator.functions.BaseFunctionFragment;
@ -17,6 +19,7 @@ import org.solovyev.android.calculator.plot.PlotDimensionsFragment;
import org.solovyev.android.calculator.plot.PlotEditFunctionFragment;
import org.solovyev.android.calculator.plot.PlotFunctionsFragment;
import org.solovyev.android.calculator.preferences.PreferencesActivity;
import org.solovyev.android.calculator.preferences.PreferencesFragment;
import org.solovyev.android.calculator.preferences.PurchaseDialogActivity;
import org.solovyev.android.calculator.variables.EditVariableFragment;
import org.solovyev.android.calculator.variables.VariablesFragment;
@ -24,11 +27,10 @@ import org.solovyev.android.calculator.view.Tabs;
import org.solovyev.android.calculator.widget.CalculatorWidget;
import org.solovyev.android.calculator.wizard.DragButtonWizardStep;
import org.solovyev.android.calculator.wizard.WizardActivity;
import org.solovyev.android.calculator.wizard.WizardFragment;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
@ -65,4 +67,7 @@ public interface AppComponent {
void inject(CalculatorWidget widget);
void inject(WizardActivity activity);
void inject(BaseActivity activity);
void inject(PreferencesFragment fragment);
void inject(WizardFragment fragment);
void inject(FloatingCalculatorBroadcastReceiver receiver);
}

View File

@ -15,9 +15,11 @@ import dagger.Module;
import dagger.Provides;
import jscl.JsclMathEngine;
import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.checkout.*;
import org.solovyev.android.plotter.Plot;
import org.solovyev.android.plotter.Plotter;
import org.solovyev.android.wizard.Wizards;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -137,6 +139,12 @@ public class AppModule {
return new AcraErrorReporter();
}
@Provides
@Singleton
Wizards provideWizards(@Nonnull Application application) {
return new CalculatorWizards(application);
}
@Provides
@Singleton
@Named(THREAD_UI)

View File

@ -1,8 +1,6 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
@ -21,6 +19,7 @@ import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.ga.Ga;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.language.Language;
import org.solovyev.android.calculator.language.Languages;
@ -51,6 +50,8 @@ public class BaseActivity extends AppCompatActivity {
@Inject
Calculator calculator;
@Inject
Ga ga;
@Inject
Typeface typeface;
@Bind(R.id.main)
ViewGroup mainView;
@ -77,12 +78,12 @@ public class BaseActivity extends AppCompatActivity {
this.tabs = new Tabs(this);
}
public static void reportActivityStop(@Nonnull Activity activity) {
App.getGa().getAnalytics().reportActivityStop(activity);
public void reportActivityStop(@Nonnull Activity activity) {
ga.getAnalytics().reportActivityStop(activity);
}
public static void reportActivityStart(@Nonnull Activity activity) {
App.getGa().getAnalytics().reportActivityStart(activity);
public void reportActivityStart(@Nonnull Activity activity) {
ga.getAnalytics().reportActivityStart(activity);
}
public static void setFont(@Nonnull TextView view, @Nonnull Typeface newTypeface) {

View File

@ -34,6 +34,7 @@ import org.acra.ACRA;
import org.acra.ACRAConfiguration;
import org.acra.sender.HttpSender;
import org.solovyev.android.calculator.floating.FloatingCalculatorActivity;
import org.solovyev.android.calculator.ga.Ga;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.language.Language;
import org.solovyev.android.calculator.language.Languages;
@ -96,6 +97,9 @@ public class CalculatorApplication extends android.app.Application implements Sh
@Inject
ActivityLauncher launcher;
@Inject
Ga ga;
@Nonnull
private final TimingLogger timer = new TimingLogger("App", "onCreate");
@ -104,7 +108,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
timer.reset();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final Languages languages = new Languages(preferences);
final Languages languages = new Languages(this, preferences);
timer.addSplit("languages");
onPreCreate(preferences, languages);
@ -131,11 +135,12 @@ public class CalculatorApplication extends android.app.Application implements Sh
}
private void onPostCreate(@Nonnull SharedPreferences preferences, @Nonnull Languages languages) {
App.init(this, languages);
App.init(this);
languages.init();
preferences.registerOnSharedPreferenceChangeListener(this);
languages.updateContextLocale(this, true);
App.getGa().reportInitially(preferences);
ga.reportInitially(preferences);
calculator.init(initThread);

View File

@ -22,6 +22,7 @@
package org.solovyev.android.calculator;
import android.app.Application;
import android.content.SharedPreferences;
import android.text.TextUtils;
@ -54,8 +55,8 @@ public class Editor {
Bus bus;
@Inject
public Editor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
textProcessor = new EditorTextProcessor(preferences, engine);
public Editor(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
textProcessor = new EditorTextProcessor(application, preferences, engine);
}
public void init() {

View File

@ -29,6 +29,7 @@ import com.squareup.otto.Bus;
import dagger.Lazy;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.ga.Ga;
import org.solovyev.android.calculator.math.MathType;
import javax.annotation.Nonnull;
@ -51,6 +52,8 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
@Inject
Engine engine;
@Inject
Ga ga;
@Inject
Lazy<Clipboard> clipboard;
@Inject
Lazy<Bus> bus;
@ -72,7 +75,7 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
if (TextUtils.isEmpty(text)) {
return false;
}
App.getGa().onButtonPressed(text);
ga.onButtonPressed(text);
if (!processSpecialAction(text)) {
processText(prepareText(text));
}

View File

@ -28,6 +28,8 @@ public class StartupHelper {
SharedPreferences uiPreferences;
@Inject
SharedPreferences preferences;
@Inject
Wizards wizards;
@Inject
public StartupHelper() {
@ -44,7 +46,6 @@ public class StartupHelper {
private void handleOnMainActivityOpened(@NonNull final AppCompatActivity activity, @NonNull SharedPreferences.Editor editor, int opened) {
final int currentVersion = App.getAppVersionCode(activity);
final Wizards wizards = App.getWizards();
final Wizard wizard = wizards.getWizard(CalculatorWizards.FIRST_TIME_WIZARD);
if (wizard.isStarted() && !wizard.isFinished()) {
continueWizard(wizards, wizard.getName(), activity);

View File

@ -107,7 +107,7 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
stringName = prefix + substitute;
}
return getDescription(App.getApplication(), stringName);
return getDescription(application, stringName);
}
@Override

View File

@ -27,14 +27,21 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.ga.Ga;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public final class FloatingCalculatorBroadcastReceiver extends BroadcastReceiver {
@Inject
SharedPreferences preferences;
@Inject
Ga ga;
public FloatingCalculatorBroadcastReceiver() {
}
@ -42,10 +49,10 @@ public final class FloatingCalculatorBroadcastReceiver extends BroadcastReceiver
public void onReceive(@Nonnull Context context,
@Nonnull Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
cast(context).getComponent().inject(this);
if (Preferences.Onscreen.startOnBoot.getPreferenceNoError(preferences)) {
FloatingCalculatorService.showNotification(context);
App.getGa().onBootStart();
ga.onBootStart();
}
} else {
final Intent newIntent = new Intent(intent);

View File

@ -32,16 +32,11 @@ import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Display;
import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.ga.Ga;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -64,6 +59,8 @@ public class FloatingCalculatorService extends Service implements FloatingViewLi
@Inject
Display display;
@Inject
Ga ga;
@Inject
SharedPreferences preferences;
public static void showNotification(@Nonnull Context context) {
@ -148,7 +145,7 @@ public class FloatingCalculatorService extends Service implements FloatingViewLi
if (isShowWindowIntent(intent)) {
hideNotification();
createView();
App.getGa().onFloatingCalculatorOpened();
ga.onFloatingCalculatorOpened();
} else if (isShowNotificationIntent(intent)) {
showNotification();
}

View File

@ -1,23 +1,20 @@
package org.solovyev.android.calculator.ga;
import android.content.Context;
import android.app.Application;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public final class Ga implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final int LAYOUT = 1;
@ -29,24 +26,13 @@ public final class Ga implements SharedPreferences.OnSharedPreferenceChangeListe
@Nonnull
private final Tracker tracker;
public Ga(@Nonnull Context context, @Nonnull SharedPreferences preferences) {
analytics = GoogleAnalytics.getInstance(context);
@Inject
public Ga(@Nonnull Application application, @Nonnull SharedPreferences preferences) {
analytics = GoogleAnalytics.getInstance(application);
tracker = analytics.newTracker(R.xml.ga);
preferences.registerOnSharedPreferenceChangeListener(this);
}
@Nonnull
private String getStackTrace(@Nonnull Exception e) {
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(out));
return new String(out.toByteArray());
} catch (Exception e1) {
Log.e("Ga", e1.getMessage(), e1);
}
return "";
}
private void reportLayout(@Nonnull Preferences.Gui.Layout layout) {
tracker.send(new HitBuilders.EventBuilder().setCustomDimension(LAYOUT, layout.name()).build());
}
@ -60,11 +46,6 @@ public final class Ga implements SharedPreferences.OnSharedPreferenceChangeListe
return analytics;
}
@Nonnull
public Tracker getTracker() {
return tracker;
}
public void onButtonPressed(@Nullable String text) {
if (TextUtils.isEmpty(text)) {
return;

View File

@ -4,17 +4,16 @@ import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.view.ScreenMetrics;
import org.solovyev.android.views.Adjuster;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DragButton;
@ -26,9 +25,9 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.view.HapticFeedbackConstants.*;
import static org.solovyev.android.calculator.App.cast;
import static org.solovyev.android.calculator.App.getScreenMetrics;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile;
@ -59,7 +58,7 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
ActivityLauncher launcher;
@Inject
PreferredPreferences preferredPreferences;
protected int orientation = Configuration.ORIENTATION_PORTRAIT;
protected int orientation = ORIENTATION_PORTRAIT;
private int textSize;
private Preferences.Gui.Layout layout;
private final float textScale;
@ -83,7 +82,7 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
orientation = App.getScreenOrientation(activity);
layout = Preferences.Gui.layout.getPreferenceNoError(preferences);
textSize = calculateTextSize();
textSize = calculateTextSize(activity);
}
protected final void prepareButton(@Nullable ImageView button) {
@ -141,12 +140,13 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
preferences.unregisterOnSharedPreferenceChangeListener(this);
}
public static int calculateTextSize() {
final ScreenMetrics metrics = getScreenMetrics();
final boolean portrait = metrics.isInPortraitMode();
public static int calculateTextSize(@Nonnull Activity activity) {
final boolean portrait = App.getScreenOrientation(activity) == ORIENTATION_PORTRAIT;
final DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int buttonsCount = portrait ? 5 : 4;
final int buttonsWeight = portrait ? (2 + 1 + buttonsCount) : (2 + buttonsCount);
final int buttonSize = metrics.getHeightPxs() / buttonsWeight;
final int buttonSize = metrics.heightPixels / buttonsWeight;
return 5 * buttonSize / 12;
}

View File

@ -1,5 +1,6 @@
package org.solovyev.android.calculator.language;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
@ -8,7 +9,6 @@ import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Preferences;
import javax.annotation.Nonnull;
@ -26,9 +26,12 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
@Nonnull
private final List<Language> list = new ArrayList<>();
@Nonnull
private final Application application;
@Nonnull
private final SharedPreferences preferences;
public Languages(@Nonnull SharedPreferences preferences) {
public Languages(@Nonnull Application application, @Nonnull SharedPreferences preferences) {
this.application = application;
this.preferences = preferences;
}
@ -160,7 +163,7 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
@Override
public void onSharedPreferenceChanged(@Nonnull SharedPreferences p, String key) {
if (Preferences.Gui.language.isSameKey(key)) {
updateContextLocale(App.getApplication(), false);
updateContextLocale(application, false);
}
}

View File

@ -88,7 +88,7 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.getPreferences().registerOnSharedPreferenceChangeListener(this);
preferences.registerOnSharedPreferenceChangeListener(this);
final Intent intent = getIntent();
final int preferenceTitle = intent.getIntExtra(EXTRA_PREFERENCE_TITLE, 0);
@ -139,7 +139,7 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc
@Override
protected void onDestroy() {
checkout.stop();
App.getPreferences().unregisterOnSharedPreferenceChangeListener(this);
preferences.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}

View File

@ -10,9 +10,7 @@ import android.support.v4.app.FragmentActivity;
import android.util.SparseArray;
import android.view.View;
import android.widget.ListView;
import org.solovyev.android.calculator.AdView;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
@ -22,12 +20,14 @@ import org.solovyev.android.checkout.BillingRequests;
import org.solovyev.android.checkout.Checkout;
import org.solovyev.android.checkout.ProductTypes;
import org.solovyev.android.checkout.RequestListener;
import java.util.List;
import org.solovyev.android.wizard.Wizards;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.List;
import static org.solovyev.android.calculator.App.cast;
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
import static org.solovyev.android.wizard.WizardUi.startWizard;
@ -38,6 +38,12 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
private Preference buyPremiumPreference;
@Nullable
private AdView adView;
@Inject
SharedPreferences preferences;
@Inject
Languages languages;
@Inject
Wizards wizards;
@Nonnull
public static PreferencesFragment create(int preferencesResId, int layoutResId) {
@ -49,8 +55,9 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cast(this).getComponent().inject(this);
App.getPreferences().registerOnSharedPreferenceChangeListener(this);
preferences.registerOnSharedPreferenceChangeListener(this);
}
private void setPreferenceIntent(int xml, @Nonnull PreferencesActivity.PrefDef def) {
@ -80,7 +87,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
restartWizardPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startWizard(App.getWizards(), DEFAULT_WIZARD_FLOW, getActivity());
startWizard(wizards, DEFAULT_WIZARD_FLOW, getActivity());
return true;
}
});
@ -122,7 +129,6 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
}
});
final SharedPreferences preferences = App.getPreferences();
onSharedPreferenceChanged(preferences, Engine.Preferences.Output.round.getKey());
}
@ -132,7 +138,6 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
}
final ListPreference language = (ListPreference) preferenceManager.findPreference(Preferences.Gui.language.getKey());
final Languages languages = App.getLanguages();
final List<Language> languagesList = languages.getList();
final CharSequence[] entries = new CharSequence[languagesList.size()];
final CharSequence[] entryValues = new CharSequence[languagesList.size()];
@ -195,7 +200,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
@Override
public void onDestroy() {
App.getPreferences().unregisterOnSharedPreferenceChangeListener(this);
preferences.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}

View File

@ -30,11 +30,10 @@ import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.BaseDialogFragment;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.ga.Ga;
import org.solovyev.android.checkout.*;
import javax.annotation.Nonnull;
@ -49,6 +48,8 @@ public class PurchaseDialogActivity extends AppCompatActivity implements Request
Billing billing;
@Inject
Products products;
@Inject
Ga ga;
ActivityCheckout checkout;
@Override
@ -69,12 +70,12 @@ public class PurchaseDialogActivity extends AppCompatActivity implements Request
@Override
protected void onStart() {
super.onStart();
BaseActivity.reportActivityStart(this);
ga.getAnalytics().reportActivityStart(this);
}
@Override
protected void onStop() {
BaseActivity.reportActivityStop(this);
ga.getAnalytics().reportActivityStop(this);
super.onStop();
}

View File

@ -2,8 +2,6 @@ package org.solovyev.android.calculator.view;
import android.app.Application;
import android.content.SharedPreferences;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.text.TextProcessor;
@ -21,11 +19,14 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
@Nullable
private TextHighlighter textHighlighter;
@Nonnull
private final Application application;
@Nonnull
private final SharedPreferences preferences;
@Nonnull
private final Engine engine;
public EditorTextProcessor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
public EditorTextProcessor(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
this.application = application;
this.preferences = preferences;
this.engine = engine;
preferences.registerOnSharedPreferenceChangeListener(this);
@ -66,7 +67,6 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
private int getTextColor(@Nonnull SharedPreferences preferences) {
final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(preferences);
final Application application = App.getApplication();
return theme.getTextColorFor(application).normal;
}
}

View File

@ -1,120 +0,0 @@
package org.solovyev.android.calculator.view;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import javax.annotation.Nonnull;
public class ScreenMetrics {
@Nonnull
private final android.view.Display display;
@Nonnull
private final DisplayMetrics metrics;
private final int layout;
private float xDpi;
private float yDpi;
private float diagonalIns;
private float widthIns;
private float heightIns;
public ScreenMetrics(@Nonnull Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
metrics = new DisplayMetrics();
display.getMetrics(metrics);
initDpi();
initDimensions();
layout = context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
}
private void initDpi() {
if ((Build.DEVICE.equals("qsd8250_surf")
|| Build.MODEL.equals("Dell Streak"))) {
xDpi = 190f;
yDpi = 190f;
} else if (Build.MODEL.equals("VTAB1008")) {
xDpi = 160f;
yDpi = 160f;
} else if (Build.MODEL.equals("Dell Streak 7")) {
xDpi = 150f;
yDpi = 150f;
} else if (Build.MODEL.equals("A1_07")) {
xDpi = 127.5f;
yDpi = 100f;
} else if (Build.MODEL.equals("N12GPS")
|| Build.MODEL.equals("MID_Serials")) {
xDpi = 133f;
yDpi = 133f;
} else if (Build.MODEL.startsWith("GT-N710")
|| Build.MODEL.equalsIgnoreCase("SCH-N719")
|| Build.MODEL.startsWith("SHV-E250")) {
xDpi = 267f;
yDpi = 267f;
} else if (metrics.densityDpi - metrics.xdpi >= 79.0
|| metrics.densityDpi - metrics.ydpi >= 79.0
|| Math.abs(metrics.ydpi - metrics.xdpi) > 40.0) {
xDpi = yDpi = metrics.densityDpi;
} else {
xDpi = metrics.xdpi;
yDpi = metrics.ydpi;
}
}
private void initDimensions() {
final float w = getWidthPxs() / xDpi;
final float h = getHeightPxs() / yDpi;
diagonalIns = (float) Math.sqrt(w * w + h * h);
widthIns = w;
heightIns = h;
}
public boolean isInPortraitMode() {
return getWidthPxs() < getHeightPxs();
}
@SuppressWarnings("deprecation")
public int getWidthPxs() {
return display.getWidth();
}
@SuppressWarnings("deprecation")
public int getHeightPxs() {
return display.getHeight();
}
public float getWidthIns() {
return widthIns;
}
public float getHeightIns() {
return heightIns;
}
public int getDensityDpi() {
return metrics.densityDpi;
}
public float getDensity() {
return metrics.density;
}
public float getXDpi() {
return xDpi;
}
public float getYDpi() {
return yDpi;
}
public float getDiagonalIns() {
return diagonalIns;
}
public int getLayout() {
return layout;
}
}

View File

@ -27,7 +27,6 @@ import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
@ -52,7 +51,7 @@ public class ChooseModeWizardStep extends WizardFragment implements AdapterView.
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
final CalculatorMode mode = CalculatorMode.fromGuiLayout(Preferences.Gui.layout.getPreference(App.getPreferences()));
final CalculatorMode mode = CalculatorMode.fromGuiLayout(Preferences.Gui.layout.getPreference(preferences));
final Spinner spinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
spinner.setAdapter(WizardArrayAdapter.create(getActivity(), R.array.cpp_modes));
spinner.setSelection(mode == simple ? 0 : 1);
@ -80,7 +79,7 @@ public class ChooseModeWizardStep extends WizardFragment implements AdapterView.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final CalculatorMode mode = position == 0 ? simple : engineer;
mode.apply(App.getPreferences());
mode.apply(preferences);
updateDescription(mode);
}

View File

@ -55,7 +55,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(App.getPreferences());
final Preferences.Gui.Theme theme = Preferences.Gui.getTheme(preferences);
final Spinner spinner = (Spinner) root.findViewById(R.id.wizard_theme_spinner);
themes.clear();
themes.add(new ThemeUi(Preferences.Gui.Theme.material_theme, R.string.cpp_theme_dark));
@ -97,7 +97,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final ThemeUi theme = adapter.getItem(position);
Preferences.Gui.theme.putPreference(App.getPreferences(), theme.theme);
Preferences.Gui.theme.putPreference(preferences, theme.theme);
updateImage(theme.theme);
}

View File

@ -49,7 +49,7 @@ public class OnScreenCalculatorWizardStep extends WizardFragment implements Comp
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
final Boolean enabled = Preferences.Onscreen.showAppIcon.getPreference(App.getPreferences());
final Boolean enabled = Preferences.Onscreen.showAppIcon.getPreference(preferences);
checkbox = (CheckBox) root.findViewById(R.id.wizard_onscreen_app_enabled_checkbox);
checkbox.setChecked(enabled);
checkbox.setOnCheckedChangeListener(this);
@ -67,7 +67,7 @@ public class OnScreenCalculatorWizardStep extends WizardFragment implements Comp
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
Preferences.Onscreen.showAppIcon.putPreference(App.getPreferences(), checked);
Preferences.Onscreen.showAppIcon.putPreference(preferences, checked);
}
}

View File

@ -38,8 +38,6 @@ public class WizardActivity extends BaseActivity implements WizardsAware, Shared
private ViewPager pager;
@Nonnull
private WizardPagerAdapter pagerAdapter;
@Nonnull
private Wizards wizards = App.getWizards();
@Nullable
private AlertDialog dialog;
@ -47,6 +45,8 @@ public class WizardActivity extends BaseActivity implements WizardsAware, Shared
SharedPreferences preferences;
@Inject
Languages languages;
@Inject
Wizards wizards;
public WizardActivity() {
super(R.layout.cpp_activity_wizard, 0);

View File

@ -1,5 +1,6 @@
package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
@ -8,6 +9,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteFragment;
import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteStep;
@ -20,7 +22,9 @@ import org.solovyev.android.wizard.WizardStep;
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.App.toPixels;
public abstract class WizardFragment extends Fragment implements View.OnClickListener {
@ -30,16 +34,22 @@ public abstract class WizardFragment extends Fragment implements View.OnClickLis
@Nullable
protected TextView prevButton;
@Inject
SharedPreferences preferences;
private WizardStep step;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inject(cast(this).getComponent());
step = findStepByClassName();
}
protected void inject(@Nonnull AppComponent component) {
component.inject(this);
}
@Nonnull
private WizardStep findStepByClassName() {
if (this instanceof ReleaseNoteFragment) {