Editor/display changes

This commit is contained in:
Sergey Solovyev 2012-09-25 22:25:08 +04:00
parent 726ef0795c
commit 631ea40c15
13 changed files with 1264 additions and 1266 deletions

View File

@ -1,89 +1,64 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev. ~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com ~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org ~ or visit http://se.solovyev.org
--> -->
<LinearLayout <LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android" xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" a:layout_width="match_parent"
a:layout_width="match_parent" a:layout_height="match_parent"
a:layout_height="match_parent" a:id="@+id/main_layout"
a:id="@+id/main_layout" a:orientation="vertical"
a:orientation="vertical" a:layout_gravity="center"
a:layout_gravity="center" a:background="@color/default_background">
a:background="@color/default_background">
<LinearLayout a:id="@+id/editorContainer"
<LinearLayout a:id="@+id/editorContainer" a:layout_weight="2"
a:layout_weight="2" a:layout_width="match_parent"
a:layout_width="match_parent" a:layout_height="0dp"/>
a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1"
<LinearLayout a:layout_weight="1" a:layout_width="match_parent"
a:layout_width="match_parent" a:layout_height="0dp">
a:layout_height="0dp">
<include layout="@layout/calc_left_button"
<org.solovyev.android.view.drag.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" a:layout_margin="@dimen/button_margin"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" a:layout_width="0dp"
a:id="@+id/leftButton" a:layout_height="match_parent"
c:textUp="◀◀" a:layout_weight="1"/>
a:text="◀"
c:directionTextScale="0.5" <include layout="@layout/calc_erase_button"
style="?controlButtonStyle" a:layout_margin="@dimen/button_margin"
a:onClick="moveLeftButtonClickHandler" a:layout_width="0dp"
a:layout_width="0dp" a:layout_height="match_parent"
a:layout_height="match_parent" a:layout_weight="1"/>
a:layout_weight="1"/>
<LinearLayout a:id="@+id/displayContainer"
a:layout_margin="@dimen/display_margin"
<org.solovyev.android.view.ColorButton xmlns:a="http://schemas.android.com/apk/res/android" a:layout_weight="4"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" a:layout_width="0dp"
a:id="@+id/eraseButton" a:layout_height="match_parent"/>
a:drawableTop="@drawable/sym_keyboard_delete"
style="?controlImageButtonStyle" <include layout="@layout/calc_clear_button"
a:onClick="eraseButtonClickHandler" a:layout_margin="@dimen/button_margin"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="match_parent" a:layout_height="match_parent"
a:layout_weight="1"/> a:layout_weight="1"/>
<LinearLayout a:id="@+id/displayContainer" <include layout="@layout/calc_right_button"
a:layout_weight="4" a:layout_margin="@dimen/button_margin"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="match_parent"/> a:layout_height="match_parent"
a:layout_weight="1"/>
<org.solovyev.android.calculator.view.NumeralBasesButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" </LinearLayout>
a:id="@+id/clearButton"
c:textUp="dec" <LinearLayout a:id="@+id/keyboardContainer"
a:text="@string/c_clear" a:layout_weight="3"
c:textDown="bin" a:layout_width="match_parent"
c:textLeft="hex" a:layout_height="0dp"/>
a:textStyle="bold"
style="?controlImageButtonStyle"
a:onClick="clearButtonClickHandler" </LinearLayout>
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<org.solovyev.android.view.drag.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/rightButton"
c:textUp="▶▶"
a:text="▶"
c:directionTextScale="0.5"
style="?controlButtonStyle"
a:onClick="moveRightButtonClickHandler"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
</LinearLayout>
<LinearLayout a:id="@+id/keyboardContainer"
a:layout_weight="4"
a:layout_width="match_parent"
a:layout_height="0dp"/>
</LinearLayout>

View File

@ -1,44 +1,46 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev. ~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com ~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org ~ or visit http://se.solovyev.org
--> -->
<LinearLayout <LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android" xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent" a:layout_height="match_parent"
a:id="@+id/main_layout" a:id="@+id/main_layout"
a:orientation="vertical" a:orientation="vertical"
a:layout_gravity="center" a:layout_gravity="center"
a:background="@color/default_background"> a:background="@color/default_background">
<LinearLayout a:id="@+id/editorContainer" <LinearLayout a:id="@+id/editorContainer"
a:layout_weight="2" a:layout_weight="2"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="0dp"/> a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1" <LinearLayout a:layout_weight="1"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="0dp"> a:layout_height="0dp">
<include layout="@layout/calc_equals_button" <include layout="@layout/calc_equals_button"
a:layout_weight="1" a:layout_margin="@dimen/button_margin"
a:layout_width="0dp" a:layout_weight="1"
a:layout_height="match_parent"/> a:layout_width="0dp"
a:layout_height="match_parent"/>
<LinearLayout a:id="@+id/displayContainer"
a:layout_weight="4" <LinearLayout a:id="@+id/displayContainer"
a:layout_width="0dp" a:layout_margin="@dimen/display_margin"
a:layout_height="match_parent"/> a:layout_weight="4"
a:layout_width="0dp"
</LinearLayout> a:layout_height="match_parent"/>
<LinearLayout a:id="@+id/keyboardContainer" </LinearLayout>
a:layout_weight="5"
a:layout_width="match_parent" <LinearLayout a:id="@+id/keyboardContainer"
a:layout_height="0dp"/> a:layout_weight="5"
a:layout_width="match_parent"
</LinearLayout> a:layout_height="0dp"/>
</LinearLayout>

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:a="http://schemas.android.com/apk/res/android">
<item a:id="@+id/main_menu_item_settings"
a:title="@string/c_settings"/>
<item a:id="@+id/main_menu_item_history"
a:title="@string/c_history"/>
<item a:id="@+id/main_menu_conversion_tool"
a:title="@string/c_conversion_tool"/>
<item a:id="@+id/main_menu_item_help"
a:title="@string/c_help"/>
<item a:id="@+id/main_menu_item_about"
a:title="@string/c_about"/>
<item a:id="@+id/main_menu_item_exit"
a:title="@string/c_exit"/>
</menu>

View File

@ -0,0 +1,4 @@
<resources>
<dimen name="button_margin">0.5dp</dimen>
<dimen name="display_margin">@dimen/button_margin</dimen>
</resources>

View File

