GA integration + Base activity

This commit is contained in:
serso 2015-01-26 12:38:45 +01:00
parent c30645e5c7
commit d23f8e2c40
30 changed files with 899 additions and 1150 deletions

View File

@ -1,10 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false"> <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false">
<file url="file://$PROJECT_DIR$/android-app-core-tests/build.gradle" charset="UTF-8" /> <file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/android-app-core/src/main/java/org/solovyev/android/calculator/App.java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/android-app-core/src/main/java/org/solovyev/android/calculator/ServiceLocator.java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/android-app-tests/build.gradle" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/android-app-tests/build.gradle" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/android-app-tests/src/test/java/org/solovyev/android/CalculatorTestRunner.java" charset="UTF-8" />
</component> </component>
</project> </project>

View File

@ -1,349 +1,337 @@
/* /*
* Copyright 2013 serso aka se.solovyev * Copyright 2013 serso aka se.solovyev
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details * Contact details
* *
* Email: se.solovyev@gmail.com * Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org * Site: http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.solovyev.android.Activities; import org.solovyev.android.Activities;
import org.solovyev.android.Views; import org.solovyev.android.Views;
import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener; import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** public class ActivityUi extends BaseUi {
* User: serso
* Date: 9/25/12 /*
* Time: 10:32 PM **********************************************************************
*/ *
public class CalculatorActivityHelperImpl extends AbstractCalculatorHelper implements CalculatorActivityHelper { * CONSTANTS
*
/* **********************************************************************
********************************************************************** */
*
* CONSTANTS /*
* **********************************************************************
********************************************************************** *
*/ * FIELDS
*
/* **********************************************************************
********************************************************************** */
*
* FIELDS private int layoutId;
*
********************************************************************** private boolean homeIcon = false;
*/
@Nonnull
private int layoutId; private CalculatorPreferences.Gui.Theme theme;
private boolean homeIcon = false; @Nonnull
private CalculatorPreferences.Gui.Layout layout;
@Nonnull
private CalculatorPreferences.Gui.Theme theme; private int selectedNavigationIndex = 0;
@Nonnull public ActivityUi(int layoutId, @Nonnull String logTag) {
private CalculatorPreferences.Gui.Layout layout; super(logTag);
this.layoutId = layoutId;
private int selectedNavigationIndex = 0; }
public CalculatorActivityHelperImpl(int layoutId, @Nonnull String logTag) { public ActivityUi(int layoutId, boolean homeIcon) {
super(logTag); this.layoutId = layoutId;
this.layoutId = layoutId; this.homeIcon = homeIcon;
} }
public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) { @Override
this.layoutId = layoutId; public void onCreate(@Nonnull Activity activity) {
this.homeIcon = homeIcon; super.onCreate(activity);
}
if (activity instanceof CalculatorEventListener) {
@Override Locator.getInstance().getCalculator().addCalculatorEventListener((CalculatorEventListener) activity);
public void onCreate(@Nonnull Activity activity, @Nullable Bundle savedInstanceState) { }
super.onCreate(activity);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
if (activity instanceof CalculatorEventListener) {
Locator.getInstance().getCalculator().addCalculatorEventListener((CalculatorEventListener) activity); this.theme = CalculatorPreferences.Gui.getTheme(preferences);
} activity.setTheme(this.theme.getThemeId());
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); this.layout = CalculatorPreferences.Gui.getLayout(preferences);
this.theme = CalculatorPreferences.Gui.getTheme(preferences); activity.setContentView(layoutId);
activity.setTheme(this.theme.getThemeId());
final View root = activity.findViewById(R.id.main_layout);
this.layout = CalculatorPreferences.Gui.getLayout(preferences); if (root != null) {
processButtons(activity, root);
activity.setContentView(layoutId); addHelpInfo(activity, root);
} else {
final View root = activity.findViewById(R.id.main_layout); Log.e(ActivityUi.class.getSimpleName(), "Root is null for " + activity.getClass().getName());
if (root != null) { }
processButtons(activity, root); }
addHelpInfo(activity, root);
} else { public void onCreate(@Nonnull final SherlockFragmentActivity activity) {
Log.e(CalculatorActivityHelperImpl.class.getSimpleName(), "Root is null for " + activity.getClass().getName()); onCreate((Activity)activity);
}
} final ActionBar actionBar = activity.getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false);
@Override actionBar.setDisplayHomeAsUpEnabled(homeIcon);
public void onCreate(@Nonnull final SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState) { actionBar.setHomeButtonEnabled(false);
this.onCreate((Activity) activity, savedInstanceState); actionBar.setDisplayShowHomeEnabled(true);
final ActionBar actionBar = activity.getSupportActionBar(); toggleTitle(activity, true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(homeIcon); actionBar.setIcon(R.drawable.ab_icon);
actionBar.setHomeButtonEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(true); }
toggleTitle(activity, true); private void toggleTitle(@Nonnull SherlockFragmentActivity activity, boolean showTitle) {
final ActionBar actionBar = activity.getSupportActionBar();
actionBar.setIcon(R.drawable.ab_icon);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); if (activity instanceof CalculatorActivity) {
} if (Views.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT) {
actionBar.setDisplayShowTitleEnabled(true);
private void toggleTitle(@Nonnull SherlockFragmentActivity activity, boolean showTitle) { } else {
final ActionBar actionBar = activity.getSupportActionBar(); actionBar.setDisplayShowTitleEnabled(false);
}
if (activity instanceof CalculatorActivity) { } else {
if (Views.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT) { actionBar.setDisplayShowTitleEnabled(showTitle);
actionBar.setDisplayShowTitleEnabled(true); }
} else { }
actionBar.setDisplayShowTitleEnabled(false);
} public void restoreSavedTab(@Nonnull SherlockFragmentActivity activity) {
} else { final ActionBar actionBar = activity.getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(showTitle); if (selectedNavigationIndex >= 0 && selectedNavigationIndex < actionBar.getTabCount()) {
} actionBar.setSelectedNavigationItem(selectedNavigationIndex);
} }
}
public void restoreSavedTab(@Nonnull SherlockFragmentActivity activity) {
final ActionBar actionBar = activity.getSupportActionBar(); public void onSaveInstanceState(@Nonnull SherlockFragmentActivity activity, @Nonnull Bundle outState) {
if (selectedNavigationIndex >= 0 && selectedNavigationIndex < actionBar.getTabCount()) { onSaveInstanceState((Activity) activity, outState);
actionBar.setSelectedNavigationItem(selectedNavigationIndex); }
}
} public void onSaveInstanceState(@Nonnull Activity activity, @Nonnull Bundle outState) {
}
@Override
public void onSaveInstanceState(@Nonnull SherlockFragmentActivity activity, @Nonnull Bundle outState) { public void onResume(@Nonnull Activity activity) {
onSaveInstanceState((Activity) activity, outState); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
}
final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences);
@Override if (!theme.equals(newTheme)) {
public void onSaveInstanceState(@Nonnull Activity activity, @Nonnull Bundle outState) { Activities.restartActivity(activity);
} }
}
@Override
public void onResume(@Nonnull Activity activity) { public void onPause(@Nonnull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); }
final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences); public void onPause(@Nonnull SherlockFragmentActivity activity) {
if (!theme.equals(newTheme)) { onPause((Activity) activity);
Activities.restartActivity(activity);
} final int selectedNavigationIndex = activity.getSupportActionBar().getSelectedNavigationIndex();
} if (selectedNavigationIndex >= 0) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
@Override final SharedPreferences.Editor editor = preferences.edit();
public void onPause(@Nonnull Activity activity) { editor.putInt(getSavedTabPreferenceName(activity), selectedNavigationIndex);
} editor.apply();
}
@Override
public void onPause(@Nonnull SherlockFragmentActivity activity) { }
onPause((Activity) activity);
@Nonnull
final int selectedNavigationIndex = activity.getSupportActionBar().getSelectedNavigationIndex(); private String getSavedTabPreferenceName(@Nonnull Activity activity) {
if (selectedNavigationIndex >= 0) { return "tab_" + activity.getClass().getSimpleName();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); }
final SharedPreferences.Editor editor = preferences.edit();
editor.putInt(getSavedTabPreferenceName(activity), selectedNavigationIndex); @Override
editor.apply(); public void onDestroy(@Nonnull Activity activity) {
} super.onDestroy(activity);
} if (activity instanceof CalculatorEventListener) {
Locator.getInstance().getCalculator().removeCalculatorEventListener((CalculatorEventListener) activity);
@Nonnull }
private String getSavedTabPreferenceName(@Nonnull Activity activity) { }
return "tab_" + activity.getClass().getSimpleName();
} public void onDestroy(@Nonnull SherlockFragmentActivity activity) {
this.onDestroy((Activity) activity);
@Override }
public void onDestroy(@Nonnull Activity activity) {
super.onDestroy(activity); public void addTab(@Nonnull SherlockFragmentActivity activity,
@Nonnull String tag,
if (activity instanceof CalculatorEventListener) { @Nonnull Class<? extends Fragment> fragmentClass,
Locator.getInstance().getCalculator().removeCalculatorEventListener((CalculatorEventListener) activity); @Nullable Bundle fragmentArgs,
} int captionResId,
} int parentViewId) {
final ActionBar actionBar = activity.getSupportActionBar();
@Override
public void onDestroy(@Nonnull SherlockFragmentActivity activity) { final ActionBar.Tab tab = actionBar.newTab();
this.onDestroy((Activity) activity); tab.setTag(tag);
} tab.setText(captionResId);
@Override final ActionBarFragmentTabListener listener = new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId);
public void addTab(@Nonnull SherlockFragmentActivity activity, tab.setTabListener(listener);
@Nonnull String tag, actionBar.addTab(tab);
@Nonnull Class<? extends Fragment> fragmentClass, }
@Nullable Bundle fragmentArgs,
int captionResId, public void addTab(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType, @Nullable Bundle fragmentArgs, int parentViewId) {
int parentViewId) { addTab(activity, fragmentType.getFragmentTag(), fragmentType.getFragmentClass(), fragmentArgs, fragmentType.getDefaultTitleResId(), parentViewId);
final ActionBar actionBar = activity.getSupportActionBar(); }
final ActionBar.Tab tab = actionBar.newTab(); public void setFragment(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType, @Nullable Bundle fragmentArgs, int parentViewId) {
tab.setTag(tag); final FragmentManager fm = activity.getSupportFragmentManager();
tab.setText(captionResId);
Fragment fragment = fm.findFragmentByTag(fragmentType.getFragmentTag());
final ActionBarFragmentTabListener listener = new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId); if (fragment == null) {
tab.setTabListener(listener); fragment = Fragment.instantiate(activity, fragmentType.getFragmentClass().getName(), fragmentArgs);
actionBar.addTab(tab); final FragmentTransaction ft = fm.beginTransaction();
} ft.add(parentViewId, fragment, fragmentType.getFragmentTag());
ft.commit();
@Override } else {
public void addTab(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType, @Nullable Bundle fragmentArgs, int parentViewId) { if (fragment.isDetached()) {
addTab(activity, fragmentType.getFragmentTag(), fragmentType.getFragmentClass(), fragmentArgs, fragmentType.getDefaultTitleResId(), parentViewId); final FragmentTransaction ft = fm.beginTransaction();
} ft.attach(fragment);
ft.commit();
@Override }
public void setFragment(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType, @Nullable Bundle fragmentArgs, int parentViewId) {
final FragmentManager fm = activity.getSupportFragmentManager(); }
}
Fragment fragment = fm.findFragmentByTag(fragmentType.getFragmentTag());
if (fragment == null) { public void selectTab(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType) {
fragment = Fragment.instantiate(activity, fragmentType.getFragmentClass().getName(), fragmentArgs); final ActionBar actionBar = activity.getSupportActionBar();
final FragmentTransaction ft = fm.beginTransaction(); for (int i = 0; i < actionBar.getTabCount(); i++) {
ft.add(parentViewId, fragment, fragmentType.getFragmentTag()); final ActionBar.Tab tab = actionBar.getTabAt(i);
ft.commit(); if (tab != null && fragmentType.getFragmentTag().equals(tab.getTag())) {
} else { actionBar.setSelectedNavigationItem(i);
if (fragment.isDetached()) { break;
final FragmentTransaction ft = fm.beginTransaction(); }
ft.attach(fragment); }
ft.commit(); }
}
public int getLayoutId() {
} return layoutId;
} }
@Override @Nonnull
public void selectTab(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType) { public CalculatorPreferences.Gui.Theme getTheme() {
final ActionBar actionBar = activity.getSupportActionBar(); return theme;
for (int i = 0; i < actionBar.getTabCount(); i++) { }
final ActionBar.Tab tab = actionBar.getTabAt(i);
if (tab != null && fragmentType.getFragmentTag().equals(tab.getTag())) { @Nonnull
actionBar.setSelectedNavigationItem(i); public CalculatorPreferences.Gui.Layout getLayout() {
break; return layout;
} }
}
} public void onResume(@Nonnull SherlockFragmentActivity activity) {
onResume((Activity) activity);
@Override
public int getLayoutId() { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
return layoutId; selectedNavigationIndex = preferences.getInt(getSavedTabPreferenceName(activity), -1);
} restoreSavedTab(activity);
}
@Override
@Nonnull private void addHelpInfo(@Nonnull Activity activity, @Nonnull View root) {
public CalculatorPreferences.Gui.Theme getTheme() { if (CalculatorApplication.isMonkeyRunner(activity)) {
return theme; if (root instanceof ViewGroup) {
} final TextView helperTextView = new TextView(activity);
@Override final DisplayMetrics dm = new DisplayMetrics();
@Nonnull activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
public CalculatorPreferences.Gui.Layout getLayout() {
return layout; helperTextView.setTextSize(15);
} helperTextView.setTextColor(Color.WHITE);
@Override final Configuration c = activity.getResources().getConfiguration();
public void onResume(@Nonnull SherlockFragmentActivity activity) {
onResume((Activity) activity); final StringBuilder helpText = new StringBuilder();
helpText.append("Size: ");
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE, c)) {
selectedNavigationIndex = preferences.getInt(getSavedTabPreferenceName(activity), -1); helpText.append("xlarge");
restoreSavedTab(activity); } else if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, c)) {
} helpText.append("large");
} else if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL, c)) {
private void addHelpInfo(@Nonnull Activity activity, @Nonnull View root) { helpText.append("normal");
if (CalculatorApplication.isMonkeyRunner(activity)) { } else if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_SMALL, c)) {
if (root instanceof ViewGroup) { helpText.append("small");
final TextView helperTextView = new TextView(activity); } else {
helpText.append("unknown");
final DisplayMetrics dm = new DisplayMetrics(); }
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
helpText.append(" (").append(dm.widthPixels).append("x").append(dm.heightPixels).append(")");
helperTextView.setTextSize(15);
helperTextView.setTextColor(Color.WHITE); helpText.append(" Density: ");
switch (dm.densityDpi) {
final Configuration c = activity.getResources().getConfiguration(); case DisplayMetrics.DENSITY_LOW:
helpText.append("ldpi");
final StringBuilder helpText = new StringBuilder(); break;
helpText.append("Size: "); case DisplayMetrics.DENSITY_MEDIUM:
if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE, c)) { helpText.append("mdpi");
helpText.append("xlarge"); break;
} else if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, c)) { case DisplayMetrics.DENSITY_HIGH:
helpText.append("large"); helpText.append("hdpi");
} else if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL, c)) { break;
helpText.append("normal"); case DisplayMetrics.DENSITY_XHIGH:
} else if (Views.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_SMALL, c)) { helpText.append("xhdpi");
helpText.append("small"); break;
} else { case DisplayMetrics.DENSITY_TV:
helpText.append("unknown"); helpText.append("tv");
} break;
}
helpText.append(" (").append(dm.widthPixels).append("x").append(dm.heightPixels).append(")");
helpText.append(" (").append(dm.densityDpi).append(")");
helpText.append(" Density: ");
switch (dm.densityDpi) { helperTextView.setText(helpText);
case DisplayMetrics.DENSITY_LOW:
helpText.append("ldpi"); ((ViewGroup) root).addView(helperTextView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
break; }
case DisplayMetrics.DENSITY_MEDIUM: }
helpText.append("mdpi"); }
break;
case DisplayMetrics.DENSITY_HIGH: public void onStop(@Nonnull Activity activity) {
helpText.append("hdpi"); App.getGa().getAnalytics().reportActivityStop(activity);
break; }
case DisplayMetrics.DENSITY_XHIGH:
helpText.append("xhdpi"); public void onStart(@Nonnull Activity activity) {
break; App.getGa().getAnalytics().reportActivityStart(activity);
case DisplayMetrics.DENSITY_TV: }
helpText.append("tv"); }
break;
}
helpText.append(" (").append(dm.densityDpi).append(")");
helperTextView.setText(helpText);
((ViewGroup) root).addView(helperTextView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
}

