diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2015-11-15 23:24:20 +0100 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2015-12-27 15:00:14 +0100 |
commit | aca0cd340bda38cc15547aa5a116c679782ff740 (patch) | |
tree | 18d843a31002c8893f55e2f6499541162c22e4fa /OpenKeychain/src/main/java/org | |
parent | 69220cb4c0466112426e7d10f865da834b464d90 (diff) | |
download | open-keychain-aca0cd340bda38cc15547aa5a116c679782ff740.tar.gz open-keychain-aca0cd340bda38cc15547aa5a116c679782ff740.tar.bz2 open-keychain-aca0cd340bda38cc15547aa5a116c679782ff740.zip |
inline-edit: move action mode menu handling int activity
Diffstat (limited to 'OpenKeychain/src/main/java/org')
3 files changed, 135 insertions, 62 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvActivity.java index 86cabaf96..fe4135cd8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvActivity.java @@ -18,19 +18,26 @@ package org.sufficientlysecure.keychain.ui; +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; -import android.support.v4.app.LoaderManager; +import android.support.v4.app.Fragment; +import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.view.ViewPager; -import android.support.v4.view.ViewPager.SimpleOnPageChangeListener; +import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.ActionMode; +import android.view.LayoutInflater; import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import android.view.ViewPropertyAnimator; +import android.view.animation.OvershootInterpolator; import android.widget.Toast; import com.astuetz.PagerSlidingTabStrip; @@ -47,7 +54,7 @@ import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.util.Log; public class ViewKeyAdvActivity extends BaseActivity implements - LoaderManager.LoaderCallbacks<Cursor> { + LoaderCallbacks<Cursor>, OnPageChangeListener { ProviderHelper mProviderHelper; @@ -64,6 +71,11 @@ public class ViewKeyAdvActivity extends BaseActivity implements private PagerSlidingTabStrip mSlidingTabLayout; private static final int LOADER_ID_UNIFIED = 0; + private ActionMode mActionMode; + private boolean mHasSecret; + private PagerTabStripAdapter mTabAdapter; + private boolean mActionIconShown; + private boolean[] mTabsWithActionMode; @Override protected void onCreate(Bundle savedInstanceState) { @@ -109,33 +121,44 @@ public class ViewKeyAdvActivity extends BaseActivity implements } private void initTabs(Uri dataUri, boolean hasSecret) { - PagerTabStripAdapter adapter = new PagerTabStripAdapter(this); - mViewPager.setAdapter(adapter); + + mHasSecret = hasSecret; + + mTabAdapter = new PagerTabStripAdapter(this); + mViewPager.setAdapter(mTabAdapter); + + // keep track which of these are action mode enabled! + mTabsWithActionMode = new boolean[4]; Bundle shareBundle = new Bundle(); shareBundle.putParcelable(ViewKeyAdvShareFragment.ARG_DATA_URI, dataUri); - adapter.addTab(ViewKeyAdvShareFragment.class, + mTabAdapter.addTab(ViewKeyAdvShareFragment.class, shareBundle, getString(R.string.key_view_tab_share)); + mTabsWithActionMode[0] = false; Bundle userIdsBundle = new Bundle(); userIdsBundle.putParcelable(ViewKeyAdvUserIdsFragment.ARG_DATA_URI, dataUri); userIdsBundle.putBoolean(ViewKeyAdvUserIdsFragment.ARG_HAS_SECRET, hasSecret); - adapter.addTab(ViewKeyAdvUserIdsFragment.class, + mTabAdapter.addTab(ViewKeyAdvUserIdsFragment.class, userIdsBundle, getString(R.string.section_user_ids)); + mTabsWithActionMode[1] = true; Bundle keysBundle = new Bundle(); keysBundle.putParcelable(ViewKeyAdvSubkeysFragment.ARG_DATA_URI, dataUri); keysBundle.putBoolean(ViewKeyAdvSubkeysFragment.ARG_HAS_SECRET, hasSecret); - adapter.addTab(ViewKeyAdvSubkeysFragment.class, + mTabAdapter.addTab(ViewKeyAdvSubkeysFragment.class, keysBundle, getString(R.string.key_view_tab_keys)); + mTabsWithActionMode[2] = true; Bundle certsBundle = new Bundle(); certsBundle.putParcelable(ViewKeyAdvCertsFragment.ARG_DATA_URI, dataUri); - adapter.addTab(ViewKeyAdvCertsFragment.class, + mTabAdapter.addTab(ViewKeyAdvCertsFragment.class, certsBundle, getString(R.string.key_view_tab_certs)); + mTabsWithActionMode[3] = false; // update layout after operations mSlidingTabLayout.setViewPager(mViewPager); + mSlidingTabLayout.setOnPageChangeListener(this); // switch to tab selected by extra Intent intent = getIntent(); @@ -245,22 +268,83 @@ public class ViewKeyAdvActivity extends BaseActivity implements } @Override - public void onActionModeStarted(final ActionMode mode) { - super.onActionModeStarted(mode); - // Leave whatever action mode we are in when we change the page - mSlidingTabLayout.setOnPageChangeListener(new SimpleOnPageChangeListener() { + public boolean onCreateOptionsMenu(Menu menu) { + + if (!mHasSecret) { + return false; + } + + // always add the item, switch its visibility depending on fragment + getMenuInflater().inflate(R.menu.action_mode_edit, menu); + final MenuItem vActionModeItem = menu.findItem(R.id.menu_action_mode_edit); + + boolean isCurrentActionFragment = mTabsWithActionMode[mViewPager.getCurrentItem()]; + + // if the state is as it should be, never mind + if (isCurrentActionFragment == mActionIconShown) { + return isCurrentActionFragment; + } + + // show or hide accordingly + mActionIconShown = isCurrentActionFragment; + vActionModeItem.setEnabled(isCurrentActionFragment); + animateMenuItem(vActionModeItem, isCurrentActionFragment); + + return true; + } + + private void animateMenuItem(final MenuItem vEditSubkeys, final boolean animateShow) { + + View actionView = LayoutInflater.from(this).inflate(R.layout.edit_icon, null); + vEditSubkeys.setActionView(actionView); + actionView.setTranslationX(animateShow ? 150 : 0); + + ViewPropertyAnimator animator = actionView.animate(); + animator.translationX(animateShow ? 0 : 150); + animator.setDuration(300); + animator.setInterpolator(new OvershootInterpolator(1.5f)); + animator.setListener(new AnimatorListenerAdapter() { @Override - public void onPageSelected(int position) { - super.onPageSelected(position); - mode.finish(); + public void onAnimationEnd(Animator animation) { + if (!animateShow) { + vEditSubkeys.setVisible(false); + } + vEditSubkeys.setActionView(null); } }); + animator.start(); + + } + + @Override + public void onActionModeStarted(final ActionMode mode) { + super.onActionModeStarted(mode); + mActionMode = mode; } @Override public void onActionModeFinished(ActionMode mode) { super.onActionModeFinished(mode); - mSlidingTabLayout.setOnPageChangeListener(null); + mActionMode = null; + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + if (mActionMode != null) { + mActionMode.finish(); + mActionMode = null; + } + invalidateOptionsMenu(); + } + + @Override + public void onPageScrollStateChanged(int state) { + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvSubkeysFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvSubkeysFragment.java index 048dd4f77..b93eccb92 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvSubkeysFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvSubkeysFragment.java @@ -17,6 +17,7 @@ package org.sufficientlysecure.keychain.ui; + import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -28,7 +29,6 @@ import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.ActionMode; -import android.view.ActionMode.Callback; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -123,6 +123,8 @@ public class ViewKeyAdvSubkeysFragment extends LoaderFragment implements } }); + setHasOptionsMenu(true); + return root; } @@ -138,8 +140,6 @@ public class ViewKeyAdvSubkeysFragment extends LoaderFragment implements } mHasSecret = getArguments().getBoolean(ARG_HAS_SECRET); - setHasOptionsMenu(true); - loadData(dataUri); } @@ -184,9 +184,23 @@ public class ViewKeyAdvSubkeysFragment extends LoaderFragment implements mSubkeysAdapter.swapCursor(null); } - private void enterEditMode() { + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_action_mode_edit: + enterEditMode(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + public void enterEditMode() { FragmentActivity activity = getActivity(); - activity.startActionMode(new Callback() { + if (activity == null) { + return; + } + activity.startActionMode(new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { @@ -229,25 +243,6 @@ public class ViewKeyAdvSubkeysFragment extends LoaderFragment implements }); } - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - super.onCreateOptionsMenu(menu, inflater); - inflater.inflate(R.menu.edit_subkeys, menu); - final MenuItem vEditSubkeys = menu.findItem(R.id.menu_edit_subkeys); - vEditSubkeys.setVisible(mHasSecret); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.menu_edit_subkeys: - enterEditMode(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - private void addSubkey() { boolean willBeMasterKey; if (mSubkeysAdapter != null) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvUserIdsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvUserIdsFragment.java index 49c0184c7..1e38a823a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvUserIdsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvUserIdsFragment.java @@ -30,10 +30,8 @@ import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.ActionMode; -import android.view.ActionMode.Callback; import android.view.LayoutInflater; import android.view.Menu; -import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -109,6 +107,8 @@ public class ViewKeyAdvUserIdsFragment extends LoaderFragment implements } }); + setHasOptionsMenu(true); + return root; } @@ -218,8 +218,6 @@ public class ViewKeyAdvUserIdsFragment extends LoaderFragment implements } boolean hasSecret = getArguments().getBoolean(ARG_HAS_SECRET); - setHasOptionsMenu(true); - loadData(dataUri, hasSecret); } @@ -278,16 +276,22 @@ public class ViewKeyAdvUserIdsFragment extends LoaderFragment implements } @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - super.onCreateOptionsMenu(menu, inflater); - inflater.inflate(R.menu.edit_user_id, menu); - MenuItem vEditUserIds = menu.findItem(R.id.menu_edit_user_ids); - vEditUserIds.setVisible(mHasSecret); + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_action_mode_edit: + enterEditMode(); + return true; + default: + return super.onOptionsItemSelected(item); + } } - private void enterEditMode() { + public void enterEditMode() { FragmentActivity activity = getActivity(); - activity.startActionMode(new Callback() { + if (activity == null) { + return; + } + activity.startActionMode(new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { @@ -330,14 +334,4 @@ public class ViewKeyAdvUserIdsFragment extends LoaderFragment implements }); } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.menu_edit_user_ids: - enterEditMode(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } } |