Fragments

This commit is contained in:
Sergey Solovyev 2012-09-25 23:06:08 +04:00
parent 631ea40c15
commit c6c2682362
16 changed files with 782 additions and 539 deletions

View File

@ -9,7 +9,7 @@
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8"/>
<application android:debuggable="true" android:hardwareAccelerated="false" android:icon="@drawable/icon" android:label="@string/c_app_name" android:name=".CalculatorApplication">
<application android:debuggable="true" android:hardwareAccelerated="false" android:icon="@drawable/icon" android:label="@string/c_app_name" android:name=".CalculatorApplication" android:theme="@style/metro_blue_theme">
<activity android:label="@string/c_app_name" android:name=".CalculatorActivity" android:windowSoftInputMode="adjustPan">
@ -23,11 +23,11 @@
<!--NOTE: a:configChanges="orientation|keyboardHidden" is needed to correct work of dialog windows (not to close them on orientation change) -->
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_settings" android:name=".CalculatorPreferencesActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_history" android:name=".history.CalculatorHistoryTabActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_history" android:name=".history.CalculatorHistoryFragmentActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_history" android:name=".history.HistoryActivityTab"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_history" android:name=".history.CalculatorHistoryFragment"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_saved_history" android:name=".history.SavedHistoryActivityTab"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_saved_history" android:name=".history.CalculatorSavedHistoryFragment"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_about" android:name=".about.CalculatorAboutTabActivity"/>

View File

@ -72,6 +72,14 @@
<type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId>
<artifactId>android-common-sherlock</artifactId>
<version>1.0.0</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev</groupId>
<artifactId>jscl</artifactId>
@ -90,6 +98,12 @@
<artifactId>support-v4</artifactId>
</dependency>
<dependency>
<groupId>com.actionbarsherlock</groupId>
<artifactId>library</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>

View File

@ -15,5 +15,8 @@ android.library.reference.3=gen-external-apklibs/org.solovyev.android_android-co
android.library.reference.4=gen-external-apklibs/org.solovyev.android_android-common-preferences_1.0.0
android.library.reference.5=gen-external-apklibs/org.solovyev.android_android-common-other_1.0.0
android.library.reference.6=gen-external-apklibs/org.solovyev.android_android-common-menu_1.0.0
android.library.reference.7=gen-external-apklibs/org.solovyev.android_android-common-sherlock_1.0.0
android.library.reference.8=gen-external-apklibs/com.actionbarsherlock_library_4.0.2
android.library.reference.9=gen-external-apklibs/org.solovyev.android_android-common-list_1.0.0

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org
-->
<LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:id="@+id/main_layout"
a:orientation="horizontal"
a:layout_gravity="center"
a:background="@color/default_background"
a:baselineAligned="false">
<LinearLayout a:orientation="vertical"
a:layout_height="match_parent"
a:layout_width="0dp"
a:layout_weight="1">
<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="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/content_second_pane"
a:orientation="vertical"
a:layout_height="match_parent"
a:layout_width="0dp"
a:layout_weight="1"/>
</LinearLayout>

View File

@ -16,7 +16,7 @@
<item name="android:padding">6dp</item>
</style>
<style name="metro_blue_theme">
<style name="metro_blue_theme" parent="Theme.Sherlock">
<item name="digitButtonStyle">@style/metro_digit_button_style</item>
<item name="controlButtonStyle">@style/metro_control_button_style</item>
<item name="controlImageButtonStyle">@style/metro_control_image_button_style</item>

View File

@ -0,0 +1,19 @@
package org.solovyev.android.calculator;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 9/25/12
* Time: 10:31 PM
*/
public interface CalculatorActivityHelper {
void onCreate(@NotNull SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState);
void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState);
}

View File

