comparison src/com/five_ten_sg/connectbot/util/FileChooser.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1 /*
2 * File Chooser Class for VX ConnectBot
3 * Copyright 2012 Martin Matuska
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package com.five_ten_sg.connectbot.util;
18
19 import java.io.File;
20 import java.net.URI;
21
22 import org.openintents.intents.FileManagerIntents;
23
24 import com.five_ten_sg.connectbot.R;
25 import android.app.Activity;
26 import android.content.ActivityNotFoundException;
27 import android.content.Intent;
28 import android.content.SharedPreferences;
29 import android.net.Uri;
30 import android.os.Environment;
31 import android.preference.PreferenceManager;
32 import android.util.Log;
33 import android.widget.Toast;
34
35 import com.lamerman.FileDialog;
36 import com.lamerman.SelectionMode;
37
38 public class FileChooser {
39 public final static String TAG = "ConnectBot.FileChooser";
40
41 public static final int REQUEST_CODE_SELECT_FILE = 1;
42
43 // Constants for AndExplorer's file picking intent
44 private static final String ANDEXPLORER_TITLE = "explorer_title";
45 private static final String MIME_TYPE_ANDEXPLORER_FILE = "vnd.android.cursor.dir/lysesoft.andexplorer.file";
46
47 public static void selectFile(Activity source, FileChooserCallback callback, int requestcode) {
48 selectFile(source, callback, requestcode, null);
49 }
50
51 public static void selectFile(Activity source, FileChooserCallback callback, int requestcode, String title) {
52 final File sdcard = Environment.getExternalStorageDirectory();
53
54 if (title == null)
55 title = source.getString(R.string.file_chooser_select_file);
56
57 int mode = SelectionMode.MODE_OPEN;
58 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(source);
59 Intent intent = null;
60 String filedialog;
61 String appString = null;
62
63 if (prefs == null)
64 return;
65
66 filedialog = prefs.getString(PreferenceConstants.FILE_DIALOG, "built-in");
67
68 if (filedialog.equals("OI")) {
69 appString = "OpenIntents File Manager";
70 intent = new Intent(FileManagerIntents.ACTION_PICK_FILE);
71 intent.setData(Uri.fromFile(sdcard));
72 intent.putExtra(FileManagerIntents.EXTRA_TITLE, title);
73 intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, source.getString(android.R.string.ok));
74 }
75 else if (filedialog.equals("AE")) {
76 appString = "AndExplorer";
77 intent = new Intent(Intent.ACTION_PICK);
78 intent.setDataAndType(Uri.fromFile(sdcard), MIME_TYPE_ANDEXPLORER_FILE);
79 intent.putExtra(ANDEXPLORER_TITLE, title);
80 }
81
82 if (intent != null && appString != null) {
83 try {
84 source.startActivityForResult(intent, requestcode);
85 return;
86 }
87 catch (ActivityNotFoundException e1) {
88 Toast.makeText(source,
89 source.getString(R.string.error_starting_app, appString),
90 Toast.LENGTH_LONG).show();
91 }
92 }
93
94 intent = new Intent(source.getBaseContext(), FileDialog.class);
95 intent.putExtra(FileDialog.START_PATH, sdcard.toString());
96 intent.putExtra(FileDialog.TITLE, title);
97 intent.putExtra(FileDialog.SELECTION_MODE, mode);
98 source.startActivityForResult(intent, requestcode);
99 }
100
101 public static File getSelectedFile(Intent intent) {
102 File file = null;
103
104 if (intent == null)
105 return null;
106
107 Uri uri = intent.getData();
108
109 try {
110 if (uri != null) {
111 file = new File(URI.create(uri.toString()));
112 }
113 else {
114 String filename = intent.getDataString();
115
116 if (filename != null)
117 file = new File(URI.create(filename));
118 }
119 }
120 catch (IllegalArgumentException e) {
121 Log.e(TAG, "Couldn't read selected file", e);
122 return null;
123 }
124
125 return file;
126 }
127 }