use KeyRepository in BackupRestoreFragment
This commit is contained in:
@@ -22,12 +22,11 @@ import java.util.ArrayList;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
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;
|
||||||
@@ -38,9 +37,10 @@ import android.view.ViewGroup;
|
|||||||
|
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.model.SubKey;
|
import org.sufficientlysecure.keychain.model.SubKey;
|
||||||
|
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||||
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
|
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
|
||||||
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
||||||
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
|
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
|
||||||
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;
|
||||||
@@ -54,33 +54,16 @@ public class BackupRestoreFragment extends Fragment {
|
|||||||
private static final int REQUEST_CODE_INPUT = 0x00007003;
|
private static final int REQUEST_CODE_INPUT = 0x00007003;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.backup_restore_fragment, container, false);
|
View view = inflater.inflate(R.layout.backup_restore_fragment, container, false);
|
||||||
|
|
||||||
View backupAll = view.findViewById(R.id.backup_all);
|
View backupAll = view.findViewById(R.id.backup_all);
|
||||||
View backupPublicKeys = view.findViewById(R.id.backup_public_keys);
|
View backupPublicKeys = view.findViewById(R.id.backup_public_keys);
|
||||||
final View restore = view.findViewById(R.id.restore);
|
final View restore = view.findViewById(R.id.restore);
|
||||||
|
|
||||||
backupAll.setOnClickListener(new View.OnClickListener() {
|
backupAll.setOnClickListener(v -> exportToFile(true));
|
||||||
@Override
|
backupPublicKeys.setOnClickListener(v -> exportToFile(false));
|
||||||
public void onClick(View v) {
|
restore.setOnClickListener(v -> restore());
|
||||||
exportToFile(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
backupPublicKeys.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
exportToFile(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
restore.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
restore();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@@ -96,50 +79,46 @@ public class BackupRestoreFragment extends Fragment {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new AsyncTask<ContentResolver, Void, ArrayList<Pair<Long, Long>>>() {
|
KeyRepository keyRepository = KeyRepository.create(requireContext());
|
||||||
|
|
||||||
|
// This can probably be optimized quite a bit.
|
||||||
|
// Typically there are only few secret keys though, so it doesn't really matter.
|
||||||
|
|
||||||
|
new AsyncTask<Void, Void, ArrayList<Pair<Long, Long>>>() {
|
||||||
@Override
|
@Override
|
||||||
protected ArrayList<Pair<Long,Long>> doInBackground(ContentResolver... resolver) {
|
protected ArrayList<Pair<Long,Long>> doInBackground(Void... ignored) {
|
||||||
|
KeyRepository keyRepository = KeyRepository.create(requireContext());
|
||||||
ArrayList<Pair<Long, Long>> askPassphraseIds = new ArrayList<>();
|
ArrayList<Pair<Long, Long>> askPassphraseIds = new ArrayList<>();
|
||||||
Cursor cursor = resolver[0].query(
|
for (UnifiedKeyInfo keyInfo : keyRepository.getAllUnifiedKeyInfoWithSecret()) {
|
||||||
KeyRings.buildUnifiedKeyRingsUri(), new String[]{
|
long masterKeyId = keyInfo.master_key_id();
|
||||||
KeyRings.MASTER_KEY_ID,
|
SecretKeyType secretKeyType;
|
||||||
KeyRings.HAS_SECRET,
|
try {
|
||||||
}, KeyRings.HAS_SECRET + " != 0", null, null);
|
secretKeyType = keyRepository.getSecretKeyType(keyInfo.master_key_id());
|
||||||
try {
|
} catch (NotFoundException e) {
|
||||||
if (cursor != null) {
|
throw new IllegalStateException("Error: no secret key type for secret key!");
|
||||||
while (cursor.moveToNext()) {
|
|
||||||
SecretKeyType secretKeyType = SecretKeyType.fromNum(cursor.getInt(1));
|
|
||||||
switch (secretKeyType) {
|
|
||||||
// all of these make no sense to ask
|
|
||||||
case PASSPHRASE_EMPTY:
|
|
||||||
case DIVERT_TO_CARD:
|
|
||||||
case UNAVAILABLE:
|
|
||||||
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: {
|
|
||||||
long masterKeyId = cursor.getLong(0);
|
|
||||||
askPassphraseIds.add(new Pair<>(masterKeyId, masterKeyId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} finally {
|
switch (secretKeyType) {
|
||||||
if (cursor != null) {
|
// all of these make no sense to ask
|
||||||
cursor.close();
|
case PASSPHRASE_EMPTY:
|
||||||
|
case DIVERT_TO_CARD:
|
||||||
|
case UNAVAILABLE:
|
||||||
|
continue;
|
||||||
|
case GNU_DUMMY: {
|
||||||
|
Long subKeyId = getFirstSubKeyWithPassphrase(masterKeyId);
|
||||||
|
if(subKeyId != null) {
|
||||||
|
askPassphraseIds.add(new Pair<>(masterKeyId, subKeyId));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
askPassphraseIds.add(new Pair<>(masterKeyId, masterKeyId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return askPassphraseIds;
|
return askPassphraseIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long getFirstSubKeyWithPassphrase(long masterKeyId, ContentResolver resolver) {
|
private Long getFirstSubKeyWithPassphrase(long masterKeyId) {
|
||||||
KeyRepository keyRepository = KeyRepository.create(requireContext());
|
|
||||||
for (SubKey subKey : keyRepository.getSubKeysByMasterKeyId(masterKeyId)) {
|
for (SubKey subKey : keyRepository.getSubKeysByMasterKeyId(masterKeyId)) {
|
||||||
switch (subKey.has_secret()) {
|
switch (subKey.has_secret()) {
|
||||||
case PASSPHRASE_EMPTY:
|
case PASSPHRASE_EMPTY:
|
||||||
@@ -174,7 +153,7 @@ public class BackupRestoreFragment extends Fragment {
|
|||||||
startBackup(true);
|
startBackup(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}.execute(activity.getContentResolver());
|
}.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startPassphraseActivity() {
|
private void startPassphraseActivity() {
|
||||||
|
|||||||
Reference in New Issue
Block a user