@ -0,0 +1,121 @@
package org.solovyev.android.calculator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.history.CalculatorHistoryFragment;
import org.solovyev.android.sherlock.tabs.ActionBarFragmentTabListener;
/**
* User: serso
* Date: 9/25/12
* Time: 10:32 PM
*/
public class CalculatorActivityHelperImpl implements CalculatorActivityHelper {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
private static final String SELECTED_NAV = "selected_nav";
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
private int layoutId;
private boolean showActionBarTabs = true;
private boolean homeIcon = false;
public CalculatorActivityHelperImpl(int layoutId) {
this.layoutId = layoutId;
}
public CalculatorActivityHelperImpl(int layoutId, boolean showActionBarTabs, boolean homeIcon) {
this.layoutId = layoutId;
this.showActionBarTabs = showActionBarTabs;
this.homeIcon = homeIcon;
}
@Override
public void onCreate(@NotNull final SherlockFragmentActivity activity, @Nullable Bundle savedInstanceState) {
activity.setContentView(layoutId);
final ActionBar actionBar = activity.getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(homeIcon);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
if (showActionBarTabs) {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
addTab(activity, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.drawable.icon);
//addTab(activity, "messages", MessengerChatsFragment.class, null, R.string.c_messages, R.drawable.msg_footer_messages_icon);
// settings tab
final ActionBar.Tab tab = actionBar.newTab();
tab.setTag("settings");
tab.setText(R.string.c_settings);
//tab.setIcon(R.drawable.msg_footer_settings_icon);
tab.setTabListener(new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
activity.startActivity(new Intent(activity.getApplicationContext(), CalculatorPreferencesActivity.class));
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
});
actionBar.addTab(tab);
int navPosition = -1;
if (savedInstanceState != null) {
navPosition = savedInstanceState.getInt(SELECTED_NAV, -1);
}
if (navPosition >= 0) {
activity.getSupportActionBar().setSelectedNavigationItem(navPosition);
}
}
}
@Override
public void onSaveInstanceState(@NotNull SherlockFragmentActivity activity, @NotNull Bundle outState) {
outState.putInt(SELECTED_NAV, activity.getSupportActionBar().getSelectedNavigationIndex());
}
private void addTab(@NotNull SherlockFragmentActivity activity,
@NotNull String tag,
@NotNull Class<? extends Fragment> fragmentClass,
@Nullable Bundle fragmentArgs,
int captionResId,
int iconResId) {
final ActionBar actionBar = activity.getSupportActionBar();
final ActionBar.Tab tab = actionBar.newTab();
tab.setTag(tag);
tab.setText(captionResId);
//tab.setIcon(iconResId);
tab.setTabListener(new ActionBarFragmentTabListener(activity, tag, fragmentClass, fragmentArgs, R.id.content_second_pane));
actionBar.addTab(tab);
}
}

View File

