From fd5e5afc9f43c551d1ac196f47079da3f0ce4500 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Fri, 12 Sep 2014 15:49:29 +0200 Subject: [PATCH 1/2] Implemented Pull2Refresh in the KeyListFragment Added KeyUpdateHelper and extended ProviderHelper for better fingerprint collection Added a class for handling the list in the swipe2refresh layout that prevents scrolling bugs Added Pull2Refresh to the KeyListFragment --- .../keychain/helper/KeyUpdateHelper.java | 100 ++++++++++++ .../keychain/provider/ProviderHelper.java | 29 +++- .../keychain/ui/KeyListFragment.java | 28 +++- .../widget/ListAwareSwipeRefreshLayout.java | 49 ++++++ .../src/main/res/layout/key_list_fragment.xml | 146 +++++++++--------- 5 files changed, 272 insertions(+), 80 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/KeyUpdateHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/ListAwareSwipeRefreshLayout.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/KeyUpdateHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/KeyUpdateHelper.java new file mode 100644 index 000000000..2d7d452b7 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/KeyUpdateHelper.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2014 Daniel Albert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.helper; + +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.Messenger; + +import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; +import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; +import org.sufficientlysecure.keychain.keyimport.Keyserver; +import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.KeychainIntentService; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.util.Log; + +import java.util.ArrayList; +import java.util.List; + +public class KeyUpdateHelper { + + public void updateAllKeys(Context context, KeychainIntentServiceHandler finishedHandler) { + UpdateTask updateTask = new UpdateTask(context, finishedHandler); + updateTask.execute(); + } + + public ImportKeysListEntry getKeyByFingerprint(Context context, String fingerprint) { + String[] servers = Preferences.getPreferences(context).getKeyServers(); + if (servers != null && servers.length != 0) { + try { + HkpKeyserver hkp = new HkpKeyserver(servers[0]); + for (ImportKeysListEntry key : hkp.search("0x" + fingerprint)) { + if (fingerprint.equals(key.getFingerprintHex())) { + return key; + } + } + } catch (Keyserver.QueryNeedsRepairException e) { + } catch (Keyserver.QueryFailedException e) { + } + } + return null; + } + + private class UpdateTask extends AsyncTask { + private Context mContext; + private KeychainIntentServiceHandler mHandler; + + public UpdateTask(Context context, KeychainIntentServiceHandler handler) { + this.mContext = context; + this.mHandler = handler; + } + + @Override + protected Void doInBackground(Void... voids) { + ProviderHelper providerHelper = new ProviderHelper(mContext); + List keys = new ArrayList(); + + // Load all the fingerprints in the database and prepare to import them + for(String fprint : providerHelper.getAllFingerprints(KeychainContract.KeyRings.buildUnifiedKeyRingsUri())) { + ImportKeysListEntry key = getKeyByFingerprint(mContext, fprint); + if(key != null) { + keys.add(key); + } + } + + // Start the service and update the keys + Intent importIntent = new Intent(mContext, KeychainIntentService.class); + importIntent.setAction(KeychainIntentService.ACTION_DOWNLOAD_AND_IMPORT_KEYS); + + Bundle importData = new Bundle(); + importData.putParcelableArrayList(KeychainIntentService.DOWNLOAD_KEY_LIST, + new ArrayList(keys)); + importIntent.putExtra(KeychainIntentService.EXTRA_DATA, importData); + + importIntent.putExtra(KeychainIntentService.EXTRA_MESSENGER, new Messenger(mHandler)); + + mContext.startService(importIntent); + + return null; + } + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index af7bdb139..684c08066 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -154,7 +154,7 @@ public class ProviderHelper { } public HashMap getGenericData(Uri uri, String[] proj, int[] types) - throws NotFoundException { + throws NotFoundException { return getGenericData(uri, proj, types, null); } @@ -208,7 +208,7 @@ public class ProviderHelper { KeyRings.HAS_ANY_SECRET, KeyRings.VERIFIED, // and of course, ring data KeyRings.PUBKEY_DATA - }, KeyRings.HAS_ANY_SECRET + " = 1", null, null); + }, KeyRings.HAS_ANY_SECRET + " = 1", null, null); try { LongSparseArray result = new LongSparseArray(); @@ -406,11 +406,11 @@ public class ProviderHelper { values.put(Keys.EXPIRY, expiryDate.getTime() / 1000); if (key.isExpired()) { log(LogLevel.DEBUG, keyId == masterKeyId ? - LogType.MSG_IP_MASTER_EXPIRED : LogType.MSG_IP_SUBKEY_EXPIRED, + LogType.MSG_IP_MASTER_EXPIRED : LogType.MSG_IP_SUBKEY_EXPIRED, expiryDate.toString()); } else { log(LogLevel.DEBUG, keyId == masterKeyId ? - LogType.MSG_IP_MASTER_EXPIRES : LogType.MSG_IP_SUBKEY_EXPIRES, + LogType.MSG_IP_MASTER_EXPIRES : LogType.MSG_IP_SUBKEY_EXPIRES, expiryDate.toString()); } } @@ -1308,6 +1308,27 @@ public class ProviderHelper { return keyIds; } + public Set getAllFingerprints(Uri uri) { + Set fingerprints = new HashSet(); + String[] projection = new String[]{KeyRings.FINGERPRINT}; + Cursor cursor = mContentResolver.query(uri, projection, null, null, null); + try { + if(cursor != null) { + int fingerprintColumn = cursor.getColumnIndex(KeyRings.FINGERPRINT); + while(cursor.moveToNext()) { + fingerprints.add( + PgpKeyHelper.convertFingerprintToHex(cursor.getBlob(fingerprintColumn)) + ); + } + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + return fingerprints; + } + public byte[] getApiAppSignature(String packageName) { Uri queryUri = ApiApps.buildByPackageNameUri(packageName); 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 e7edc6058..065ed5841 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -35,6 +35,7 @@ import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.view.MenuItemCompat; import android.support.v4.widget.CursorAdapter; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.view.ActionMode; @@ -55,9 +56,12 @@ import android.widget.TextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; +import org.sufficientlysecure.keychain.helper.KeyUpdateHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; +import org.sufficientlysecure.keychain.ui.widget.ListAwareSwipeRefreshLayout; import org.sufficientlysecure.keychain.util.Highlighter; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Notify; @@ -74,10 +78,11 @@ import se.emilsjolander.stickylistheaders.StickyListHeadersListView; */ public class KeyListFragment extends LoaderFragment implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener, - LoaderManager.LoaderCallbacks { + LoaderManager.LoaderCallbacks, SwipeRefreshLayout.OnRefreshListener { private KeyListAdapter mAdapter; private StickyListHeadersListView mStickyList; + private ListAwareSwipeRefreshLayout mSwipeRefreshLayout; // saves the mode object for multiselect, needed for reset at some point private ActionMode mActionMode = null; @@ -120,6 +125,15 @@ public class KeyListFragment extends LoaderFragment } }); + mSwipeRefreshLayout = (ListAwareSwipeRefreshLayout) view.findViewById(R.id.key_list_swipe_container); + mSwipeRefreshLayout.setOnRefreshListener(this); + mSwipeRefreshLayout.setColorScheme( + R.color.android_purple_dark, + R.color.android_purple_light, + R.color.android_purple_dark, + R.color.android_purple_light); + mSwipeRefreshLayout.setStickyListHeadersListView(mStickyList); + return root; } @@ -690,4 +704,16 @@ public class KeyListFragment extends LoaderFragment } + /** + * Implements OnRefreshListener for drag-to-refresh + */ + public void onRefresh() { + KeyUpdateHelper updateHelper = new KeyUpdateHelper(); + KeychainIntentServiceHandler finishedHandler = new KeychainIntentServiceHandler(getActivity()) { + public void handleMessage(Message message) { + mSwipeRefreshLayout.setRefreshing(false); + } + }; + updateHelper.updateAllKeys(getActivity(), finishedHandler); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/ListAwareSwipeRefreshLayout.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/ListAwareSwipeRefreshLayout.java new file mode 100644 index 000000000..3b6f7d7f3 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/ListAwareSwipeRefreshLayout.java @@ -0,0 +1,49 @@ +package org.sufficientlysecure.keychain.ui.widget; + +import android.content.Context; +import android.support.v4.widget.SwipeRefreshLayout; +import android.util.AttributeSet; + +import se.emilsjolander.stickylistheaders.StickyListHeadersListView; + +public class ListAwareSwipeRefreshLayout extends SwipeRefreshLayout { + + /** + * A StickyListHeadersListView whose parent view is this SwipeRefreshLayout + */ + private StickyListHeadersListView mStickyListHeadersListView; + + public ListAwareSwipeRefreshLayout(Context context) { + super(context); + } + + public ListAwareSwipeRefreshLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public void setStickyListHeadersListView(StickyListHeadersListView stickyListHeadersListView) { + mStickyListHeadersListView = stickyListHeadersListView; + } + + @Override + public boolean canChildScrollUp() { + if (mStickyListHeadersListView != null) { + // In order to scroll a StickyListHeadersListView up: + // Firstly, the wrapped ListView must have at least one item + return (mStickyListHeadersListView.getListChildCount() > 0) && + // And then, the first visible item must not be the first item + ((mStickyListHeadersListView.getFirstVisiblePosition() > 0) || + // If the first visible item is the first item, + // (we've reached the first item) + // make sure that its top must not cross over the padding top of the wrapped ListView + (mStickyListHeadersListView.getListChildAt(0).getTop() < 0)); + + // If the wrapped ListView is empty or, + // the first item is located below the padding top of the wrapped ListView, + // we can allow performing refreshing now + } else { + // Fall back to default implementation + return super.canChildScrollUp(); + } + } +} \ No newline at end of file diff --git a/OpenKeychain/src/main/res/layout/key_list_fragment.xml b/OpenKeychain/src/main/res/layout/key_list_fragment.xml index f1da19b72..6af40106d 100644 --- a/OpenKeychain/src/main/res/layout/key_list_fragment.xml +++ b/OpenKeychain/src/main/res/layout/key_list_fragment.xml @@ -1,82 +1,78 @@ - - - - - - - - + + - - - - - -