From 9bbb286c5e5a15208a7fe0cb9a3c52c7c6c1c42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Wed, 26 Feb 2014 17:58:12 +0100 Subject: [PATCH 1/8] Added the AsyncTaskResultWrapper --- .../ui/adapter/AsyncTaskResultWrapper.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java new file mode 100644 index 000000000..518ff8f27 --- /dev/null +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * 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.adapter; + +public class AsyncTaskResultWrapper { + + private final T result; + private final Exception error; + + public AsyncTaskResultWrapper(T result, Exception error){ + this.result = result; + this.error = error; + } + + public T getResult() { + return result; + } + + public Exception getError() { + return error; + } + +} From 5d1f61bff78e4f09eb18e5cc7020906af40655ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Wed, 26 Feb 2014 18:00:13 +0100 Subject: [PATCH 2/8] Reworked ImportKeysListFragment to handel exceptions, ImportKeyListLoader is temporarily disabled --- .../keychain/ui/ImportKeysListFragment.java | 33 +++++++++++-------- .../adapter/ImportKeysListServerLoader.java | 21 ++++++++---- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index b52de5bc9..e88df9e43 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -27,6 +27,7 @@ import java.util.List; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.ui.adapter.AsyncTaskResultWrapper; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListLoader; @@ -45,7 +46,7 @@ import android.widget.ListView; import android.widget.Toast; public class ImportKeysListFragment extends ListFragment implements - LoaderManager.LoaderCallbacks> { + LoaderManager.LoaderCallbacks>> { private static final String ARG_DATA_URI = "uri"; private static final String ARG_BYTES = "bytes"; private static final String ARG_SERVER_QUERY = "query"; @@ -181,12 +182,13 @@ public class ImportKeysListFragment extends ListFragment implements } @Override - public Loader> onCreateLoader(int id, Bundle args) { + public Loader>> onCreateLoader(int id, Bundle args) { switch (id) { case LOADER_ID_BYTES: { InputData inputData = getInputData(mKeyBytes, mDataUri); - return new ImportKeysListLoader(mActivity, inputData); + //TODO Rewrite ImportKeysListLoader + //return new ImportKeysListLoader(mActivity, inputData); } case LOADER_ID_SERVER_QUERY: { return new ImportKeysListServerLoader(getActivity(), mServerQuery, mKeyServer); @@ -198,15 +200,15 @@ public class ImportKeysListFragment extends ListFragment implements } @Override - public void onLoadFinished(Loader> loader, - List data) { + public void onLoadFinished(Loader>> loader, + AsyncTaskResultWrapper> data) { // Swap the new cursor in. (The framework will take care of closing the // old cursor once we return.) - Log.d(Constants.TAG, "data: " + data); + Log.d(Constants.TAG, "data: " + data.getResult()); // swap in the real data! - mAdapter.setData(data); + mAdapter.setData(data.getResult()); mAdapter.notifyDataSetChanged(); setListAdapter(mAdapter); @@ -222,11 +224,16 @@ public class ImportKeysListFragment extends ListFragment implements break; case LOADER_ID_SERVER_QUERY: - Toast.makeText( - getActivity(), getResources().getQuantityString(R.plurals.keys_found, - mAdapter.getCount(), mAdapter.getCount()), - Toast.LENGTH_SHORT - ).show(); + + if(data.getError() == null){ + Toast.makeText( + getActivity(), getResources().getQuantityString(R.plurals.keys_found, + mAdapter.getCount(), mAdapter.getCount()), + Toast.LENGTH_SHORT + ).show(); + } else { + Toast.makeText(getActivity(), "Server connection timed out!", Toast.LENGTH_SHORT).show(); + } break; default: @@ -235,7 +242,7 @@ public class ImportKeysListFragment extends ListFragment implements } @Override - public void onLoaderReset(Loader> loader) { + public void onLoaderReset(Loader>> loader) { switch (loader.getId()) { case LOADER_ID_BYTES: // Clear the data in the adapter. diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java index b3bc39127..c7f6eee32 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java @@ -28,13 +28,15 @@ import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; import java.util.List; -public class ImportKeysListServerLoader extends AsyncTaskLoader> { +public class ImportKeysListServerLoader extends AsyncTaskLoader>> { Context mContext; String mServerQuery; String mKeyServer; - ArrayList data = new ArrayList(); + //ArrayList data = new ArrayList(); + ArrayList entryList = new ArrayList(); + AsyncTaskResultWrapper> entryListWrapper; public ImportKeysListServerLoader(Context context, String serverQuery, String keyServer) { super(context); @@ -44,15 +46,16 @@ public class ImportKeysListServerLoader extends AsyncTaskLoader loadInBackground() { + public AsyncTaskResultWrapper> loadInBackground() { if (mServerQuery == null) { Log.e(Constants.TAG, "mServerQuery is null!"); - return data; + entryListWrapper = new AsyncTaskResultWrapper>(entryList, null); + return entryListWrapper; } queryServer(mServerQuery, mKeyServer); - return data; + return entryListWrapper; } @Override @@ -74,7 +77,7 @@ public class ImportKeysListServerLoader extends AsyncTaskLoader data) { + public void deliverResult(AsyncTaskResultWrapper> data) { super.deliverResult(data); } @@ -87,13 +90,17 @@ public class ImportKeysListServerLoader extends AsyncTaskLoader searchResult = server.search(query); // add result to data - data.addAll(searchResult); + entryList.addAll(searchResult); + entryListWrapper = new AsyncTaskResultWrapper>(entryList, null); } catch (KeyServer.InsufficientQuery e) { Log.e(Constants.TAG, "InsufficientQuery", e); + entryListWrapper = new AsyncTaskResultWrapper>(entryList, e); } catch (KeyServer.QueryException e) { Log.e(Constants.TAG, "QueryException", e); + entryListWrapper = new AsyncTaskResultWrapper>(entryList, e); } catch (KeyServer.TooManyResponses e) { Log.e(Constants.TAG, "TooManyResponses", e); + entryListWrapper = new AsyncTaskResultWrapper>(entryList, e); } } From 20470748a971d819a64e842d2847891cbd63f72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Wed, 26 Feb 2014 19:16:28 +0100 Subject: [PATCH 3/8] Adapted the ImportKeysLoader to work with the new AsyncWrapper --- .../keychain/ui/ImportKeysListFragment.java | 4 +--- .../keychain/ui/adapter/ImportKeysListLoader.java | 14 +++++++++----- .../ui/adapter/ImportKeysListServerLoader.java | 4 +++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index e88df9e43..a11a78dd6 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -186,9 +186,7 @@ public class ImportKeysListFragment extends ListFragment implements switch (id) { case LOADER_ID_BYTES: { InputData inputData = getInputData(mKeyBytes, mDataUri); - - //TODO Rewrite ImportKeysListLoader - //return new ImportKeysListLoader(mActivity, inputData); + return new ImportKeysListLoader(mActivity, inputData); } case LOADER_ID_SERVER_QUERY: { return new ImportKeysListServerLoader(getActivity(), mServerQuery, mKeyServer); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java index 00ad8c957..29e418db7 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java @@ -33,12 +33,13 @@ import org.sufficientlysecure.keychain.util.PositionAwareInputStream; import android.content.Context; import android.support.v4.content.AsyncTaskLoader; -public class ImportKeysListLoader extends AsyncTaskLoader> { +public class ImportKeysListLoader extends AsyncTaskLoader>> { Context mContext; InputData mInputData; ArrayList data = new ArrayList(); + AsyncTaskResultWrapper> entryListWrapper; public ImportKeysListLoader(Context context, InputData inputData) { super(context); @@ -47,15 +48,18 @@ public class ImportKeysListLoader extends AsyncTaskLoader loadInBackground() { + public AsyncTaskResultWrapper> loadInBackground() { + + entryListWrapper = new AsyncTaskResultWrapper>(data, null); + if (mInputData == null) { Log.e(Constants.TAG, "Input data is null!"); - return data; + return entryListWrapper; } generateListOfKeyrings(mInputData); - return data; + return entryListWrapper; } @Override @@ -77,7 +81,7 @@ public class ImportKeysListLoader extends AsyncTaskLoader data) { + public void deliverResult(AsyncTaskResultWrapper> data) { super.deliverResult(data); } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java index c7f6eee32..78e4d23fb 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java @@ -47,9 +47,11 @@ public class ImportKeysListServerLoader extends AsyncTaskLoader> loadInBackground() { + + entryListWrapper = new AsyncTaskResultWrapper>(entryList, null); + if (mServerQuery == null) { Log.e(Constants.TAG, "mServerQuery is null!"); - entryListWrapper = new AsyncTaskResultWrapper>(entryList, null); return entryListWrapper; } From 49769645a0f2a0e7369627f093c7971677676ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Thu, 27 Feb 2014 10:17:38 +0100 Subject: [PATCH 4/8] Added documentation to the AsyncTaskResultWrapper --- .../keychain/ui/adapter/AsyncTaskResultWrapper.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java index 518ff8f27..2914b1f01 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java @@ -17,6 +17,11 @@ package org.sufficientlysecure.keychain.ui.adapter; +/** + * The AsyncTaskResultWrapper is used to wrap a result from a AsyncTask (for example: Loader). + * You can pass the result and an exception in it if an error occurred. + * @param - Typ of the result which is wrapped + */ public class AsyncTaskResultWrapper { private final T result; From 2fa9067edb2b381acd71c34082c1be9c2246f9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Thu, 27 Feb 2014 10:20:24 +0100 Subject: [PATCH 5/8] Added StackOverflow link to documentation --- .../keychain/ui/adapter/AsyncTaskResultWrapper.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java index 2914b1f01..2ac19c1d9 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/AsyncTaskResultWrapper.java @@ -20,6 +20,8 @@ package org.sufficientlysecure.keychain.ui.adapter; /** * The AsyncTaskResultWrapper is used to wrap a result from a AsyncTask (for example: Loader). * You can pass the result and an exception in it if an error occurred. + * Concept found at: + * https://stackoverflow.com/questions/19593577/how-to-handle-errors-in-custom-asynctaskloader * @param - Typ of the result which is wrapped */ public class AsyncTaskResultWrapper { From c60ec7a8a7b80364d2cc92aeb60e0da5522e9e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Thu, 27 Feb 2014 22:10:07 +0100 Subject: [PATCH 6/8] Added error strings for key import from keyserver --- OpenPGP-Keychain/src/main/res/values/strings.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/OpenPGP-Keychain/src/main/res/values/strings.xml b/OpenPGP-Keychain/src/main/res/values/strings.xml index 178ef8f17..91359701f 100644 --- a/OpenPGP-Keychain/src/main/res/values/strings.xml +++ b/OpenPGP-Keychain/src/main/res/values/strings.xml @@ -286,6 +286,9 @@ expiry date must come after creation date you can not delete this contact because it is your own. you can not delete the following contacts because they are your own:\n%s + Insufficient server query + Querying keyserver failed + Too many responses Please delete it from the \'My Keys\' screen! Please delete them from the \'My Keys\' screen! From 7e229d4247a4416150574669269b5f0409fc3ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Thu, 27 Feb 2014 22:15:50 +0100 Subject: [PATCH 7/8] Added different notifys for different exceptions --- .../keychain/ui/ImportKeysListFragment.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index a11a78dd6..1118f0264 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -33,6 +33,7 @@ import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListLoader; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListServerLoader; import org.sufficientlysecure.keychain.util.InputData; +import org.sufficientlysecure.keychain.util.KeyServer; import org.sufficientlysecure.keychain.util.Log; import android.app.Activity; @@ -43,7 +44,8 @@ import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.view.View; import android.widget.ListView; -import android.widget.Toast; + +import com.devspark.appmsg.AppMsg; public class ImportKeysListFragment extends ListFragment implements LoaderManager.LoaderCallbacks>> { @@ -223,14 +225,23 @@ public class ImportKeysListFragment extends ListFragment implements case LOADER_ID_SERVER_QUERY: - if(data.getError() == null){ - Toast.makeText( + Exception error = data.getError(); + + if(error == null){ + AppMsg.makeText( getActivity(), getResources().getQuantityString(R.plurals.keys_found, mAdapter.getCount(), mAdapter.getCount()), - Toast.LENGTH_SHORT + AppMsg.STYLE_INFO ).show(); - } else { - Toast.makeText(getActivity(), "Server connection timed out!", Toast.LENGTH_SHORT).show(); + } else if(error instanceof KeyServer.InsufficientQuery){ + AppMsg.makeText(getActivity(), R.string.error_keyserver_insufficient_query, + AppMsg.STYLE_ALERT).show(); + }else if(error instanceof KeyServer.QueryException){ + AppMsg.makeText(getActivity(), R.string.error_keyserver_query, + AppMsg.STYLE_ALERT).show(); + }else if(error instanceof KeyServer.TooManyResponses){ + AppMsg.makeText(getActivity(), R.string.error_keyserver_too_many_responses, + AppMsg.STYLE_ALERT).show(); } break; From 414839b9155b03e3fd50d6d692cfda55e69897c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ha=C3=9F?= Date: Fri, 28 Feb 2014 22:43:31 +0100 Subject: [PATCH 8/8] Some clean up --- .../keychain/ui/adapter/ImportKeysListServerLoader.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java index 78e4d23fb..3a3b6e58b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListServerLoader.java @@ -26,7 +26,6 @@ import org.sufficientlysecure.keychain.util.KeyServer; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; -import java.util.List; public class ImportKeysListServerLoader extends AsyncTaskLoader>> { Context mContext; @@ -34,9 +33,8 @@ public class ImportKeysListServerLoader extends AsyncTaskLoader data = new ArrayList(); - ArrayList entryList = new ArrayList(); - AsyncTaskResultWrapper> entryListWrapper; + private ArrayList entryList = new ArrayList(); + private AsyncTaskResultWrapper> entryListWrapper; public ImportKeysListServerLoader(Context context, String serverQuery, String keyServer) { super(context);