@ -1,43 +1,43 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev. ~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com ~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org ~ or visit http://se.solovyev.org
--> -->
<resources> <resources>
<style name="button_style" parent="button_style_parent"> <style name="button_style" parent="button_style_parent">
<item name="android:layout_width">fill_parent</item> <item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item> <item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">1</item> <item name="android:layout_weight">1</item>
<item name="android:focusable">true</item> <item name="android:focusable">true</item>
<item name="android:background">@drawable/default_button_dark</item> <item name="android:background">@drawable/default_button_dark</item>
<item name="android:layout_margin">0.5dp</item> <item name="android:layout_margin">@dimen/button_margin</item>
</style> </style>
<style name="editor_style" parent="editor_style_parent"> <style name="editor_style" parent="editor_style_parent">
<item name="android:gravity">left|top</item> <item name="android:gravity">left|top</item>
<item name="android:layout_width">fill_parent</item> <item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item> <item name="android:layout_height">fill_parent</item>
</style> </style>
<style name="display_style" parent="display_style_parent"> <style name="display_style" parent="display_style_parent">
<item name="android:gravity">top|right</item> <item name="android:gravity">top|right</item>
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item> <item name="android:layout_height">match_parent</item>
<item name="android:layout_margin">0.5dp</item> <item name="android:layout_margin">@dimen/display_margin</item>
</style> </style>
<style name="about_style" parent="about_style_parent"> <style name="about_style" parent="about_style_parent">
<item name="android:gravity">center</item> <item name="android:gravity">center</item>
<item name="android:layout_width">fill_parent</item> <item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item> <item name="android:layout_height">fill_parent</item>
</style> </style>
<style name="default_text"> <style name="default_text">
<item name="android:background">@color/default_background</item> <item name="android:background">@color/default_background</item>
<item name="android:textColor">@color/default_text_color</item> <item name="android:textColor">@color/default_text_color</item>
</style> </style>
</resources> </resources>

View File

