handle onActivityResult in FileDialogFragment directly

This commit is contained in:
Dominik Schürmann 2014-01-18 22:37:00 +01:00
parent e8b9dfe379
commit 96936dda23
9 changed files with 63 additions and 110 deletions

View File

@ -76,7 +76,7 @@ public final class Id {
public static final int public_keys = 0x00007001;
public static final int secret_keys = 0x00007002;
public static final int filename = 0x00007003;
public static final int output_filename = 0x00007004;
// public static final int output_filename = 0x00007004;
public static final int key_server_preference = 0x00007005;
public static final int look_up_key_id = 0x00007006;
public static final int export_to_server = 0x00007007;

View File

@ -27,7 +27,6 @@ import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment;
import org.sufficientlysecure.keychain.ui.dialog.FileDialogFragment;
import org.sufficientlysecure.keychain.util.Log;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
@ -107,7 +106,7 @@ public class ExportHelper {
}
mFileDialog = FileDialogFragment.newInstance(messenger, title, message,
exportFilename, null, Id.request.filename);
exportFilename, null);
mFileDialog.show(activity.getSupportFragmentManager(), "fileDialog");
}
@ -182,22 +181,4 @@ public class ExportHelper {
activity.startService(intent);
}
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Id.request.filename) {
if (resultCode == Activity.RESULT_OK && data != null) {
try {
String path = data.getData().getPath();
Log.d(Constants.TAG, "path=" + path);
// set filename used in export/import dialogs
mFileDialog.setFilename(path);
} catch (NullPointerException e) {
Log.e(Constants.TAG, "Nullpointer while retrieving path!", e);
}
}
return true;
}
return false;
}
}

View File

