Removed unnecessary querying of keyservers, reimplemented the ListAwareSwipeRefreshLayout and implemented a pull-lock in case there are no keyservers

This commit is contained in:
Daniel Albert 2014-09-13 17:02:10 +02:00
parent fd5e5afc9f
commit a86ec573fa
3 changed files with 84 additions and 41 deletions

View File

@ -44,7 +44,7 @@ public class KeyUpdateHelper {
public ImportKeysListEntry getKeyByFingerprint(Context context, String fingerprint) {
String[] servers = Preferences.getPreferences(context).getKeyServers();
if (servers != null && servers.length != 0) {
if (servers != null && servers.length != 0 && servers[0] != null) {
try {
HkpKeyserver hkp = new HkpKeyserver(servers[0]);
for (ImportKeysListEntry key : hkp.search("0x" + fingerprint)) {
@ -72,28 +72,31 @@ public class KeyUpdateHelper {
protected Void doInBackground(Void... voids) {
ProviderHelper providerHelper = new ProviderHelper(mContext);
List<ImportKeysListEntry> keys = new ArrayList<ImportKeysListEntry>();
String[] servers = Preferences.getPreferences(mContext).getKeyServers();
// 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) {
if (servers != null && servers.length > 0) {
// Load all the fingerprints in the database and prepare to import them
for (String fprint : providerHelper.getAllFingerprints(KeychainContract.KeyRings.buildUnifiedKeyRingsUri())) {
ImportKeysListEntry key = new ImportKeysListEntry();
key.setFingerprintHex(fprint);
key.setBitStrength(1337);
key.setOrigin(servers[0]);
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);
}
// 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

@ -57,6 +57,7 @@ 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.helper.Preferences;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler;
@ -137,6 +138,13 @@ public class KeyListFragment extends LoaderFragment
return root;
}
@Override
public void onResume() {
String[] servers = Preferences.getPreferences(getActivity()).getKeyServers();
mSwipeRefreshLayout.setIsLocked(servers == null || servers.length == 0 || servers[0] == null);
super.onResume();
}
/**
* Define Adapter and Loader on create of Activity
*/

View File

@ -1,49 +1,81 @@
/*
* 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.ui.widget;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import org.sufficientlysecure.keychain.util.Log;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
public class ListAwareSwipeRefreshLayout extends SwipeRefreshLayout {
/**
* A StickyListHeadersListView whose parent view is this SwipeRefreshLayout
*/
private StickyListHeadersListView mStickyListHeadersListView;
private StickyListHeadersListView mStickyListHeadersListView = null;
private boolean mIsLocked = false;
/**
* Constructors
*/
public ListAwareSwipeRefreshLayout(Context context) {
super(context);
}
public ListAwareSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Getters / Setters
*/
public void setStickyListHeadersListView(StickyListHeadersListView stickyListHeadersListView) {
mStickyListHeadersListView = stickyListHeadersListView;
}
public StickyListHeadersListView getStickyListHeadersListView() {
return mStickyListHeadersListView;
}
public void setIsLocked(boolean locked) {
mIsLocked = locked;
Log.d("ListAwareSwipeRefreshLayout", (mIsLocked ? "is locked" : "not locked"));
}
public boolean getIsLocked() {
return mIsLocked;
}
@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
if (mStickyListHeadersListView == null)
return super.canChildScrollUp();
}
return (
mIsLocked
||
(
mStickyListHeadersListView.getWrappedList().getChildCount() > 0
&&
(
mStickyListHeadersListView.getTop() > 0
||
mStickyListHeadersListView.getFirstVisiblePosition() > 0
)
)
);
}
}