diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-05-08 14:20:07 +0200 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-05-08 14:20:07 +0200 |
commit | 5a1120e79113c94bcc8620ba086d6203d62b1b1d (patch) | |
tree | c2cc9c40c416c907c98da305bd4e765953353d3a /OpenKeychain/src/main/java/org/sufficientlysecure | |
parent | 5c747a747f20fbbfeb31b60ed4d45ea96fa04f41 (diff) | |
parent | 185c4eed4349d7539c2ddcbe696a8c80b523d214 (diff) | |
download | open-keychain-5a1120e79113c94bcc8620ba086d6203d62b1b1d.tar.gz open-keychain-5a1120e79113c94bcc8620ba086d6203d62b1b1d.tar.bz2 open-keychain-5a1120e79113c94bcc8620ba086d6203d62b1b1d.zip |
Merge pull request #619 from Valodim/loader-fragment
Introduce LoaderFragment
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure')
6 files changed, 120 insertions, 105 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 5464a9b96..3fd958bcc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -29,7 +29,6 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Messenger; -import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; @@ -45,10 +44,8 @@ import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; -import android.view.animation.AnimationUtils; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.AdapterView; -import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ImageView; @@ -78,18 +75,13 @@ import se.emilsjolander.stickylistheaders.StickyListHeadersListView; * Public key list with sticky list headers. It does _not_ extend ListFragment because it uses * StickyListHeaders library which does not extend upon ListView. */ -public class KeyListFragment extends Fragment +public class KeyListFragment extends LoaderFragment implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> { private KeyListAdapter mAdapter; private StickyListHeadersListView mStickyList; - // rebuild functionality of ListFragment, http://stackoverflow.com/a/12504097 - boolean mListShown; - View mProgressContainer; - View mListContainer; - private String mCurQuery; private SearchView mSearchView; // empty list layout @@ -101,14 +93,15 @@ public class KeyListFragment extends Fragment * Load custom layout with StickyListView from library */ @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View root = inflater.inflate(R.layout.key_list_fragment, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) { + View root = super.onCreateView(inflater, superContainer, savedInstanceState); + View view = inflater.inflate(R.layout.key_list_fragment, getContainer()); - mStickyList = (StickyListHeadersListView) root.findViewById(R.id.key_list_list); + mStickyList = (StickyListHeadersListView) view.findViewById(R.id.key_list_list); mStickyList.setOnItemClickListener(this); // empty view - mButtonEmptyCreate = (BootstrapButton) root.findViewById(R.id.key_list_empty_button_create); + mButtonEmptyCreate = (BootstrapButton) view.findViewById(R.id.key_list_empty_button_create); mButtonEmptyCreate.setOnClickListener(new OnClickListener() { @Override @@ -120,7 +113,7 @@ public class KeyListFragment extends Fragment startActivityForResult(intent, 0); } }); - mButtonEmptyImport = (BootstrapButton) root.findViewById(R.id.key_list_empty_button_import); + mButtonEmptyImport = (BootstrapButton) view.findViewById(R.id.key_list_empty_button_import); mButtonEmptyImport.setOnClickListener(new OnClickListener() { @Override @@ -131,11 +124,6 @@ public class KeyListFragment extends Fragment } }); - // rebuild functionality of ListFragment, http://stackoverflow.com/a/12504097 - mListContainer = root.findViewById(R.id.key_list_list_container); - mProgressContainer = root.findViewById(R.id.key_list_progress_container); - mListShown = true; - return root; } @@ -234,9 +222,8 @@ public class KeyListFragment extends Fragment // We have a menu item to show in action bar. setHasOptionsMenu(true); - // NOTE: Not supported by StickyListHeader, but reimplemented here // Start out with a progress indicator. - setListShown(false); + setContentShown(false); // Create an empty adapter we will use to display the loaded data. mAdapter = new KeyListAdapter(getActivity(), null, 0); @@ -298,12 +285,11 @@ public class KeyListFragment extends Fragment // this view is made visible if no data is available mStickyList.setEmptyView(getActivity().findViewById(R.id.key_list_empty)); - // NOTE: Not supported by StickyListHeader, but reimplemented here // The list should now be shown. if (isResumed()) { - setListShown(true); + setContentShown(true); } else { - setListShownNoAnimation(true); + setContentShownNoAnimation(true); } } @@ -418,43 +404,6 @@ public class KeyListFragment extends Fragment return true; } - // rebuild functionality of ListFragment, http://stackoverflow.com/a/12504097 - public void setListShown(boolean shown, boolean animate) { - if (mListShown == shown) { - return; - } - mListShown = shown; - if (shown) { - if (animate) { - mProgressContainer.startAnimation(AnimationUtils.loadAnimation( - getActivity(), android.R.anim.fade_out)); - mListContainer.startAnimation(AnimationUtils.loadAnimation( - getActivity(), android.R.anim.fade_in)); - } - mProgressContainer.setVisibility(View.GONE); - mListContainer.setVisibility(View.VISIBLE); - } else { - if (animate) { - mProgressContainer.startAnimation(AnimationUtils.loadAnimation( - getActivity(), android.R.anim.fade_in)); - mListContainer.startAnimation(AnimationUtils.loadAnimation( - getActivity(), android.R.anim.fade_out)); - } - mProgressContainer.setVisibility(View.VISIBLE); - mListContainer.setVisibility(View.INVISIBLE); - } - } - - // rebuild functionality of ListFragment, http://stackoverflow.com/a/12504097 - public void setListShown(boolean shown) { - setListShown(shown, true); - } - - // rebuild functionality of ListFragment, http://stackoverflow.com/a/12504097 - public void setListShownNoAnimation(boolean shown) { - setListShown(shown, false); - } - /** * Implements StickyListHeadersAdapter from library */ diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LoaderFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LoaderFragment.java new file mode 100644 index 000000000..87ab1bb8c --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LoaderFragment.java @@ -0,0 +1,78 @@ +package org.sufficientlysecure.keychain.ui; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.AnimationUtils; + +import org.sufficientlysecure.keychain.R; + +/** This is a fragment helper class, which implements a generic + * progressbar/container view. + * + * To use it in a fragment, simply subclass, use onCreateView to create the + * layout's root view, and ues getContainer() as root view of your subclass. + * The layout shows a progress bar by default, and can be switched to the + * actual contents by calling setContentShown(). + * + */ +public class LoaderFragment extends Fragment { + private boolean mContentShown; + private View mProgressContainer; + private ViewGroup mContainer; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View root = inflater.inflate(R.layout.loader_layout, container, false); + + mContentShown = true; + mContainer = (ViewGroup) root.findViewById(R.id.loader_container); + mProgressContainer = root.findViewById(R.id.loader_progress); + + // content is not shown (by visibility statuses in the layout files) + mContentShown = false; + + return root; + + } + + protected ViewGroup getContainer() { + return mContainer; + } + + public void setContentShown(boolean shown, boolean animate) { + if (mContentShown == shown) { + return; + } + mContentShown = shown; + if (shown) { + if (animate) { + mProgressContainer.startAnimation(AnimationUtils.loadAnimation( + getActivity(), android.R.anim.fade_out)); + mContainer.startAnimation(AnimationUtils.loadAnimation( + getActivity(), android.R.anim.fade_in)); + } + mProgressContainer.setVisibility(View.GONE); + mContainer.setVisibility(View.VISIBLE); + } else { + if (animate) { + mProgressContainer.startAnimation(AnimationUtils.loadAnimation( + getActivity(), android.R.anim.fade_in)); + mContainer.startAnimation(AnimationUtils.loadAnimation( + getActivity(), android.R.anim.fade_out)); + } + mProgressContainer.setVisibility(View.VISIBLE); + mContainer.setVisibility(View.INVISIBLE); + } + } + + public void setContentShown(boolean shown) { + setContentShown(shown, true); + } + + public void setContentShownNoAnimation(boolean shown) { + setContentShown(shown, false); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java index e1c2013ea..d5658586d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java @@ -23,7 +23,6 @@ import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; @@ -46,7 +45,7 @@ import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; import se.emilsjolander.stickylistheaders.StickyListHeadersListView; -public class ViewKeyCertsFragment extends Fragment +public class ViewKeyCertsFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener { // These are the rows that we will retrieve. @@ -79,18 +78,19 @@ public class ViewKeyCertsFragment extends Fragment private static final int LOADER_ID = 4; @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.view_key_certs_fragment, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) { + View root = super.onCreateView(inflater, superContainer, savedInstanceState); + View view = inflater.inflate(R.layout.view_key_certs_fragment, getContainer()); - return view; + mStickyList = (StickyListHeadersListView) view.findViewById(R.id.list); + + return root; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - mStickyList = (StickyListHeadersListView) getActivity().findViewById(R.id.list); - if (!getArguments().containsKey(ARG_DATA_URI)) { Log.e(Constants.TAG, "Data missing. Should be Uri of key!"); getActivity().finish(); @@ -120,6 +120,7 @@ public class ViewKeyCertsFragment extends Fragment @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { + setContentShown(false); // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. return new CursorLoader(getActivity(), mDataUri, PROJECTION, null, null, SORT_ORDER); @@ -132,6 +133,8 @@ public class ViewKeyCertsFragment extends Fragment mAdapter.swapCursor(data); mStickyList.setAdapter(mAdapter); + + setContentShown(true); } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyKeysFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyKeysFragment.java index 9ab6878d0..fb228f032 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyKeysFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyKeysFragment.java @@ -44,12 +44,11 @@ import org.sufficientlysecure.keychain.util.Log; import java.util.Date; -public class ViewKeyKeysFragment extends Fragment implements +public class ViewKeyKeysFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks<Cursor> { public static final String ARG_DATA_URI = "uri"; - private LinearLayout mContainer; private ListView mKeys; private ViewKeyKeysAdapter mKeysAdapter; @@ -57,13 +56,13 @@ public class ViewKeyKeysFragment extends Fragment implements private Uri mDataUri; @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.view_key_keys_fragment, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) { + View root = super.onCreateView(inflater, superContainer, savedInstanceState); + View view = inflater.inflate(R.layout.view_key_keys_fragment, getContainer()); - mContainer = (LinearLayout) view.findViewById(R.id.container); mKeys = (ListView) view.findViewById(R.id.keys); - return view; + return root; } @Override @@ -81,9 +80,6 @@ public class ViewKeyKeysFragment extends Fragment implements } private void loadData(Uri dataUri) { - getActivity().setProgressBarIndeterminateVisibility(true); - mContainer.setVisibility(View.GONE); - mDataUri = dataUri; Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString()); @@ -104,6 +100,7 @@ public class ViewKeyKeysFragment extends Fragment implements }; public Loader<Cursor> onCreateLoader(int id, Bundle args) { + setContentShown(false); Uri baseUri = Keys.buildKeysUri(mDataUri); return new CursorLoader(getActivity(), baseUri, KEYS_PROJECTION, null, null, null); } @@ -117,8 +114,7 @@ public class ViewKeyKeysFragment extends Fragment implements // old cursor once we return.) mKeysAdapter.swapCursor(data); - getActivity().setProgressBarIndeterminateVisibility(false); - mContainer.setVisibility(View.VISIBLE); + setContentShown(true); } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index b30c9aaec..026417776 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -21,15 +21,12 @@ import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; -import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.LinearLayout; import android.widget.ListView; import com.devspark.appmsg.AppMsg; @@ -37,7 +34,6 @@ import com.devspark.appmsg.AppMsg; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R;import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; -import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.adapter.ViewKeyUserIdsAdapter; @@ -45,12 +41,11 @@ import org.sufficientlysecure.keychain.util.Log; import java.util.Date; -public class ViewKeyMainFragment extends Fragment implements +public class ViewKeyMainFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks<Cursor> { public static final String ARG_DATA_URI = "uri"; - private LinearLayout mContainer; private View mActionEdit; private View mActionEditDivider; private View mActionEncrypt; @@ -70,10 +65,10 @@ public class ViewKeyMainFragment extends Fragment implements private Uri mDataUri; @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.view_key_main_fragment, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) { + View root = super.onCreateView(inflater, superContainer, savedInstanceState); + View view = inflater.inflate(R.layout.view_key_main_fragment, getContainer()); - mContainer = (LinearLayout) view.findViewById(R.id.container); mUserIds = (ListView) view.findViewById(R.id.view_key_user_ids); mActionEdit = view.findViewById(R.id.view_key_action_edit); mActionEditDivider = view.findViewById(R.id.view_key_action_edit_divider); @@ -81,7 +76,7 @@ public class ViewKeyMainFragment extends Fragment implements mActionCertify = view.findViewById(R.id.view_key_action_certify); mActionCertifyDivider = view.findViewById(R.id.view_key_action_certify_divider); - return view; + return root; } @Override @@ -99,9 +94,6 @@ public class ViewKeyMainFragment extends Fragment implements } private void loadData(Uri dataUri) { - getActivity().setProgressBarIndeterminateVisibility(true); - mContainer.setVisibility(View.GONE); - mDataUri = dataUri; Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString()); @@ -143,6 +135,8 @@ public class ViewKeyMainFragment extends Fragment implements static final int INDEX_UNIFIED_HAS_ENCRYPT = 5; public Loader<Cursor> onCreateLoader(int id, Bundle args) { + setContentShown(false); + switch (id) { case LOADER_ID_UNIFIED: { Uri baseUri = KeyRings.buildUnifiedKeyRingUri(mDataUri); @@ -219,8 +213,7 @@ public class ViewKeyMainFragment extends Fragment implements break; } - getActivity().setProgressBarIndeterminateVisibility(false); - mContainer.setVisibility(View.VISIBLE); + setContentShown(true); } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java index 1a76f5eed..b3655133d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java @@ -53,12 +53,11 @@ import org.sufficientlysecure.keychain.util.QrCodeUtils; import java.io.IOException; -public class ViewKeyShareFragment extends Fragment implements +public class ViewKeyShareFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks<Cursor> { public static final String ARG_DATA_URI = "uri"; - private LinearLayout mContainer; private TextView mFingerprint; private ImageView mFingerprintQrCode; private View mFingerprintShareButton; @@ -77,12 +76,12 @@ public class ViewKeyShareFragment extends Fragment implements private Uri mDataUri; @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.view_key_share_fragment, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) { + View root = super.onCreateView(inflater, superContainer, savedInstanceState); + View view = inflater.inflate(R.layout.view_key_share_fragment, getContainer()); mProviderHelper = new ProviderHelper(ViewKeyShareFragment.this.getActivity()); - mContainer = (LinearLayout) view.findViewById(R.id.container); mFingerprint = (TextView) view.findViewById(R.id.view_key_fingerprint); mFingerprintQrCode = (ImageView) view.findViewById(R.id.view_key_fingerprint_qr_code_image); mFingerprintShareButton = view.findViewById(R.id.view_key_action_fingerprint_share); @@ -142,7 +141,7 @@ public class ViewKeyShareFragment extends Fragment implements } }); - return view; + return root; } private void share(Uri dataUri, ProviderHelper providerHelper, boolean fingerprintOnly, @@ -232,9 +231,6 @@ public class ViewKeyShareFragment extends Fragment implements } private void loadData(Uri dataUri) { - getActivity().setProgressBarIndeterminateVisibility(true); - mContainer.setVisibility(View.GONE); - mDataUri = dataUri; Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString()); @@ -260,6 +256,7 @@ public class ViewKeyShareFragment extends Fragment implements static final int INDEX_UNIFIED_EXPIRY = 8; public Loader<Cursor> onCreateLoader(int id, Bundle args) { + setContentShown(false); switch (id) { case LOADER_ID_UNIFIED: { Uri baseUri = KeyRings.buildUnifiedKeyRingUri(mDataUri); @@ -299,8 +296,7 @@ public class ViewKeyShareFragment extends Fragment implements } } - getActivity().setProgressBarIndeterminateVisibility(false); - mContainer.setVisibility(View.VISIBLE); + setContentShown(true); } /** |