View File

@ -24,6 +24,7 @@ package org.solovyev.android.calculator;
import android.app.Application; import android.app.Application;
import org.solovyev.android.UiThreadExecutor; import org.solovyev.android.UiThreadExecutor;
import org.solovyev.android.calculator.ga.Ga;
import org.solovyev.common.listeners.JEvent; import org.solovyev.common.listeners.JEvent;
import org.solovyev.common.listeners.JEventListener; import org.solovyev.common.listeners.JEventListener;
import org.solovyev.common.listeners.JEventListeners; import org.solovyev.common.listeners.JEventListeners;
@ -72,6 +73,9 @@ public final class App {
@Nonnull @Nonnull
private static CalculatorBroadcaster broadcaster; private static CalculatorBroadcaster broadcaster;
@Nonnull
private static volatile Ga ga;
private App() { private App() {
throw new AssertionError(); throw new AssertionError();
} }
@ -100,6 +104,7 @@ public final class App {
App.application = application; App.application = application;
App.uiThreadExecutor = uiThreadExecutor; App.uiThreadExecutor = uiThreadExecutor;
App.eventBus = eventBus; App.eventBus = eventBus;
App.ga = new Ga(application, eventBus);
if (serviceLocator != null) { if (serviceLocator != null) {
App.locator = serviceLocator; App.locator = serviceLocator;
} else { } else {
@ -138,16 +143,6 @@ public final class App {
return (A) application; return (A) application;
} }
/**
* @param <L> real type of service locator
* @return instance of service locator user in application
*/
@Nonnull
public static <L extends ServiceLocator> L getLocator() {
checkInit();
return (L) locator;
}
/** /**
* Method returns executor which runs on Main Application's thread. It's safe to do all UI work on this executor * Method returns executor which runs on Main Application's thread. It's safe to do all UI work on this executor
* *
@ -172,4 +167,9 @@ public final class App {
public static CalculatorBroadcaster getBroadcaster() { public static CalculatorBroadcaster getBroadcaster() {
return broadcaster; return broadcaster;
} }
@Nonnull
public static Ga getGa() {
return ga;
}
} }

View File

@ -0,0 +1,73 @@
package org.solovyev.android.calculator;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull;
public class BaseActivity extends SherlockFragmentActivity {
@Nonnull
protected final ActivityUi ui;
public BaseActivity(@Nonnull ActivityUi ui) {
this.ui = ui;
}
public BaseActivity(@LayoutRes int layout) {
this(layout, "Activity");
}
public BaseActivity(@LayoutRes int layout, @Nonnull String logTag) {
this.ui = CalculatorApplication.getInstance().createActivityHelper(layout, logTag);
}
@Nonnull
public ActivityUi getUi() {
return ui;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ui.onCreate(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ui.onSaveInstanceState(this, outState);
}
@Override
protected void onStart() {
super.onStart();
ui.onStart(this);
}
@Override
protected void onStop() {
ui.onStop(this);
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
ui.onResume(this);
}
@Override
protected void onPause() {
this.ui.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
ui.onDestroy(this);
}
}

View File

@ -1,321 +1,321 @@
/* /*
* Copyright 2013 serso aka se.solovyev * Copyright 2013 serso aka se.solovyev
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details * Contact details
* *
* Email: se.solovyev@gmail.com * Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org * Site: http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.KeyguardManager; import android.app.KeyguardManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import org.solovyev.android.Views; import org.solovyev.android.Views;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.NumeralBasesButton; import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.calculator.view.OnDragListenerVibrator; import org.solovyev.android.calculator.view.OnDragListenerVibrator;
import org.solovyev.android.calculator.view.ViewsCache; import org.solovyev.android.calculator.view.ViewsCache;
import org.solovyev.android.history.HistoryDragProcessor; import org.solovyev.android.history.HistoryDragProcessor;
import org.solovyev.android.view.drag.*; import org.solovyev.android.view.drag.*;
import org.solovyev.common.listeners.JListeners; import org.solovyev.common.listeners.JListeners;
import org.solovyev.common.listeners.Listeners; import org.solovyev.common.listeners.Listeners;
import org.solovyev.common.math.Point2d; import org.solovyev.common.math.Point2d;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple; import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple_mobile; import static org.solovyev.android.calculator.CalculatorPreferences.Gui.Layout.simple_mobile;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.angleUnit; import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.angleUnit;
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase; import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase;
/** /**
* User: serso * User: serso
* Date: 9/28/12 * Date: 9/28/12
* Time: 12:12 AM * Time: 12:12 AM
*/ */
public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSharedPreferenceChangeListener { public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChangeListener {
@Nonnull @Nonnull
private static final List<Integer> viewIds = new ArrayList<Integer>(200); private static final List<Integer> viewIds = new ArrayList<Integer>(200);
@Nonnull @Nonnull
private CalculatorPreferences.Gui.Layout layout; private CalculatorPreferences.Gui.Layout layout;
@Nonnull @Nonnull
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
@Nullable @Nullable
private Vibrator vibrator; private Vibrator vibrator;
@Nonnull @Nonnull
private final JListeners<DragPreferencesChangeListener> dpclRegister = Listeners.newHardRefListeners(); private final JListeners<DragPreferencesChangeListener> dpclRegister = Listeners.newHardRefListeners();
@Nonnull @Nonnull
private String logTag = "CalculatorActivity"; private String logTag = "CalculatorActivity";
@Nullable @Nullable
private AngleUnitsButton angleUnitsButton; private AngleUnitsButton angleUnitsButton;
@Nullable @Nullable
private NumeralBasesButton clearButton; private NumeralBasesButton clearButton;
protected AbstractCalculatorHelper() { protected BaseUi() {
} }
protected AbstractCalculatorHelper(@Nonnull String logTag) { protected BaseUi(@Nonnull String logTag) {
this.logTag = logTag; this.logTag = logTag;
} }
protected void onCreate(@Nonnull Activity activity) { protected void onCreate(@Nonnull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE); vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE);
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
// let's disable locking of screen for monkeyrunner // let's disable locking of screen for monkeyrunner
if (CalculatorApplication.isMonkeyRunner(activity)) { if (CalculatorApplication.isMonkeyRunner(activity)) {
final KeyguardManager km = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE); final KeyguardManager km = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
km.newKeyguardLock(activity.getClass().getName()).disableKeyguard(); km.newKeyguardLock(activity.getClass().getName()).disableKeyguard();
} }
} }
public void logDebug(@Nonnull String message) { public void logDebug(@Nonnull String message) {
Log.d(logTag, message); Log.d(logTag, message);
} }
public void logError(@Nonnull String message) { public void logError(@Nonnull String message) {
Log.e(logTag, message); Log.e(logTag, message);
} }
public void processButtons(@Nonnull final Activity activity, @Nonnull View root) { public void processButtons(@Nonnull final Activity activity, @Nonnull View root) {
dpclRegister.removeListeners(); dpclRegister.removeListeners();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, activity); final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, activity);
final ViewsCache views = ViewsCache.forView(root); final ViewsCache views = ViewsCache.forView(root);
setOnDragListeners(views, dragPreferences, preferences); setOnDragListeners(views, dragPreferences, preferences);
final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences); final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(getCalculator()), dragPreferences), vibrator, preferences);
final DragButton historyButton = getButton(views, R.id.cpp_button_history); final DragButton historyButton = getButton(views, R.id.cpp_button_history);
if (historyButton != null) { if (historyButton != null) {
historyButton.setOnDragListener(historyOnDragListener); historyButton.setOnDragListener(historyOnDragListener);
} }
final DragButton subtractionButton = getButton(views, R.id.cpp_button_subtraction); final DragButton subtractionButton = getButton(views, R.id.cpp_button_subtraction);
if (subtractionButton != null) { if (subtractionButton != null) {
subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() { subtractionButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() {
@Override @Override
public boolean processDragEvent(@Nonnull DragDirection dragDirection, @Nonnull DragButton dragButton, @Nonnull Point2d startPoint2d, @Nonnull MotionEvent motionEvent) { public boolean processDragEvent(@Nonnull DragDirection dragDirection, @Nonnull DragButton dragButton, @Nonnull Point2d startPoint2d, @Nonnull MotionEvent motionEvent) {
if (dragDirection == DragDirection.down) { if (dragDirection == DragDirection.down) {
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_operators, null);
return true; return true;
} }
return false; return false;
} }
}, dragPreferences), vibrator, preferences)); }, dragPreferences), vibrator, preferences));
} }
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences); final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(), dragPreferences), vibrator, preferences);
final DragButton rightButton = getButton(views, R.id.cpp_button_right); final DragButton rightButton = getButton(views, R.id.cpp_button_right);
if (rightButton != null) { if (rightButton != null) {
rightButton.setOnDragListener(toPositionOnDragListener); rightButton.setOnDragListener(toPositionOnDragListener);
} }
final DragButton leftButton = getButton(views, R.id.cpp_button_left); final DragButton leftButton = getButton(views, R.id.cpp_button_left);
if (leftButton != null) { if (leftButton != null) {
leftButton.setOnDragListener(toPositionOnDragListener); leftButton.setOnDragListener(toPositionOnDragListener);
} }
final DragButton equalsButton = getButton(views, R.id.cpp_button_equals); final DragButton equalsButton = getButton(views, R.id.cpp_button_equals);
if (equalsButton != null) { if (equalsButton != null) {
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EqualsDragProcessor(), dragPreferences), vibrator, preferences)); equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EqualsDragProcessor(), dragPreferences), vibrator, preferences));
} }
angleUnitsButton = getButton(views, R.id.cpp_button_6); angleUnitsButton = getButton(views, R.id.cpp_button_6);
if (angleUnitsButton != null) { if (angleUnitsButton != null) {
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.AngleUnitsChanger(activity), dragPreferences), vibrator, preferences)); angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.AngleUnitsChanger(activity), dragPreferences), vibrator, preferences));
} }
clearButton = getButton(views, R.id.cpp_button_clear); clearButton = getButton(views, R.id.cpp_button_clear);
if (clearButton != null) { if (clearButton != null) {
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.NumeralBasesChanger(activity), dragPreferences), vibrator, preferences)); clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.NumeralBasesChanger(activity), dragPreferences), vibrator, preferences));
} }
final DragButton varsButton = getButton(views, R.id.cpp_button_vars); final DragButton varsButton = getButton(views, R.id.cpp_button_vars);
if (varsButton != null) { if (varsButton != null) {
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.VarsDragProcessor(activity), dragPreferences), vibrator, preferences)); varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.VarsDragProcessor(activity), dragPreferences), vibrator, preferences));
} }
final DragButton functionsButton = getButton(views, R.id.cpp_button_functions); final DragButton functionsButton = getButton(views, R.id.cpp_button_functions);
if (functionsButton != null) { if (functionsButton != null) {
functionsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.FunctionsDragProcessor(activity), dragPreferences), vibrator, preferences)); functionsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.FunctionsDragProcessor(activity), dragPreferences), vibrator, preferences));
} }
final DragButton roundBracketsButton = getButton(views, R.id.cpp_button_round_brackets); final DragButton roundBracketsButton = getButton(views, R.id.cpp_button_round_brackets);
if (roundBracketsButton != null) { if (roundBracketsButton != null) {
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new CalculatorButtons.RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
} }
if (layout == simple || layout == simple_mobile) { if (layout == simple || layout == simple_mobile) {
toggleButtonDirectionText(views, R.id.cpp_button_1, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_1, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_2, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_2, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_3, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_3, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_6, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_6, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_7, false, DragDirection.left, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_7, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_8, false, DragDirection.left, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_8, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_clear, false, DragDirection.left, DragDirection.up, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_clear, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_4, false, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_4, false, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_5, false, DragDirection.down); toggleButtonDirectionText(views, R.id.cpp_button_5, false, DragDirection.down);
toggleButtonDirectionText(views, R.id.cpp_button_9, false, DragDirection.left); toggleButtonDirectionText(views, R.id.cpp_button_9, false, DragDirection.left);
toggleButtonDirectionText(views, R.id.cpp_button_multiplication, false, DragDirection.left); toggleButtonDirectionText(views, R.id.cpp_button_multiplication, false, DragDirection.left);
toggleButtonDirectionText(views, R.id.cpp_button_plus, false, DragDirection.down, DragDirection.up); toggleButtonDirectionText(views, R.id.cpp_button_plus, false, DragDirection.down, DragDirection.up);
} }
CalculatorButtons.processButtons(theme, layout, root); CalculatorButtons.processButtons(theme, layout, root);
CalculatorButtons.toggleEqualsButton(preferences, activity); CalculatorButtons.toggleEqualsButton(preferences, activity);
CalculatorButtons.initMultiplicationButton(root); CalculatorButtons.initMultiplicationButton(root);
NumeralBaseButtons.toggleNumericDigits(activity, preferences); NumeralBaseButtons.toggleNumericDigits(activity, preferences);
// some devices ship own fonts which causes issues with rendering. Let's use our own font for all text views // some devices ship own fonts which causes issues with rendering. Let's use our own font for all text views
final Typeface typeFace = CalculatorApplication.getInstance().getTypeFace(); final Typeface typeFace = CalculatorApplication.getInstance().getTypeFace();
Views.processViewsOfType(root, TextView.class, new Views.ViewProcessor<TextView>() { Views.processViewsOfType(root, TextView.class, new Views.ViewProcessor<TextView>() {
@Override @Override
public void process(@Nonnull TextView view) { public void process(@Nonnull TextView view) {
int style = Typeface.NORMAL; int style = Typeface.NORMAL;
final Typeface oldTypeface = view.getTypeface(); final Typeface oldTypeface = view.getTypeface();
if (oldTypeface != null) { if (oldTypeface != null) {
style = oldTypeface.getStyle(); style = oldTypeface.getStyle();
} }
view.setTypeface(typeFace, style); view.setTypeface(typeFace, style);
} }
}); });
} }
private void toggleButtonDirectionText(@Nonnull ViewsCache views, int id, boolean showDirectionText, @Nonnull DragDirection... dragDirections) { private void toggleButtonDirectionText(@Nonnull ViewsCache views, int id, boolean showDirectionText, @Nonnull DragDirection... dragDirections) {
final View v = getButton(views, id); final View v = getButton(views, id);
if (v instanceof DirectionDragButton) { if (v instanceof DirectionDragButton) {
final DirectionDragButton button = (DirectionDragButton) v; final DirectionDragButton button = (DirectionDragButton) v;
for (DragDirection dragDirection : dragDirections) { for (DragDirection dragDirection : dragDirections) {
button.showDirectionText(showDirectionText, dragDirection); button.showDirectionText(showDirectionText, dragDirection);
} }
} }
} }
@Nonnull @Nonnull
private Calculator getCalculator() { private Calculator getCalculator() {
return Locator.getInstance().getCalculator(); return Locator.getInstance().getCalculator();
} }
private void setOnDragListeners(@Nonnull ViewsCache views, @Nonnull SimpleOnDragListener.Preferences dragPreferences, @Nonnull SharedPreferences preferences) { private void setOnDragListeners(@Nonnull ViewsCache views, @Nonnull SimpleOnDragListener.Preferences dragPreferences, @Nonnull SharedPreferences preferences) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences); final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new DigitButtonDragProcessor(getKeyboard()), dragPreferences), vibrator, preferences);
final List<Integer> viewIds = getViewIds(); final List<Integer> viewIds = getViewIds();
for (Integer viewId : viewIds) { for (Integer viewId : viewIds) {
final View view = views.findViewById(viewId); final View view = views.findViewById(viewId);
if (view instanceof DragButton) { if (view instanceof DragButton) {
((DragButton) view).setOnDragListener(onDragListener); ((DragButton) view).setOnDragListener(onDragListener);
} }
} }
} }
@Nonnull @Nonnull
private static List<Integer> getViewIds() { private static List<Integer> getViewIds() {
if (viewIds.isEmpty()) { if (viewIds.isEmpty()) {
for (Field field : R.id.class.getDeclaredFields()) { for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers(); int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try { try {
viewIds.add(field.getInt(R.id.class)); viewIds.add(field.getInt(R.id.class));
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
Log.e(R.id.class.getName(), e.getMessage()); Log.e(R.id.class.getName(), e.getMessage());
} }
} }
} }
} }
return viewIds; return viewIds;
} }
@Nonnull @Nonnull
private CalculatorKeyboard getKeyboard() { private CalculatorKeyboard getKeyboard() {
return Locator.getInstance().getKeyboard(); return Locator.getInstance().getKeyboard();
} }
@Nullable @Nullable
private <T extends DragButton> T getButton(@Nonnull ViewsCache views, int buttonId) { private <T extends DragButton> T getButton(@Nonnull ViewsCache views, int buttonId) {
return (T) views.findViewById(buttonId); return (T) views.findViewById(buttonId);
} }
@Nonnull @Nonnull
private SimpleOnDragListener newOnDragListener(@Nonnull SimpleOnDragListener.DragProcessor dragProcessor, private SimpleOnDragListener newOnDragListener(@Nonnull SimpleOnDragListener.DragProcessor dragProcessor,
@Nonnull SimpleOnDragListener.Preferences dragPreferences) { @Nonnull SimpleOnDragListener.Preferences dragPreferences) {
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences); final SimpleOnDragListener onDragListener = new SimpleOnDragListener(dragProcessor, dragPreferences);
dpclRegister.addListener(onDragListener); dpclRegister.addListener(onDragListener);
return onDragListener; return onDragListener;
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { if (key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) {
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, CalculatorApplication.getInstance()); final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, CalculatorApplication.getInstance());
for (DragPreferencesChangeListener dragPreferencesChangeListener : dpclRegister.getListeners()) { for (DragPreferencesChangeListener dragPreferencesChangeListener : dpclRegister.getListeners()) {
dragPreferencesChangeListener.onDragPreferencesChange(dragPreferences); dragPreferencesChangeListener.onDragPreferencesChange(dragPreferences);
} }
} }
if (angleUnit.isSameKey(key) || numeralBase.isSameKey(key)) { if (angleUnit.isSameKey(key) || numeralBase.isSameKey(key)) {
if (angleUnitsButton != null) { if (angleUnitsButton != null) {
angleUnitsButton.setAngleUnit(angleUnit.getPreference(preferences)); angleUnitsButton.setAngleUnit(angleUnit.getPreference(preferences));
} }
if (clearButton != null) { if (clearButton != null) {
clearButton.setNumeralBase(numeralBase.getPreference(preferences)); clearButton.setNumeralBase(numeralBase.getPreference(preferences));
} }
} }
} }
public void onDestroy(@Nonnull Activity activity) { public void onDestroy(@Nonnull Activity activity) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
preferences.unregisterOnSharedPreferenceChangeListener(this); preferences.unregisterOnSharedPreferenceChangeListener(this);
} }
} }

View File

@ -70,7 +70,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
private boolean useBackAsPrev; private boolean useBackAsPrev;
@Nonnull @Nonnull
private CalculatorActivityHelper activityHelper; private ActivityUi activityUi;
/** /**
* Called when the activity is first created. * Called when the activity is first created.
@ -81,20 +81,20 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
activityHelper = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG); activityUi = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG);
activityHelper.logDebug("onCreate"); activityUi.logDebug("onCreate");
activityHelper.onCreate(this, savedInstanceState); activityUi.onCreate(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityHelper.logDebug("super.onCreate"); activityUi.logDebug("super.onCreate");
if (isMultiPane()) { if (isMultiPane()) {
activityHelper.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane); activityUi.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane); activityUi.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane); activityUi.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane); activityUi.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane); activityUi.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane);
activityHelper.addTab(this, CalculatorPlotActivity.getPlotterFragmentType(), null, R.id.main_second_pane); activityUi.addTab(this, CalculatorPlotActivity.getPlotterFragmentType(), null, R.id.main_second_pane);
} else { } else {
final ActionBar actionBar = getSupportActionBar(); final ActionBar actionBar = getSupportActionBar();
if (Build.VERSION.SDK_INT <= GINGERBREAD_MR1 || (Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH && hasPermanentMenuKey())) { if (Build.VERSION.SDK_INT <= GINGERBREAD_MR1 || (Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH && hasPermanentMenuKey())) {
@ -127,6 +127,18 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
} }
} }
@Override
protected void onStart() {
super.onStart();
activityUi.onStart(this);
}
@Override
protected void onStop() {
activityUi.onStop(this);
super.onStop();
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private boolean hasPermanentMenuKey() { private boolean hasPermanentMenuKey() {
return ViewConfiguration.get(this).hasPermanentMenuKey(); return ViewConfiguration.get(this).hasPermanentMenuKey();
@ -232,7 +244,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
@Override @Override
protected void onPause() { protected void onPause() {
this.activityHelper.onPause(this); this.activityUi.onPause(this);
super.onPause(); super.onPause();
} }
@ -243,7 +255,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences);
if (newLayout != activityHelper.getLayout()) { if (newLayout != activityUi.getLayout()) {
Activities.restartActivity(this); Activities.restartActivity(this);
} }
@ -254,12 +266,12 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
window.clearFlags(FLAG_KEEP_SCREEN_ON); window.clearFlags(FLAG_KEEP_SCREEN_ON);
} }
this.activityHelper.onResume(this); this.activityUi.onResume(this);
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
activityHelper.onDestroy(this); activityUi.onDestroy(this);
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
@ -281,7 +293,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState); activityUi.onSaveInstanceState(this, outState);
} }
private void toggleOrientationChange(@Nullable SharedPreferences preferences) { private void toggleOrientationChange(@Nullable SharedPreferences preferences) {
@ -391,7 +403,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
// do nothing - fragment shown and already registered for plot updates // do nothing - fragment shown and already registered for plot updates
} else { } else {
// otherwise - open fragment // otherwise - open fragment
activityHelper.selectTab(CalculatorActivity.this, CalculatorFragmentType.plotter); activityUi.selectTab(CalculatorActivity.this, CalculatorFragmentType.plotter);
} }
} else { } else {
// start new activity // start new activity

View File

@ -1,95 +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 android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 9/25/12
* Time: 10:31 PM
*/
public interface CalculatorActivityHelper {
void onCreate(@Nonnull SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState);
void onCreate(@Nonnull Activity activity, @Nullable Bundle savedInstanceState);
void onSaveInstanceState(@Nonnull SherlockFragmentActivity activity, @Nonnull Bundle outState);
void onSaveInstanceState(@Nonnull Activity activity, @Nonnull Bundle outState);
int getLayoutId();
@Nonnull
CalculatorPreferences.Gui.Theme getTheme();
@Nonnull
CalculatorPreferences.Gui.Layout getLayout();
void onResume(@Nonnull SherlockFragmentActivity activity);
void onResume(@Nonnull Activity activity);
void onPause(@Nonnull Activity activity);
void onPause(@Nonnull SherlockFragmentActivity activity);
void onDestroy(@Nonnull SherlockFragmentActivity activity);
void onDestroy(@Nonnull Activity activity);
void addTab(@Nonnull SherlockFragmentActivity activity,
@Nonnull String tag,
@Nonnull Class<? extends Fragment> fragmentClass,
@Nullable Bundle fragmentArgs,
int captionResId,
int parentViewId);
void addTab(@Nonnull SherlockFragmentActivity activity,
@Nonnull CalculatorFragmentType fragmentType,
@Nullable Bundle fragmentArgs,
int parentViewId);
void setFragment(@Nonnull SherlockFragmentActivity activity,
@Nonnull CalculatorFragmentType fragmentType,
@Nullable Bundle fragmentArgs,
int parentViewId);
void logDebug(@Nonnull String message);
void processButtons(@Nonnull Activity activity, @Nonnull View root);
void logError(@Nonnull String message);
void selectTab(@Nonnull SherlockFragmentActivity activity, @Nonnull CalculatorFragmentType fragmentType);
}

