CalculatorActivity is now BaseActivity

This commit is contained in:
serso 2015-01-27 19:36:16 +01:00
parent 9682322b64
commit 108477f8bb
4 changed files with 24 additions and 92 deletions

View File

@ -28,6 +28,7 @@ import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
@ -47,26 +48,8 @@ import javax.annotation.Nullable;
public class ActivityUi extends BaseUi { public class ActivityUi extends BaseUi {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
private int layoutId; private int layoutId;
private boolean homeIcon = false;
@Nonnull @Nonnull
private Preferences.Gui.Theme theme; private Preferences.Gui.Theme theme;
@ -75,14 +58,13 @@ public class ActivityUi extends BaseUi {
private int selectedNavigationIndex = 0; private int selectedNavigationIndex = 0;
public ActivityUi(int layoutId, @Nonnull String logTag) { public ActivityUi(@LayoutRes int layoutId, @Nonnull String logTag) {
super(logTag); super(logTag);
this.layoutId = layoutId; this.layoutId = layoutId;
} }
public ActivityUi(int layoutId, boolean homeIcon) { public void setLayoutId(int layoutId) {
this.layoutId = layoutId; this.layoutId = layoutId;
this.homeIcon = homeIcon;
} }
@Override @Override
@ -95,8 +77,8 @@ public class ActivityUi extends BaseUi {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
this.theme = Preferences.Gui.getTheme(preferences); theme = Preferences.Gui.getTheme(preferences);
activity.setTheme(this.theme.getThemeId()); activity.setTheme(theme.getThemeId());
this.layout = Preferences.Gui.getLayout(preferences); this.layout = Preferences.Gui.getLayout(preferences);
@ -115,7 +97,7 @@ public class ActivityUi extends BaseUi {
onCreate((Activity) activity); onCreate((Activity) activity);
final ActionBar actionBar = activity.getSupportActionBar(); final ActionBar actionBar = activity.getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false); actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(homeIcon); actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false); actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowHomeEnabled(true);

View File

@ -20,7 +20,7 @@ public class BaseActivity extends ActionBarActivity {
} }
public BaseActivity(@LayoutRes int layout, @Nonnull String logTag) { public BaseActivity(@LayoutRes int layout, @Nonnull String logTag) {
this.ui = CalculatorApplication.getInstance().createActivityHelper(layout, logTag); this.ui = new ActivityUi(layout, logTag);
} }
@Nonnull @Nonnull

View File

@ -30,14 +30,13 @@ import android.content.pm.ActivityInfo;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
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.view.*; import android.view.*;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import org.solovyev.android.Activities; import org.solovyev.android.Activities;
import org.solovyev.android.Android; import org.solovyev.android.Android;
import org.solovyev.android.Threads; import org.solovyev.android.Threads;
@ -62,38 +61,35 @@ import static org.solovyev.android.calculator.Preferences.Gui.preventScreenFromF
import static org.solovyev.android.wizard.WizardUi.continueWizard; import static org.solovyev.android.wizard.WizardUi.continueWizard;
import static org.solovyev.android.wizard.WizardUi.startWizard; import static org.solovyev.android.wizard.WizardUi.startWizard;
public class CalculatorActivity extends ActionBarActivity implements SharedPreferences.OnSharedPreferenceChangeListener, CalculatorEventListener { public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener, CalculatorEventListener {
@Nonnull @Nonnull
public static final String TAG = CalculatorActivity.class.getSimpleName(); public static final String TAG = CalculatorActivity.class.getSimpleName();
private boolean useBackAsPrev; private boolean useBackAsPrev;
@Nonnull public CalculatorActivity() {
private ActivityUi activityUi; super(0, TAG);
}
/** /**
* 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) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final Preferences.Gui.Layout layout = Preferences.Gui.layout.getPreferenceNoError(preferences);
ui.setLayoutId(layout.getLayoutId());
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final Preferences.Gui.Layout layout = Preferences.Gui.layout.getPreferenceNoError(preferences);
activityUi = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG);
activityUi.logDebug("onCreate");
activityUi.onCreate(this);
if (isMultiPane()) { if (isMultiPane()) {
activityUi.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane); ui.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane);
activityUi.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane); ui.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane);
activityUi.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane); ui.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane);
activityUi.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane); ui.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane);
activityUi.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane); ui.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane);
activityUi.addTab(this, CalculatorPlotActivity.getPlotterFragmentType(), null, R.id.main_second_pane); ui.addTab(this, CalculatorPlotActivity.getPlotterFragmentType(), null, R.id.main_second_pane);
} else { } else {
final ActionBar actionBar = getSupportActionBar(); final ActionBar actionBar = getSupportActionBar();
if (Build.VERSION.SDK_INT <= GINGERBREAD_MR1 || (Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH && hasPermanentMenuKey())) { if (Build.VERSION.SDK_INT <= GINGERBREAD_MR1 || (Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH && hasPermanentMenuKey())) {
@ -126,18 +122,6 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
} }
} }
@Override
protected void onStart() {
super.onStart();
activityUi.onStart(this);
}
@Override
protected void onStop() {
activityUi.onStop(this);
super.onStop();
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private boolean hasPermanentMenuKey() { private boolean hasPermanentMenuKey() {
return ViewConfiguration.get(this).hasPermanentMenuKey(); return ViewConfiguration.get(this).hasPermanentMenuKey();
@ -241,20 +225,13 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
buttonPressed(CalculatorSpecialButton.equals); buttonPressed(CalculatorSpecialButton.equals);
} }
@Override
protected void onPause() {
this.activityUi.onPause(this);
super.onPause();
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final Preferences.Gui.Layout newLayout = Preferences.Gui.layout.getPreference(preferences); final Preferences.Gui.Layout newLayout = Preferences.Gui.layout.getPreference(preferences);
if (newLayout != activityUi.getLayout()) { if (newLayout != ui.getLayout()) {
Activities.restartActivity(this); Activities.restartActivity(this);
} }
@ -264,14 +241,10 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
} else { } else {
window.clearFlags(FLAG_KEEP_SCREEN_ON); window.clearFlags(FLAG_KEEP_SCREEN_ON);
} }
this.activityUi.onResume(this);
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
activityUi.onDestroy(this);
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy(); super.onDestroy();
@ -288,13 +261,6 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
} }
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityUi.onSaveInstanceState(this, outState);
}
private void toggleOrientationChange(@Nullable SharedPreferences preferences) { private void toggleOrientationChange(@Nullable SharedPreferences preferences) {
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences; preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences;
if (Preferences.Gui.autoOrientation.getPreference(preferences)) { if (Preferences.Gui.autoOrientation.getPreference(preferences)) {
@ -317,27 +283,22 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
throw new UnsupportedOperationException("Not implemented yet!"); throw new UnsupportedOperationException("Not implemented yet!");
} }
@SuppressWarnings({"UnusedDeclaration"})
public void historyButtonClickHandler(@Nonnull View v) { public void historyButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.history); buttonPressed(CalculatorSpecialButton.history);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void eraseButtonClickHandler(@Nonnull View v) { public void eraseButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.erase); buttonPressed(CalculatorSpecialButton.erase);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void simplifyButtonClickHandler(@Nonnull View v) { public void simplifyButtonClickHandler(@Nonnull View v) {
throw new UnsupportedOperationException("Not implemented yet!"); throw new UnsupportedOperationException("Not implemented yet!");
} }
@SuppressWarnings({"UnusedDeclaration"})
public void pasteButtonClickHandler(@Nonnull View v) { public void pasteButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.paste); buttonPressed(CalculatorSpecialButton.paste);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void copyButtonClickHandler(@Nonnull View v) { public void copyButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.copy); buttonPressed(CalculatorSpecialButton.copy);
} }
@ -347,12 +308,10 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
return Locator.getInstance().getKeyboard(); return Locator.getInstance().getKeyboard();
} }
@SuppressWarnings({"UnusedDeclaration"})
public void clearButtonClickHandler(@Nonnull View v) { public void clearButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.clear); buttonPressed(CalculatorSpecialButton.clear);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void digitButtonClickHandler(@Nonnull View v) { public void digitButtonClickHandler(@Nonnull View v) {
Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed()); Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed());
@ -369,22 +328,18 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
getKeyboard().buttonPressed(text); getKeyboard().buttonPressed(text);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void functionsButtonClickHandler(@Nonnull View v) { public void functionsButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.functions); buttonPressed(CalculatorSpecialButton.functions);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void operatorsButtonClickHandler(@Nonnull View v) { public void operatorsButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.operators); buttonPressed(CalculatorSpecialButton.operators);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void varsButtonClickHandler(@Nonnull View v) { public void varsButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.vars); buttonPressed(CalculatorSpecialButton.vars);
} }
@SuppressWarnings({"UnusedDeclaration"})
public void likeButtonClickHandler(@Nonnull View v) { public void likeButtonClickHandler(@Nonnull View v) {
buttonPressed(CalculatorSpecialButton.like); buttonPressed(CalculatorSpecialButton.like);
} }
@ -402,7 +357,7 @@ public class CalculatorActivity extends ActionBarActivity implements SharedPrefe
// do nothing - fragment shown and already registered for plot updates // do nothing - fragment shown and already registered for plot updates
} else { } else {
// otherwise - open fragment // otherwise - open fragment
activityUi.selectTab(CalculatorActivity.this, CalculatorFragmentType.plotter); ui.selectTab(CalculatorActivity.this, CalculatorFragmentType.plotter);
} }
} else { } else {
// start new activity // start new activity

View File

@ -211,11 +211,6 @@ public class CalculatorApplication extends android.app.Application implements Sh
setTheme(theme.getThemeId()); setTheme(theme.getThemeId());
} }
@Nonnull
public ActivityUi createActivityHelper(int layoutResId, @Nonnull String logTag) {
return new ActivityUi(layoutResId, logTag);
}
@Nonnull @Nonnull
public FragmentUi createFragmentHelper(int layoutId) { public FragmentUi createFragmentHelper(int layoutId) {
return new FragmentUi(layoutId); return new FragmentUi(layoutId);