mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-30 04:22:18 -05:00
use OI File Manager intents to handle open/save file selection
This commit is contained in:
parent
de6743e4f5
commit
09741b0286
BIN
res/drawable/ic_launcher_folder.png
Normal file
BIN
res/drawable/ic_launcher_folder.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
BIN
res/drawable/ic_launcher_folder_small.png
Normal file
BIN
res/drawable/ic_launcher_folder_small.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
35
res/layout/file_dialog.xml
Normal file
35
res/layout/file_dialog.xml
Normal file
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingLeft="5dip"
|
||||
android:paddingRight="5dip"
|
||||
>
|
||||
<EditText
|
||||
android:id="@+id/input"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
/>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/btn_browse"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/ic_launcher_folder_small" android:layout_width="wrap_content"/>
|
||||
</LinearLayout>
|
@ -65,5 +65,12 @@
|
||||
<string name="using_clipboard_content">Using clipboard content.</string>
|
||||
<string name="key_saved">Key saved.</string>
|
||||
<string name="set_a_pass_phrase">Set a pass phrase via the option menu first.</string>
|
||||
|
||||
<string name="no_filemanager_installed">OI File Manager not installed.</string>
|
||||
<string name="filemanager_title_open">Open...</string>
|
||||
<string name="filemanager_btn_open">Open</string>
|
||||
<string name="filemanager_title_save">Save As...</string>
|
||||
<string name="filemanager_btn_save">Save</string>
|
||||
|
||||
</resources>
|
||||
|
||||
|
78
src/org/openintents/intents/FileManager.java
Normal file
78
src/org/openintents/intents/FileManager.java
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) 2008 OpenIntents.org
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.openintents.intents;
|
||||
|
||||
// Version Dec 9, 2008
|
||||
|
||||
/**
|
||||
* Provides OpenIntents actions, extras, and categories used by providers.
|
||||
* <p>
|
||||
* These specifiers extend the standard Android specifiers.
|
||||
* </p>
|
||||
*/
|
||||
public final class FileManager {
|
||||
|
||||
/**
|
||||
* Activity Action: Pick a file through the file manager, or let user
|
||||
* specify a custom file name. Data is the current file name or file name
|
||||
* suggestion. Returns a new file name as file URI in data.
|
||||
*
|
||||
* <p>
|
||||
* Constant Value: "org.openintents.action.PICK_FILE"
|
||||
* </p>
|
||||
*/
|
||||
public static final String ACTION_PICK_FILE = "org.openintents.action.PICK_FILE";
|
||||
|
||||
/**
|
||||
* Activity Action: Pick a directory through the file manager, or let user
|
||||
* specify a custom file name. Data is the current directory name or
|
||||
* directory name suggestion. Returns a new directory name as file URI in
|
||||
* data.
|
||||
*
|
||||
* <p>
|
||||
* Constant Value: "org.openintents.action.PICK_DIRECTORY"
|
||||
* </p>
|
||||
*/
|
||||
public static final String ACTION_PICK_DIRECTORY = "org.openintents.action.PICK_DIRECTORY";
|
||||
|
||||
/**
|
||||
* The title to display.
|
||||
*
|
||||
* <p>
|
||||
* This is shown in the title bar of the file manager.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Constant Value: "org.openintents.extra.TITLE"
|
||||
* </p>
|
||||
*/
|
||||
public static final String EXTRA_TITLE = "org.openintents.extra.TITLE";
|
||||
|
||||
/**
|
||||
* The text on the button to display.
|
||||
*
|
||||
* <p>
|
||||
* Depending on the use, it makes sense to set this to "Open" or "Save".
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Constant Value: "org.openintents.extra.BUTTON_TEXT"
|
||||
* </p>
|
||||
*/
|
||||
public static final String EXTRA_BUTTON_TEXT = "org.openintents.extra.BUTTON_TEXT";
|
||||
|
||||
}
|
@ -16,34 +16,66 @@
|
||||
|
||||
package org.thialfihar.android.apg;
|
||||
|
||||
import org.openintents.intents.FileManager;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class FileDialog {
|
||||
public static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 12345;
|
||||
private static EditText mInput;
|
||||
private static ImageButton mBrowse;
|
||||
private static Activity mActivity;
|
||||
private static String mFileManagerTitle;
|
||||
private static String mFileManagerButton;
|
||||
|
||||
public static interface OnClickListener {
|
||||
public void onCancelClick();
|
||||
public void onOkClick(String filename);
|
||||
}
|
||||
|
||||
public static AlertDialog build(Context context, String title, String message,
|
||||
String defaultFile, OnClickListener onClickListener) {
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(context);
|
||||
public static AlertDialog build(Activity activity, String title, String message,
|
||||
String defaultFile, OnClickListener onClickListener,
|
||||
String fileManagerTitle, String fileManagerButton) {
|
||||
LayoutInflater inflater =
|
||||
(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
|
||||
|
||||
alert.setTitle(title);
|
||||
alert.setMessage(message);
|
||||
|
||||
final EditText input = new EditText(context);
|
||||
input.setText(defaultFile);
|
||||
alert.setView(input);
|
||||
View view = (View) inflater.inflate(R.layout.file_dialog, null);
|
||||
|
||||
mActivity = activity;
|
||||
mInput = (EditText) view.findViewById(R.id.input);
|
||||
mInput.setText(defaultFile);
|
||||
mBrowse = (ImageButton) view.findViewById(R.id.btn_browse);
|
||||
mBrowse.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
openFile();
|
||||
}
|
||||
});
|
||||
mFileManagerTitle = fileManagerTitle;
|
||||
mFileManagerButton = fileManagerButton;
|
||||
|
||||
alert.setView(view);
|
||||
|
||||
final OnClickListener clickListener = onClickListener;
|
||||
|
||||
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
clickListener.onOkClick(input.getText().toString());
|
||||
clickListener.onOkClick(mInput.getText().toString());
|
||||
}
|
||||
});
|
||||
|
||||
@ -55,4 +87,31 @@ public class FileDialog {
|
||||
});
|
||||
return alert.create();
|
||||
}
|
||||
|
||||
public static void setFilename(String filename) {
|
||||
if (mInput != null) {
|
||||
mInput.setText(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the file manager to select a file to open.
|
||||
*/
|
||||
private static void openFile() {
|
||||
String fileName = mInput.getText().toString();
|
||||
|
||||
Intent intent = new Intent(FileManager.ACTION_PICK_FILE);
|
||||
|
||||
intent.setData(Uri.parse("file://" + fileName));
|
||||
|
||||
intent.putExtra(FileManager.EXTRA_TITLE, mFileManagerTitle);
|
||||
intent.putExtra(FileManager.EXTRA_BUTTON_TEXT, mFileManagerButton);
|
||||
|
||||
try {
|
||||
mActivity.startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
// No compatible file manager was found.
|
||||
Toast.makeText(mActivity, R.string.no_filemanager_installed, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -72,6 +72,8 @@ public class MainActivity extends Activity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main);
|
||||
|
||||
Apg.initialize(this);
|
||||
|
||||
Button encryptMessageButton = (Button) findViewById(R.id.btn_encryptMessage);
|
||||
Button decryptMessageButton = (Button) findViewById(R.id.btn_decryptMessage);
|
||||
mAccounts = (ListView) findViewById(R.id.account_list);
|
||||
@ -220,9 +222,7 @@ public class MainActivity extends Activity {
|
||||
SpannableString info =
|
||||
new SpannableString("Read the warnings!\n\n" +
|
||||
"Changes:\n" +
|
||||
" * display signed-only mails\n" +
|
||||
" * verify signed-only mails\n" +
|
||||
" * bug fixes, layout fixes\n" +
|
||||
" * OI File Manager support\n" +
|
||||
"\n" +
|
||||
"WARNING: be careful editing your existing keys, as they " +
|
||||
"WILL be stripped of certificates right now.\n" +
|
||||
|
@ -31,10 +31,13 @@ import android.app.ExpandableListActivity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
@ -72,13 +75,14 @@ public class PublicKeyListActivity extends ExpandableListActivity
|
||||
static final int TASK_EXPORT = 2;
|
||||
|
||||
protected int mSelectedItem = -1;
|
||||
protected String mImportFilename = null;
|
||||
protected String mExportFilename = null;
|
||||
protected int mTask = 0;
|
||||
|
||||
private ProgressDialog mProgressDialog = null;
|
||||
private Thread mRunningThread = null;
|
||||
|
||||
private String mImportFilename = Environment.getExternalStorageDirectory() + "/pubring.gpg";
|
||||
private String mExportFilename = Environment.getExternalStorageDirectory() + "/pubexport.asc";
|
||||
|
||||
private Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
@ -301,7 +305,7 @@ public class PublicKeyListActivity extends ExpandableListActivity
|
||||
case DIALOG_IMPORT_KEYS: {
|
||||
return FileDialog.build(this, "Import Keys",
|
||||
"Please specify which file to import from.",
|
||||
Environment.getExternalStorageDirectory() + "/pubring.gpg",
|
||||
mImportFilename,
|
||||
new FileDialog.OnClickListener() {
|
||||
|
||||
@Override
|
||||
@ -315,7 +319,9 @@ public class PublicKeyListActivity extends ExpandableListActivity
|
||||
public void onCancelClick() {
|
||||
removeDialog(DIALOG_IMPORT_KEYS);
|
||||
}
|
||||
});
|
||||
},
|
||||
getString(R.string.filemanager_title_open),
|
||||
getString(R.string.filemanager_btn_open));
|
||||
}
|
||||
|
||||
case DIALOG_EXPORT_KEY: {
|
||||
@ -335,7 +341,7 @@ public class PublicKeyListActivity extends ExpandableListActivity
|
||||
return FileDialog.build(this, title,
|
||||
"Please specify which file to export to.\n" +
|
||||
"WARNING! File will be overwritten if it exists.",
|
||||
Environment.getExternalStorageDirectory() + "/pubexport.asc",
|
||||
mExportFilename,
|
||||
new FileDialog.OnClickListener() {
|
||||
|
||||
@Override
|
||||
@ -349,7 +355,9 @@ public class PublicKeyListActivity extends ExpandableListActivity
|
||||
public void onCancelClick() {
|
||||
removeDialog(thisDialogId);
|
||||
}
|
||||
});
|
||||
},
|
||||
getString(R.string.filemanager_title_save),
|
||||
getString(R.string.filemanager_btn_save));
|
||||
}
|
||||
|
||||
case DIALOG_IMPORTING: {
|
||||
@ -621,4 +629,32 @@ public class PublicKeyListActivity extends ExpandableListActivity
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
switch (requestCode) {
|
||||
case FileDialog.REQUEST_CODE_PICK_FILE_OR_DIRECTORY: {
|
||||
if (resultCode == RESULT_OK && data != null) {
|
||||
String filename = data.getDataString();
|
||||
if (filename != null) {
|
||||
// Get rid of URI prefix:
|
||||
if (filename.startsWith("file://")) {
|
||||
filename = filename.substring(7);
|
||||
}
|
||||
// replace %20 and so on
|
||||
filename = Uri.decode(filename);
|
||||
|
||||
FileDialog.setFilename(filename);
|
||||
}
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
@ -81,13 +82,14 @@ public class SecretKeyListActivity extends ExpandableListActivity
|
||||
static final int TASK_EXPORT = 2;
|
||||
|
||||
protected int mSelectedItem = -1;
|
||||
protected String mImportFilename = null;
|
||||
protected String mExportFilename = null;
|
||||
protected int mTask = 0;
|
||||
|
||||
private ProgressDialog mProgressDialog = null;
|
||||
private Thread mRunningThread = null;
|
||||
|
||||
private String mImportFilename = Environment.getExternalStorageDirectory() + "/secring.gpg";
|
||||
private String mExportFilename = Environment.getExternalStorageDirectory() + "/secexport.asc";
|
||||
|
||||
private Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
@ -335,7 +337,7 @@ public class SecretKeyListActivity extends ExpandableListActivity
|
||||
case DIALOG_IMPORT_KEYS: {
|
||||
return FileDialog.build(this, "Import Keys",
|
||||
"Please specify which file to import from.",
|
||||
Environment.getExternalStorageDirectory() + "/secring.gpg",
|
||||
mImportFilename,
|
||||
new FileDialog.OnClickListener() {
|
||||
|
||||
@Override
|
||||
@ -349,7 +351,9 @@ public class SecretKeyListActivity extends ExpandableListActivity
|
||||
public void onCancelClick() {
|
||||
removeDialog(DIALOG_IMPORT_KEYS);
|
||||
}
|
||||
});
|
||||
},
|
||||
getString(R.string.filemanager_title_open),
|
||||
getString(R.string.filemanager_btn_open));
|
||||
}
|
||||
|
||||
case DIALOG_EXPORT_KEY: {
|
||||
@ -370,7 +374,7 @@ public class SecretKeyListActivity extends ExpandableListActivity
|
||||
"Please specify which file to export to.\n" +
|
||||
"WARNING! You are about to export SECRET keys.\n" +
|
||||
"WARNING! File will be overwritten if it exists.",
|
||||
Environment.getExternalStorageDirectory() + "/secexport.asc",
|
||||
mExportFilename,
|
||||
new FileDialog.OnClickListener() {
|
||||
|
||||
@Override
|
||||
@ -384,7 +388,9 @@ public class SecretKeyListActivity extends ExpandableListActivity
|
||||
public void onCancelClick() {
|
||||
removeDialog(thisDialogId);
|
||||
}
|
||||
});
|
||||
},
|
||||
getString(R.string.filemanager_title_save),
|
||||
getString(R.string.filemanager_btn_save));
|
||||
}
|
||||
|
||||
case DIALOG_IMPORTING: {
|
||||
@ -441,6 +447,24 @@ public class SecretKeyListActivity extends ExpandableListActivity
|
||||
break;
|
||||
}
|
||||
|
||||
case FileDialog.REQUEST_CODE_PICK_FILE_OR_DIRECTORY: {
|
||||
if (resultCode == RESULT_OK && data != null) {
|
||||
String filename = data.getDataString();
|
||||
if (filename != null) {
|
||||
// Get rid of URI prefix:
|
||||
if (filename.startsWith("file://")) {
|
||||
filename = filename.substring(7);
|
||||
}
|
||||
// replace %20 and so on
|
||||
filename = Uri.decode(filename);
|
||||
|
||||
FileDialog.setFilename(filename);
|
||||
}
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user