View File

@ -226,23 +226,23 @@ public class CalculatorApplication extends android.app.Application implements Sh
} }
@Nonnull @Nonnull
public CalculatorActivityHelper createActivityHelper(int layoutResId, @Nonnull String logTag) { public ActivityUi createActivityHelper(int layoutResId, @Nonnull String logTag) {
return new CalculatorActivityHelperImpl(layoutResId, logTag); return new ActivityUi(layoutResId, logTag);
} }
@Nonnull @Nonnull
public CalculatorFragmentHelper createFragmentHelper(int layoutId) { public FragmentUi createFragmentHelper(int layoutId) {
return new CalculatorFragmentHelperImpl(layoutId); return new FragmentUi(layoutId);
} }
@Nonnull @Nonnull
public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId) { public FragmentUi createFragmentHelper(int layoutId, int titleResId) {
return new CalculatorFragmentHelperImpl(layoutId, titleResId); return new FragmentUi(layoutId, titleResId);
} }
@Nonnull @Nonnull
public CalculatorFragmentHelper createFragmentHelper(int layoutId, int titleResId, boolean listenersOnCreate) { public FragmentUi createFragmentHelper(int layoutId, int titleResId, boolean listenersOnCreate) {
return new CalculatorFragmentHelperImpl(layoutId, titleResId, listenersOnCreate); return new FragmentUi(layoutId, titleResId, listenersOnCreate);
} }
@Nonnull @Nonnull

