some cleanup in FileHelper, and allow input of filename during export

This commit is contained in:
Vincent Breitmoser
2015-08-27 16:13:32 +02:00
parent 905e738621
commit 9e3c041549
9 changed files with 159 additions and 140 deletions

View File

@@ -47,7 +47,20 @@ public class BackupFragment extends Fragment {
private int mIndex;
static final int REQUEST_REPEAT_PASSPHRASE = 1;
private ExportHelper mExportHelper;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// we won't get attached to a non-fragment activity, so the cast should be safe
mExportHelper = new ExportHelper((FragmentActivity) activity);
}
@Override
public void onDetach() {
super.onDetach();
mExportHelper = null;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -80,8 +93,7 @@ public class BackupFragment extends Fragment {
}
if (!includeSecretKeys) {
ExportHelper exportHelper = new ExportHelper(activity);
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, false);
mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, false);
return;
}
@@ -136,8 +148,7 @@ public class BackupFragment extends Fragment {
return;
}
ExportHelper exportHelper = new ExportHelper(activity);
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true);
mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true);
}
}.execute(activity.getContentResolver());
@@ -167,8 +178,7 @@ public class BackupFragment extends Fragment {
return;
}
ExportHelper exportHelper = new ExportHelper(getActivity());
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true);
mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true);
}
}
}

View File

@@ -36,7 +36,6 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.DefaultItemAnimator;
@@ -222,18 +221,6 @@ public class DecryptListFragment
}
}
private void askForOutputFilename(Uri inputUri, String originalFilename, String mimeType) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
File file = new File(inputUri.getPath());
File parentDir = file.exists() ? file.getParentFile() : Constants.Path.APP_DIR;
File targetFile = new File(parentDir, originalFilename);
FileHelper.saveFile(this, getString(R.string.title_decrypt_to_file),
getString(R.string.specify_file_to_decrypt_to), targetFile, REQUEST_CODE_OUTPUT);
} else {
FileHelper.saveDocument(this, mimeType, originalFilename, REQUEST_CODE_OUTPUT);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
@@ -534,7 +521,8 @@ public class DecryptListFragment
return true;
}
mCurrentInputUri = model.mInputUri;
askForOutputFilename(model.mInputUri, metadata.getFilename(), metadata.getMimeType());
FileHelper.saveDocument(this, metadata.getFilename(), model.mInputUri, metadata.getMimeType(),
R.string.title_decrypt_to_file, R.string.specify_file_to_decrypt_to, REQUEST_CODE_OUTPUT);
return true;
case R.id.decrypt_delete:
deleteFile(activity, model.mInputUri);

View File

@@ -83,11 +83,7 @@ public class EncryptDecryptOverviewFragment extends Fragment {
mDecryptFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.openDocument(EncryptDecryptOverviewFragment.this, "*/*", REQUEST_CODE_INPUT);
} else {
FileHelper.openFile(EncryptDecryptOverviewFragment.this, null, "*/*", REQUEST_CODE_INPUT);
}
FileHelper.openDocument(EncryptDecryptOverviewFragment.this, null, "*/*", false, REQUEST_CODE_INPUT);
}
});

View File

@@ -18,7 +18,6 @@
package org.sufficientlysecure.keychain.ui;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
@@ -196,13 +195,9 @@ public class EncryptFilesFragment
}
private void addInputUri() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.openDocument(EncryptFilesFragment.this, "*/*", true, REQUEST_CODE_INPUT);
} else {
FileHelper.openFile(EncryptFilesFragment.this, mFilesAdapter.getModelCount() == 0 ?
null : mFilesAdapter.getModelItem(mFilesAdapter.getModelCount() - 1).inputUri,
"*/*", REQUEST_CODE_INPUT);
}
FileHelper.openDocument(EncryptFilesFragment.this, mFilesAdapter.getModelCount() == 0 ?
null : mFilesAdapter.getModelItem(mFilesAdapter.getModelCount() - 1).inputUri,
"*/*", true, REQUEST_CODE_INPUT);
}
private void addInputUri(Uri inputUri) {
@@ -230,19 +225,8 @@ public class EncryptFilesFragment
(mEncryptFilenames ? "1" : FileHelper.getFilename(getActivity(), model.inputUri))
+ (mUseArmor ? Constants.FILE_EXTENSION_ASC : Constants.FILE_EXTENSION_PGP_MAIN);
Uri inputUri = model.inputUri;
saveDocumentIntent(targetName, inputUri);
}
private void saveDocumentIntent(String targetName, Uri inputUri) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
File file = new File(inputUri.getPath());
File parentDir = file.exists() ? file.getParentFile() : Constants.Path.APP_DIR;
File targetFile = new File(parentDir, targetName);
FileHelper.saveFile(this, getString(R.string.title_encrypt_to_file),
getString(R.string.specify_file_to_encrypt_to), targetFile, REQUEST_CODE_OUTPUT);
} else {
FileHelper.saveDocument(this, "*/*", targetName, REQUEST_CODE_OUTPUT);
}
FileHelper.saveDocument(this, targetName, inputUri,
R.string.title_encrypt_to_file, R.string.specify_file_to_encrypt_to, REQUEST_CODE_OUTPUT);
}
public void addFile(Intent data) {

View File

@@ -64,8 +64,8 @@ public class ImportKeysFileFragment extends Fragment {
// open .asc or .gpg files
// setting it to text/plain prevents Cyanogenmod's file manager from selecting asc
// or gpg types!
FileHelper.openFile(ImportKeysFileFragment.this, Uri.fromFile(Constants.Path.APP_DIR),
"*/*", REQUEST_CODE_FILE);
FileHelper.openDocument(ImportKeysFileFragment.this,
Uri.fromFile(Constants.Path.APP_DIR), "*/*", false, REQUEST_CODE_FILE);
}
});

View File

@@ -252,7 +252,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
String message = this.getString(R.string.specify_file_to_export_log_to);
FileHelper.saveFile(new FileHelper.FileDialogCallback() {
FileHelper.saveDocumentDialog(new FileHelper.FileDialogCallback() {
@Override
public void onFileSelected(File file, boolean checked) {
writeToLogFile(mResult.getLog(), file);

View File

@@ -23,6 +23,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
@@ -119,14 +120,19 @@ public class FileDialogFragment extends DialogFragment {
mFilename = (EditText) view.findViewById(R.id.input);
mFilename.setText(mFile.getName());
mBrowse = (ImageButton) 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, Uri.fromFile(mFile), "*/*", REQUEST_CODE);
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mBrowse.setVisibility(View.GONE);
} else {
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.saveDocumentKitKat(
FileDialogFragment.this, "*/*", mFile.getName(), REQUEST_CODE);
}
});
}
mCheckBox = (CheckBox) view.findViewById(R.id.checkbox);
if (checkboxText == null) {