Merge branch 'master' of github.com:open-keychain/open-keychain

This commit is contained in:
Dominik Schürmann
2016-05-20 15:48:24 +03:00

View File

@@ -18,6 +18,7 @@
package org.sufficientlysecure.keychain.ui; package org.sufficientlysecure.keychain.ui;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver; import android.content.ContentResolver;
@@ -29,23 +30,24 @@ import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.FileHelper;
public class BackupRestoreFragment extends Fragment { public class BackupRestoreFragment extends Fragment {
// This ids for multiple key export. // masterKeyId & subKeyId for multi-key export
private ArrayList<Long> mIdsForRepeatAskPassphrase; private Iterator<Pair<Long, Long>> mIdsForRepeatAskPassphrase;
// This index for remembering the number of master key.
private int mIndex;
private static final int REQUEST_REPEAT_PASSPHRASE = 0x00007002; private static final int REQUEST_REPEAT_PASSPHRASE = 0x00007002;
private static final int REQUEST_CODE_INPUT = 0x00007003; private static final int REQUEST_CODE_INPUT = 0x00007003;
@@ -93,10 +95,10 @@ public class BackupRestoreFragment extends Fragment {
return; return;
} }
new AsyncTask<ContentResolver, Void, ArrayList<Long>>() { new AsyncTask<ContentResolver, Void, ArrayList<Pair<Long, Long>>>() {
@Override @Override
protected ArrayList<Long> doInBackground(ContentResolver... resolver) { protected ArrayList<Pair<Long,Long>> doInBackground(ContentResolver... resolver) {
ArrayList<Long> askPassphraseIds = new ArrayList<>(); ArrayList<Pair<Long, Long>> askPassphraseIds = new ArrayList<>();
Cursor cursor = resolver[0].query( Cursor cursor = resolver[0].query(
KeyRings.buildUnifiedKeyRingsUri(), new String[]{ KeyRings.buildUnifiedKeyRingsUri(), new String[]{
KeyRings.MASTER_KEY_ID, KeyRings.MASTER_KEY_ID,
@@ -109,13 +111,20 @@ public class BackupRestoreFragment extends Fragment {
switch (secretKeyType) { switch (secretKeyType) {
// all of these make no sense to ask // all of these make no sense to ask
case PASSPHRASE_EMPTY: case PASSPHRASE_EMPTY:
case GNU_DUMMY:
case DIVERT_TO_CARD: case DIVERT_TO_CARD:
case UNAVAILABLE: case UNAVAILABLE:
continue; continue;
case GNU_DUMMY: {
Long masterKeyId = cursor.getLong(0);
Long subKeyId = getFirstSubKeyWithPassphrase(masterKeyId, resolver[0]);
if(subKeyId != null) {
askPassphraseIds.add(new Pair<>(masterKeyId, subKeyId));
}
continue;
}
default: { default: {
long keyId = cursor.getLong(0); long masterKeyId = cursor.getLong(0);
askPassphraseIds.add(keyId); askPassphraseIds.add(new Pair<>(masterKeyId, masterKeyId));
} }
} }
} }
@@ -128,18 +137,48 @@ public class BackupRestoreFragment extends Fragment {
return askPassphraseIds; return askPassphraseIds;
} }
private Long getFirstSubKeyWithPassphrase(long masterKeyId, ContentResolver resolver) {
Cursor cursor = resolver.query(
KeychainContract.Keys.buildKeysUri(masterKeyId), new String[]{
Keys.KEY_ID,
Keys.HAS_SECRET,
}, Keys.HAS_SECRET + " != 0", null, null);
try {
if (cursor != null) {
while(cursor.moveToNext()) {
SecretKeyType secretKeyType = SecretKeyType.fromNum(cursor.getInt(1));
switch (secretKeyType) {
case PASSPHRASE_EMPTY:
case DIVERT_TO_CARD:
case UNAVAILABLE:
return null;
case GNU_DUMMY:
continue;
default: {
return cursor.getLong(0);
}
}
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
@Override @Override
protected void onPostExecute(ArrayList<Long> askPassphraseIds) { protected void onPostExecute(ArrayList<Pair<Long, Long>> askPassphraseIds) {
super.onPostExecute(askPassphraseIds); super.onPostExecute(askPassphraseIds);
FragmentActivity activity = getActivity(); FragmentActivity activity = getActivity();
if (activity == null) { if (activity == null) {
return; return;
} }
mIdsForRepeatAskPassphrase = askPassphraseIds; mIdsForRepeatAskPassphrase = askPassphraseIds.iterator();
mIndex = 0;
if (mIdsForRepeatAskPassphrase.size() != 0) { if (mIdsForRepeatAskPassphrase.hasNext()) {
startPassphraseActivity(); startPassphraseActivity();
return; return;
} }
@@ -157,9 +196,11 @@ public class BackupRestoreFragment extends Fragment {
} }
Intent intent = new Intent(activity, PassphraseDialogActivity.class); Intent intent = new Intent(activity, PassphraseDialogActivity.class);
long masterKeyId = mIdsForRepeatAskPassphrase.get(mIndex++); Pair<Long, Long> keyPair = mIdsForRepeatAskPassphrase.next();
long masterKeyId = keyPair.first;
long subKeyId = keyPair.second;
RequiredInputParcel requiredInput = RequiredInputParcel requiredInput =
RequiredInputParcel.createRequiredDecryptPassphrase(masterKeyId, masterKeyId); RequiredInputParcel.createRequiredDecryptPassphrase(masterKeyId, subKeyId);
requiredInput.mSkipCaching = true; requiredInput.mSkipCaching = true;
intent.putExtra(PassphraseDialogActivity.EXTRA_REQUIRED_INPUT, requiredInput); intent.putExtra(PassphraseDialogActivity.EXTRA_REQUIRED_INPUT, requiredInput);
startActivityForResult(intent, REQUEST_REPEAT_PASSPHRASE); startActivityForResult(intent, REQUEST_REPEAT_PASSPHRASE);
@@ -172,7 +213,7 @@ public class BackupRestoreFragment extends Fragment {
if (resultCode != Activity.RESULT_OK) { if (resultCode != Activity.RESULT_OK) {
return; return;
} }
if (mIndex < mIdsForRepeatAskPassphrase.size()) { if (mIdsForRepeatAskPassphrase.hasNext()) {
startPassphraseActivity(); startPassphraseActivity();
return; return;
} }