@ -629,8 +629,7 @@ public class DecryptActivity extends DrawerActivity {
mFileDialog = FileDialogFragment.newInstance(messenger,
getString(R.string.title_decrypt_to_file),
getString(R.string.specify_file_to_decrypt_to), mOutputFilename, null,
Id.request.output_filename);
getString(R.string.specify_file_to_decrypt_to), mOutputFilename, null);
mFileDialog.show(getSupportFragmentManager(), "fileDialog");
}
@ -826,20 +825,6 @@ public class DecryptActivity extends DrawerActivity {
return;
}
case Id.request.output_filename: {
if (resultCode == RESULT_OK && data != null) {
try {
String path = FileHelper.getPath(this, data.getData());
Log.d(Constants.TAG, "path=" + path);
mFileDialog.setFilename(path);
} catch (NullPointerException e) {
Log.e(Constants.TAG, "Nullpointer while retrieving path!");
}
}
return;
}
// this request is returned after LookupUnknownKeyDialogFragment started
// KeyServerQueryActivity and user looked uo key
case Id.request.look_up_key_id: {

View File

@ -663,10 +663,4 @@ public class EditKeyActivity extends SherlockFragmentActivity {
: getString(R.string.btn_set_passphrase));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mExportHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

View File

@ -590,8 +590,7 @@ public class EncryptActivity extends DrawerActivity {
mFileDialog = FileDialogFragment.newInstance(messenger,
getString(R.string.title_encrypt_to_file),
getString(R.string.specify_file_to_encrypt_to), mOutputFilename, null,
Id.request.output_filename);
getString(R.string.specify_file_to_encrypt_to), mOutputFilename, null);
mFileDialog.show(getSupportFragmentManager(), "fileDialog");
}
@ -973,20 +972,6 @@ public class EncryptActivity extends DrawerActivity {
return;
}
case Id.request.output_filename: {
if (resultCode == RESULT_OK && data != null) {
try {
String path = data.getData().getPath();
Log.d(Constants.TAG, "path=" + path);
mFileDialog.setFilename(path);
} catch (NullPointerException e) {
Log.e(Constants.TAG, "Nullpointer while retrieving path!");
}
}
return;
}
case Id.request.public_keys: {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2013 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -56,7 +56,7 @@ public class KeyListPublicActivity extends DrawerActivity {
switch (item.getItemId()) {
case R.id.menu_key_list_public_import:
Intent intentImport = new Intent(this, ImportKeysActivity.class);
startActivityForResult(intentImport, Id.request.import_from_qr_code);
startActivityForResult(intentImport, 0);
return true;
case R.id.menu_key_list_public_export:
@ -69,30 +69,27 @@ public class KeyListPublicActivity extends DrawerActivity {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mExportHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
// switch (requestCode) {
// case Id.request.look_up_key_id: {
// if (resultCode == RESULT_CANCELED || data == null
// || data.getStringExtra(KeyServerQueryActivity.RESULT_EXTRA_TEXT) == null) {
// return;
// }
//
// Intent intent = new Intent(this, KeyListPublicActivity.class);
// intent.setAction(KeyListPublicActivity.ACTION_IMPORT);
// intent.putExtra(KeyListPublicActivity.EXTRA_TEXT,
// data.getStringExtra(KeyListActivity.EXTRA_TEXT));
// handleActions(intent);
// break;
// }
//
// default: {
// super.onActivityResult(requestCode, resultCode, data);
// break;
// }
// }
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// switch (requestCode) {
// case Id.request.look_up_key_id: {
// if (resultCode == RESULT_CANCELED || data == null
// || data.getStringExtra(KeyServerQueryActivity.RESULT_EXTRA_TEXT) == null) {
// return;
// }
//
// Intent intent = new Intent(this, KeyListPublicActivity.class);
// intent.setAction(KeyListPublicActivity.ACTION_IMPORT);
// intent.putExtra(KeyListPublicActivity.EXTRA_TEXT,
// data.getStringExtra(KeyListActivity.EXTRA_TEXT));
// handleActions(intent);
// break;
// }
//
// default: {
// super.onActivityResult(requestCode, resultCode, data);
// break;
// }
// }
// }
}

View File

@ -91,11 +91,4 @@ public class KeyListSecretActivity extends DrawerActivity {
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mExportHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

View File

@ -503,11 +503,4 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements CreateN
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mExportHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2013 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,6 +27,7 @@ import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
@ -39,15 +40,12 @@ import android.widget.EditText;
import com.beardedhen.androidbootstrap.BootstrapButton;
// TODO: return result from file manager activity to this dialog! not the activity!
// do it like in ImportFileFragment!
public class FileDialogFragment extends DialogFragment {
private static final String ARG_MESSENGER = "messenger";
private static final String ARG_TITLE = "title";
private static final String ARG_MESSAGE = "message";
private static final String ARG_DEFAULT_FILE = "default_file";
private static final String ARG_CHECKBOX_TEXT = "checkbox_text";
private static final String ARG_REQUEST_CODE = "request_code";
public static final int MESSAGE_OKAY = 1;
@ -60,11 +58,13 @@ public class FileDialogFragment extends DialogFragment {
private BootstrapButton mBrowse;
private CheckBox mCheckBox;
private static final int REQUEST_CODE = 0x00007004;
/**
* Creates new instance of this file dialog fragment
*/
public static FileDialogFragment newInstance(Messenger messenger, String title, String message,
String defaultFile, String checkboxText, int requestCode) {
String defaultFile, String checkboxText) {
FileDialogFragment frag = new FileDialogFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_MESSENGER, messenger);
@ -73,7 +73,6 @@ public class FileDialogFragment extends DialogFragment {
args.putString(ARG_MESSAGE, message);
args.putString(ARG_DEFAULT_FILE, defaultFile);
args.putString(ARG_CHECKBOX_TEXT, checkboxText);
args.putInt(ARG_REQUEST_CODE, requestCode);
frag.setArguments(args);
@ -93,7 +92,6 @@ public class FileDialogFragment extends DialogFragment {
String message = getArguments().getString(ARG_MESSAGE);
String defaultFile = getArguments().getString(ARG_DEFAULT_FILE);
String checkboxText = getArguments().getString(ARG_CHECKBOX_TEXT);
final int requestCode = getArguments().getInt(ARG_REQUEST_CODE);
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@ -112,7 +110,8 @@ public class FileDialogFragment extends DialogFragment {
// only .asc or .gpg files
// setting it to text/plain prevents Cynaogenmod's file manager from selecting asc
// or gpg types!
FileHelper.openFile(activity, mFilename.getText().toString(), "*/*", requestCode);
FileHelper.openFile(FileDialogFragment.this, mFilename.getText().toString(), "*/*",
REQUEST_CODE);
}
});
@ -166,7 +165,7 @@ public class FileDialogFragment extends DialogFragment {
* @param progress
* @param max
*/
public void setFilename(String filename) {
private void setFilename(String filename) {
AlertDialog dialog = (AlertDialog) getDialog();
EditText filenameEditText = (EditText) dialog.findViewById(R.id.input);
@ -175,6 +174,32 @@ public class FileDialogFragment extends DialogFragment {
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode & 0xFFFF) {
case REQUEST_CODE: {
if (resultCode == Activity.RESULT_OK && data != null) {
try {
String path = data.getData().getPath();
Log.d(Constants.TAG, "path=" + path);
// set filename used in export/import dialogs
setFilename(path);
} catch (NullPointerException e) {
Log.e(Constants.TAG, "Nullpointer while retrieving path!", e);
}
}
break;
}
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
/**
* Send message back to handler which is initialized in a activity
*