Fragments

This commit is contained in:
Sergey Solovyev 2012-09-26 21:10:18 +04:00
parent ff1dd19018
commit 5371dbaac1
7 changed files with 1037 additions and 953 deletions

View File

@ -1,31 +1,83 @@
<?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="horizontal" a:orientation="horizontal"
a:layout_gravity="center" a:layout_gravity="center"
a:background="@color/default_background" a:background="@color/default_background"
a:baselineAligned="false"> a:baselineAligned="false">
<include layout="@layout/main_first_pane" <LinearLayout
a:layout_height="match_parent" a:layout_height="match_parent"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_weight="1"/> a:layout_weight="1"
a:orientation="vertical"
a:layout_gravity="center">
<LinearLayout a:id="@+id/main_second_pane"
a:orientation="vertical" <LinearLayout a:id="@+id/editorContainer"
a:layout_height="match_parent" a:layout_weight="4"
a:layout_width="0dp" a:layout_width="match_parent"
a:layout_weight="1"/> a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1"
</LinearLayout> a:layout_width="match_parent"
a:layout_height="0dp">
<include layout="@layout/calc_left_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<include layout="@layout/calc_erase_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<LinearLayout a:id="@+id/displayContainer"
a:layout_margin="@dimen/display_margin"
a:layout_weight="4"
a:layout_width="0dp"
a:layout_height="match_parent"/>
<include layout="@layout/calc_clear_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
<include layout="@layout/calc_right_button"
a:layout_margin="@dimen/button_margin"
a:layout_width="0dp"
a:layout_height="match_parent"
a:layout_weight="1"/>
</LinearLayout>
<LinearLayout a:id="@+id/keyboardContainer"
a:layout_weight="3"
a:layout_width="match_parent"
a:layout_height="0dp"/>
</LinearLayout>
<LinearLayout a:id="@+id/main_second_pane"
a:orientation="vertical"
a:layout_height="match_parent"
a:layout_width="0dp"
a:layout_weight="1"/>
</LinearLayout>

View File

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

View File