@ -1,392 +1,381 @@
/* /*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.*; import android.app.Activity;
import android.content.Context; import android.app.AlertDialog;
import android.content.SharedPreferences; import android.content.Context;
import android.content.pm.ActivityInfo; import android.content.SharedPreferences;
import android.os.Bundle; import android.content.pm.ActivityInfo;
import android.preference.PreferenceManager; import android.os.Bundle;
import android.text.Html; import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod; import android.support.v4.app.FragmentActivity;
import android.util.Log; import android.text.Html;
import android.util.TypedValue; import android.text.method.LinkMovementMethod;
import android.view.*; import android.util.Log;
import android.widget.TextView; import android.util.TypedValue;
import net.robotmedia.billing.BillingController; import android.view.*;
import net.robotmedia.billing.IBillingObserver; import android.widget.TextView;
import org.jetbrains.annotations.NotNull; import net.robotmedia.billing.BillingController;
import org.jetbrains.annotations.Nullable; import net.robotmedia.billing.IBillingObserver;
import org.solovyev.android.AndroidUtils; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.FontSizeAdjuster; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.FontSizeAdjuster;
import org.solovyev.android.calculator.view.CalculatorAdditionalTitle; import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity;
import org.solovyev.android.menu.ActivityMenu; import org.solovyev.android.calculator.view.CalculatorAdditionalTitle;
import org.solovyev.android.menu.LayoutActivityMenu; import org.solovyev.android.fragments.FragmentUtils;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.menu.ActivityMenu;
import org.solovyev.android.view.ColorButton; import org.solovyev.android.menu.AndroidMenuHelper;
import org.solovyev.common.equals.EqualsTool; import org.solovyev.android.menu.ListActivityMenu;
import org.solovyev.common.history.HistoryAction; import org.solovyev.android.prefs.Preference;
import org.solovyev.common.text.StringUtils; import org.solovyev.android.view.ColorButton;
import org.solovyev.common.equals.EqualsTool;
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.text.StringUtils;
@NotNull
public static final String TAG = "Calculator++"; public class CalculatorActivity extends FragmentActivity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
private static final int HVGA_WIDTH_PIXELS = 320; @NotNull
public static final String TAG = "Calculator++";
@Nullable
private IBillingObserver billingObserver; private static final int HVGA_WIDTH_PIXELS = 320;
@NotNull @Nullable
private CalculatorPreferences.Gui.Theme theme; private IBillingObserver billingObserver;
@NotNull @NotNull
private CalculatorPreferences.Gui.Layout layout; private CalculatorPreferences.Gui.Theme theme;
private boolean useBackAsPrev; @NotNull
private CalculatorPreferences.Gui.Layout layout;
@NotNull
private ActivityMenu<Menu, MenuItem> menu = LayoutActivityMenu.newInstance(R.menu.main_menu, CalculatorMenu.class); private boolean useBackAsPrev;
/** @NotNull
* Called when the activity is first created. private ActivityMenu<Menu, MenuItem> menu = ListActivityMenu.fromList(CalculatorMenu.class, AndroidMenuHelper.getInstance());
*/
@Override /**
public void onCreate(@Nullable Bundle savedInstanceState) { * Called when the activity is first created.
*/
CalculatorApplication.registerOnRemoteStackTrace(); @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
CalculatorApplication.registerOnRemoteStackTrace();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setTheme(preferences);
super.onCreate(savedInstanceState); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
setLayout(preferences);
this.theme = CalculatorPreferences.Gui.getTheme(preferences);
CalculatorKeyboardFragment.fixThemeParameters(true, theme, this.getWindow().getDecorView()); setTheme(this.theme.getThemeId());
createFragment(CalculatorEditorFragment.class, R.id.editorContainer, "tag"); super.onCreate(savedInstanceState);
createFragment(CalculatorDisplayFragment.class, R.id.displayContainer, "display"); setLayout(preferences);
createFragment(CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard");
CalculatorKeyboardFragment.fixThemeParameters(true, theme, this.getWindow().getDecorView());
if (customTitleSupported) {
try { FragmentUtils.createFragment(this, CalculatorEditorFragment.class, R.id.editorContainer, "editor");
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); FragmentUtils.createFragment(this, CalculatorDisplayFragment.class, R.id.displayContainer, "display");
final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text); FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard");
additionalAdditionalTitleText.init(preferences);
preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText); if (customTitleSupported) {
} catch (ClassCastException e) { try {
// super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports) getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);
Log.e(CalculatorActivity.class.getName(), e.getMessage(), e); final CalculatorAdditionalTitle additionalAdditionalTitleText = (CalculatorAdditionalTitle)findViewById(R.id.additional_title_text);
} additionalAdditionalTitleText.init(preferences);
} preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText);
} catch (ClassCastException e) {
billingObserver = new CalculatorBillingObserver(this); // super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports)
BillingController.registerObserver(billingObserver); Log.e(CalculatorActivity.class.getName(), e.getMessage(), e);
}
this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); }
firstTimeInit(preferences, this);
billingObserver = new CalculatorBillingObserver(this);
// init billing controller BillingController.registerObserver(billingObserver);
BillingController.checkBillingSupported(this);
this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
toggleOrientationChange(preferences); firstTimeInit(preferences, this);
CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, theme, findViewById(R.id.main_layout)); // init billing controller
BillingController.checkBillingSupported(this);
preferences.registerOnSharedPreferenceChangeListener(this);
} toggleOrientationChange(preferences);
private void createFragment(@NotNull Class<? extends Fragment> fragmentClass, int parentViewId, @NotNull String tag) { CalculatorKeyboardFragment.toggleEqualsButton(preferences, this, theme, findViewById(R.id.main_layout));
final FragmentManager fm = getFragmentManager();
preferences.registerOnSharedPreferenceChangeListener(this);
Fragment messagesFragment = fm.findFragmentByTag(tag); }
final FragmentTransaction ft = fm.beginTransaction(); @NotNull
try { private AndroidCalculator getCalculator() {
if (messagesFragment == null) { return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator());
messagesFragment = Fragment.instantiate(this, fragmentClass.getName(), null); }
ft.add(parentViewId, messagesFragment, tag);
} else { private synchronized void setLayout(@NotNull SharedPreferences preferences) {
if (messagesFragment.isDetached()) { layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
ft.attach(messagesFragment);
} setContentView(layout.getLayoutId());
} }
} finally {
ft.commit(); private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) {
} final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences);
} if (appOpenedCounter != null) {
CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1);
@NotNull }
private AndroidCalculatorEngine getEngine() {
return ((AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine()); final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences);
}
final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName());
@NotNull
private AndroidCalculator getCalculator() { CalculatorPreferences.appVersion.putPreference(preferences, appVersion);
return ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator());
} boolean dialogShown = false;
if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
private synchronized void setLayout(@NotNull SharedPreferences preferences) { // new start
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text);
builder.setPositiveButton(android.R.string.ok, null);
setContentView(layout.getLayoutId()); builder.setTitle(R.string.c_first_start_text_title);
} builder.create().show();
dialogShown = true;
private synchronized void setTheme(@NotNull SharedPreferences preferences) { } else {
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); if (savedVersion < appVersion) {
setTheme(theme.getThemeId()); final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences);
} if (showReleaseNotes) {
final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1);
private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) { if (!StringUtils.isEmpty(releaseNotes)) {
final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
if (appOpenedCounter != null) { builder.setPositiveButton(android.R.string.ok, null);
CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1); builder.setTitle(R.string.c_release_notes);
} builder.create().show();
dialogShown = true;
final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences); }
}
final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName()); }
}
CalculatorPreferences.appVersion.putPreference(preferences, appVersion);
boolean dialogShown = false; //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!");
if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) { if (!dialogShown) {
// new start if (appOpenedCounter != null && appOpenedCounter > 10) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text); dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context);
builder.setPositiveButton(android.R.string.ok, null); }
builder.setTitle(R.string.c_first_start_text_title); }
builder.create().show();
dialogShown = true; if (!dialogShown) {
} else { dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context);
if (savedVersion < appVersion) { }
final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); }
if (showReleaseNotes) {
final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1); private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) {
if (!StringUtils.isEmpty(releaseNotes)) { boolean result = false;
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
builder.setPositiveButton(android.R.string.ok, null); final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences);
builder.setTitle(R.string.c_release_notes); if ( specialWindowShown != null && !specialWindowShown ) {
builder.create().show(); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
dialogShown = true; final View view = layoutInflater.inflate(layoutId, null);
}
} final TextView feedbackTextView = (TextView) view.findViewById(textViewId);
} feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view);
builder.setPositiveButton(android.R.string.ok, null);
//Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); builder.create().show();
if (!dialogShown) {
if (appOpenedCounter != null && appOpenedCounter > 10) { result = true;
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context); specialWindowShownPref.putPreference(preferences, true);
} }
}
return result;
if (!dialogShown) { }
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context);
} @Override
} public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) { if (useBackAsPrev) {
boolean result = false; getCalculator().doHistoryAction(HistoryAction.undo);
return true;
final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences); }
if ( specialWindowShown != null && !specialWindowShown ) { }
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); return super.onKeyDown(keyCode, event);
final View view = layoutInflater.inflate(layoutId, null); }
final TextView feedbackTextView = (TextView) view.findViewById(textViewId); @SuppressWarnings({"UnusedDeclaration"})
feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); public void equalsButtonClickHandler(@NotNull View v) {
getCalculator().evaluate();
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view); }
builder.setPositiveButton(android.R.string.ok, null);
builder.create().show(); /*
**********************************************************************
result = true; *
specialWindowShownPref.putPreference(preferences, true); * MENU
} *
**********************************************************************
return result; */
}
@Override @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { public boolean onPrepareOptionsMenu(Menu menu) {
if (keyCode == KeyEvent.KEYCODE_BACK) { return this.menu.onPrepareOptionsMenu(this, menu);
if (useBackAsPrev) { }
getCalculator().doHistoryAction(HistoryAction.undo);
return true; @Override
} public boolean onCreateOptionsMenu(Menu menu) {
} return this.menu.onCreateOptionsMenu(this, menu);
return super.onKeyDown(keyCode, event); }
}
@Override
@SuppressWarnings({"UnusedDeclaration"}) public boolean onOptionsItemSelected(MenuItem item) {
public void equalsButtonClickHandler(@NotNull View v) { return menu.onOptionsItemSelected(this, item);
getCalculator().evaluate(); }
}
/**
@Override * The font sizes in the layout files are specified for a HVGA display.
public boolean onCreateOptionsMenu(Menu menu) { * Adjust the font sizes accordingly if we are running on a different
return this.menu.onCreateOptionsMenu(this, menu); * display.
} */
@Override
@Override public void adjustFontSize(@NotNull TextView view) {
public boolean onOptionsItemSelected(MenuItem item) { float fontPixelSize = view.getTextSize();
return menu.onOptionsItemSelected(this, item); Display display = getWindowManager().getDefaultDisplay();
} int h = Math.min(display.getWidth(), display.getHeight());
float ratio = (float) h / HVGA_WIDTH_PIXELS;
/** view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);
* The font sizes in the layout files are specified for a HVGA display. }
* Adjust the font sizes accordingly if we are running on a different
* display. @Override
*/ protected void onResume() {
@Override super.onResume();
public void adjustFontSize(@NotNull TextView view) {
float fontPixelSize = view.getTextSize(); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Display display = getWindowManager().getDefaultDisplay();
int h = Math.min(display.getWidth(), display.getHeight()); final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences);
float ratio = (float) h / HVGA_WIDTH_PIXELS; final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences);
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); if (!theme.equals(newTheme) || !layout.equals(newLayout)) {
} AndroidUtils.restartActivity(this);
}
@Override }
protected void onResume() {
super.onResume(); @Override
protected void onDestroy() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); if (billingObserver != null) {
BillingController.unregisterObserver(billingObserver);
final CalculatorPreferences.Gui.Layout newLayout = CalculatorPreferences.Gui.layout.getPreference(preferences); }
final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences);
if (!theme.equals(newTheme) || !layout.equals(newLayout)) { super.onDestroy();
AndroidUtils.restartActivity(this); }
}
} @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
@Override if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) {
protected void onDestroy() { useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
if (billingObserver != null) { }
BillingController.unregisterObserver(billingObserver);
} if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) {
toggleOrientationChange(preferences);
super.onDestroy(); }
} }
@Override private void toggleOrientationChange(@Nullable SharedPreferences preferences) {
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences;
if ( CalculatorPreferences.Gui.usePrevAsBack.getKey().equals(key) ) { if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) {
useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} } else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if ( CalculatorPreferences.Gui.autoOrientation.getKey().equals(key) ) { }
toggleOrientationChange(preferences); }
}
} /*
**********************************************************************
private void toggleOrientationChange(@Nullable SharedPreferences preferences) { *
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; * BUTTON HANDLERS
if (CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { *
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); **********************************************************************
} else { */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} @SuppressWarnings({"UnusedDeclaration"})
} public void elementaryButtonClickHandler(@NotNull View v) {
throw new UnsupportedOperationException("Not implemented yet!");
/* }
**********************************************************************
* @SuppressWarnings({"UnusedDeclaration"})
* BUTTON HANDLERS public void historyButtonClickHandler(@NotNull View v) {
* CalculatorActivityLauncher.showHistory(this);
********************************************************************** }
*/
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void eraseButtonClickHandler(@NotNull View v) {
public void elementaryButtonClickHandler(@NotNull View v) { CalculatorLocatorImpl.getInstance().getEditor().erase();
throw new UnsupportedOperationException("Not implemented yet!"); }
}
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void simplifyButtonClickHandler(@NotNull View v) {
public void historyButtonClickHandler(@NotNull View v) { throw new UnsupportedOperationException("Not implemented yet!");
CalculatorActivityLauncher.showHistory(this); }
}
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void moveLeftButtonClickHandler(@NotNull View v) {
public void eraseButtonClickHandler(@NotNull View v) { getKeyboard().moveCursorLeft();
CalculatorLocatorImpl.getInstance().getEditor().erase(); }
}
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void moveRightButtonClickHandler(@NotNull View v) {
public void simplifyButtonClickHandler(@NotNull View v) { getKeyboard().moveCursorRight();
throw new UnsupportedOperationException("Not implemented yet!"); }
}
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void pasteButtonClickHandler(@NotNull View v) {
public void moveLeftButtonClickHandler(@NotNull View v) { getKeyboard().pasteButtonPressed();
getKeyboard().moveCursorLeft(); }
}
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void copyButtonClickHandler(@NotNull View v) {
public void moveRightButtonClickHandler(@NotNull View v) { getKeyboard().copyButtonPressed();
getKeyboard().moveCursorRight(); }
}
@NotNull
@SuppressWarnings({"UnusedDeclaration"}) private static CalculatorKeyboard getKeyboard() {
public void pasteButtonClickHandler(@NotNull View v) { return CalculatorLocatorImpl.getInstance().getKeyboard();
getKeyboard().pasteButtonPressed(); }
}
@SuppressWarnings({"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration"}) public void clearButtonClickHandler(@NotNull View v) {
public void copyButtonClickHandler(@NotNull View v) { getKeyboard().clearButtonPressed();
getKeyboard().copyButtonPressed(); }
}
@SuppressWarnings({"UnusedDeclaration"})
@NotNull public void digitButtonClickHandler(@NotNull View v) {
private static CalculatorKeyboard getKeyboard() { Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed());
return CalculatorLocatorImpl.getInstance().getKeyboard(); if (((ColorButton) v).isShowText()) {
} getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString());
}
@SuppressWarnings({"UnusedDeclaration"}) }
public void clearButtonClickHandler(@NotNull View v) {
getKeyboard().clearButtonPressed(); @SuppressWarnings({"UnusedDeclaration"})
} public void functionsButtonClickHandler(@NotNull View v) {
CalculatorActivityLauncher.showFunctions(this);
@SuppressWarnings({"UnusedDeclaration"}) }
public void digitButtonClickHandler(@NotNull View v) {
Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); @SuppressWarnings({"UnusedDeclaration"})
if (((ColorButton) v).isShowText()) { public void operatorsButtonClickHandler(@NotNull View v) {
getKeyboard().digitButtonPressed(((ColorButton) v).getText().toString()); CalculatorActivityLauncher.showOperators(this);
} }
}
public static void operatorsButtonClickHandler(@NotNull Activity activity) {
@SuppressWarnings({"UnusedDeclaration"}) CalculatorActivityLauncher.showOperators(activity);
public void functionsButtonClickHandler(@NotNull View v) { }
CalculatorActivityLauncher.showFunctions(this);
} @SuppressWarnings({"UnusedDeclaration"})
public void varsButtonClickHandler(@NotNull View v) {
@SuppressWarnings({"UnusedDeclaration"}) CalculatorActivityLauncher.showVars(this);
public void operatorsButtonClickHandler(@NotNull View v) { }
CalculatorActivityLauncher.showOperators(this);
} @SuppressWarnings({"UnusedDeclaration"})
public void donateButtonClickHandler(@NotNull View v) {
public static void operatorsButtonClickHandler(@NotNull Activity activity, @NotNull View view) { CalculatorApplication.showDonationDialog(this);
CalculatorActivityLauncher.showOperators(activity); }
}
@SuppressWarnings({"UnusedDeclaration"})
public void varsButtonClickHandler(@NotNull View v) {
CalculatorActivityLauncher.showVars(this);
}
@SuppressWarnings({"UnusedDeclaration"})
public void donateButtonClickHandler(@NotNull View v) {
CalculatorApplication.showDonationDialog(this);
}
} }

