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
This commit is contained in:
Daniel Albert 2014-09-12 15:49:29 +02:00
parent 40c81c9429
commit fd5e5afc9f
5 changed files with 272 additions and 80 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<Void, Void, Void> {
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<ImportKeysListEntry> keys = new ArrayList<ImportKeysListEntry>();
// 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<ImportKeysListEntry>(keys));
importIntent.putExtra(KeychainIntentService.EXTRA_DATA, importData);
importIntent.putExtra(KeychainIntentService.EXTRA_MESSENGER, new Messenger(mHandler));
mContext.startService(importIntent);
return null;
}
}
}

View File

@ -154,7 +154,7 @@ public class ProviderHelper {
}
public HashMap<String, Object> 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<CanonicalizedPublicKey> result = new LongSparseArray<CanonicalizedPublicKey>();
@ -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<String> getAllFingerprints(Uri uri) {
Set<String> fingerprints = new HashSet<String>();
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);

View File

@ -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<Cursor> {
LoaderManager.LoaderCallbacks<Cursor>, 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);
}
}

View File

@ -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();
}
}
}

View File

@ -1,82 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<org.sufficientlysecure.keychain.ui.widget.ListAwareSwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/key_list_swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--rebuild functionality of ListFragment -->
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/key_list_list"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:drawSelectorOnTop="true"
android:fastScrollEnabled="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="32dp"
android:scrollbarStyle="outsideOverlay" />
<LinearLayout
android:id="@+id/key_list_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="match_parent">
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/key_list_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:drawSelectorOnTop="true"
android:fastScrollEnabled="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="32dp"
android:scrollbarStyle="outsideOverlay" />
<LinearLayout
android:id="@+id/key_list_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/key_list_empty_text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:text="@string/key_list_empty_text2"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/key_list_empty_button_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="14sp"
android:text="@string/key_list_empty_button_create"
android:drawableLeft="@drawable/ic_action_new_account"
android:drawablePadding="8dp"
android:background="@drawable/button_edgy"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:text="@string/key_list_empty_text3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/key_list_empty_button_import"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="14sp"
android:text="@string/key_list_empty_button_import"
android:drawableLeft="@drawable/ic_action_collection"
android:drawablePadding="8dp"
android:background="@drawable/button_edgy" />
</LinearLayout>
</FrameLayout>
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/key_list_empty_text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:text="@string/key_list_empty_text2"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/key_list_empty_button_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="14sp"
android:text="@string/key_list_empty_button_create"
android:drawableLeft="@drawable/ic_action_new_account"
android:drawablePadding="8dp"
android:background="@drawable/button_edgy"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:text="@string/key_list_empty_text3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/key_list_empty_button_import"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="14sp"
android:text="@string/key_list_empty_button_import"
android:drawableLeft="@drawable/ic_action_collection"
android:drawablePadding="8dp"
android:background="@drawable/button_edgy" />
</LinearLayout>
</FrameLayout>
</org.sufficientlysecure.keychain.ui.widget.ListAwareSwipeRefreshLayout>