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..bd565fccf --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/KeyUpdateHelper.java @@ -0,0 +1,103 @@ +/* + * 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 && servers[0] != null) { + 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(); + String[] servers = Preferences.getPreferences(mContext).getKeyServers(); + + 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(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 36c2dcf9f..fdf8c1f38 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -155,7 +155,7 @@ public class ProviderHelper { } public HashMap getGenericData(Uri uri, String[] proj, int[] types) - throws NotFoundException { + throws NotFoundException { return getGenericData(uri, proj, types, null); } @@ -209,7 +209,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(); @@ -407,11 +407,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()); } } @@ -1312,6 +1312,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..4fda4cede 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,13 @@ 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.helper.Preferences; 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 +79,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,9 +126,25 @@ 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; } + @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 */ @@ -690,4 +712,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..58e8e81e9 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/ListAwareSwipeRefreshLayout.java @@ -0,0 +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 . + */ + +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 { + + + 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) + return super.canChildScrollUp(); + + return ( + mIsLocked + || + ( + mStickyListHeadersListView.getWrappedList().getChildCount() > 0 + && + ( + mStickyListHeadersListView.getTop() > 0 + || + mStickyListHeadersListView.getFirstVisiblePosition() > 0 + ) + ) + ); + } +} \ 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 @@ - - - - - - - - + + - - - - - -