View File

@ -1,105 +1,112 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import net.robotmedia.billing.BillingController; import net.robotmedia.billing.BillingController;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.ads.AdsController; import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.history.AndroidCalculatorHistory; import org.solovyev.android.calculator.history.AndroidCalculatorHistory;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
/** /**
* User: serso * User: serso
* Date: 12/1/11 * Date: 12/1/11
* Time: 1:21 PM * Time: 1:21 PM
*/ */
public class CalculatorApplication extends android.app.Application { public class CalculatorApplication extends android.app.Application {
private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted";
public static final String AD_FREE_PRODUCT_ID = "ad_free"; public static final String AD_FREE_PRODUCT_ID = "ad_free";
public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free";
public static final String ADMOB_USER_ID = "a14f02cf9c80cbc"; public static final String ADMOB_USER_ID = "a14f02cf9c80cbc";
public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php"; public static final String REMOTE_STACK_TRACE_URL = "http://calculatorpp.com/crash_reports/upload.php";
@NotNull @NotNull
private static CalculatorApplication instance; private static CalculatorApplication instance;
public CalculatorApplication() { public CalculatorApplication() {
instance = this; instance = this;
} }
@NotNull @NotNull
public static CalculatorApplication getInstance() { public static CalculatorApplication getInstance() {
return instance; return instance;
} }
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final AndroidCalculator calculator = new AndroidCalculator(); setTheme(preferences);
CalculatorLocatorImpl.getInstance().init(calculator, super.onCreate();
new AndroidCalculatorEngine(this),
new AndroidCalculatorClipboard(this), final AndroidCalculator calculator = new AndroidCalculator();
new AndroidCalculatorNotifier(this),
new AndroidCalculatorHistory(this, calculator)); CalculatorLocatorImpl.getInstance().init(calculator,
new AndroidCalculatorEngine(this),
CalculatorLocatorImpl.getInstance().getCalculator().init(); new AndroidCalculatorClipboard(this),
new AndroidCalculatorNotifier(this),
AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() { new AndroidCalculatorHistory(this, calculator));
@Override CalculatorLocatorImpl.getInstance().getCalculator().init();
public byte[] getObfuscationSalt() {
return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82}; AdsController.getInstance().init(ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() {
}
@Override
@Override public byte[] getObfuscationSalt() {
public String getPublicKey() { return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82};
return CalculatorSecurity.getPK(); }
}
}); @Override
public String getPublicKey() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); return CalculatorSecurity.getPK();
}
CalculatorPreferences.setDefaultValues(preferences); });
}
CalculatorPreferences.setDefaultValues(preferences);
public static void showDonationDialog(@NotNull final Context context) { }
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
final View view = layoutInflater.inflate(R.layout.donate, null); private void setTheme(@NotNull SharedPreferences preferences) {
final CalculatorPreferences.Gui.Theme theme = CalculatorPreferences.Gui.getTheme(preferences);
final TextView donate = (TextView) view.findViewById(R.id.donateText); setTheme(theme.getThemeId());
donate.setMovementMethod(LinkMovementMethod.getInstance()); }
final AlertDialog.Builder builder = new AlertDialog.Builder(context) public static void showDonationDialog(@NotNull final Context context) {
.setCancelable(true) final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
.setNegativeButton(R.string.c_cancel, null) final View view = layoutInflater.inflate(R.layout.donate, null);
.setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() {
@Override final TextView donate = (TextView) view.findViewById(R.id.donateText);
public void onClick(DialogInterface dialog, int which) { donate.setMovementMethod(LinkMovementMethod.getInstance());
final Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(paypalDonateUrl)); final AlertDialog.Builder builder = new AlertDialog.Builder(context)
context.startActivity(i); .setCancelable(true)
} .setNegativeButton(R.string.c_cancel, null)
}) .setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() {
.setView(view); @Override
public void onClick(DialogInterface dialog, int which) {
builder.create().show(); final Intent i = new Intent(Intent.ACTION_VIEW);
} i.setData(Uri.parse(paypalDonateUrl));
context.startActivity(i);
public static void registerOnRemoteStackTrace() { }
//Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL)); })
} .setView(view);
}
builder.create().show();
}
public static void registerOnRemoteStackTrace() {
//Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL));
}
}