View File

@ -41,7 +41,7 @@ import javax.annotation.Nonnull;
public class CalculatorDisplayFragment extends SherlockFragment { public class CalculatorDisplayFragment extends SherlockFragment {
@Nonnull @Nonnull
private CalculatorFragmentHelper fragmentHelper; private FragmentUi fragmentHelper;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {

View File

@ -49,7 +49,7 @@ import org.solovyev.android.sherlock.menu.SherlockMenuHelper;
public class CalculatorEditorFragment extends SherlockFragment { public class CalculatorEditorFragment extends SherlockFragment {
@Nonnull @Nonnull
private CalculatorFragmentHelper fragmentHelper; private FragmentUi fragmentHelper;
@Nonnull @Nonnull
private ActivityMenu<Menu, MenuItem> menu = ListActivityMenu.fromEnum(CalculatorMenu.class, SherlockMenuHelper.getInstance()); private ActivityMenu<Menu, MenuItem> menu = ListActivityMenu.fromEnum(CalculatorMenu.class, SherlockMenuHelper.getInstance());

View File

@ -40,7 +40,7 @@ import javax.annotation.Nonnull;
public abstract class CalculatorFragment extends SherlockFragment { public abstract class CalculatorFragment extends SherlockFragment {
@Nonnull @Nonnull
private final CalculatorFragmentHelper fragmentHelper; private final FragmentUi fragmentHelper;
protected CalculatorFragment(int layoutResId, int titleResId) { protected CalculatorFragment(int layoutResId, int titleResId) {
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(layoutResId, titleResId); fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(layoutResId, titleResId);
@ -50,7 +50,7 @@ public abstract class CalculatorFragment extends SherlockFragment {
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId()); fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId());
} }
protected CalculatorFragment(@Nonnull CalculatorFragmentHelper fragmentHelper) { protected CalculatorFragment(@Nonnull FragmentUi fragmentHelper) {
this.fragmentHelper = fragmentHelper; this.fragmentHelper = fragmentHelper;
} }

View File

@ -1,89 +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 android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull;
/**
* User: Solovyev_S
* Date: 03.10.12
* Time: 14:07
*/
public abstract class CalculatorFragmentActivity extends SherlockFragmentActivity {
@Nonnull
private final CalculatorActivityHelper activityHelper;
protected CalculatorFragmentActivity() {
this(R.layout.main_empty);
}
protected CalculatorFragmentActivity(int layoutResId) {
this.activityHelper = CalculatorApplication.getInstance().createActivityHelper(layoutResId, getClass().getSimpleName());
}
@Nonnull
protected CalculatorActivityHelper getActivityHelper() {
return activityHelper;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onPause() {
this.activityHelper.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
activityHelper.onDestroy(this);
}
}

View File

@ -55,7 +55,7 @@ public class CalculatorKeyboardFragment extends SherlockFragment implements Shar
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
@Nonnull @Nonnull
private CalculatorFragmentHelper fragmentHelper; private FragmentUi fragmentHelper;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {

View File

@ -40,7 +40,7 @@ import javax.annotation.Nonnull;
public abstract class CalculatorListFragment extends SherlockListFragment { public abstract class CalculatorListFragment extends SherlockListFragment {
@Nonnull @Nonnull
private final CalculatorFragmentHelper fragmentHelper; private final FragmentUi fragmentHelper;
protected CalculatorListFragment(int layoutResId, int titleResId) { protected CalculatorListFragment(int layoutResId, int titleResId) {
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(layoutResId, titleResId); fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(layoutResId, titleResId);
@ -50,7 +50,7 @@ public abstract class CalculatorListFragment extends SherlockListFragment {
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId()); fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId());
} }
protected CalculatorListFragment(@Nonnull CalculatorFragmentHelper fragmentHelper) { protected CalculatorListFragment(@Nonnull FragmentUi fragmentHelper) {
this.fragmentHelper = fragmentHelper; this.fragmentHelper = fragmentHelper;
} }

View File

@ -30,7 +30,7 @@ import android.webkit.WebView;
* Date: 6/16/13 * Date: 6/16/13
* Time: 6:26 PM * Time: 6:26 PM
*/ */
public final class CalculatorWikiActivity extends CalculatorFragmentActivity { public final class CalculatorWikiActivity extends EmptyActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {

View File

@ -22,35 +22,13 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.support.v4.app.Fragment; public abstract class EmptyActivity extends BaseActivity {
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import javax.annotation.Nonnull; protected EmptyActivity() {
import javax.annotation.Nullable; this(R.layout.main_empty);
}
/** protected EmptyActivity(int layoutResId) {
* User: serso super(layoutResId);
* Date: 9/26/12 }
* Time: 10:14 PM
*/
public interface CalculatorFragmentHelper {
boolean isPane(@Nonnull Fragment fragment);
void setPaneTitle(@Nonnull Fragment fragment, int titleResId);
void onCreate(@Nonnull Fragment fragment);
@Nonnull
View onCreateView(@Nonnull Fragment fragment, @Nonnull LayoutInflater inflater, @Nullable ViewGroup container);
void onViewCreated(@Nonnull Fragment fragment, @Nonnull View root);
void onResume(@Nonnull Fragment fragment);
void onPause(@Nonnull Fragment fragment);
void onDestroy(@Nonnull Fragment fragment);
} }

View File

@ -42,7 +42,7 @@ import static java.util.Arrays.asList;
* Date: 9/26/12 * Date: 9/26/12
* Time: 10:14 PM * Time: 10:14 PM
*/ */
public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper implements CalculatorFragmentHelper { public class FragmentUi extends BaseUi {
private ActivityCheckout checkout; private ActivityCheckout checkout;
@ -58,22 +58,21 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
@Nullable @Nullable
private Boolean adFree = null; private Boolean adFree = null;
public CalculatorFragmentHelperImpl(int layoutId) { public FragmentUi(int layoutId) {
this.layoutId = layoutId; this.layoutId = layoutId;
} }
public CalculatorFragmentHelperImpl(int layoutId, int titleResId) { public FragmentUi(int layoutId, int titleResId) {
this.layoutId = layoutId; this.layoutId = layoutId;
this.titleResId = titleResId; this.titleResId = titleResId;
} }
public CalculatorFragmentHelperImpl(int layoutId, int titleResId, boolean listenersOnCreate) { public FragmentUi(int layoutId, int titleResId, boolean listenersOnCreate) {
this.layoutId = layoutId; this.layoutId = layoutId;
this.titleResId = titleResId; this.titleResId = titleResId;
this.listenersOnCreate = listenersOnCreate; this.listenersOnCreate = listenersOnCreate;
} }
@Override
public boolean isPane(@Nonnull Fragment fragment) { public boolean isPane(@Nonnull Fragment fragment) {
return fragment.getActivity() instanceof CalculatorActivity; return fragment.getActivity() instanceof CalculatorActivity;
} }
@ -89,7 +88,6 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
} }
} }
@Override
public void onCreate(@Nonnull Fragment fragment) { public void onCreate(@Nonnull Fragment fragment) {
final FragmentActivity activity = fragment.getActivity(); final FragmentActivity activity = fragment.getActivity();
super.onCreate(activity); super.onCreate(activity);
@ -104,7 +102,6 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
checkout.start(); checkout.start();
} }
@Override
public void onResume(@Nonnull Fragment fragment) { public void onResume(@Nonnull Fragment fragment) {
if (adView != null) { if (adView != null) {
adView.resume(); adView.resume();
@ -136,7 +133,6 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
} }
} }
@Override
public void onPause(@Nonnull Fragment fragment) { public void onPause(@Nonnull Fragment fragment) {
adFree = null; adFree = null;
if (adView != null) { if (adView != null) {
@ -149,7 +145,6 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
} }
} }
@Override
public void onViewCreated(@Nonnull Fragment fragment, @Nonnull View root) { public void onViewCreated(@Nonnull Fragment fragment, @Nonnull View root) {
adView = (AdView) root.findViewById(R.id.ad); adView = (AdView) root.findViewById(R.id.ad);
final ViewGroup mainFragmentLayout = (ViewGroup) root.findViewById(R.id.main_fragment_layout); final ViewGroup mainFragmentLayout = (ViewGroup) root.findViewById(R.id.main_fragment_layout);
@ -170,7 +165,6 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
} }
} }
@Override
public void onDestroy(@Nonnull Fragment fragment) { public void onDestroy(@Nonnull Fragment fragment) {
if (adView != null) { if (adView != null) {
adView.destroy(); adView.destroy();
@ -189,7 +183,6 @@ public class CalculatorFragmentHelperImpl extends AbstractCalculatorHelper imple
} }
@Nonnull @Nonnull
@Override
public View onCreateView(@Nonnull Fragment fragment, @Nonnull LayoutInflater inflater, @Nullable ViewGroup container) { public View onCreateView(@Nonnull Fragment fragment, @Nonnull LayoutInflater inflater, @Nullable ViewGroup container) {
return inflater.inflate(layoutId, container, false); return inflater.inflate(layoutId, container, false);
} }

View File

@ -23,25 +23,24 @@
package org.solovyev.android.calculator.about; package org.solovyev.android.calculator.about;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.EmptyActivity;
import javax.annotation.Nullable;
import org.solovyev.android.calculator.CalculatorFragmentActivity;
import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import javax.annotation.Nullable;
/** /**
* User: serso * User: serso
* Date: 9/16/11 * Date: 9/16/11
* Time: 11:52 PM * Time: 11:52 PM
*/ */
public class CalculatorAboutActivity extends CalculatorFragmentActivity { public class CalculatorAboutActivity extends EmptyActivity {
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getActivityHelper().addTab(this, CalculatorFragmentType.about, null, R.id.main_layout); getUi().addTab(this, CalculatorFragmentType.about, null, R.id.main_layout);
getActivityHelper().addTab(this, CalculatorFragmentType.release_notes, null, R.id.main_layout); getUi().addTab(this, CalculatorFragmentType.release_notes, null, R.id.main_layout);
} }
} }

View File

@ -0,0 +1,50 @@
package org.solovyev.android.calculator.ga;
import android.content.Context;
import android.util.Log;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import org.solovyev.android.calculator.R;
import org.solovyev.common.listeners.JEvent;
import org.solovyev.common.listeners.JEventListener;
import org.solovyev.common.listeners.JEventListeners;
import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public final class Ga {
@Nonnull
private final GoogleAnalytics analytics;
@Nonnull
private final Tracker tracker;
public Ga(@Nonnull Context context, @Nonnull JEventListeners<JEventListener<? extends JEvent>, JEvent> bus) {
analytics = GoogleAnalytics.getInstance(context);
tracker = analytics.newTracker(R.xml.ga);
}
@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 "";
}
@Nonnull
public GoogleAnalytics getAnalytics() {
return analytics;
}
@Nonnull
public Tracker getTracker() {
return tracker;
}
}

View File

@ -108,7 +108,7 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
private HistoryArrayAdapter adapter; private HistoryArrayAdapter adapter;
@Nonnull @Nonnull
private CalculatorFragmentHelper fragmentHelper; private FragmentUi fragmentHelper;
private final ActivityMenu<Menu, MenuItem> menu = ListActivityMenu.fromResource(R.menu.history_menu, HistoryMenu.class, SherlockMenuHelper.getInstance(), new HistoryMenuFilter()); private final ActivityMenu<Menu, MenuItem> menu = ListActivityMenu.fromResource(R.menu.history_menu, HistoryMenu.class, SherlockMenuHelper.getInstance(), new HistoryMenuFilter());

View File

@ -23,64 +23,26 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.*;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.solovyev.android.calculator.*;
import static org.solovyev.android.calculator.CalculatorFragmentType.history; import static org.solovyev.android.calculator.CalculatorFragmentType.history;
import static org.solovyev.android.calculator.CalculatorFragmentType.saved_history; import static org.solovyev.android.calculator.CalculatorFragmentType.saved_history;
/** public class CalculatorHistoryActivity extends BaseActivity implements CalculatorEventListener {
* User: serso
* Date: 12/18/11
* Time: 7:37 PM
*/
public class CalculatorHistoryActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@Nonnull public CalculatorHistoryActivity() {
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName()); super(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
}
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState); ui.addTab(this, history, null, R.id.main_layout);
ui.addTab(this, saved_history, null, R.id.main_layout);
activityHelper.addTab(this, history, null, R.id.main_layout);
activityHelper.addTab(this, saved_history, null, R.id.main_layout);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onPause() {
this.activityHelper.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
activityHelper.onDestroy(this);
} }
@Override @Override
@ -89,9 +51,4 @@ public class CalculatorHistoryActivity extends SherlockFragmentActivity implemen
this.finish(); this.finish();
} }
} }
@Nonnull
CalculatorActivityHelper getActivityHelper() {
return activityHelper;
}
} }