@ -1,152 +1,169 @@
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.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 com.actionbarsherlock.app.ActionBar; import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.ActionBar;
import org.jetbrains.annotations.NotNull; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.AndroidUtils; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.fragments.FragmentUtils; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener; import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* User: serso * User: serso
* Date: 9/25/12 * Date: 9/25/12
* Time: 10:32 PM * Time: 10:32 PM
*/ */
public class CalculatorActivityHelperImpl implements CalculatorActivityHelper { public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
/* /*
********************************************************************** **********************************************************************
* *
* CONSTANTS * CONSTANTS
* *
********************************************************************** **********************************************************************
*/ */
private static final String SELECTED_NAV = "selected_nav"; private static final String SELECTED_NAV = "selected_nav";
/* /*
********************************************************************** **********************************************************************
* *
* FIELDS * FIELDS
* *
********************************************************************** **********************************************************************
*/ */
private int layoutId; private int layoutId;
private boolean homeIcon = false; private boolean homeIcon = false;
@NotNull @NotNull
private final List<String> fragmentTags = new ArrayList<String>(); private final List<String> fragmentTags = new ArrayList<String>();
@NotNull @NotNull
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
private int navPosition = 0; private int navPosition = 0;
public CalculatorActivityHelperImpl(int layoutId) { @NotNull
this.layoutId = layoutId; private String logTag = "CalculatorActivity";
}
public CalculatorActivityHelperImpl(int layoutId, @NotNull String logTag) {
public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) { this.layoutId = layoutId;
this.layoutId = layoutId; this.logTag = logTag;
this.homeIcon = homeIcon; }
}
public CalculatorActivityHelperImpl(int layoutId, boolean homeIcon) {
@Override this.layoutId = layoutId;
public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) { this.homeIcon = homeIcon;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); }
this.theme = CalculatorPreferences.Gui.getTheme(preferences); @Override
activity.setTheme(this.theme.getThemeId()); public void onCreate(@NotNull Activity activity, @Nullable Bundle savedInstanceState) {
Log.d(logTag + ": helper", "onCreate");
activity.setContentView(layoutId);
} final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
@Override this.theme = CalculatorPreferences.Gui.getTheme(preferences);
public void onCreate(@NotNull final SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState) { activity.setTheme(this.theme.getThemeId());
this.onCreate((Activity) activity, savedInstanceState);
activity.setContentView(layoutId);
final ActionBar actionBar = activity.getSupportActionBar(); }
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(homeIcon); @Override
actionBar.setDisplayShowHomeEnabled(true); public void onCreate(@NotNull final SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState) {
actionBar.setDisplayShowTitleEnabled(true); this.onCreate((Activity) activity, savedInstanceState);
if (savedInstanceState != null) { final ActionBar actionBar = activity.getSupportActionBar();
navPosition = savedInstanceState.getInt(SELECTED_NAV, 0); actionBar.setDisplayUseLogoEnabled(false);
} actionBar.setDisplayHomeAsUpEnabled(homeIcon);
} actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
@Override
public void restoreSavedTab(@NotNull SherlockFragmentActivity activity) { if (savedInstanceState != null) {
final ActionBar actionBar = activity.getSupportActionBar(); navPosition = savedInstanceState.getInt(SELECTED_NAV, 0);
if (navPosition >= 0 && navPosition < actionBar.getTabCount()) { }
activity.getSupportActionBar().setSelectedNavigationItem(navPosition); }
}
} @Override
public void restoreSavedTab(@NotNull SherlockFragmentActivity activity) {
@Override final ActionBar actionBar = activity.getSupportActionBar();
public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) { if (navPosition >= 0 && navPosition < actionBar.getTabCount()) {
FragmentUtils.detachFragments(activity, fragmentTags); activity.getSupportActionBar().setSelectedNavigationItem(navPosition);
}
onSaveInstanceState((Activity) activity, outState); }
outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex());
} @Override
public void logDebug(@NotNull String message) {
@Override Log.d(logTag, message);
public void onSaveInstanceState(@NotNull Activity activity, @NotNull Bundle outState) { }
}
@Override
@Override public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) {
public void onResume(@NotNull Activity activity) { onSaveInstanceState((Activity) activity, outState);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex());
}
final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences);
if (!theme.equals(newTheme)) { @Override
AndroidUtils.restartActivity(activity); public void onSaveInstanceState(@NotNull Activity activity, @NotNull Bundle outState) {
} }
}
@Override
@Override public void onResume(@NotNull Activity activity) {
public void addTab(@NotNull SherlockFragmentActivity activity, final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
@NotNull String tag,
@NotNull Class<? extends Fragment> fragmentClass, final CalculatorPreferences.Gui.Theme newTheme = CalculatorPreferences.Gui.theme.getPreference(preferences);
@Nullable Bundle fragmentArgs, if (!theme.equals(newTheme)) {
int captionResId, AndroidUtils.restartActivity(activity);
int parentViewId) { }
activity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); }
final ActionBar actionBar = activity.getSupportActionBar(); @Override
final ActionBar.Tab tab = actionBar.newTab(); public void onDestroy(@NotNull SherlockFragmentActivity activity) {
tab.setTag(tag); }
tab.setText(captionResId);
tab.setTabListener(new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId)); @Override
actionBar.addTab(tab); public void onPause(@NotNull SherlockFragmentActivity activity) {
}
fragmentTags.add(tag);
} @Override
public void addTab(@NotNull SherlockFragmentActivity activity,
@Override @NotNull String tag,
public int getLayoutId() { @NotNull Class<? extends Fragment> fragmentClass,
return layoutId; @Nullable Bundle fragmentArgs,
} int captionResId,
int parentViewId) {
@Override activity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
@NotNull
public CalculatorPreferences.Gui.Theme getTheme() { final ActionBar actionBar = activity.getSupportActionBar();
return theme; final ActionBar.Tab tab = actionBar.newTab();
} tab.setTag(tag);
tab.setText(captionResId);
@Override tab.setTabListener(new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, parentViewId));
public void onResume(@NotNull SherlockFragmentActivity activity) { actionBar.addTab(tab);
onResume((Activity) activity);
} fragmentTags.add(tag);
} }
@Override
public int getLayoutId() {
return layoutId;
}
@Override
@NotNull
public CalculatorPreferences.Gui.Theme getTheme() {
return theme;
}
@Override
public void onResume(@NotNull SherlockFragmentActivity activity) {
onResume((Activity) activity);
}
}

View File

@ -111,7 +111,7 @@ public class CalculatorApplication extends android.app.Application {
} }
@NotNull @NotNull
public CalculatorActivityHelper createCalculatorHistoryHelper(int layoutResId) { public CalculatorActivityHelper createCalculatorHistoryHelper(int layoutResId, @NotNull String logTag) {
return new CalculatorActivityHelperImpl(layoutResId); return new CalculatorActivityHelperImpl(layoutResId, logTag);
} }
} }

View File

