2013-01-08 11:01:45 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2013 Dominik Schürmann <dominik@dominikschuermann.de>
|
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* 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.
|
2013-01-08 11:01:45 -05:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* 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.
|
2013-01-08 11:01:45 -05:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-01-08 11:01:45 -05:00
|
|
|
*/
|
|
|
|
|
2013-09-22 13:58:33 -04:00
|
|
|
package org.sufficientlysecure.keychain.ui.adapter;
|
2013-01-08 11:01:45 -05:00
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
2013-01-08 13:19:45 -05:00
|
|
|
import java.io.InputStream;
|
2013-01-08 11:01:45 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import org.spongycastle.openpgp.PGPKeyRing;
|
2013-01-08 13:19:45 -05:00
|
|
|
import org.spongycastle.openpgp.PGPObjectFactory;
|
|
|
|
import org.spongycastle.openpgp.PGPUtil;
|
2013-01-16 08:31:16 -05:00
|
|
|
import org.sufficientlysecure.keychain.Constants;
|
|
|
|
import org.sufficientlysecure.keychain.util.InputData;
|
|
|
|
import org.sufficientlysecure.keychain.util.Log;
|
|
|
|
import org.sufficientlysecure.keychain.util.PositionAwareInputStream;
|
2013-01-08 11:01:45 -05:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.v4.content.AsyncTaskLoader;
|
|
|
|
|
2014-02-26 13:16:28 -05:00
|
|
|
public class ImportKeysListLoader extends AsyncTaskLoader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
|
2014-03-03 03:36:10 -05:00
|
|
|
|
|
|
|
public static class FileHasNoContent extends Exception {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:42:38 -05:00
|
|
|
public static class NonPGPPart extends Exception {
|
|
|
|
private int count;
|
|
|
|
public NonPGPPart(int count) {
|
|
|
|
this.count = count;
|
|
|
|
}
|
|
|
|
public int getCount() {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 11:01:45 -05:00
|
|
|
Context mContext;
|
2013-09-22 13:58:33 -04:00
|
|
|
|
2013-09-22 15:50:46 -04:00
|
|
|
InputData mInputData;
|
2013-01-08 11:01:45 -05:00
|
|
|
|
2013-09-22 15:50:46 -04:00
|
|
|
ArrayList<ImportKeysListEntry> data = new ArrayList<ImportKeysListEntry>();
|
2014-02-26 13:16:28 -05:00
|
|
|
AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> entryListWrapper;
|
2013-01-08 11:01:45 -05:00
|
|
|
|
2013-09-22 15:50:46 -04:00
|
|
|
public ImportKeysListLoader(Context context, InputData inputData) {
|
2013-01-08 11:01:45 -05:00
|
|
|
super(context);
|
|
|
|
this.mContext = context;
|
2013-09-22 15:50:46 -04:00
|
|
|
this.mInputData = inputData;
|
2013-01-08 11:01:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-26 13:16:28 -05:00
|
|
|
public AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> loadInBackground() {
|
|
|
|
|
|
|
|
entryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(data, null);
|
|
|
|
|
2013-09-22 15:50:46 -04:00
|
|
|
if (mInputData == null) {
|
|
|
|
Log.e(Constants.TAG, "Input data is null!");
|
2014-02-26 13:16:28 -05:00
|
|
|
return entryListWrapper;
|
2013-01-08 11:01:45 -05:00
|
|
|
}
|
|
|
|
|
2013-09-22 15:50:46 -04:00
|
|
|
generateListOfKeyrings(mInputData);
|
2013-01-08 13:19:45 -05:00
|
|
|
|
2014-02-26 13:16:28 -05:00
|
|
|
return entryListWrapper;
|
2013-01-08 11:01:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onReset() {
|
|
|
|
super.onReset();
|
|
|
|
|
|
|
|
// Ensure the loader is stopped
|
|
|
|
onStopLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStartLoading() {
|
|
|
|
forceLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStopLoading() {
|
|
|
|
cancelLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-26 13:16:28 -05:00
|
|
|
public void deliverResult(AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> data) {
|
2013-01-08 11:01:45 -05:00
|
|
|
super.deliverResult(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-22 13:58:33 -04:00
|
|
|
* Reads all PGPKeyRing objects from input
|
2013-01-08 11:01:45 -05:00
|
|
|
*
|
|
|
|
* @param keyringBytes
|
|
|
|
* @return
|
|
|
|
*/
|
2013-01-08 13:19:45 -05:00
|
|
|
private void generateListOfKeyrings(InputData inputData) {
|
2014-03-03 03:36:10 -05:00
|
|
|
|
|
|
|
boolean isEmpty = true;
|
2014-03-03 14:42:38 -05:00
|
|
|
int nonPGPcounter = 0;
|
2014-03-03 03:36:10 -05:00
|
|
|
|
2013-01-08 11:01:45 -05:00
|
|
|
PositionAwareInputStream progressIn = new PositionAwareInputStream(
|
|
|
|
inputData.getInputStream());
|
2013-01-08 13:19:45 -05:00
|
|
|
|
2013-01-08 11:01:45 -05:00
|
|
|
// 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
|
|
|
|
// armour blocks
|
|
|
|
BufferedInputStream bufferedInput = new BufferedInputStream(progressIn);
|
|
|
|
try {
|
|
|
|
|
2013-01-08 13:19:45 -05:00
|
|
|
// read all available blocks... (asc files can contain many blocks with BEGIN END)
|
|
|
|
while (bufferedInput.available() > 0) {
|
2014-03-03 03:36:10 -05:00
|
|
|
isEmpty = false;
|
2013-01-08 13:19:45 -05:00
|
|
|
InputStream in = PGPUtil.getDecoderStream(bufferedInput);
|
|
|
|
PGPObjectFactory objectFactory = new PGPObjectFactory(in);
|
|
|
|
|
|
|
|
// go through all objects in this block
|
|
|
|
Object obj;
|
|
|
|
while ((obj = objectFactory.nextObject()) != null) {
|
|
|
|
Log.d(Constants.TAG, "Found class: " + obj.getClass());
|
|
|
|
|
|
|
|
if (obj instanceof PGPKeyRing) {
|
|
|
|
PGPKeyRing newKeyring = (PGPKeyRing) obj;
|
|
|
|
addToData(newKeyring);
|
|
|
|
} else {
|
|
|
|
Log.e(Constants.TAG, "Object not recognized as PGPKeyRing!");
|
2014-03-03 14:42:38 -05:00
|
|
|
nonPGPcounter++;
|
2013-01-08 13:19:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 11:01:45 -05:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2014-03-03 09:07:07 -05:00
|
|
|
Log.e(Constants.TAG, "Exception on parsing key file!", e);
|
2014-03-03 14:42:38 -05:00
|
|
|
entryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(data, e);
|
|
|
|
nonPGPcounter = 0;
|
2014-03-03 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(isEmpty) {
|
|
|
|
Log.e(Constants.TAG, "File has no content!", new FileHasNoContent());
|
2014-03-03 14:42:38 -05:00
|
|
|
entryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>
|
|
|
|
(data, new FileHasNoContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nonPGPcounter > 0) {
|
|
|
|
entryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>
|
|
|
|
(data, new NonPGPPart(nonPGPcounter));
|
2013-01-08 11:01:45 -05:00
|
|
|
}
|
2013-01-08 13:19:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addToData(PGPKeyRing keyring) {
|
2013-09-22 13:58:33 -04:00
|
|
|
ImportKeysListEntry item = new ImportKeysListEntry(keyring);
|
|
|
|
data.add(item);
|
2013-01-08 11:01:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|