Use Document API on KitKat for file encrypt/decrypt

This commit is contained in:
mar-v-in 2014-06-10 20:57:48 +02:00
parent 08d63340c9
commit f55bc41682
6 changed files with 198 additions and 52 deletions

View File

@ -17,6 +17,7 @@
package org.sufficientlysecure.keychain;
import android.os.Build;
import android.os.Environment;
import org.spongycastle.bcpg.CompressionAlgorithmTags;
@ -48,6 +49,8 @@ public final class Constants {
public static final String CUSTOM_CONTACT_DATA_MIME_TYPE = "vnd.android.cursor.item/vnd.org.sufficientlysecure.keychain.key";
public static boolean KITKAT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
public static final class Path {
public static final String APP_DIR = Environment.getExternalStorageDirectory()
+ "/OpenKeychain";

View File

@ -17,12 +17,14 @@
package org.sufficientlysecure.keychain.helper;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.widget.Toast;
@ -82,6 +84,39 @@ public class FileHelper {
}
}
/**
* Opens the storage browser on Android 4.4 or later for opening a file
* @param fragment
* @param last default selected file
* @param mimeType can be text/plain for example
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void openDocument(Fragment fragment, Uri last, String mimeType, int requestCode) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setData(last);
intent.setType(mimeType);
fragment.startActivityForResult(intent, requestCode);
}
/**
* Opens the storage browser on Android 4.4 or later for saving a file
* @param fragment
* @param last default selected file
* @param mimeType can be text/plain for example
* @param requestCode used to identify the result coming back from storage browser onActivityResult() in your
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void saveDocument(Fragment fragment, Uri last, String mimeType, int requestCode) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setData(last);
intent.setType(mimeType);
fragment.startActivityForResult(intent, requestCode);
}
private static Intent buildFileIntent(String filename, String mimeType) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);

View File

@ -788,7 +788,7 @@ public class KeychainIntentService extends IntentService
// InputStream
InputStream in = getContentResolver().openInputStream(providerUri);
return new InputData(in, PgpHelper.getLengthOfStream(in));
return new InputData(in, 0);
default:
throw new PgpGeneralException("No target choosen!");

View File

@ -20,10 +20,13 @@ package org.sufficientlysecure.keychain.ui;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.provider.OpenableColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -57,7 +60,9 @@ public class DecryptFileFragment extends DecryptFragment {
private View mDecryptButton;
private String mInputFilename = null;
private Uri mInputUri = null;
private String mOutputFilename = null;
private Uri mOutputUri = null;
private FileDialogFragment mFileDialog;
@ -74,9 +79,13 @@ public class DecryptFileFragment extends DecryptFragment {
mDecryptButton = view.findViewById(R.id.decrypt_file_action_decrypt);
mBrowse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Constants.KITKAT) {
FileHelper.openDocument(DecryptFileFragment.this, mInputUri, "*/*", RESULT_CODE_FILE);
} else {
FileHelper.openFile(DecryptFileFragment.this, mFilename.getText().toString(), "*/*",
RESULT_CODE_FILE);
}
}
});
mDecryptButton.setOnClickListener(new View.OnClickListener() {
@Override
@ -98,20 +107,24 @@ public class DecryptFileFragment extends DecryptFragment {
}
}
private void guessOutputFilename() {
mInputFilename = mFilename.getText().toString();
private String guessOutputFilename() {
File file = new File(mInputFilename);
String filename = file.getName();
if (filename.endsWith(".asc") || filename.endsWith(".gpg") || filename.endsWith(".pgp")) {
filename = filename.substring(0, filename.length() - 4);
}
mOutputFilename = Constants.Path.APP_DIR + "/" + filename;
return Constants.Path.APP_DIR + "/" + filename;
}
private void decryptAction() {
String currentFilename = mFilename.getText().toString();
if (mInputFilename == null || !mInputFilename.equals(currentFilename)) {
guessOutputFilename();
mInputUri = null;
mInputFilename = mFilename.getText().toString();
}
if (mInputUri == null) {
mOutputFilename = guessOutputFilename();
}
if (mInputFilename.equals("")) {
@ -119,7 +132,7 @@ public class DecryptFileFragment extends DecryptFragment {
return;
}
if (mInputFilename.startsWith("file")) {
if (mInputUri == null && mInputFilename.startsWith("file")) {
File file = new File(mInputFilename);
if (!file.exists() || !file.isFile()) {
AppMsg.makeText(
@ -141,7 +154,11 @@ public class DecryptFileFragment extends DecryptFragment {
public void handleMessage(Message message) {
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
Bundle data = message.getData();
if (data.containsKey(FileDialogFragment.MESSAGE_DATA_URI)) {
mOutputUri = data.getParcelable(FileDialogFragment.MESSAGE_DATA_URI);
} else {
mOutputFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
}
decryptStart(null);
}
}
@ -170,13 +187,25 @@ public class DecryptFileFragment extends DecryptFragment {
intent.setAction(KeychainIntentService.ACTION_DECRYPT_VERIFY);
// data
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.IO_FILE);
Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename="
+ mOutputFilename);
+ mOutputFilename + ",mInputUri=" + mInputUri + ", mOutputUri="
+ mOutputUri);
if (mInputUri != null) {
data.putInt(KeychainIntentService.SOURCE, KeychainIntentService.IO_URI);
data.putParcelable(KeychainIntentService.ENCRYPT_INPUT_URI, mInputUri);
} else {
data.putInt(KeychainIntentService.SOURCE, KeychainIntentService.IO_FILE);
data.putString(KeychainIntentService.ENCRYPT_INPUT_FILE, mInputFilename);
}
if (mOutputUri != null) {
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.IO_URI);
data.putParcelable(KeychainIntentService.ENCRYPT_OUTPUT_URI, mOutputUri);
} else {
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.IO_FILE);
data.putString(KeychainIntentService.ENCRYPT_OUTPUT_FILE, mOutputFilename);
}
data.putString(KeychainIntentService.DECRYPT_PASSPHRASE, passphrase);
@ -232,6 +261,17 @@ public class DecryptFileFragment extends DecryptFragment {
switch (requestCode) {
case RESULT_CODE_FILE: {
if (resultCode == Activity.RESULT_OK && data != null) {
if (Constants.KITKAT) {
mInputUri = data.getData();
Cursor cursor = getActivity().getContentResolver().query(mInputUri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
mInputFilename = cursor.getString(0);
mFilename.setText(mInputFilename);
}
cursor.close();
}
} else {
try {
String path = FileHelper.getPath(getActivity(), data.getData());
Log.d(Constants.TAG, "path=" + path);
@ -241,6 +281,7 @@ public class DecryptFileFragment extends DecryptFragment {
Log.e(Constants.TAG, "Nullpointer while retrieving path!");
}
}
}
return;
}

View File

@ -20,11 +20,13 @@ package org.sufficientlysecure.keychain.ui;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.provider.OpenableColumns;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
@ -73,7 +75,9 @@ public class EncryptFileFragment extends Fragment {
// model
private String mInputFilename = null;
private Uri mInputUri = null;
private String mOutputFilename = null;
private Uri mOutputUri = null;
@Override
public void onAttach(Activity activity) {
@ -104,9 +108,13 @@ public class EncryptFileFragment extends Fragment {
mBrowse = (BootstrapButton) view.findViewById(R.id.btn_browse);
mBrowse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Constants.KITKAT) {
FileHelper.openDocument(EncryptFileFragment.this, mInputUri, "*/*", RESULT_CODE_FILE);
} else {
FileHelper.openFile(EncryptFileFragment.this, mFilename.getText().toString(), "*/*",
RESULT_CODE_FILE);
}
}
});
mFileCompression = (Spinner) view.findViewById(R.id.fileCompression);
@ -178,7 +186,11 @@ public class EncryptFileFragment extends Fragment {
public void handleMessage(Message message) {
if (message.what == FileDialogFragment.MESSAGE_OKAY) {
Bundle data = message.getData();
if (data.containsKey(FileDialogFragment.MESSAGE_DATA_URI)) {
mOutputUri = data.getParcelable(FileDialogFragment.MESSAGE_DATA_URI);
} else {
mOutputFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
}
encryptStart();
}
}
@ -197,17 +209,20 @@ public class EncryptFileFragment extends Fragment {
private void encryptClicked() {
String currentFilename = mFilename.getText().toString();
if (mInputFilename == null || !mInputFilename.equals(currentFilename)) {
mInputUri = null;
mInputFilename = mFilename.getText().toString();
}
if (mInputUri == null) {
mOutputFilename = guessOutputFilename(mInputFilename);
}
if (mInputFilename.equals("")) {
AppMsg.makeText(getActivity(), R.string.no_file_selected, AppMsg.STYLE_ALERT).show();
return;
}
if (!mInputFilename.startsWith("content")) {
if (mInputUri == null && !mInputFilename.startsWith("content")) {
File file = new File(mInputFilename);
if (!file.exists() || !file.isFile()) {
AppMsg.makeText(
@ -280,7 +295,25 @@ public class EncryptFileFragment extends Fragment {
// fill values for this action
Bundle data = new Bundle();
Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename="
+ mOutputFilename + ",mInputUri=" + mInputUri + ", mOutputUri="
+ mOutputUri);
if (mInputUri != null) {
data.putInt(KeychainIntentService.SOURCE, KeychainIntentService.IO_URI);
data.putParcelable(KeychainIntentService.ENCRYPT_INPUT_URI, mInputUri);
} else {
data.putInt(KeychainIntentService.SOURCE, KeychainIntentService.IO_FILE);
data.putString(KeychainIntentService.ENCRYPT_INPUT_FILE, mInputFilename);
}
if (mOutputUri != null) {
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.IO_URI);
data.putParcelable(KeychainIntentService.ENCRYPT_OUTPUT_URI, mOutputUri);
} else {
data.putInt(KeychainIntentService.TARGET, KeychainIntentService.IO_FILE);
data.putString(KeychainIntentService.ENCRYPT_OUTPUT_FILE, mOutputFilename);
}
if (mEncryptInterface.isModeSymmetric()) {
Log.d(Constants.TAG, "Symmetric encryption enabled!");
@ -296,12 +329,6 @@ public class EncryptFileFragment extends Fragment {
mEncryptInterface.getEncryptionKeys());
}
Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename="
+ mOutputFilename);
data.putString(KeychainIntentService.ENCRYPT_INPUT_FILE, mInputFilename);
data.putString(KeychainIntentService.ENCRYPT_OUTPUT_FILE, mOutputFilename);
boolean useAsciiArmor = mAsciiArmor.isChecked();
data.putBoolean(KeychainIntentService.ENCRYPT_USE_ASCII_ARMOR, useAsciiArmor);
@ -356,6 +383,17 @@ public class EncryptFileFragment extends Fragment {
switch (requestCode) {
case RESULT_CODE_FILE: {
if (resultCode == Activity.RESULT_OK && data != null) {
if (Constants.KITKAT) {
mInputUri = data.getData();
Cursor cursor = getActivity().getContentResolver().query(mInputUri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
mInputFilename = cursor.getString(0);
mFilename.setText(mInputFilename);
}
cursor.close();
}
} else {
try {
String path = FileHelper.getPath(getActivity(), data.getData());
Log.d(Constants.TAG, "path=" + path);
@ -365,6 +403,7 @@ public class EncryptFileFragment extends Fragment {
Log.e(Constants.TAG, "Nullpointer while retrieving path!");
}
}
}
return;
}

View File

@ -23,10 +23,13 @@ import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.provider.OpenableColumns;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
@ -50,6 +53,7 @@ public class FileDialogFragment extends DialogFragment {
public static final int MESSAGE_OKAY = 1;
public static final String MESSAGE_DATA_URI = "uri";
public static final String MESSAGE_DATA_FILENAME = "filename";
public static final String MESSAGE_DATA_CHECKED = "checked";
@ -60,6 +64,9 @@ public class FileDialogFragment extends DialogFragment {
private CheckBox mCheckBox;
private TextView mMessageTextView;
private String mOutputFilename;
private Uri mOutputUri;
private static final int REQUEST_CODE = 0x00007004;
/**
@ -92,7 +99,7 @@ public class FileDialogFragment extends DialogFragment {
String title = getArguments().getString(ARG_TITLE);
String message = getArguments().getString(ARG_MESSAGE);
String defaultFile = getArguments().getString(ARG_DEFAULT_FILE);
mOutputFilename = getArguments().getString(ARG_DEFAULT_FILE);
String checkboxText = getArguments().getString(ARG_CHECKBOX_TEXT);
LayoutInflater inflater = (LayoutInflater) activity
@ -106,15 +113,18 @@ public class FileDialogFragment extends DialogFragment {
mMessageTextView.setText(message);
mFilename = (EditText) view.findViewById(R.id.input);
mFilename.setText(defaultFile);
mFilename.setText(mOutputFilename);
mBrowse = (BootstrapButton) view.findViewById(R.id.btn_browse);
mBrowse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// only .asc or .gpg files
// setting it to text/plain prevents Cynaogenmod's file manager from selecting asc
// or gpg types!
FileHelper.openFile(FileDialogFragment.this, mFilename.getText().toString(), "*/*",
REQUEST_CODE);
if (Constants.KITKAT) {
FileHelper.saveDocument(FileDialogFragment.this, mOutputUri, "*/*", REQUEST_CODE);
} else {
FileHelper.openFile(FileDialogFragment.this, mOutputFilename, "*/*", REQUEST_CODE);
}
}
});
@ -136,13 +146,19 @@ public class FileDialogFragment extends DialogFragment {
public void onClick(DialogInterface dialog, int id) {
dismiss();
boolean checked = false;
if (mCheckBox.isEnabled()) {
checked = mCheckBox.isChecked();
String currentFilename = mFilename.getText().toString();
if (mOutputFilename == null || !mOutputFilename.equals(currentFilename)) {
mOutputUri = null;
mOutputFilename = mFilename.getText().toString();
}
boolean checked = mCheckBox.isEnabled() && mCheckBox.isChecked();
// return resulting data back to activity
Bundle data = new Bundle();
if (mOutputUri != null) {
data.putParcelable(MESSAGE_DATA_URI, mOutputUri);
}
data.putString(MESSAGE_DATA_FILENAME, mFilename.getText().toString());
data.putBoolean(MESSAGE_DATA_CHECKED, checked);
@ -178,6 +194,17 @@ public class FileDialogFragment extends DialogFragment {
switch (requestCode & 0xFFFF) {
case REQUEST_CODE: {
if (resultCode == Activity.RESULT_OK && data != null) {
if (Constants.KITKAT) {
mOutputUri = data.getData();
Cursor cursor = getActivity().getContentResolver().query(mOutputUri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
mOutputFilename = cursor.getString(0);
mFilename.setText(mOutputFilename);
}
cursor.close();
}
} else {
try {
String path = data.getData().getPath();
Log.d(Constants.TAG, "path=" + path);
@ -188,6 +215,7 @@ public class FileDialogFragment extends DialogFragment {
Log.e(Constants.TAG, "Nullpointer while retrieving path!", e);
}
}
}
break;
}