@ -1,285 +1,307 @@
/* /*
* 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
*/ */
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.util.Log;
import android.view.View; import android.view.LayoutInflater;
import android.view.ViewGroup; import android.view.View;
import android.widget.AdapterView; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ArrayAdapter;
import com.actionbarsherlock.app.SherlockListFragment; import android.widget.ListView;
import com.google.ads.AdView; import com.actionbarsherlock.app.SherlockListFragment;
import org.jetbrains.annotations.NotNull; import com.google.ads.AdView;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.ads.AdsController; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.*;
import org.solovyev.android.menu.AMenuBuilder; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.menu.MenuImpl; import org.solovyev.android.menu.AMenuBuilder;
import org.solovyev.common.collections.CollectionsUtils; import org.solovyev.android.menu.MenuImpl;
import org.solovyev.common.equals.Equalizer; import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.filter.Filter; import org.solovyev.common.equals.Equalizer;
import org.solovyev.common.filter.FilterRule; import org.solovyev.common.filter.Filter;
import org.solovyev.common.filter.FilterRulesChain; import org.solovyev.common.filter.FilterRule;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.filter.FilterRulesChain;
import org.solovyev.common.text.StringUtils;
import java.util.ArrayList;
import java.util.Collections; import java.util.ArrayList;
import java.util.Comparator; import java.util.Collections;
import java.util.List; import java.util.Comparator;
import java.util.List;
/**
* User: serso /**
* Date: 10/15/11 * User: serso
* Time: 1:13 PM * Date: 10/15/11
*/ * Time: 1:13 PM
public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { */
public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener {
public static final Comparator<CalculatorHistoryState> COMPARATOR = new Comparator<CalculatorHistoryState>() { @NotNull
@Override private static final String TAG = "CalculatorHistoryFragment";
public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) {
if (state1.isSaved() == state2.isSaved()) { public static final Comparator<CalculatorHistoryState> COMPARATOR = new Comparator<CalculatorHistoryState>() {
long l = state2.getTime() - state1.getTime(); @Override
return l > 0l ? 1 : (l < 0l ? -1 : 0); public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) {
} else if (state1.isSaved()) { if (state1.isSaved() == state2.isSaved()) {
return -1; long l = state2.getTime() - state1.getTime();
} else if (state2.isSaved()) { return l > 0l ? 1 : (l < 0l ? -1 : 0);
return 1; } else if (state1.isSaved()) {
} return -1;
return 0; } else if (state2.isSaved()) {
} return 1;
}; }
return 0;
}
@NotNull };
private ArrayAdapter<CalculatorHistoryState> adapter;
@Nullable @NotNull
private AdView adView; private ArrayAdapter<CalculatorHistoryState> adapter;
@Override @Nullable
public void onCreate(Bundle savedInstanceState) { private AdView adView;
super.onCreate(savedInstanceState);
} @Override
public void onCreate(Bundle savedInstanceState) {
@Override super.onCreate(savedInstanceState);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.history_fragment, container, false); logDebug("onCreate");
} }
@Override private int logDebug(@NotNull String msg) {
public void onViewCreated(View view, Bundle savedInstanceState) { return Log.d(TAG + ": " + getTag(), msg);
super.onViewCreated(view, savedInstanceState); }
adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>()); @Override
setListAdapter(adapter); public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View result = inflater.inflate(R.layout.history_fragment, container, false);
final ListView lv = getListView(); logDebug("onCreateView");
lv.setTextFilterEnabled(true); return result;
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, @Override
final View view, public void onViewCreated(View view, Bundle savedInstanceState) {
final int position, super.onViewCreated(view, savedInstanceState);
final long id) {
logDebug("onViewCreated");
useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position));
} adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
}); setListAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { final ListView lv = getListView();
@Override lv.setTextFilterEnabled(true);
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent,
final Context context = getActivity(); final View view,
final int position,
final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter); final long id) {
final List<HistoryItemMenuItem> menuItems = CollectionsUtils.asList(HistoryItemMenuItem.values()); useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position));
}
if (historyState.isSaved()) { });
menuItems.remove(HistoryItemMenuItem.save);
} else { lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
if (isAlreadySaved(historyState)) { @Override
menuItems.remove(HistoryItemMenuItem.save); public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
} final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position);
menuItems.remove(HistoryItemMenuItem.remove);
menuItems.remove(HistoryItemMenuItem.edit); final Context context = getActivity();
}
final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter);
if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) {
menuItems.remove(HistoryItemMenuItem.copy_result); final List<HistoryItemMenuItem> menuItems = CollectionsUtils.asList(HistoryItemMenuItem.values());
}
if (historyState.isSaved()) {
final AMenuBuilder<HistoryItemMenuItem, HistoryItemMenuData> menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems)); menuItems.remove(HistoryItemMenuItem.save);
menuBuilder.create(data).show(); } else {
if (isAlreadySaved(historyState)) {
return true; menuItems.remove(HistoryItemMenuItem.save);
} }
}); menuItems.remove(HistoryItemMenuItem.remove);
menuItems.remove(HistoryItemMenuItem.edit);
adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view); }
}
if (historyState.getDisplayState().isValid() && StringUtils.isEmpty(historyState.getDisplayState().getEditorState().getText())) {
@Override menuItems.remove(HistoryItemMenuItem.copy_result);
public void onDestroy() { }
if ( this.adView != null ) {
this.adView.destroy(); final AMenuBuilder<HistoryItemMenuItem, HistoryItemMenuData> menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems));
} menuBuilder.create(data).show();
super.onDestroy();
} return true;
}
protected abstract int getItemLayoutId(); });
@Override adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view);
public void onResume() { }
super.onResume();
@Override
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); public void onDestroy() {
logDebug("onDestroy");
updateAdapter();
} if ( this.adView != null ) {
this.adView.destroy();
@Override }
public void onPause() { super.onDestroy();
super.onPause(); }
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); protected abstract int getItemLayoutId();
} @Override
public void onResume() {
private void updateAdapter() { logDebug("onResume");
final List<CalculatorHistoryState> historyList = getHistoryList();
super.onResume();
final ArrayAdapter<CalculatorHistoryState> adapter = getAdapter();
try {
adapter.setNotifyOnChange(false); CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
adapter.clear();
for (CalculatorHistoryState historyState : historyList) { updateAdapter();
adapter.add(historyState); }
}
} finally { @Override
adapter.setNotifyOnChange(true); public void onPause() {
} logDebug("onPause");
adapter.notifyDataSetChanged(); super.onPause();
}
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) {
assert !historyState.isSaved(); }
boolean result = false; private void updateAdapter() {
try { final List<CalculatorHistoryState> historyList = getHistoryList();
historyState.setSaved(true);
if ( CollectionsUtils.contains(historyState, CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory(), new Equalizer<CalculatorHistoryState>() { final ArrayAdapter<CalculatorHistoryState> adapter = getAdapter();
@Override try {
public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { adapter.setNotifyOnChange(false);
return first != null && second != null && adapter.clear();
first.getTime() == second.getTime() && for (CalculatorHistoryState historyState : historyList) {
first.getDisplayState().equals(second.getDisplayState()) && adapter.add(historyState);
first.getEditorState().equals(second.getEditorState()); }
} } finally {
}) ) { adapter.setNotifyOnChange(true);
result = true; }
}
} finally { adapter.notifyDataSetChanged();
historyState.setSaved(false); }
}
return result; public static boolean isAlreadySaved(@NotNull CalculatorHistoryState historyState) {
} assert !historyState.isSaved();
public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState) { boolean result = false;
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); try {
} historyState.setSaved(true);
if ( CollectionsUtils.contains(historyState, CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory(), new Equalizer<CalculatorHistoryState>() {
@NotNull @Override
private List<CalculatorHistoryState> getHistoryList() { public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) {
final List<CalculatorHistoryState> calculatorHistoryStates = getHistoryItems(); return first != null && second != null &&
first.getTime() == second.getTime() &&
Collections.sort(calculatorHistoryStates, COMPARATOR); first.getDisplayState().equals(second.getDisplayState()) &&
first.getEditorState().equals(second.getEditorState());
final FilterRulesChain<CalculatorHistoryState> filterRulesChain = new FilterRulesChain<CalculatorHistoryState>(); }
filterRulesChain.addFilterRule(new FilterRule<CalculatorHistoryState>() { }) ) {
@Override result = true;
public boolean isFiltered(CalculatorHistoryState object) { }
return object == null || StringUtils.isEmpty(object.getEditorState().getText()); } finally {
} historyState.setSaved(false);
}); }
return result;
new Filter<CalculatorHistoryState>(filterRulesChain).filter(calculatorHistoryStates.iterator()); }
return calculatorHistoryStates; public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState) {
} CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState);
}
@NotNull
protected abstract List<CalculatorHistoryState> getHistoryItems(); @NotNull
private List<CalculatorHistoryState> getHistoryList() {
@NotNull final List<CalculatorHistoryState> calculatorHistoryStates = getHistoryItems();
public static String getHistoryText(@NotNull CalculatorHistoryState state) {
final StringBuilder result = new StringBuilder(); Collections.sort(calculatorHistoryStates, COMPARATOR);
result.append(state.getEditorState().getText());
result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); final FilterRulesChain<CalculatorHistoryState> filterRulesChain = new FilterRulesChain<CalculatorHistoryState>();
final String expressionResult = state.getDisplayState().getEditorState().getText(); filterRulesChain.addFilterRule(new FilterRule<CalculatorHistoryState>() {
if (expressionResult != null) { @Override
result.append(expressionResult); public boolean isFiltered(CalculatorHistoryState object) {
} return object == null || StringUtils.isEmpty(object.getEditorState().getText());
return result.toString(); }
} });
@NotNull new Filter<CalculatorHistoryState>(filterRulesChain).filter(calculatorHistoryStates.iterator());
private static String getIdentitySign(@NotNull JsclOperation jsclOperation) {
return jsclOperation == JsclOperation.simplify ? "" : "="; return calculatorHistoryStates;
} }
// todo serso: menu @NotNull
/* @Override protected abstract List<CalculatorHistoryState> getHistoryItems();
public boolean onCreateOptionsMenu(android.view.Menu menu) {
final MenuInflater menuInflater = getMenuInflater(); @NotNull
menuInflater.inflate(R.menu.history_menu, menu); public static String getHistoryText(@NotNull CalculatorHistoryState state) {
return true; final StringBuilder result = new StringBuilder();
} result.append(state.getEditorState().getText());
result.append(getIdentitySign(state.getDisplayState().getJsclOperation()));
@Override final String expressionResult = state.getDisplayState().getEditorState().getText();
public boolean onOptionsItemSelected(MenuItem item) { if (expressionResult != null) {
boolean result; result.append(expressionResult);
}
switch (item.getItemId()) { return result.toString();
case R.id.history_menu_clear_history: }
clearHistory();
result = true; @NotNull
break; private static String getIdentitySign(@NotNull JsclOperation jsclOperation) {
default: return jsclOperation == JsclOperation.simplify ? "" : "=";
result = super.onOptionsItemSelected(item); }
}
// todo serso: menu
return result; /* @Override
}*/ public boolean onCreateOptionsMenu(android.view.Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
protected abstract void clearHistory(); menuInflater.inflate(R.menu.history_menu, menu);
return true;
@NotNull }
protected ArrayAdapter<CalculatorHistoryState> getAdapter() {
return adapter; @Override
} public boolean onOptionsItemSelected(MenuItem item) {
boolean result;
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { switch (item.getItemId()) {
if ( calculatorEventType == CalculatorEventType.history_state_added ) { case R.id.history_menu_clear_history:
getActivity().runOnUiThread(new Runnable() { clearHistory();
@Override result = true;
public void run() { break;
updateAdapter(); default:
} result = super.onOptionsItemSelected(item);
}); }
}
} return result;
} }*/
protected abstract void clearHistory();
@NotNull
protected ArrayAdapter<CalculatorHistoryState> getAdapter() {
return adapter;
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
if ( calculatorEventType == CalculatorEventType.history_state_added ) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
logDebug("onCalculatorEvent");
updateAdapter();
}
});
}
}
}

View File

@ -1,64 +1,64 @@
/* /*
* 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
*/ */
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import android.os.Bundle; import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
/** /**
* User: serso * User: serso
* Date: 12/18/11 * Date: 12/18/11
* Time: 7:37 PM * Time: 7:37 PM
*/ */
public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull @NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(R.layout.main_empty); private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.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); activityHelper.onCreate(this, savedInstanceState);
activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_layout); activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_layout);
activityHelper.addTab(this, "saved_history", CalculatorSavedHistoryFragment.class, null, R.string.c_saved_history, R.id.main_layout); activityHelper.addTab(this, "saved_history", CalculatorSavedHistoryFragment.class, null, R.string.c_saved_history, R.id.main_layout);
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this); CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
} }
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState); activityHelper.onSaveInstanceState(this, outState);
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
activityHelper.onResume(this); activityHelper.onResume(this);
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this); CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
} }
@Override @Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) { public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
if ( calculatorEventType == CalculatorEventType.use_history_state ) { if ( calculatorEventType == CalculatorEventType.use_history_state ) {
this.finish(); this.finish();
} }
} }
} }