Wrap EXTRA_REPLACEMENT_EXTRAS section so it only runs on Lollipop or greater

This commit is contained in:
William Faulk 2015-04-30 18:48:25 -04:00
parent b06e7cd737
commit 6383d19dd3
1 changed files with 38 additions and 36 deletions

View File

@ -228,48 +228,50 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
// Bluetooth Share will convert text/plain sent via EXTRA_TEXT to HTML // Bluetooth Share will convert text/plain sent via EXTRA_TEXT to HTML
// Add replacement extra to send a text/plain file instead. // Add replacement extra to send a text/plain file instead.
try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final File contentFile = new File(getActivity().getExternalCacheDir(), try {
"key-" + KeyFormattingUtils.getShortKeyIdAsHexFromFingerprint(fingerprintData, false) + final File contentFile = new File(getActivity().getExternalCacheDir(),
".pgp.asc"); "key-" + KeyFormattingUtils.getShortKeyIdAsHexFromFingerprint(fingerprintData, false) +
FileWriter contentFileWriter = new FileWriter(contentFile, false); ".pgp.asc");
BufferedWriter contentWriter = new BufferedWriter(contentFileWriter); FileWriter contentFileWriter = new FileWriter(contentFile, false);
contentWriter.write(content); BufferedWriter contentWriter = new BufferedWriter(contentFileWriter);
contentWriter.close(); contentWriter.write(content);
Uri contentUri = Uri.fromFile(contentFile); contentWriter.close();
Uri contentUri = Uri.fromFile(contentFile);
final Runnable deleteContentFile = new Runnable() { final Runnable deleteContentFile = new Runnable() {
public void run() { public void run() {
contentFile.delete(); contentFile.delete();
} }
}; };
// delete the file after Bluetooth Share closes the file // delete the file after Bluetooth Share closes the file
FileObserver tempFileObserver = new FileObserver(contentFile.getAbsolutePath(), FileObserver tempFileObserver = new FileObserver(contentFile.getAbsolutePath(),
FileObserver.CLOSE_NOWRITE) { FileObserver.CLOSE_NOWRITE) {
@Override @Override
public void onEvent(int event, String path) { public void onEvent(int event, String path) {
// Hopefully it will only be opened and then closed by the share process once // Hopefully it will only be opened and then closed by the share process once
getContainer().post(deleteContentFile); getContainer().post(deleteContentFile);
} }
}; };
tempFileObserver.startWatching(); tempFileObserver.startWatching();
// If it's not complete in 1m, the file was not used; delete it // If it's not complete in 1m, the file was not used; delete it
getContainer().postDelayed(deleteContentFile, 1 * 60 * 1000); getContainer().postDelayed(deleteContentFile, 1 * 60 * 1000);
// create replacement extras inside try{}: // create replacement extras inside try{}:
// if file creation fails, just don't add the replacements // if file creation fails, just don't add the replacements
Bundle replacements = new Bundle(); Bundle replacements = new Bundle();
shareChooser.putExtra(Intent.EXTRA_REPLACEMENT_EXTRAS, replacements); shareChooser.putExtra(Intent.EXTRA_REPLACEMENT_EXTRAS, replacements);
Bundle bluetoothExtra = new Bundle(sendIntent.getExtras()); Bundle bluetoothExtra = new Bundle(sendIntent.getExtras());
replacements.putBundle("com.android.bluetooth", bluetoothExtra); replacements.putBundle("com.android.bluetooth", bluetoothExtra);
bluetoothExtra.putParcelable(Intent.EXTRA_STREAM, contentUri); bluetoothExtra.putParcelable(Intent.EXTRA_STREAM, contentUri);
} catch (IOException e) { } catch (IOException e) {
Log.e(Constants.TAG, "error creating temporary Bluetooth key share file!", 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(); Notify.create(getActivity(), R.string.error_bluetooth_file, Notify.Style.ERROR).show();
}
} }
startActivity(shareChooser); startActivity(shareChooser);