Cleanup, fix advanced sharing
This commit is contained in:
@@ -52,7 +52,7 @@ import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.operations.results.ExportResult;
|
||||
import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
|
||||
import org.sufficientlysecure.keychain.service.ExportKeyringParcel;
|
||||
import org.sufficientlysecure.keychain.service.BackupKeyringParcel;
|
||||
import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify.ActionListener;
|
||||
@@ -61,7 +61,7 @@ import org.sufficientlysecure.keychain.ui.widget.ToolableViewAnimator;
|
||||
import org.sufficientlysecure.keychain.util.FileHelper;
|
||||
import org.sufficientlysecure.keychain.util.Passphrase;
|
||||
|
||||
public class BackupCodeFragment extends CryptoOperationFragment<ExportKeyringParcel, ExportResult>
|
||||
public class BackupCodeFragment extends CryptoOperationFragment<BackupKeyringParcel, ExportResult>
|
||||
implements OnBackStackChangedListener {
|
||||
|
||||
public static final String ARG_BACKUP_CODE = "backup_code";
|
||||
@@ -499,8 +499,8 @@ public class BackupCodeFragment extends CryptoOperationFragment<ExportKeyringPar
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ExportKeyringParcel createOperationInput() {
|
||||
return new ExportKeyringParcel(new Passphrase(mBackupCode), mMasterKeyIds, mExportSecret, mCachedBackupUri);
|
||||
public BackupKeyringParcel createOperationInput() {
|
||||
return new BackupKeyringParcel(new Passphrase(mBackupCode), mMasterKeyIds, mExportSecret, mCachedBackupUri);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
package org.sufficientlysecure.keychain.ui;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -86,7 +85,6 @@ import org.sufficientlysecure.keychain.ui.util.Notify.ActionListener;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
|
||||
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
||||
import org.sufficientlysecure.keychain.util.ContactHelper;
|
||||
import org.sufficientlysecure.keychain.util.ExportHelper;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.NfcHelper;
|
||||
import org.sufficientlysecure.keychain.util.Preferences;
|
||||
@@ -397,8 +395,8 @@ public class ViewKeyActivity extends BaseNfcActivity implements
|
||||
MenuItem editKey = menu.findItem(R.id.menu_key_view_edit);
|
||||
editKey.setVisible(mIsSecret);
|
||||
|
||||
MenuItem exportKey = menu.findItem(R.id.menu_key_view_backup);
|
||||
exportKey.setVisible(mIsSecret);
|
||||
MenuItem backupKey = menu.findItem(R.id.menu_key_view_backup);
|
||||
backupKey.setVisible(mIsSecret);
|
||||
|
||||
MenuItem addLinked = menu.findItem(R.id.menu_key_view_add_linked_identity);
|
||||
addLinked.setVisible(mIsSecret
|
||||
|
||||
@@ -41,13 +41,11 @@ import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter;
|
||||
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||
import org.sufficientlysecure.keychain.util.ContactHelper;
|
||||
import org.sufficientlysecure.keychain.util.ExportHelper;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
|
||||
public class ViewKeyAdvActivity extends BaseActivity implements
|
||||
LoaderManager.LoaderCallbacks<Cursor> {
|
||||
|
||||
ExportHelper mExportHelper;
|
||||
ProviderHelper mProviderHelper;
|
||||
|
||||
protected Uri mDataUri;
|
||||
@@ -75,7 +73,6 @@ public class ViewKeyAdvActivity extends BaseActivity implements
|
||||
}
|
||||
});
|
||||
|
||||
mExportHelper = new ExportHelper(this);
|
||||
mProviderHelper = new ProviderHelper(this);
|
||||
|
||||
mViewPager = (ViewPager) findViewById(R.id.pager);
|
||||
|
||||
@@ -52,6 +52,7 @@ import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
||||
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
|
||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
||||
@@ -63,7 +64,6 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
|
||||
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
||||
import org.sufficientlysecure.keychain.util.ExportHelper;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.NfcHelper;
|
||||
|
||||
@@ -84,7 +84,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
private Uri mDataUri;
|
||||
|
||||
private byte[] mFingerprint;
|
||||
private long mMasterKeyId;
|
||||
private String mUserId;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup superContainer, Bundle savedInstanceState) {
|
||||
@@ -107,7 +107,6 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
View vFingerprintShareButton = view.findViewById(R.id.view_key_action_fingerprint_share);
|
||||
View vFingerprintClipboardButton = view.findViewById(R.id.view_key_action_fingerprint_clipboard);
|
||||
View vKeyShareButton = view.findViewById(R.id.view_key_action_key_share);
|
||||
View vKeySaveButton = view.findViewById(R.id.view_key_action_key_export);
|
||||
View vKeyNfcButton = view.findViewById(R.id.view_key_action_key_nfc);
|
||||
View vKeyClipboardButton = view.findViewById(R.id.view_key_action_key_clipboard);
|
||||
ImageButton vKeySafeSlingerButton = (ImageButton) view.findViewById(R.id.view_key_action_key_safeslinger);
|
||||
@@ -118,35 +117,25 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
vFingerprintShareButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
share(true, false);
|
||||
shareFingerprint(false);
|
||||
}
|
||||
});
|
||||
vFingerprintClipboardButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
share(true, true);
|
||||
shareFingerprint(true);
|
||||
}
|
||||
});
|
||||
vKeyShareButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
share(false, false);
|
||||
}
|
||||
});
|
||||
// Show save only on Android >= 4.4 (Document Provider)
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
||||
vKeySaveButton.setVisibility(View.GONE);
|
||||
}
|
||||
vKeySaveButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
exportToFile();
|
||||
shareKey(false);
|
||||
}
|
||||
});
|
||||
vKeyClipboardButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
share(false, true);
|
||||
shareKey(true);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -178,11 +167,6 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
return root;
|
||||
}
|
||||
|
||||
private void exportToFile() {
|
||||
new ExportHelper(getActivity()).showExportKeysDialog(
|
||||
mMasterKeyId, Constants.Path.APP_DIR_FILE, false);
|
||||
}
|
||||
|
||||
private void startSafeSlinger(Uri dataUri) {
|
||||
long keyId = 0;
|
||||
try {
|
||||
@@ -197,7 +181,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
startActivityForResult(safeSlingerIntent, 0);
|
||||
}
|
||||
|
||||
private void share(boolean fingerprintOnly, boolean toClipboard) {
|
||||
private void shareKey(boolean toClipboard) {
|
||||
Activity activity = getActivity();
|
||||
if (activity == null || mFingerprint == null) {
|
||||
return;
|
||||
@@ -205,18 +189,8 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
ProviderHelper providerHelper = new ProviderHelper(activity);
|
||||
|
||||
try {
|
||||
String content;
|
||||
if (fingerprintOnly) {
|
||||
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(mFingerprint);
|
||||
if (!toClipboard) {
|
||||
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
|
||||
} else {
|
||||
content = fingerprint;
|
||||
}
|
||||
} else {
|
||||
content = providerHelper.getKeyRingAsArmoredString(
|
||||
KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri));
|
||||
}
|
||||
String content = providerHelper.getKeyRingAsArmoredString(
|
||||
KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri));
|
||||
|
||||
if (toClipboard) {
|
||||
ClipboardManager clipMan = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
@@ -228,29 +202,26 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
ClipData clip = ClipData.newPlainText(Constants.CLIPBOARD_LABEL, content);
|
||||
clipMan.setPrimaryClip(clip);
|
||||
|
||||
Notify.create(activity, fingerprintOnly ? R.string.fingerprint_copied_to_clipboard
|
||||
: R.string.key_copied_to_clipboard, Notify.Style.OK).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// Android will fail with android.os.TransactionTooLargeException if key is too big
|
||||
// see http://www.lonestarprod.com/?p=34
|
||||
if (content.length() >= 86389) {
|
||||
Notify.create(activity, R.string.key_too_big_for_sharing, Notify.Style.ERROR).show();
|
||||
Notify.create(activity, R.string.key_copied_to_clipboard, Notify.Style.OK).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// let user choose application
|
||||
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
||||
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
|
||||
sendIntent.setType("text/plain");
|
||||
|
||||
// Bluetooth Share will convert text/plain sent via EXTRA_TEXT to HTML
|
||||
// Add replacement extra to send a text/plain file instead.
|
||||
// NOTE: Don't use Intent.EXTRA_TEXT to send the key
|
||||
// better send it via a Uri!
|
||||
// example: Bluetooth Share will convert text/plain sent via Intent.EXTRA_TEXT to HTML
|
||||
try {
|
||||
TemporaryFileProvider shareFileProv = new TemporaryFileProvider();
|
||||
Uri contentUri = TemporaryFileProvider.createFile(activity,
|
||||
KeyFormattingUtils.convertFingerprintToHex(mFingerprint) + Constants.FILE_EXTENSION_ASC);
|
||||
|
||||
String filename = KeyFormattingUtils.convertFingerprintToHex(mFingerprint);
|
||||
KeyRing.UserId mainUserId = KeyRing.splitUserId(mUserId);
|
||||
if (mainUserId.name != null) {
|
||||
filename = mainUserId.name;
|
||||
}
|
||||
Uri contentUri = TemporaryFileProvider.createFile(activity, filename + Constants.FILE_EXTENSION_ASC);
|
||||
|
||||
BufferedWriter contentWriter = new BufferedWriter(new OutputStreamWriter(
|
||||
new ParcelFileDescriptor.AutoCloseOutputStream(
|
||||
@@ -260,18 +231,15 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
|
||||
sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(Constants.TAG, "error creating temporary Bluetooth key share file!", e);
|
||||
Log.e(Constants.TAG, "Error creating temporary key share file!", e);
|
||||
// no need for a snackbar because one sharing option doesn't work
|
||||
// Notify.create(getActivity(), R.string.error_temp_file, Notify.Style.ERROR).show();
|
||||
}
|
||||
|
||||
|
||||
String title = getString(fingerprintOnly
|
||||
? R.string.title_share_fingerprint_with : R.string.title_share_key);
|
||||
String title = getString(R.string.title_share_key);
|
||||
Intent shareChooser = Intent.createChooser(sendIntent, title);
|
||||
|
||||
startActivity(shareChooser);
|
||||
|
||||
} catch (PgpGeneralException | IOException e) {
|
||||
Log.e(Constants.TAG, "error processing key!", e);
|
||||
Notify.create(activity, R.string.error_key_processing, Notify.Style.ERROR).show();
|
||||
@@ -281,6 +249,45 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
}
|
||||
}
|
||||
|
||||
private void shareFingerprint(boolean toClipboard) {
|
||||
Activity activity = getActivity();
|
||||
if (activity == null || mFingerprint == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String content;
|
||||
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(mFingerprint);
|
||||
if (!toClipboard) {
|
||||
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
|
||||
} else {
|
||||
content = fingerprint;
|
||||
}
|
||||
|
||||
if (toClipboard) {
|
||||
ClipboardManager clipMan = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (clipMan == null) {
|
||||
Notify.create(activity, R.string.error_clipboard_copy, Style.ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
ClipData clip = ClipData.newPlainText(Constants.CLIPBOARD_LABEL, content);
|
||||
clipMan.setPrimaryClip(clip);
|
||||
|
||||
Notify.create(activity, R.string.fingerprint_copied_to_clipboard, Notify.Style.OK).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// let user choose application
|
||||
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
||||
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
|
||||
sendIntent.setType("text/plain");
|
||||
|
||||
String title = getString(R.string.title_share_fingerprint_with);
|
||||
Intent shareChooser = Intent.createChooser(sendIntent, title);
|
||||
|
||||
startActivity(shareChooser);
|
||||
}
|
||||
|
||||
private void showQrCodeDialog() {
|
||||
Intent qrCodeIntent = new Intent(getActivity(), QrCodeViewActivity.class);
|
||||
|
||||
@@ -322,11 +329,12 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
mNfcHelper.initNfc(mDataUri);
|
||||
}
|
||||
|
||||
static final String[] UNIFIED_PROJECTION = new String[] {
|
||||
KeyRings._ID, KeyRings.FINGERPRINT
|
||||
static final String[] UNIFIED_PROJECTION = new String[]{
|
||||
KeyRings._ID, KeyRings.FINGERPRINT, KeyRings.USER_ID
|
||||
};
|
||||
|
||||
static final int INDEX_UNIFIED_FINGERPRINT = 1;
|
||||
static final int INDEX_UNIFIED_USER_ID = 2;
|
||||
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
setContentShown(false);
|
||||
@@ -355,6 +363,8 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
byte[] fingerprintBlob = data.getBlob(INDEX_UNIFIED_FINGERPRINT);
|
||||
setFingerprint(fingerprintBlob);
|
||||
|
||||
mUserId = data.getString(INDEX_UNIFIED_USER_ID);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -371,10 +381,11 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||
mFingerprint = null;
|
||||
}
|
||||
|
||||
/** Load QR Code asynchronously and with a fade in animation */
|
||||
/**
|
||||
* Load QR Code asynchronously and with a fade in animation
|
||||
*/
|
||||
private void setFingerprint(byte[] fingerprintBlob) {
|
||||
mFingerprint = fingerprintBlob;
|
||||
mMasterKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(fingerprintBlob);
|
||||
|
||||
final String fingerprint = KeyFormattingUtils.convertFingerprintToHex(fingerprintBlob);
|
||||
mFingerprintView.setText(KeyFormattingUtils.colorizeFingerprint(fingerprint));
|
||||
|
||||
Reference in New Issue
Block a user