ImportKeys: Cleanup

- Move loader package inside keyimport package
- Change name of variables
- Remove unused images
This commit is contained in:
Andrea Torlaschi
2016-06-17 22:41:00 +02:00
parent 155ebc4d69
commit 9f7320b857
18 changed files with 30 additions and 34 deletions

View File

@@ -44,7 +44,7 @@ import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction;
import org.sufficientlysecure.keychain.ui.CreateKeyActivity.SecurityTokenListenerFragment;
import org.sufficientlysecure.keychain.ui.base.QueueingCryptoOperationFragment;
import org.sufficientlysecure.keychain.ui.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.Preferences;

View File

@@ -24,8 +24,6 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -39,9 +37,9 @@ import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.service.ImportKeyringParcel;
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper;
import org.sufficientlysecure.keychain.ui.loader.BytesLoaderState;
import org.sufficientlysecure.keychain.ui.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.ui.loader.LoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.BytesLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.LoaderState;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.util.Log;
@@ -349,7 +347,7 @@ public class ImportKeysActivity extends BaseActivity
Log.d(Constants.TAG, "importKeys started");
// get DATA from selected key entries
IteratorWithSize<ParcelableKeyRing> selectedEntries = keyListFragment.getSelectedData();
IteratorWithSize<ParcelableKeyRing> entries = keyListFragment.getData();
// instead of giving the entries by Intent extra, cache them into a
// file to prevent Java Binder problems on heavy imports
@@ -359,17 +357,16 @@ public class ImportKeysActivity extends BaseActivity
// display here, we should be able to import.
ParcelableFileCache<ParcelableKeyRing> cache =
new ParcelableFileCache<>(this, "key_import.pcl");
cache.writeCache(selectedEntries);
mKeyList = null;
mKeyserver = null;
mOperationHelper.cryptoOperation();
cache.writeCache(entries);
} catch (IOException e) {
Log.e(Constants.TAG, "Problem writing cache file", e);
Notify.create(this, "Problem writing cache file!", Notify.Style.ERROR)
.show((ViewGroup) findViewById(R.id.import_snackbar));
return;
}
mKeyList = null;
mKeyserver = null;
} else if (ls instanceof CloudLoaderState) {
CloudLoaderState sls = (CloudLoaderState) ls;
@@ -386,9 +383,9 @@ public class ImportKeysActivity extends BaseActivity
mKeyList = keys;
mKeyserver = sls.mCloudPrefs.keyserver;
mOperationHelper.cryptoOperation();
}
mOperationHelper.cryptoOperation();
}
@Override

View File

@@ -39,7 +39,7 @@ import android.widget.TextView;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.util.ContactHelper;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Preferences;

View File

@@ -40,7 +40,7 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.ui.loader.BytesLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.BytesLoaderState;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.util.FileHelper;

View File