View File

@ -1,34 +1,34 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Fragment; import android.os.Bundle;
import android.os.Bundle; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 25.09.12 * Date: 25.09.12
* Time: 12:03 * Time: 12:03
*/ */
public class CalculatorDisplayFragment extends Fragment { public class CalculatorDisplayFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.calc_display, null); final View view = inflater.inflate(R.layout.calc_display, null);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return view; return view;
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setDisplay(getActivity()); ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setDisplay(getActivity());
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
} }
} }

View File

@ -1,32 +1,32 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Fragment; import android.os.Bundle;
import android.os.Bundle; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 25.09.12 * Date: 25.09.12
* Time: 10:49 * Time: 10:49
*/ */
public class CalculatorEditorFragment extends Fragment { public class CalculatorEditorFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.calc_editor, null); return inflater.inflate(R.layout.calc_editor, null);
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity()); ((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity());
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
} }
} }

View File

@ -1,493 +1,493 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.content.Context;
import android.content.Context; import android.content.SharedPreferences;
import android.content.SharedPreferences; import android.content.res.Configuration;
import android.content.res.Configuration; import android.os.Bundle;
import android.os.Bundle; import android.os.Vibrator;
import android.os.Vibrator; import android.preference.PreferenceManager;
import android.preference.PreferenceManager; import android.support.v4.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.*; import android.view.*;
import android.widget.Button; import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import jscl.AngleUnit; import jscl.AngleUnit;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
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.history.HistoryDragProcessor; import org.solovyev.android.history.HistoryDragProcessor;
import org.solovyev.android.view.ColorButton; import org.solovyev.android.view.ColorButton;
import org.solovyev.android.view.drag.*; import org.solovyev.android.view.drag.*;
import org.solovyev.common.Announcer; import org.solovyev.common.Announcer;
import org.solovyev.common.math.Point2d; import org.solovyev.common.math.Point2d;
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;
/** /**
* User: Solovyev_S * User: Solovyev_S
* Date: 25.09.12 * Date: 25.09.12
* Time: 12:25 * Time: 12:25
*/ */
public class CalculatorKeyboardFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener { public class CalculatorKeyboardFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener {
@Nullable @Nullable
private Vibrator vibrator; private Vibrator vibrator;
@NotNull @NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class); private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
@NotNull @NotNull
private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons(); private NumeralBaseButtons numeralBaseButtons = new NumeralBaseButtons();
@NotNull @NotNull
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
@NotNull @NotNull
private CalculatorPreferences.Gui.Layout layout; private CalculatorPreferences.Gui.Layout layout;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
vibrator = (Vibrator) this.getActivity().getSystemService(Activity.VIBRATOR_SERVICE); vibrator = (Vibrator) this.getActivity().getSystemService(Activity.VIBRATOR_SERVICE);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.calc_keyboard, null); final View view = inflater.inflate(R.layout.calc_keyboard, null);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return view; return view;
} }
@Override @Override
public void onViewCreated(View root, Bundle savedInstanceState) { public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState); super.onViewCreated(root, savedInstanceState);
dpclRegister.clear(); dpclRegister.clear();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this.getActivity()); final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this.getActivity());
setOnDragListeners(root, dragPreferences, preferences); setOnDragListeners(root, 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(root, R.id.historyButton); final DragButton historyButton = getButton(root, R.id.historyButton);
if (historyButton != null) { if (historyButton != null) {
historyButton.setOnDragListener(historyOnDragListener); historyButton.setOnDragListener(historyOnDragListener);
} }
final DragButton subtractionButton = getButton(root, R.id.subtractionButton); final DragButton subtractionButton = getButton(root, R.id.subtractionButton);
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(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
if (dragDirection == DragDirection.down) { if (dragDirection == DragDirection.down) {
CalculatorActivity.operatorsButtonClickHandler(getActivity(), dragButton); CalculatorActivity.operatorsButtonClickHandler(getActivity());
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(root, R.id.rightButton); final DragButton rightButton = getButton(root, R.id.rightButton);
if (rightButton != null) { if (rightButton != null) {
rightButton.setOnDragListener(toPositionOnDragListener); rightButton.setOnDragListener(toPositionOnDragListener);
} }
final DragButton leftButton = getButton(root, R.id.leftButton); final DragButton leftButton = getButton(root, R.id.leftButton);
if (leftButton != null) { if (leftButton != null) {
leftButton.setOnDragListener(toPositionOnDragListener); leftButton.setOnDragListener(toPositionOnDragListener);
} }
final DragButton equalsButton = getButton(root, R.id.equalsButton); final DragButton equalsButton = getButton(root, R.id.equalsButton);
if (equalsButton != null) { if (equalsButton != null) {
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences)); equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(), dragPreferences), vibrator, preferences));
} }
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton); final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) getButton(root, R.id.sixDigitButton);
if (angleUnitsButton != null) { if (angleUnitsButton != null) {
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences)); angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(this.getActivity()), dragPreferences), vibrator, preferences));
} }
final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton); final NumeralBasesButton clearButton = (NumeralBasesButton) getButton(root, R.id.clearButton);
if (clearButton != null) { if (clearButton != null) {
clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences)); clearButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(this.getActivity()), dragPreferences), vibrator, preferences));
} }
final DragButton varsButton = getButton(root, R.id.varsButton); final DragButton varsButton = getButton(root, R.id.varsButton);
if (varsButton != null) { if (varsButton != null) {
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences)); varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(this.getActivity()), dragPreferences), vibrator, preferences));
} }
final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton); final DragButton roundBracketsButton = getButton(root, R.id.roundBracketsButton);
if (roundBracketsButton != null) { if (roundBracketsButton != null) {
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences)); roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDragProcessor(), dragPreferences), vibrator, preferences));
} }
if (layout == CalculatorPreferences.Gui.Layout.simple) { if (layout == CalculatorPreferences.Gui.Layout.simple) {
toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.oneDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.twoDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.threeDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.sixDigitButton, false, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.sevenDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.eightDigitButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down); toggleButtonDirectionText(root, R.id.clearButton, false, DragDirection.left, DragDirection.up, DragDirection.down);
toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down); toggleButtonDirectionText(root, R.id.fourDigitButton, false, DragDirection.down);
toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down); toggleButtonDirectionText(root, R.id.fiveDigitButton, false, DragDirection.down);
toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left); toggleButtonDirectionText(root, R.id.nineDigitButton, false, DragDirection.left);
toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left); toggleButtonDirectionText(root, R.id.multiplicationButton, false, DragDirection.left);
toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up); toggleButtonDirectionText(root, R.id.plusButton, false, DragDirection.down, DragDirection.up);
} }
numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences);
fixThemeParameters(true, theme, this.getView()); fixThemeParameters(true, theme, this.getView());
toggleEqualsButton(preferences, this.getActivity(), theme, root); toggleEqualsButton(preferences, this.getActivity(), theme, root);
initMultiplicationButton(); initMultiplicationButton();
} }
@Nullable @Nullable
private <T extends DragButton> T getButton(@NotNull View root, int buttonId) { private <T extends DragButton> T getButton(@NotNull View root, int buttonId) {
return (T) root.findViewById(buttonId); return (T) root.findViewById(buttonId);
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
preferences.unregisterOnSharedPreferenceChangeListener(this); preferences.unregisterOnSharedPreferenceChangeListener(this);
} }
public static void fixThemeParameters(boolean fixMagicFlames, public static void fixThemeParameters(boolean fixMagicFlames,
@NotNull CalculatorPreferences.Gui.Theme theme, @NotNull CalculatorPreferences.Gui.Theme theme,
@NotNull View root) { @NotNull View root) {
if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) {
if (fixMagicFlames) { if (fixMagicFlames) {
// for metro themes we should turn off magic flames // for metro themes we should turn off magic flames
AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor<ColorButton>() { AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor<ColorButton>() {
@Override @Override
public void process(@NotNull ColorButton colorButton) { public void process(@NotNull ColorButton colorButton) {
colorButton.setDrawMagicFlame(false); colorButton.setDrawMagicFlame(false);
} }
}); });
} }
} }
} }
private void initMultiplicationButton() { private void initMultiplicationButton() {
final View multiplicationButton = getView().findViewById(R.id.multiplicationButton); final View multiplicationButton = getView().findViewById(R.id.multiplicationButton);
if ( multiplicationButton instanceof Button) { if ( multiplicationButton instanceof Button) {
((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign());
} }
} }
/* private static void setMarginsForView(@Nullable View view, int marginLeft, int marginBottom, @NotNull Context context) { /* private static void setMarginsForView(@Nullable View view, int marginLeft, int marginBottom, @NotNull Context context) {
// IMPORTANT: this is workaround for probably android bug // IMPORTANT: this is workaround for probably android bug
// currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually // currently margin values set in styles are not applied for some reasons to the views (using include tag) => set them manually
if (view != null) { if (view != null) {
final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final DisplayMetrics dm = context.getResources().getDisplayMetrics();
if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams(); final LinearLayout.LayoutParams oldParams = (LinearLayout.LayoutParams) view.getLayoutParams();
final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight); final LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(oldParams.width, oldParams.height, oldParams.weight);
newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom)); newParams.setMargins(AndroidUtils.toPixels(dm, marginLeft), 0, 0, AndroidUtils.toPixels(dm, marginBottom));
view.setLayoutParams(newParams); view.setLayoutParams(newParams);
} }
} }
}*/ }*/
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
} }
private synchronized void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { private synchronized void setOnDragListeners(@NotNull View root, @NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull 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> dragButtonIds = new ArrayList<Integer>(); final List<Integer> dragButtonIds = new ArrayList<Integer>();
final List<Integer> buttonIds = new ArrayList<Integer>(); final List<Integer> buttonIds = new ArrayList<Integer>();
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 {
int viewId = field.getInt(R.id.class); int viewId = field.getInt(R.id.class);
final View view = root.findViewById(viewId); final View view = root.findViewById(viewId);
if (view instanceof DragButton) { if (view instanceof DragButton) {
dragButtonIds.add(viewId); dragButtonIds.add(viewId);
} }
if (view instanceof Button) { if (view instanceof Button) {
buttonIds.add(viewId); buttonIds.add(viewId);
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
Log.e(R.id.class.getName(), e.getMessage()); Log.e(R.id.class.getName(), e.getMessage());
} }
} }
} }
for (Integer dragButtonId : dragButtonIds) { for (Integer dragButtonId : dragButtonIds) {
final DragButton button = getButton(root, dragButtonId); final DragButton button = getButton(root, dragButtonId);
if (button != null) { if (button != null) {
button.setOnDragListener(onDragListener); button.setOnDragListener(onDragListener);
} }
} }
} }
@NotNull @NotNull
private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor, private SimpleOnDragListener newOnDragListener(@NotNull SimpleOnDragListener.DragProcessor dragProcessor,
@NotNull SimpleOnDragListener.Preferences dragPreferences) { @NotNull 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 != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) { if (key != null && key.startsWith("org.solovyev.android.calculator.DragButtonCalibrationActivity")) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this.getActivity())); dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this.getActivity()));
} }
if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) { if (AndroidCalculatorEngine.Preferences.numeralBase.getKey().equals(key)) {
numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences); numeralBaseButtons.toggleNumericDigits(this.getActivity(), preferences);
} }
if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) { if ( CalculatorPreferences.Gui.showEqualsButton.getKey().equals(key) ) {
toggleEqualsButton(preferences, this.getActivity(), theme, getView()); toggleEqualsButton(preferences, this.getActivity(), theme, getView());
} }
if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { if ( AndroidCalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) {
initMultiplicationButton(); initMultiplicationButton();
} }
} }
private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) { private void toggleButtonDirectionText(@NotNull View root, int id, boolean showDirectionText, @NotNull DragDirection... dragDirections) {
final View v = getButton(root, id); final View v = getButton(root, 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);
} }
} }
} }
public static void toggleEqualsButton(@Nullable SharedPreferences preferences, public static void toggleEqualsButton(@Nullable SharedPreferences preferences,
@NotNull Activity activity, @NotNull Activity activity,
@NotNull CalculatorPreferences.Gui.Theme theme, @NotNull CalculatorPreferences.Gui.Theme theme,
@NotNull View root) { @NotNull View root) {
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences;
if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) {
final Display display = activity.getWindowManager().getDefaultDisplay(); final Display display = activity.getWindowManager().getDefaultDisplay();
final DragButton button = (DragButton)activity.findViewById(R.id.equalsButton); final DragButton button = (DragButton)activity.findViewById(R.id.equalsButton);
if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) {
button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1f)); button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1f));
if (display.getWidth() <= 480) { if (display.getWidth() <= 480) {
// mobile phones // mobile phones
final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView();
if (calculatorDisplayView != null) { if (calculatorDisplayView != null) {
calculatorDisplayView.setBackgroundDrawable(null); calculatorDisplayView.setBackgroundDrawable(null);
} }
} }
} else { } else {
button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 0f)); button.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 0f));
if (display.getWidth() <= 480) { if (display.getWidth() <= 480) {
// mobile phones // mobile phones
final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView(); final AndroidCalculatorDisplayView calculatorDisplayView = getCalculatorDisplayView();
if (calculatorDisplayView != null) { if (calculatorDisplayView != null) {
calculatorDisplayView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.equals9)); calculatorDisplayView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.equals9));
} }
} }
} }
fixThemeParameters(false, theme, root); fixThemeParameters(false, theme, root);
} }
} }
@Nullable @Nullable
private static AndroidCalculatorDisplayView getCalculatorDisplayView() { private static AndroidCalculatorDisplayView getCalculatorDisplayView() {
return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView();
} }
@NotNull @NotNull
private Calculator getCalculator() { private Calculator getCalculator() {
return CalculatorLocatorImpl.getInstance().getCalculator(); return CalculatorLocatorImpl.getInstance().getCalculator();
} }
@NotNull @NotNull
private static CalculatorKeyboard getKeyboard() { private static CalculatorKeyboard getKeyboard() {
return CalculatorLocatorImpl.getInstance().getKeyboard(); return CalculatorLocatorImpl.getInstance().getKeyboard();
} }
/* /*
********************************************************************** **********************************************************************
* *
* STATIC CLASSES * STATIC CLASSES
* *
********************************************************************** **********************************************************************
*/ */
private static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { private static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor {
@Override @Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
final boolean result; final boolean result;
if (dragDirection == DragDirection.left) { if (dragDirection == DragDirection.left) {
getKeyboard().roundBracketsButtonPressed(); getKeyboard().roundBracketsButtonPressed();
result = true; result = true;
} else { } else {
result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent);
} }
return result; return result;
} }
} }
private static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { private static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor {
@NotNull @NotNull
private Context context; private Context context;
private VarsDragProcessor(Context context) { private VarsDragProcessor(Context context) {
this.context = context; this.context = context;
} }
@Override @Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, public boolean processDragEvent(@NotNull DragDirection dragDirection,
@NotNull DragButton dragButton, @NotNull DragButton dragButton,
@NotNull Point2d startPoint2d, @NotNull Point2d startPoint2d,
@NotNull MotionEvent motionEvent) { @NotNull MotionEvent motionEvent) {
boolean result = false; boolean result = false;
if (dragDirection == DragDirection.up) { if (dragDirection == DragDirection.up) {
CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay());
result = true; result = true;
} }
return result; return result;
} }
} }
private static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { private static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor {
@NotNull @NotNull
private final DigitButtonDragProcessor processor; private final DigitButtonDragProcessor processor;
@NotNull @NotNull
private final Context context; private final Context context;
private AngleUnitsChanger(@NotNull Context context) { private AngleUnitsChanger(@NotNull Context context) {
this.context = context; this.context = context;
this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard()); this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard());
} }
@Override @Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, public boolean processDragEvent(@NotNull DragDirection dragDirection,
@NotNull DragButton dragButton, @NotNull DragButton dragButton,
@NotNull Point2d startPoint2d, @NotNull Point2d startPoint2d,
@NotNull MotionEvent motionEvent) { @NotNull MotionEvent motionEvent) {
boolean result = false; boolean result = false;
if (dragButton instanceof AngleUnitsButton) { if (dragButton instanceof AngleUnitsButton) {
if (dragDirection != DragDirection.left) { if (dragDirection != DragDirection.left) {
final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection);
if (directionText != null) { if (directionText != null) {
try { try {
final AngleUnit angleUnits = AngleUnit.valueOf(directionText); final AngleUnit angleUnits = AngleUnit.valueOf(directionText);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits);
Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show();
result = true; result = true;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText);
} }
} }
} else if (dragDirection == DragDirection.left) { } else if (dragDirection == DragDirection.left) {
result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent);
} }
} }
return result; return result;
} }
} }
private static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { private static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor {
@NotNull @NotNull
private final Context context; private final Context context;
private NumeralBasesChanger(@NotNull Context context) { private NumeralBasesChanger(@NotNull Context context) {
this.context = context; this.context = context;
} }
@Override @Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, public boolean processDragEvent(@NotNull DragDirection dragDirection,
@NotNull DragButton dragButton, @NotNull DragButton dragButton,
@NotNull Point2d startPoint2d, @NotNull Point2d startPoint2d,
@NotNull MotionEvent motionEvent) { @NotNull MotionEvent motionEvent) {
boolean result = false; boolean result = false;
if (dragButton instanceof NumeralBasesButton) { if (dragButton instanceof NumeralBasesButton) {
final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection);
if (directionText != null) { if (directionText != null) {
try { try {
final NumeralBase numeralBase = NumeralBase.valueOf(directionText); final NumeralBase numeralBase = NumeralBase.valueOf(directionText);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show();
result = true; result = true;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText);
} }
} }
} }
return result; return result;
} }
} }
} }

