mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-27 11:12:15 -05:00
Merge pull request #1235 from wfaulk/replacement_extra
Fix Bluetooth share without breaking others
This commit is contained in:
commit
9bf01bad14
@ -48,6 +48,8 @@ public class TemporaryStorageProvider extends ContentProvider {
|
|||||||
private static final Uri BASE_URI = Uri.parse("content://org.sufficientlysecure.keychain.tempstorage/");
|
private static final Uri BASE_URI = Uri.parse("content://org.sufficientlysecure.keychain.tempstorage/");
|
||||||
private static final int DB_VERSION = 2;
|
private static final int DB_VERSION = 2;
|
||||||
|
|
||||||
|
private static File cacheDir;
|
||||||
|
|
||||||
public static Uri createFile(Context context, String targetName) {
|
public static Uri createFile(Context context, String targetName) {
|
||||||
ContentValues contentValues = new ContentValues();
|
ContentValues contentValues = new ContentValues();
|
||||||
contentValues.put(COLUMN_NAME, targetName);
|
contentValues.put(COLUMN_NAME, targetName);
|
||||||
@ -90,7 +92,7 @@ public class TemporaryStorageProvider extends ContentProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TemporaryStorageDatabase db;
|
private static TemporaryStorageDatabase db;
|
||||||
|
|
||||||
private File getFile(Uri uri) throws FileNotFoundException {
|
private File getFile(Uri uri) throws FileNotFoundException {
|
||||||
try {
|
try {
|
||||||
@ -101,13 +103,14 @@ public class TemporaryStorageProvider extends ContentProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private File getFile(String id) {
|
private File getFile(String id) {
|
||||||
return new File(getContext().getCacheDir(), "temp/" + id);
|
return new File(cacheDir, "temp/" + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreate() {
|
public boolean onCreate() {
|
||||||
db = new TemporaryStorageDatabase(getContext());
|
db = new TemporaryStorageDatabase(getContext());
|
||||||
return new File(getContext().getCacheDir(), "temp").mkdirs();
|
cacheDir = getContext().getCacheDir();
|
||||||
|
return new File(cacheDir, "temp").mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,6 +26,7 @@ import android.net.Uri;
|
|||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.app.LoaderManager;
|
import android.support.v4.app.LoaderManager;
|
||||||
import android.support.v4.content.CursorLoader;
|
import android.support.v4.content.CursorLoader;
|
||||||
@ -42,19 +43,24 @@ import android.widget.TextView;
|
|||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
|
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
|
||||||
|
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
|
||||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
||||||
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
|
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
|
||||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
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.provider.KeychainContract.Keys;
|
||||||
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
||||||
|
import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider;
|
||||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||||
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
import org.sufficientlysecure.keychain.util.NfcHelper;
|
import org.sufficientlysecure.keychain.util.NfcHelper;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
|
|
||||||
public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
||||||
@ -175,11 +181,11 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
boolean toClipboard) {
|
boolean toClipboard) {
|
||||||
try {
|
try {
|
||||||
String content;
|
String content;
|
||||||
if (fingerprintOnly) {
|
byte[] fingerprintData = (byte[]) providerHelper.getGenericData(
|
||||||
byte[] data = (byte[]) providerHelper.getGenericData(
|
|
||||||
KeyRings.buildUnifiedKeyRingUri(dataUri),
|
KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||||
Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
|
Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
|
||||||
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(data);
|
if (fingerprintOnly) {
|
||||||
|
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(fingerprintData);
|
||||||
if (!toClipboard) {
|
if (!toClipboard) {
|
||||||
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
|
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
|
||||||
} else {
|
} else {
|
||||||
@ -213,13 +219,48 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
||||||
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
|
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
|
||||||
sendIntent.setType("text/plain");
|
sendIntent.setType("text/plain");
|
||||||
|
|
||||||
String title;
|
String title;
|
||||||
if (fingerprintOnly) {
|
if (fingerprintOnly) {
|
||||||
title = getResources().getString(R.string.title_share_fingerprint_with);
|
title = getResources().getString(R.string.title_share_fingerprint_with);
|
||||||
} else {
|
} else {
|
||||||
title = getResources().getString(R.string.title_share_key);
|
title = getResources().getString(R.string.title_share_key);
|
||||||
}
|
}
|
||||||
startActivity(Intent.createChooser(sendIntent, title));
|
Intent shareChooser = Intent.createChooser(sendIntent, title);
|
||||||
|
|
||||||
|
// Bluetooth Share will convert text/plain sent via EXTRA_TEXT to HTML
|
||||||
|
// Add replacement extra to send a text/plain file instead.
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
try {
|
||||||
|
String primaryUserId = UncachedKeyRing.decodeFromData(content.getBytes()).
|
||||||
|
getPublicKey().getPrimaryUserIdWithFallback();
|
||||||
|
|
||||||
|
TemporaryStorageProvider shareFileProv = new TemporaryStorageProvider();
|
||||||
|
Uri contentUri = TemporaryStorageProvider.createFile(getActivity(),
|
||||||
|
primaryUserId + Constants.FILE_EXTENSION_ASC);
|
||||||
|
|
||||||
|
BufferedWriter contentWriter = new BufferedWriter(new OutputStreamWriter(
|
||||||
|
new ParcelFileDescriptor.AutoCloseOutputStream(
|
||||||
|
shareFileProv.openFile(contentUri, "w"))));
|
||||||
|
contentWriter.write(content);
|
||||||
|
contentWriter.close();
|
||||||
|
|
||||||
|
// create replacement extras inside try{}:
|
||||||
|
// if file creation fails, just don't add the replacements
|
||||||
|
Bundle replacements = new Bundle();
|
||||||
|
shareChooser.putExtra(Intent.EXTRA_REPLACEMENT_EXTRAS, replacements);
|
||||||
|
|
||||||
|
Bundle bluetoothExtra = new Bundle(sendIntent.getExtras());
|
||||||
|
replacements.putBundle("com.android.bluetooth", bluetoothExtra);
|
||||||
|
|
||||||
|
bluetoothExtra.putParcelable(Intent.EXTRA_STREAM, contentUri);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
Log.e(Constants.TAG, "error creating temporary Bluetooth key share file!", e);
|
||||||
|
Notify.create(getActivity(), R.string.error_bluetooth_file, Notify.Style.ERROR).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
startActivity(shareChooser);
|
||||||
}
|
}
|
||||||
} catch (PgpGeneralException | IOException e) {
|
} catch (PgpGeneralException | IOException e) {
|
||||||
Log.e(Constants.TAG, "error processing key!", e);
|
Log.e(Constants.TAG, "error processing key!", e);
|
||||||
|
@ -1289,5 +1289,6 @@
|
|||||||
<string name="snack_yubi_other">Different key stored on YubiKey!</string>
|
<string name="snack_yubi_other">Different key stored on YubiKey!</string>
|
||||||
<string name="error_nfc">"NFC Error: %s"</string>
|
<string name="error_nfc">"NFC Error: %s"</string>
|
||||||
<string name="error_pin_nodefault">Default PIN was rejected!</string>
|
<string name="error_pin_nodefault">Default PIN was rejected!</string>
|
||||||
|
<string name="error_bluetooth_file">Error creating temporary file. Bluetooth sharing will fail.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user