@ -9,7 +9,7 @@ import org.achartengine.ChartFactory;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.about.CalculatorAboutTabActivity;
import org.solovyev.android.calculator.help.CalculatorHelpTabActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryTabActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity;
import org.solovyev.android.calculator.math.edit.CalculatorOperatorsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity;
@ -25,7 +25,7 @@ import org.solovyev.common.text.StringUtils;
public class CalculatorActivityLauncher {
public static void showHistory(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorHistoryTabActivity.class));
context.startActivity(new Intent(context, CalculatorHistoryFragmentActivity.class));
}
public static void showHelp(@NotNull final Context context) {

View File

@ -109,4 +109,9 @@ public class CalculatorApplication extends android.app.Application {
public static void registerOnRemoteStackTrace() {
//Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(null, REMOTE_STACK_TRACE_URL));
}
@NotNull
public CalculatorActivityHelper createCalculatorHistoryHelper(int layoutResId) {
return new CalculatorActivityHelperImpl(layoutResId);
}
}

View File

@ -6,15 +6,16 @@
package org.solovyev.android.calculator.history;
import android.app.ListActivity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
import com.google.ads.AdView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -41,7 +42,8 @@ import java.util.List;
* Date: 10/15/11
* Time: 1:13 PM
*/
public abstract class AbstractHistoryActivity extends ListActivity {
public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment {
public static final Comparator<CalculatorHistoryState> COMPARATOR = new Comparator<CalculatorHistoryState>() {
@Override
@ -66,14 +68,17 @@ public abstract class AbstractHistoryActivity extends ListActivity {
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.history_activity, null);
}
setContentView(R.layout.history_activity);
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adView = AdsController.getInstance().inflateAd(this);
adView = AdsController.getInstance().inflateAd(this.getActivity());
adapter = new HistoryArrayAdapter(this, getLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
adapter = new HistoryArrayAdapter(this.getActivity(), getLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
setListAdapter(adapter);
final ListView lv = getListView();
@ -85,7 +90,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
final int position,
final long id) {
useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position), AbstractHistoryActivity.this);
useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position), getActivity());
}
});
@ -94,7 +99,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position);
final Context context = AbstractHistoryActivity.this;
final Context context = getActivity();
final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter);
@ -123,7 +128,12 @@ public abstract class AbstractHistoryActivity extends ListActivity {
}
@Override
protected void onDestroy() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
if ( this.adView != null ) {
this.adView.destroy();
}
@ -133,7 +143,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
protected abstract int getLayoutId();
@Override
protected void onResume() {
public void onResume() {
super.onResume();
final List<CalculatorHistoryState> historyList = getHistoryList();
@ -173,7 +183,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
return result;
}
public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState, @NotNull AbstractHistoryActivity activity) {
public static void useHistoryItem(@NotNull final CalculatorHistoryState historyState, @NotNull Activity activity) {
final EditorHistoryState editorState = historyState.getEditorState();
CalculatorLocatorImpl.getInstance().getEditor().setText(StringUtils.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
@ -219,7 +229,8 @@ public abstract class AbstractHistoryActivity extends ListActivity {
return jsclOperation == JsclOperation.simplify ? "" : "=";
}
@Override
// todo serso: menu
/* @Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.history_menu, menu);
@ -240,7 +251,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
}
return result;
}
}*/
protected abstract void clearHistory();

View File

@ -18,7 +18,8 @@ import java.util.List;
* Date: 12/18/11
* Time: 7:39 PM
*/
public class HistoryActivityTab extends AbstractHistoryActivity {
public class CalculatorHistoryFragment extends AbstractCalculatorHistoryFragment {
@Override
protected int getLayoutId() {
return R.layout.history;

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.history;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorActivityHelper;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.R;
/**
* User: serso
* Date: 12/18/11
* Time: 7:37 PM
*/
public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity {
@NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(R.layout.history_activity);
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
}

View File

@ -1,50 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.history;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.LastTabSaver;
import org.solovyev.android.calculator.R;
/**
* User: serso
* Date: 12/18/11
* Time: 7:37 PM
*/
public class CalculatorHistoryTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
final TabHost tabHost = getTabHost();
AndroidUtils.addTab(this, tabHost, "saved_history", R.string.c_saved_history, SavedHistoryActivityTab.class);
AndroidUtils.addTab(this, tabHost, "history", R.string.c_history, HistoryActivityTab.class);
this.lastTabSaver = new LastTabSaver(this, "saved_history");
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if ( this.lastTabSaver != null ) {
this.lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -18,7 +18,8 @@ import java.util.List;
* Date: 12/18/11
* Time: 7:40 PM
*/
public class SavedHistoryActivityTab extends AbstractHistoryActivity {
public class CalculatorSavedHistoryFragment extends AbstractCalculatorHistoryFragment {
@Override
protected int getLayoutId() {
return R.layout.saved_history;

View File

@ -40,7 +40,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
time.setText(new SimpleDateFormat().format(new Date(state.getTime())));
final TextView editor = (TextView) result.findViewById(R.id.history_item);
editor.setText(AbstractHistoryActivity.getHistoryText(state));
editor.setText(AbstractCalculatorHistoryFragment.getHistoryText(state));
final TextView commentView = (TextView) result.findViewById(R.id.history_item_comment);
if (commentView != null) {
@ -57,7 +57,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
if (state.isSaved()) {
status.setText(getContext().getString(R.string.c_history_item_saved));
} else {
if ( AbstractHistoryActivity.isAlreadySaved(state) ) {
if ( AbstractCalculatorHistoryFragment.isAlreadySaved(state) ) {
status.setText(getContext().getString(R.string.c_history_item_already_saved));
} else {
status.setText(getContext().getString(R.string.c_history_item_not_saved));
@ -71,7 +71,7 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
@Override
public void notifyDataSetChanged() {
this.setNotifyOnChange(false);
this.sort(AbstractHistoryActivity.COMPARATOR);
this.sort(AbstractCalculatorHistoryFragment.COMPARATOR);
this.setNotifyOnChange(true);
super.notifyDataSetChanged();
}

View File

@ -33,10 +33,10 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
use(R.string.c_use) {
@Override
public void onClick(@NotNull HistoryItemMenuData data, @NotNull Context context) {
if (context instanceof AbstractHistoryActivity) {
AbstractHistoryActivity.useHistoryItem(data.getHistoryState(), (AbstractHistoryActivity) context);
if (context instanceof Activity) {
AbstractCalculatorHistoryFragment.useHistoryItem(data.getHistoryState(), (Activity) context);
} else {
Log.e(HistoryItemMenuItem.class.getName(), AbstractHistoryActivity.class + " must be passed as context!");
Log.e(HistoryItemMenuItem.class.getName(), AbstractCalculatorHistoryFragment.class + " must be passed as context!");
}
}
},
@ -110,7 +110,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData>
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.history_edit, null);
final TextView historyExpression = (TextView)editView.findViewById(R.id.history_edit_expression);
historyExpression.setText(AbstractHistoryActivity.getHistoryText(historyState));
historyExpression.setText(AbstractCalculatorHistoryFragment.getHistoryText(historyState));
final EditText comment = (EditText)editView.findViewById(R.id.history_edit_comment);
comment.setText(historyState.getComment());