View File

@ -6,51 +6,51 @@ import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.view.NumeralBaseConverterDialog; import org.solovyev.android.calculator.view.NumeralBaseConverterDialog;
import org.solovyev.android.menu.IdentifiableMenuItem; import org.solovyev.android.menu.LabeledMenuItem;
/** /**
* User: serso * User: serso
* Date: 4/23/12 * Date: 4/23/12
* Time: 2:25 PM * Time: 2:25 PM
*/ */
enum CalculatorMenu implements IdentifiableMenuItem<MenuItem> { enum CalculatorMenu implements LabeledMenuItem<MenuItem> {
settings(R.id.main_menu_item_settings){ settings(R.string.c_settings) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
CalculatorActivityLauncher.showSettings(context); CalculatorActivityLauncher.showSettings(context);
} }
}, },
history(R.id.main_menu_item_history) { history(R.string.c_history) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
CalculatorActivityLauncher.showHistory(context); CalculatorActivityLauncher.showHistory(context);
} }
}, },
about(R.id.main_menu_item_about) { about(R.string.c_about) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
CalculatorActivityLauncher.showAbout(context); CalculatorActivityLauncher.showAbout(context);
} }
}, },
help(R.id.main_menu_item_help) { help(R.string.c_help) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
CalculatorActivityLauncher.showHelp(context); CalculatorActivityLauncher.showHelp(context);
} }
}, },
conversion_tool( R.id.main_menu_conversion_tool) { conversion_tool(R.string.c_conversion_tool) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
new NumeralBaseConverterDialog(null).show(context); new NumeralBaseConverterDialog(null).show(context);
} }
}, },
exit(R.id.main_menu_item_exit) { exit(R.string.c_exit) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
if (context instanceof Activity) { if (context instanceof Activity) {
@ -61,15 +61,15 @@ enum CalculatorMenu implements IdentifiableMenuItem<MenuItem> {
} }
}; };
private final int menuItemId; private final int captionResId;
private CalculatorMenu (int menuItemId) { private CalculatorMenu(int captionResId) {
this.menuItemId = menuItemId; this.captionResId = captionResId;
} }
@NotNull @NotNull
@Override @Override
public Integer getItemId() { public String getCaption(@NotNull Context context) {
return menuItemId; return context.getString(captionResId);
} }
} }