View File

@ -99,7 +99,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
final Context context = getContext(); final Context context = getContext();
if (context instanceof CalculatorHistoryActivity) { if (context instanceof CalculatorHistoryActivity) {
final CalculatorHistoryActivity activity = (CalculatorHistoryActivity) context; final CalculatorHistoryActivity activity = (CalculatorHistoryActivity) context;
activity.getActivityHelper().selectTab(activity, saved_history); activity.getUi().selectTab(activity, saved_history);
} }
} }
}); });

View File

@ -33,7 +33,6 @@ import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -44,7 +43,7 @@ import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.CalculatorEventData;
import org.solovyev.android.calculator.CalculatorEventListener; import org.solovyev.android.calculator.CalculatorEventListener;
import org.solovyev.android.calculator.CalculatorEventType; import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.CalculatorFragmentHelper; import org.solovyev.android.calculator.FragmentUi;
import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.CalculatorMathRegistry; import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
@ -96,7 +95,7 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
private String category; private String category;
@Nonnull @Nonnull
private final CalculatorFragmentHelper fragmentHelper; private final FragmentUi fragmentHelper;
@Nonnull @Nonnull
private final Handler uiHandler = new Handler(); private final Handler uiHandler = new Handler();

View File

@ -25,31 +25,21 @@ package org.solovyev.android.calculator.math.edit;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import org.solovyev.android.calculator.*;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.solovyev.android.calculator.*; public class CalculatorFunctionsActivity extends BaseActivity implements CalculatorEventListener {
import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
/** public CalculatorFunctionsActivity() {
* User: serso super(R.layout.main_empty, CalculatorFunctionsActivity.class.getSimpleName());
* Date: 12/21/11 }
* Time: 10:33 PM
*/
public class CalculatorFunctionsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@Nonnull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
final Bundle bundle; final Bundle bundle;
final Intent intent = getIntent(); final Intent intent = getIntent();
@ -74,41 +64,11 @@ public class CalculatorFunctionsActivity extends SherlockFragmentActivity implem
fragmentParameters = AbstractMathEntityListFragment.createBundleFor(category.name()); fragmentParameters = AbstractMathEntityListFragment.createBundleFor(category.name());
} }
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidCategory.getCaptionId(), R.id.main_layout); ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidCategory.getCaptionId(), R.id.main_layout);
} else { } else {
Log.e(CalculatorFunctionsActivity.class.getSimpleName(), "Unable to find android function category for " + category); Log.e(CalculatorFunctionsActivity.class.getSimpleName(), "Unable to find android function category for " + category);
} }
} }
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onPause() {
this.activityHelper.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.activityHelper.onDestroy(this);
} }
@Override @Override