@@ -49,12 +49,12 @@ import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.operations.results.GetKeyResult;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter;
import org.sufficientlysecure.keychain.ui.loader.AsyncTaskResultWrapper;
import org.sufficientlysecure.keychain.ui.loader.BytesLoaderState;
import org.sufficientlysecure.keychain.ui.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.ui.loader.ImportKeysListCloudLoader;
import org.sufficientlysecure.keychain.ui.loader.ImportKeysListLoader;
import org.sufficientlysecure.keychain.ui.loader.LoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.AsyncTaskResultWrapper;
import org.sufficientlysecure.keychain.keyimport.loader.BytesLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.CloudLoaderState;
import org.sufficientlysecure.keychain.keyimport.loader.ImportKeysListCloudLoader;
import org.sufficientlysecure.keychain.keyimport.loader.ImportKeysListLoader;
import org.sufficientlysecure.keychain.keyimport.loader.LoaderState;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize;
import org.sufficientlysecure.keychain.util.ParcelableProxy;
@@ -108,7 +108,7 @@ public class ImportKeysListFragment extends Fragment implements
* ArrayList would have been, but we save some memory by just returning
* relevant elements on demand.
*/
public IteratorWithSize<ParcelableKeyRing> getSelectedData() {
public IteratorWithSize<ParcelableKeyRing> getData() {
final List<ImportKeysListEntry> entries = getEntries();
final Iterator<ImportKeysListEntry> it = entries.iterator();
return new IteratorWithSize<ParcelableKeyRing>() {

View File

@@ -1,48 +0,0 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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.loader;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
/**
* 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 <T> - Typ of the result which is wrapped
*/
public class AsyncTaskResultWrapper<T> {
private final T mResult;
private final OperationResult mOperationResult;
public AsyncTaskResultWrapper(T result, OperationResult operationResult) {
this.mResult = result;
this.mOperationResult = operationResult;
}
public T getResult() {
return mResult;
}
public OperationResult getOperationResult() {
return mOperationResult;
}
}

View File

@@ -1,15 +0,0 @@
package org.sufficientlysecure.keychain.ui.loader;
import android.net.Uri;
public class BytesLoaderState implements LoaderState {
public byte[] mKeyBytes;
public Uri mDataUri;
public BytesLoaderState(byte[] keyBytes, Uri dataUri) {
mKeyBytes = keyBytes;
mDataUri = dataUri;
}
}

View File

@@ -1,15 +0,0 @@
package org.sufficientlysecure.keychain.ui.loader;
import org.sufficientlysecure.keychain.util.Preferences;
public class CloudLoaderState implements LoaderState {
public Preferences.CloudSearchPrefs mCloudPrefs;
public String mServerQuery;
public CloudLoaderState(String serverQuery, Preferences.CloudSearchPrefs cloudPrefs) {
mServerQuery = serverQuery;
mCloudPrefs = cloudPrefs;
}
}

View File

@@ -1,185 +0,0 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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.loader;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.AsyncTaskLoader;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.keyimport.CloudSearch;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.keyimport.Keyserver;
import org.sufficientlysecure.keychain.operations.results.GetKeyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ParcelableProxy;
import org.sufficientlysecure.keychain.util.Preferences;
import org.sufficientlysecure.keychain.util.orbot.OrbotHelper;
import java.util.ArrayList;
public class ImportKeysListCloudLoader
extends AsyncTaskLoader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
Context mContext;
Preferences.CloudSearchPrefs mCloudPrefs;
String mServerQuery;
private ParcelableProxy mParcelableProxy;
private ArrayList<ImportKeysListEntry> mEntryList = new ArrayList<>();
private AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> mEntryListWrapper;
/**
* Searches a keyserver as specified in cloudPrefs, using an explicit proxy if passed
*
* @param serverQuery string to search on servers for. If is a fingerprint,
* will enforce fingerprint check
* @param cloudPrefs contains keyserver to search on, whether to search on the keyserver,
* and whether to search keybase.io
* @param parcelableProxy explicit proxy to use. If null, will retrieve from preferences
*/
public ImportKeysListCloudLoader(Context context, String serverQuery, Preferences.CloudSearchPrefs cloudPrefs,
@Nullable ParcelableProxy parcelableProxy) {
super(context);
mContext = context;
mServerQuery = serverQuery;
mCloudPrefs = cloudPrefs;
mParcelableProxy = parcelableProxy;
}
@Override
public AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> loadInBackground() {
mEntryListWrapper = new AsyncTaskResultWrapper<>(mEntryList, null);
if (mServerQuery == null) {
Log.e(Constants.TAG, "mServerQuery is null!");
return mEntryListWrapper;
}
if (mServerQuery.startsWith("0x") && mServerQuery.length() == 42) {
Log.d(Constants.TAG, "This search is based on a unique fingerprint. Enforce a fingerprint check!");
queryServer(true);
} else {
queryServer(false);
}
return mEntryListWrapper;
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
protected void onStopLoading() {
cancelLoad();
}
@Override
public void deliverResult(AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> data) {
super.deliverResult(data);
}
/**
* Query keyserver
*/
private void queryServer(boolean enforceFingerprint) {
ParcelableProxy parcelableProxy;
if (mParcelableProxy == null) {
// no explicit proxy specified, fetch from preferences
if (OrbotHelper.isOrbotInRequiredState(mContext)) {
parcelableProxy = Preferences.getPreferences(mContext).getProxyPrefs()
.parcelableProxy;
} else {
// user needs to enable/install orbot
mEntryList.clear();
GetKeyResult pendingResult = new GetKeyResult(null,
RequiredInputParcel.createOrbotRequiredOperation(),
new CryptoInputParcel());
mEntryListWrapper = new AsyncTaskResultWrapper<>(mEntryList, pendingResult);
return;
}
} else {
parcelableProxy = mParcelableProxy;
}
try {
ArrayList<ImportKeysListEntry> searchResult = CloudSearch.search(
mServerQuery,
mCloudPrefs,
parcelableProxy.getProxy()
);
mEntryList.clear();
// add result to data
if (enforceFingerprint) {
String fingerprint = mServerQuery.substring(2);
Log.d(Constants.TAG, "fingerprint: " + fingerprint);
// query must return only one result!
if (searchResult.size() == 1) {
ImportKeysListEntry uniqueEntry = searchResult.get(0);
/*
* set fingerprint explicitly after query
* to enforce a check when the key is imported by KeychainService
*/
uniqueEntry.setFingerprintHex(fingerprint);
mEntryList.add(uniqueEntry);
}
} else {
mEntryList.addAll(searchResult);
}
GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_OK, null);
mEntryListWrapper = new AsyncTaskResultWrapper<>(mEntryList, getKeyResult);
} catch (Keyserver.CloudSearchFailureException e) {
// convert exception to result parcel
int error = GetKeyResult.RESULT_ERROR;
OperationResult.LogType logType = null;
if (e instanceof Keyserver.QueryFailedException) {
error = GetKeyResult.RESULT_ERROR_QUERY_FAILED;
logType = OperationResult.LogType.MSG_GET_QUERY_FAILED;
} else if (e instanceof Keyserver.TooManyResponsesException) {
error = GetKeyResult.RESULT_ERROR_TOO_MANY_RESPONSES;
logType = OperationResult.LogType.MSG_GET_TOO_MANY_RESPONSES;
} else if (e instanceof Keyserver.QueryTooShortException) {
error = GetKeyResult.RESULT_ERROR_QUERY_TOO_SHORT;
logType = OperationResult.LogType.MSG_GET_QUERY_TOO_SHORT;
} else if (e instanceof Keyserver.QueryTooShortOrTooManyResponsesException) {
error = GetKeyResult.RESULT_ERROR_TOO_SHORT_OR_TOO_MANY_RESPONSES;
logType = OperationResult.LogType.MSG_GET_QUERY_TOO_SHORT_OR_TOO_MANY_RESPONSES;
}
OperationResult.OperationLog log = new OperationResult.OperationLog();
log.add(logType, 0);
GetKeyResult getKeyResult = new GetKeyResult(error, log);
mEntryListWrapper = new AsyncTaskResultWrapper<>(mEntryList, getKeyResult);
}
}
}

View File

@@ -1,165 +0,0 @@
/*
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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.loader;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.util.LongSparseArray;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.operations.results.GetKeyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow;
import org.sufficientlysecure.keychain.util.FileHelper;
import org.sufficientlysecure.keychain.util.InputData;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.PositionAwareInputStream;
public class ImportKeysListLoader
extends AsyncTaskLoader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
final Context mContext;
final BytesLoaderState mLoaderState;
ArrayList<ImportKeysListEntry> mData = new ArrayList<>();
LongSparseArray<ParcelableKeyRing> mParcelableRings = new LongSparseArray<>();
AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> mEntryListWrapper;
public ImportKeysListLoader(Context context, BytesLoaderState inputData) {
super(context);
this.mContext = context;
this.mLoaderState = inputData;
}
@Override
public AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> loadInBackground() {
// This has already been loaded! nvm any further, just return
if (mEntryListWrapper != null) {
return mEntryListWrapper;
}
{
GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_OK, null);
mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult);
}
if (mLoaderState == null) {
Log.e(Constants.TAG, "Input data is null!");
return mEntryListWrapper;
}
try {
InputData inputData = getInputData(getContext(), mLoaderState);
generateListOfKeyrings(inputData);
} catch (FileNotFoundException e) {
OperationLog log = new OperationLog();
log.add(LogType.MSG_GET_FILE_NOT_FOUND, 0);
GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_ERROR_FILE_NOT_FOUND, log);
mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult);
}
return mEntryListWrapper;
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
}
@Override
protected void onStartLoading() {
super.forceLoad();
}
@Override
protected void onStopLoading() {
super.cancelLoad();
}
@Override
public void deliverResult(AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> data) {
super.deliverResult(data);
}
public LongSparseArray<ParcelableKeyRing> getParcelableRings() {
return mParcelableRings;
}
/** Reads all PGPKeyRing objects from the bytes of an InputData object. */
private void generateListOfKeyrings(InputData inputData) {
PositionAwareInputStream progressIn = new PositionAwareInputStream(
inputData.getInputStream());
// need to have access to the bufferedInput, so we can reuse it for the possible
// PGPObject chunks after the first one, e.g. files with several consecutive ASCII
// armor blocks
BufferedInputStream bufferedInput = new BufferedInputStream(progressIn);
try {
// parse all keyrings
IteratorWithIOThrow<UncachedKeyRing> it = UncachedKeyRing.fromStream(bufferedInput);
while (it.hasNext()) {
UncachedKeyRing ring = it.next();
ImportKeysListEntry item = new ImportKeysListEntry(getContext(), ring);
mData.add(item);
mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(ring.getEncoded()));
}
} catch (IOException e) {
Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e);
OperationResult.OperationLog log = new OperationResult.OperationLog();
log.add(OperationResult.LogType.MSG_GET_NO_VALID_KEYS, 0);
GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_ERROR_NO_VALID_KEYS, log);
mData.clear();
mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult);
}
}
@NonNull
private static InputData getInputData(Context context, BytesLoaderState loaderState) throws FileNotFoundException {
InputData inputData;
if (loaderState.mKeyBytes != null) {
inputData = new InputData(new ByteArrayInputStream(loaderState.mKeyBytes), loaderState.mKeyBytes.length);
} else if (loaderState.mDataUri != null) {
InputStream inputStream = context.getContentResolver().openInputStream(loaderState.mDataUri);
long length = FileHelper.getFileSize(context, loaderState.mDataUri, -1);
inputData = new InputData(inputStream, length);
} else {
throw new AssertionError("Loader state must contain bytes or a data URI. This is a bug!");
}
return inputData;
}
}

View File

@@ -1,4 +0,0 @@
package org.sufficientlysecure.keychain.ui.loader;
public interface LoaderState {
}