Fragments

This commit is contained in:
serso 2012-09-25 12:10:11 +04:00
parent 2f2914dd46
commit 5242d65e3a
12 changed files with 1970 additions and 1938 deletions

View File

@ -85,6 +85,11 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
</dependency>
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>

View File

@ -15,7 +15,10 @@
a:layout_gravity="center"
a:background="#ff000000">
<include layout="@layout/calc_editor"/>
<LinearLayout a:id="@+id/editorContainer"
a:layout_weight="2"
a:layout_width="match_parent"
a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
@ -24,10 +27,10 @@
a:layout_width="0dp"
a:layout_height="fill_parent"/>
<include layout="@layout/calc_display"
<LinearLayout a:id="@+id/displayContainer"
a:layout_weight="4"
a:layout_width="0dp"
a:layout_height="fill_parent"/>
a:layout_height="match_parent"/>
</LinearLayout>

View File

@ -7,9 +7,8 @@
-->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_weight="2"
a:layout_width="fill_parent"
a:layout_height="0dp">
a:layout_width="match_parent"
a:layout_height="wrap_content">
<org.solovyev.android.calculator.AndroidCalculatorEditorView
a:id="@+id/calculatorEditor"

View File

@ -1,7 +1,11 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.SharedPreferences;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import jscl.NumeralBase;
import jscl.math.Generic;
import org.jetbrains.annotations.NotNull;
@ -22,17 +26,44 @@ public class AndroidCalculator implements Calculator {
@NotNull
private final Calculator calculator = new CalculatorImpl();
public void init(@NotNull final Activity activity, @NotNull SharedPreferences preferences) {
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
editorView.init(preferences);
preferences.registerOnSharedPreferenceChangeListener(editorView);
CalculatorLocatorImpl.getInstance().getEditor().setView(editorView);
public static void showEvaluationError(@NotNull Context context, @NotNull final String errorMessage) {
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View errorMessageView = layoutInflater.inflate(R.layout.display_error_message, null);
((TextView) errorMessageView.findViewById(R.id.error_message_text_view)).setText(errorMessage);
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setPositiveButton(R.string.c_cancel, null)
.setView(errorMessageView);
builder.create().show();
}
public void init(@NotNull final Activity activity) {
setEditor(activity);
setDisplay(activity);
}
public void setDisplay(@NotNull Activity activity) {
final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay);
displayView.setOnClickListener(new CalculatorDisplayOnClickListener(activity));
setDisplay(activity, displayView);
}
public void setDisplay(@NotNull Context context, @NotNull AndroidCalculatorDisplayView displayView) {
displayView.init(context);
CalculatorLocatorImpl.getInstance().getDisplay().setView(displayView);
}
public void setEditor(@NotNull Activity activity) {
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
setEditor(activity, editorView);
}
public void setEditor(@NotNull Context context, @NotNull AndroidCalculatorEditorView editorView) {
editorView.init(context);
CalculatorLocatorImpl.getInstance().getEditor().setView(editorView);
}
/*
**********************************************************************

View File

@ -55,6 +55,8 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
@NotNull
private final Handler handler = new Handler();
private volatile boolean initialized = false;
/*
**********************************************************************
*
@ -170,6 +172,14 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
}
}
public synchronized void init(@NotNull Context context) {
if (!initialized) {
this.setOnClickListener(new CalculatorDisplayOnClickListener(context));
this.initialized = true;
}
}
private final class TextWatcherImpl implements TextWatcher {
@Override

View File

@ -10,6 +10,7 @@ import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
@ -33,6 +34,8 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
private static final String CALC_COLOR_DISPLAY_KEY = "org.solovyev.android.calculator.CalculatorModel_color_display";
private static final boolean CALC_COLOR_DISPLAY_DEFAULT = true;
private volatile boolean initialized = false;
private boolean highlightText = true;
@NotNull
@ -52,20 +55,16 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
public AndroidCalculatorEditorView(Context context) {
super(context);
this.addTextChangedListener(new TextWatcherImpl());
}
public AndroidCalculatorEditorView(Context context, AttributeSet attrs) {
super(context, attrs);
this.addTextChangedListener(new TextWatcherImpl());
}
public AndroidCalculatorEditorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.addTextChangedListener(new TextWatcherImpl());
}
@Override
public boolean onCheckIsTextEditor() {
// NOTE: code below can be used carefully and should not be copied without special intention
@ -138,8 +137,18 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
}
}
public void init(@NotNull SharedPreferences preferences) {
public synchronized void init(@NotNull Context context) {
if (!initialized) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.registerOnSharedPreferenceChangeListener(this);
this.addTextChangedListener(new TextWatcherImpl());
onSharedPreferenceChanged(preferences, CALC_COLOR_DISPLAY_KEY);
initialized = true;
}
}
@Override

View File

@ -7,6 +7,9 @@ package org.solovyev.android.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
@ -68,8 +71,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
private volatile boolean initialized;
@NotNull
private CalculatorPreferences.Gui.Theme theme;
@ -105,6 +106,18 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
super.onCreate(savedInstanceState);
setLayout(preferences);
final FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorEditorFragment editorFragment = new CalculatorEditorFragment();
fragmentTransaction.add(R.id.editorContainer, editorFragment);
fragmentTransaction.commit();
fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorDisplayFragment displayFragment = new CalculatorDisplayFragment();
fragmentTransaction.add(R.id.displayContainer, displayFragment);
fragmentTransaction.commit();
if (customTitleSupported) {
try {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);
@ -120,15 +133,14 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
billingObserver = new CalculatorBillingObserver(this);
BillingController.registerObserver(billingObserver);
firstTimeInit(preferences);
this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
firstTimeInit(preferences, this);
// init billing controller
BillingController.checkBillingSupported(this);
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
getCalculator().init(this, preferences);
dpclRegister.clear();
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this);
@ -207,7 +219,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
toggleOrientationChange(preferences);
toggleEqualsButton(preferences);
// todo serso: continue
//toggleEqualsButton(preferences);
preferences.registerOnSharedPreferenceChangeListener(this);
}
@ -263,14 +276,16 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
int orientation = AndroidUtils.getScreenOrientation(this);
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
setMarginsForView(equalsButton, marginLeft, marginBottom);
setMarginsForView(getCalculatorDisplayView(), marginLeft, marginBottom);
// todo serso: continue
//setMarginsForView(getCalculatorDisplayView(), marginLeft, marginBottom);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setMarginsForView(leftButton, marginLeft, marginBottom);
setMarginsForView(eraseButton, marginLeft, marginBottom);
setMarginsForView(clearButton, marginLeft, marginBottom);
setMarginsForView(rightButton, marginLeft, marginBottom);
// magic magic magic
setMarginsForView(getCalculatorDisplayView(), 3 * marginLeft, marginBottom);
// todo serso: continue
//setMarginsForView(getCalculatorDisplayView(), 3 * marginLeft, marginBottom);
}
}
@ -423,14 +438,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private synchronized void setTheme(@NotNull SharedPreferences preferences) {
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
setTheme(theme.getThemeId());
}
private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) {
if (!initialized) {
this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
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);
@ -438,14 +449,14 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences);
final int appVersion = AndroidUtils.getAppVersionCode(this, CalculatorActivity.class.getPackage().getName());
final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName());
CalculatorPreferences.appVersion.putPreference(preferences, appVersion);
boolean dialogShown = false;
if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
// new start
final AlertDialog.Builder builder = new AlertDialog.Builder(this).setMessage(R.string.c_first_start_text);
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text);
builder.setPositiveButton(android.R.string.ok, null);
builder.setTitle(R.string.c_first_start_text_title);
builder.create().show();
@ -454,9 +465,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
if (savedVersion < appVersion) {
final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences);
if (showReleaseNotes) {
final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(this, savedVersion + 1);
final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1);
if (!StringUtils.isEmpty(releaseNotes)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this).setMessage(Html.fromHtml(releaseNotes));
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
builder.setPositiveButton(android.R.string.ok, null);
builder.setTitle(R.string.c_release_notes);
builder.create().show();
@ -469,31 +480,28 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
//Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!");
if (!dialogShown) {
if ( appOpenedCounter != null && appOpenedCounter > 10 ) {
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText);
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);
}
initialized = true;
if (!dialogShown) {
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context);
}
}
private boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId) {
private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) {
boolean result = false;
final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences);
if ( specialWindowShown != null && !specialWindowShown ) {
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
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(this).setView(view);
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view);
builder.setPositiveButton(android.R.string.ok, null);
builder.create().show();

View File

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

View File

@ -1,6 +1,6 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.menu.AMenuBuilder;
@ -17,10 +17,10 @@ import java.util.List;
public class CalculatorDisplayOnClickListener implements View.OnClickListener {
@NotNull
private final Activity activity;
private final Context context;
public CalculatorDisplayOnClickListener(@NotNull Activity activity) {
this.activity = activity;
public CalculatorDisplayOnClickListener(@NotNull Context context) {
this.context = context;
}
@Override
@ -39,13 +39,13 @@ public class CalculatorDisplayOnClickListener implements View.OnClickListener {
}
if (!filteredMenuItems.isEmpty()) {
AMenuBuilder.newInstance(activity, MenuImpl.newInstance(filteredMenuItems)).create(displayViewState).show();
AMenuBuilder.newInstance(context, MenuImpl.newInstance(filteredMenuItems)).create(displayViewState).show();
}
} else {
final String errorMessage = displayViewState.getErrorMessage();
if (errorMessage != null) {
CalculatorModel.showEvaluationError(activity, errorMessage);
AndroidCalculator.showEvaluationError(context, errorMessage);
}
}
}

View File

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

View File

@ -1,103 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
*/
package org.solovyev.android.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.gui.CursorControl;
/**
* User: serso
* Date: 9/12/11
* Time: 11:15 PM
*/
public enum CalculatorModel implements CalculatorEngineControl, CursorControl {
instance;
@NotNull
private final CalculatorEditor editor;
@NotNull
private final CalculatorDisplay display;
private CalculatorModel() {
display = CalculatorLocatorImpl.getInstance().getDisplay();
editor = CalculatorLocatorImpl.getInstance().getEditor();
}
public CalculatorModel attachViews(@NotNull final Activity activity, @NotNull SharedPreferences preferences) {
Log.d(this.getClass().getName(), "CalculatorModel initialization with activity: " + activity);
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
editorView.init(preferences);
preferences.registerOnSharedPreferenceChangeListener(editorView);
editor.setView(editorView);
final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay);
displayView.setOnClickListener(new CalculatorDisplayOnClickListener(activity));
display.setView(displayView);
return this;
}
public static void showEvaluationError(@NotNull Activity activity, @NotNull final String errorMessage) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View errorMessageView = layoutInflater.inflate(R.layout.display_error_message, null);
((TextView) errorMessageView.findViewById(R.id.error_message_text_view)).setText(errorMessage);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setPositiveButton(R.string.c_cancel, null)
.setView(errorMessageView);
builder.create().show();
}
@Override
public void setCursorOnStart() {
this.editor.setCursorOnStart();
}
@Override
public void setCursorOnEnd() {
this.editor.setCursorOnEnd();
}
@Override
public void moveCursorLeft() {
this.editor.moveCursorLeft();
}
@Override
public void moveCursorRight() {
this.editor.moveCursorRight();
}
@Override
public void evaluate() {
CalculatorLocatorImpl.getInstance().getCalculator().evaluate(JsclOperation.numeric, this.editor.getViewState().getText());
}
@Override
public void simplify() {
CalculatorLocatorImpl.getInstance().getCalculator().evaluate(JsclOperation.simplify, this.editor.getViewState().getText());
}
@NotNull
public CalculatorDisplay getDisplay() {
return display;
}
}

View File

@ -109,6 +109,12 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>r7</version>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>