View File

@ -23,72 +23,33 @@
package org.solovyev.android.calculator.math.edit; package org.solovyev.android.calculator.math.edit;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.*;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.solovyev.android.calculator.*; public class CalculatorOperatorsActivity extends BaseActivity implements CalculatorEventListener {
import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
/** public CalculatorOperatorsActivity() {
* User: serso super(R.layout.main_empty, CalculatorOperatorsActivity.class.getSimpleName());
* Date: 12/21/11 }
* Time: 10:33 PM
*/
public class CalculatorOperatorsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@Nonnull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
final CalculatorFragmentType fragmentType = CalculatorFragmentType.operators; final CalculatorFragmentType fragmentType = CalculatorFragmentType.operators;
for (OperatorCategory category : OperatorCategory.getCategoriesByTabOrder()) { for (OperatorCategory category : OperatorCategory.getCategoriesByTabOrder()) {
final AndroidOperatorCategory androidCategory = AndroidOperatorCategory.valueOf(category); final AndroidOperatorCategory androidCategory = AndroidOperatorCategory.valueOf(category);
if (androidCategory != null) { if (androidCategory != null) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout); ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), androidCategory.getCaptionId(), R.id.main_layout);
} else { } else {
activityHelper.logError("Unable to find android operator category for " + category); ui.logError("Unable to find android operator category for " + category);
} }
} }
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onPause() {
this.activityHelper.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.activityHelper.onDestroy(this);
}
@Override @Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
switch (calculatorEventType) { switch (calculatorEventType) {

View File

@ -24,31 +24,21 @@ package org.solovyev.android.calculator.math.edit;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.*;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.solovyev.android.calculator.*; public class CalculatorVarsActivity extends BaseActivity implements CalculatorEventListener {
import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
/** public CalculatorVarsActivity() {
* User: serso super(R.layout.main_empty, CalculatorVarsActivity.class.getSimpleName());
* Date: 12/21/11 }
* Time: 11:05 PM
*/
public class CalculatorVarsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@Nonnull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
final Bundle bundle; final Bundle bundle;
final Intent intent = getIntent(); final Intent intent = getIntent();
@ -75,43 +65,13 @@ public class CalculatorVarsActivity extends SherlockFragmentActivity implements
final AndroidVarCategory androidVarCategory = AndroidVarCategory.valueOf(category); final AndroidVarCategory androidVarCategory = AndroidVarCategory.valueOf(category);
if (androidVarCategory != null) { if (androidVarCategory != null) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidVarCategory.getCaptionId(), R.id.main_layout); ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, androidVarCategory.getCaptionId(), R.id.main_layout);
} else { } else {
activityHelper.logError("Unable to find android var category for " + category); ui.logError("Unable to find android var category for " + category);
} }
} }
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onPause() {
this.activityHelper.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.activityHelper.onDestroy(this);
}
@Override @Override
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
switch (calculatorEventType) { switch (calculatorEventType) {

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.matrix;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.CalculatorFragmentActivity; import org.solovyev.android.calculator.EmptyActivity;
import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
@ -35,13 +35,13 @@ import static com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_STANDARD;
* Date: 12.10.12 * Date: 12.10.12
* Time: 10:56 * Time: 10:56
*/ */
public class CalculatorMatrixActivity extends CalculatorFragmentActivity { public class CalculatorMatrixActivity extends EmptyActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD); getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD);
getActivityHelper().setFragment(this, CalculatorFragmentType.matrix_edit, null, R.id.main_layout); getUi().setFragment(this, CalculatorFragmentType.matrix_edit, null, R.id.main_layout);
} }
} }

