Merge pull request #843 from da-kid/master

Implemented Pull2Refresh
This commit is contained in:
Dominik Schürmann 2014-09-13 19:10:54 +02:00
commit a41b9f08d0
5 changed files with 315 additions and 80 deletions

View File

@ -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 <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 && 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<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>();
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<ImportKeysListEntry>(keys));
importIntent.putExtra(KeychainIntentService.EXTRA_DATA, importData);
importIntent.putExtra(KeychainIntentService.EXTRA_MESSENGER, new Messenger(mHandler));
mContext.startService(importIntent);
}
return null;
}
}
}

View File

@ -1312,6 +1312,27 @@ public class ProviderHelper {
return keyIds; 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) { public byte[] getApiAppSignature(String packageName) {
Uri queryUri = ApiApps.buildByPackageNameUri(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.content.Loader;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.view.ActionMode; import android.view.ActionMode;
@ -55,9 +56,13 @@ import android.widget.TextView;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.ExportHelper; 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.pgp.KeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; 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.dialog.DeleteKeyDialogFragment;
import org.sufficientlysecure.keychain.ui.widget.ListAwareSwipeRefreshLayout;
import org.sufficientlysecure.keychain.util.Highlighter; import org.sufficientlysecure.keychain.util.Highlighter;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Notify; import org.sufficientlysecure.keychain.util.Notify;
@ -74,10 +79,11 @@ import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
*/ */
public class KeyListFragment extends LoaderFragment public class KeyListFragment extends LoaderFragment
implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener, implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener,
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor>, SwipeRefreshLayout.OnRefreshListener {
private KeyListAdapter mAdapter; private KeyListAdapter mAdapter;
private StickyListHeadersListView mStickyList; private StickyListHeadersListView mStickyList;
private ListAwareSwipeRefreshLayout mSwipeRefreshLayout;
// saves the mode object for multiselect, needed for reset at some point // saves the mode object for multiselect, needed for reset at some point
private ActionMode mActionMode = null; 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; 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 * 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);
}
} }

View File

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

View File

@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<!--rebuild functionality of ListFragment --> <!--rebuild functionality of ListFragment -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<se.emilsjolander.stickylistheaders.StickyListHeadersListView <se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/key_list_list" android:id="@+id/key_list_list"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -17,7 +20,6 @@
android:paddingLeft="16dp" android:paddingLeft="16dp"
android:paddingRight="32dp" android:paddingRight="32dp"
android:scrollbarStyle="outsideOverlay" /> android:scrollbarStyle="outsideOverlay" />
<LinearLayout <LinearLayout
android:id="@+id/key_list_empty" android:id="@+id/key_list_empty"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -25,21 +27,18 @@
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="visible"> android:visibility="visible">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"
android:text="@string/key_list_empty_text1" android:text="@string/key_list_empty_text1"
android:textAppearance="?android:attr/textAppearanceLarge" /> android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"
android:text="" android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" /> android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -47,7 +46,6 @@
android:gravity="center" android:gravity="center"
android:text="@string/key_list_empty_text2" android:text="@string/key_list_empty_text2"
android:textAppearance="?android:attr/textAppearanceSmall" /> android:textAppearance="?android:attr/textAppearanceSmall" />
<Button <Button
android:id="@+id/key_list_empty_button_create" android:id="@+id/key_list_empty_button_create"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -58,7 +56,6 @@
android:drawableLeft="@drawable/ic_action_new_account" android:drawableLeft="@drawable/ic_action_new_account"
android:drawablePadding="8dp" android:drawablePadding="8dp"
android:background="@drawable/button_edgy"/> android:background="@drawable/button_edgy"/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -66,7 +63,6 @@
android:gravity="center" android:gravity="center"
android:text="@string/key_list_empty_text3" android:text="@string/key_list_empty_text3"
android:textAppearance="?android:attr/textAppearanceSmall" /> android:textAppearance="?android:attr/textAppearanceSmall" />
<Button <Button
android:id="@+id/key_list_empty_button_import" android:id="@+id/key_list_empty_button_import"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -78,5 +74,5 @@
android:drawablePadding="8dp" android:drawablePadding="8dp"
android:background="@drawable/button_edgy" /> android:background="@drawable/button_edgy" />
</LinearLayout> </LinearLayout>
</FrameLayout> </FrameLayout>
</org.sufficientlysecure.keychain.ui.widget.ListAwareSwipeRefreshLayout>