Use toolbar instead of action bar in main activity
This commit is contained in:
@@ -22,26 +22,28 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.CardView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.view.*;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import org.solovyev.android.Activities;
|
||||
import org.solovyev.android.Android;
|
||||
import org.solovyev.android.calculator.converter.ConverterFragment;
|
||||
import org.solovyev.android.calculator.history.History;
|
||||
import org.solovyev.android.calculator.keyboard.PartialKeyboardUi;
|
||||
import org.solovyev.android.calculator.wizard.CalculatorWizards;
|
||||
import org.solovyev.android.fragments.FragmentUtils;
|
||||
import org.solovyev.android.prefs.Preference;
|
||||
import org.solovyev.android.wizard.Wizard;
|
||||
import org.solovyev.android.wizard.Wizards;
|
||||
@@ -51,14 +53,14 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.os.Build.VERSION_CODES.GINGERBREAD_MR1;
|
||||
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
|
||||
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
|
||||
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
|
||||
import static org.solovyev.android.calculator.Preferences.Gui.preventScreenFromFading;
|
||||
import static org.solovyev.android.calculator.release.ReleaseNotes.hasReleaseNotes;
|
||||
import static org.solovyev.android.wizard.WizardUi.*;
|
||||
|
||||
public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
public class CalculatorActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener, Toolbar.OnMenuItemClickListener {
|
||||
|
||||
@Nonnull
|
||||
public static final String TAG = CalculatorActivity.class.getSimpleName();
|
||||
@@ -77,6 +79,10 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
|
||||
@Nullable
|
||||
@Bind(R.id.partial_keyboard)
|
||||
View partialKeyboard;
|
||||
@Bind(R.id.toolbar)
|
||||
Toolbar toolbar;
|
||||
@Bind(R.id.editor)
|
||||
FrameLayout editor;
|
||||
private boolean useBackAsPrev;
|
||||
|
||||
public CalculatorActivity() {
|
||||
@@ -154,36 +160,37 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
ui.setLayoutId(R.layout.main_calculator);
|
||||
ui.setLayoutId(R.layout.activity_main);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final ActionBar actionBar = getSupportActionBar();
|
||||
if (Build.VERSION.SDK_INT <= GINGERBREAD_MR1 || (Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH && hasPermanentMenuKey())) {
|
||||
actionBar.hide();
|
||||
} else {
|
||||
actionBar.setDisplayShowTitleEnabled(false);
|
||||
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
|
||||
}
|
||||
ButterKnife.bind(this);
|
||||
|
||||
FragmentUtils.createFragment(this, EditorFragment.class, R.id.editor, "editor");
|
||||
FragmentUtils.createFragment(this, DisplayFragment.class, R.id.display, "display");
|
||||
FragmentUtils.createFragment(this, KeyboardFragment.class, R.id.keyboard, "keyboard");
|
||||
if (savedInstanceState == null) {
|
||||
final FragmentManager fm = getSupportFragmentManager();
|
||||
final FragmentTransaction t = fm.beginTransaction();
|
||||
t.add(R.id.editor, new EditorFragment(), "editor");
|
||||
t.add(R.id.display, new DisplayFragment(), "display");
|
||||
t.add(R.id.keyboard, new KeyboardFragment(), "keyboard");
|
||||
t.commit();
|
||||
}
|
||||
|
||||
if (partialKeyboard != null) {
|
||||
partialKeyboardUi.onCreateView(this, partialKeyboard);
|
||||
}
|
||||
|
||||
this.useBackAsPrev = Preferences.Gui.usePrevAsBack.getPreference(preferences);
|
||||
toolbar.inflateMenu(R.menu.main);
|
||||
toolbar.setOnMenuItemClickListener(this);
|
||||
|
||||
useBackAsPrev = Preferences.Gui.usePrevAsBack.getPreference(preferences);
|
||||
if (savedInstanceState == null) {
|
||||
firstTimeInit(preferences, this);
|
||||
}
|
||||
|
||||
toggleOrientationChange(preferences);
|
||||
toggleOrientationChange();
|
||||
prepareCardAndToolbar();
|
||||
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
preferredPreferences.check(this, false);
|
||||
|
||||
if (App.isMonkeyRunner(this)) {
|
||||
@@ -199,11 +206,6 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|
||||
private boolean hasPermanentMenuKey() {
|
||||
return ViewConfiguration.get(this).hasPermanentMenuKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||
@@ -255,16 +257,60 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
|
||||
}
|
||||
|
||||
if (Preferences.Gui.autoOrientation.getKey().equals(key)) {
|
||||
toggleOrientationChange(preferences);
|
||||
toggleOrientationChange();
|
||||
}
|
||||
}
|
||||
|
||||
private void toggleOrientationChange(@Nullable SharedPreferences preferences) {
|
||||
preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(this) : preferences;
|
||||
private void toggleOrientationChange() {
|
||||
if (Preferences.Gui.autoOrientation.getPreference(preferences)) {
|
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
||||
setRequestedOrientation(SCREEN_ORIENTATION_UNSPECIFIED);
|
||||
} else {
|
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMenuItemClick(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_settings:
|
||||
launcher.showSettings();
|
||||
return true;
|
||||
case R.id.menu_history:
|
||||
launcher.showHistory();
|
||||
return true;
|
||||
case R.id.menu_plotter:
|
||||
Locator.getInstance().getPlotter().plot();
|
||||
return true;
|
||||
case R.id.menu_conversion_tool:
|
||||
ConverterFragment.show(this);
|
||||
return true;
|
||||
case R.id.menu_about:
|
||||
launcher.showAbout();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void prepareCardAndToolbar() {
|
||||
if (!(editor instanceof CardView)) {
|
||||
return;
|
||||
}
|
||||
final CardView card = (CardView) editor;
|
||||
final Resources resources = getResources();
|
||||
final int cardTopMargin = resources.getDimensionPixelSize(R.dimen.cpp_card_margin);
|
||||
final int preLollipopCardTopPadding = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? card.getPaddingTop() : 0;
|
||||
|
||||
{
|
||||
final ViewGroup.LayoutParams lp = toolbar.getLayoutParams();
|
||||
lp.height += cardTopMargin + preLollipopCardTopPadding;
|
||||
toolbar.setLayoutParams(lp);
|
||||
// center icons in toolbar
|
||||
toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + cardTopMargin / 2 + preLollipopCardTopPadding, toolbar.getPaddingRight(), toolbar.getPaddingBottom() + cardTopMargin / 2);
|
||||
}
|
||||
final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) card.getLayoutParams();
|
||||
final int actionWidth = resources.getDimensionPixelSize(R.dimen.abc_action_button_min_width_overflow_material) + 2 * resources.getDimensionPixelSize(R.dimen.abc_action_bar_overflow_padding_start_material);
|
||||
lp.leftMargin = actionWidth + 2 * toolbar.getPaddingLeft();
|
||||
lp.rightMargin = actionWidth + 2 * toolbar.getPaddingRight();
|
||||
card.setLayoutParams(lp);
|
||||
}
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/25/12
|
||||
* Time: 2:34 PM
|
||||
*/
|
||||
public class CalculatorActivityMobile extends CalculatorActivity {
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
Preferences.Gui.layout.putPreference(prefs, Preferences.Gui.Layout.main_calculator_mobile);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (!App.isMonkeyRunner(this)) {
|
||||
this.finish();
|
||||
}
|
||||
}
|
||||
}
|
@@ -22,17 +22,10 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.solovyev.android.calculator.converter.ConverterFragment;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
@@ -43,10 +36,6 @@ public class EditorFragment extends BaseFragment {
|
||||
|
||||
@Inject
|
||||
Editor editor;
|
||||
@Inject
|
||||
SharedPreferences preferences;
|
||||
@Inject
|
||||
ActivityLauncher launcher;
|
||||
@Bind(R.id.calculator_editor)
|
||||
EditorView editorView;
|
||||
|
||||
@@ -54,12 +43,6 @@ public class EditorFragment extends BaseFragment {
|
||||
super(R.layout.cpp_app_editor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inject(@Nonnull AppComponent component) {
|
||||
super.inject(component);
|
||||
@@ -79,31 +62,4 @@ public class EditorFragment extends BaseFragment {
|
||||
editor.clearView(editorView);
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.main, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_settings:
|
||||
launcher.showSettings();
|
||||
return true;
|
||||
case R.id.menu_history:
|
||||
launcher.showHistory();
|
||||
return true;
|
||||
case R.id.menu_plotter:
|
||||
Locator.getInstance().getPlotter().plot();
|
||||
return true;
|
||||
case R.id.menu_conversion_tool:
|
||||
ConverterFragment.show(getActivity());
|
||||
return true;
|
||||
case R.id.menu_about:
|
||||
launcher.showAbout();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -288,20 +288,22 @@ public final class Preferences {
|
||||
|
||||
public enum Theme {
|
||||
|
||||
default_theme(R.style.Cpp_Theme_Gray),
|
||||
violet_theme(R.style.Cpp_Theme_Violet),
|
||||
light_blue_theme(R.style.Cpp_Theme_Blue),
|
||||
metro_blue_theme(R.style.Cpp_Theme_Metro_Blue),
|
||||
metro_purple_theme(R.style.Cpp_Theme_Metro_Purple),
|
||||
metro_green_theme(R.style.Cpp_Theme_Metro_Green),
|
||||
material_theme(R.style.Cpp_Theme_Material),
|
||||
material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Material_Light_Dialog, R.style.Cpp_Theme_Material_Light_Dialog_Alert);
|
||||
default_theme(R.style.Cpp_Theme_Gray, R.style.Cpp_Theme_Gray_NoActionBar),
|
||||
violet_theme(R.style.Cpp_Theme_Violet, R.style.Cpp_Theme_Violet_NoActionBar),
|
||||
light_blue_theme(R.style.Cpp_Theme_Blue, R.style.Cpp_Theme_Blue_NoActionBar),
|
||||
metro_blue_theme(R.style.Cpp_Theme_Metro_Blue, R.style.Cpp_Theme_Metro_Blue_NoActionBar),
|
||||
metro_purple_theme(R.style.Cpp_Theme_Metro_Purple, R.style.Cpp_Theme_Metro_Purple_NoActionBar),
|
||||
metro_green_theme(R.style.Cpp_Theme_Metro_Green, R.style.Cpp_Theme_Metro_Green_NoActionBar),
|
||||
material_theme(R.style.Cpp_Theme_Material, R.style.Cpp_Theme_Material_NoActionBar),
|
||||
material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Material_Light_NoActionBar, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Material_Light_Dialog, R.style.Cpp_Theme_Material_Light_Dialog_Alert);
|
||||
|
||||
private static final SparseArray<TextColor> textColors = new SparseArray<>();
|
||||
|
||||
@StyleRes
|
||||
public final int theme;
|
||||
@StyleRes
|
||||
public final int mainTheme;
|
||||
@StyleRes
|
||||
public final int wizardTheme;
|
||||
@StyleRes
|
||||
public final int dialogTheme;
|
||||
@@ -309,12 +311,13 @@ public final class Preferences {
|
||||
public final int alertDialogTheme;
|
||||
public final boolean light;
|
||||
|
||||
Theme(@StyleRes int theme) {
|
||||
this(theme, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Material_Dialog, R.style.Cpp_Theme_Material_Dialog_Alert);
|
||||
Theme(@StyleRes int theme, int mainTheme) {
|
||||
this(theme, mainTheme, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Material_Dialog, R.style.Cpp_Theme_Material_Dialog_Alert);
|
||||
}
|
||||
|
||||
Theme(@StyleRes int theme, @StyleRes int wizardTheme, @StyleRes int dialogTheme, @StyleRes int alertDialogTheme) {
|
||||
Theme(@StyleRes int theme, int mainTheme, @StyleRes int wizardTheme, @StyleRes int dialogTheme, @StyleRes int alertDialogTheme) {
|
||||
this.theme = theme;
|
||||
this.mainTheme = mainTheme;
|
||||
this.wizardTheme = wizardTheme;
|
||||
this.dialogTheme = dialogTheme;
|
||||
this.alertDialogTheme = alertDialogTheme;
|
||||
@@ -322,6 +325,9 @@ public final class Preferences {
|
||||
}
|
||||
|
||||
public int getThemeFor(@Nullable Context context) {
|
||||
if (context instanceof CalculatorActivity) {
|
||||
return wizardTheme;
|
||||
}
|
||||
if (context instanceof WizardActivity) {
|
||||
return wizardTheme;
|
||||
}
|
||||
|
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/25/12
|
||||
* Time: 9:29 PM
|
||||
*/
|
||||
public class FragmentUtils {
|
||||
|
||||
public static void createFragment(@Nonnull FragmentActivity activity,
|
||||
@Nonnull Class<? extends Fragment> fragmentClass,
|
||||
int parentViewId,
|
||||
@Nonnull String tag) {
|
||||
createFragment(activity, fragmentClass, parentViewId, tag, null);
|
||||
}
|
||||
|
||||
public static void createFragment(@Nonnull FragmentActivity activity,
|
||||
@Nonnull Class<? extends Fragment> fragmentClass,
|
||||
int parentViewId,
|
||||
@Nonnull String tag,
|
||||
@Nullable Bundle args) {
|
||||
final FragmentManager fm = activity.getSupportFragmentManager();
|
||||
|
||||
Fragment messagesFragment = fm.findFragmentByTag(tag);
|
||||
|
||||
final FragmentTransaction ft = fm.beginTransaction();
|
||||
try {
|
||||
if (messagesFragment == null) {
|
||||
messagesFragment = Fragment.instantiate(activity, fragmentClass.getName(), args);
|
||||
ft.add(parentViewId, messagesFragment, tag);
|
||||
} else {
|
||||
if (messagesFragment.isDetached()) {
|
||||
ft.attach(messagesFragment);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
ft.commit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user