View File

@ -29,10 +29,10 @@ import android.content.res.Resources;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import org.simpleframework.xml.Serializer; import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.MathEntityDao; import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer; import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.MathPersistenceEntity; import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -111,7 +111,7 @@ public class AndroidMathEntityDao<T extends MathPersistenceEntity> implements Ma
public String getDescription(@Nonnull String descriptionId) { public String getDescription(@Nonnull String descriptionId) {
final Resources resources = context.getResources(); final Resources resources = context.getResources();
final int stringId = resources.getIdentifier(descriptionId, "string", App.getApplication().getClass().getPackage().getName()); final int stringId = resources.getIdentifier(descriptionId, "string", R.class.getPackage().getName());
try { try {
return resources.getString(stringId); return resources.getString(stringId);
} catch (Resources.NotFoundException e) { } catch (Resources.NotFoundException e) {

View File

@ -25,7 +25,7 @@ package org.solovyev.android.calculator.plot;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.CalculatorFragmentActivity; import org.solovyev.android.calculator.EmptyActivity;
import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
@ -39,7 +39,7 @@ import static com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_STANDARD;
* Date: 9/30/12 * Date: 9/30/12
* Time: 4:56 PM * Time: 4:56 PM
*/ */
public class CalculatorPlotActivity extends CalculatorFragmentActivity { public class CalculatorPlotActivity extends EmptyActivity {
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -55,7 +55,7 @@ public class CalculatorPlotActivity extends CalculatorFragmentActivity {
} }
getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD); getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD);
getActivityHelper().setFragment(this, getPlotterFragmentType(), arguments, R.id.main_layout); getUi().setFragment(this, getPlotterFragmentType(), arguments, R.id.main_layout);
} }
@Nonnull @Nonnull

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ga_trackingId">UA-28724009-2</string>
<bool name="ga_reportUncaughtExceptions">true</bool>
<bool name="ga_autoActivityTracking">true</bool>
</resources>