View File

@ -39,6 +39,11 @@ public final class CalculatorPreferences {
public static final Preference<Boolean> autoOrientation = new BooleanPreference("autoOrientation", true); public static final Preference<Boolean> autoOrientation = new BooleanPreference("autoOrientation", true);
public static final Preference<Boolean> hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true); public static final Preference<Boolean> hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true);
@NotNull
public static Theme getTheme(@NotNull SharedPreferences preferences) {
return theme.getPreferenceNoError(preferences);
}
public static enum Theme { public static enum Theme {
default_theme(ThemeType.other, R.style.default_theme), default_theme(ThemeType.other, R.style.default_theme),

View File

@ -0,0 +1,38 @@
package org.solovyev.android.fragments;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/25/12
* Time: 9:29 PM
*/
public class FragmentUtils {
public static void createFragment(@NotNull FragmentActivity activity,
@NotNull Class<? extends Fragment> fragmentClass,
int parentViewId,
@NotNull String tag) {
final FragmentManager fm = activity.getSupportFragmentManager();
Fragment messagesFragment = fm.findFragmentByTag(tag);
final FragmentTransaction ft = fm.beginTransaction();
try {
if (messagesFragment == null) {
messagesFragment = Fragment.instantiate(activity, fragmentClass.getName(), null);
ft.add(parentViewId, messagesFragment, tag);
} else {
if (messagesFragment.isDetached()) {
ft.attach(messagesFragment);
}
}
} finally {
ft.commit();
}
}
}