/
OpenFileDialog.java
242 lines (221 loc) · 6.91 KB
/
OpenFileDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
* Copyright (c) 1999, 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.paho.android.sample.service;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.eclipse.paho.android.sample.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/**
* Add SSL key file selector
* @author foxxiang
*
*/
public class OpenFileDialog {
public static String tag = "OpenFileDialog";
static final public String sRoot = "/";
static final public String sParent = "..";
static final public String sFolder = ".";
static final public String sEmpty = "";
static final private String sOnErrorMsg = "No rights to access!";
/**
* Create a File Selector Dialog windows
* @param id Dialog Id
* @param context Context that the application is running in
* @param title The tile of File Selector Window
* @param callback A callback Bundle interface for data transport
* @param suffix The file name suffix. E.g. .bks , .pem
* @param images The resource id for file icon
* @return The Dialog Window
*/
public static Dialog createDialog(int id, Context context, String title, CallbackBundle callback, String suffix, Map<String, Integer> images){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(new FileSelectView(context, id, callback, suffix, images));
Dialog dialog = builder.create();
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setTitle(title);
return dialog;
}
/** The FileSelect View with OnItemClick Listener*/
static class FileSelectView extends ListView implements OnItemClickListener{
private CallbackBundle callback = null;
private String path = sRoot;
private List<Map<String, Object>> list = null;
private int dialogid = 0;
private String suffix = null;
private Map<String, Integer> imagemap = null;
/**
* Create the File Selector Dialog Window View
* @param id Dialog Id
* @param context Context that the application is running in
* @param title The tile of File Selector Window
* @param callback A callback Bundle interface for data transport
* @param suffix The file name suffix. E.g. .bks , .pem
* @param images The resource id for file icon
*/
public FileSelectView(Context context, int dialogid, CallbackBundle callback, String suffix, Map<String, Integer> images) {
super(context);
this.imagemap = images;
this.suffix = suffix==null?"":suffix.toLowerCase(Locale.getDefault());
this.callback = callback;
this.dialogid = dialogid;
this.setOnItemClickListener(this);
refreshFileList();
}
/**
* Query the suffix of file which want to filter
* @param filename
* @return
*/
private String getSuffix(String filename){
int dix = filename.lastIndexOf('.');
if(dix<0){
return "";
}
else{
return filename.substring(dix+1);
}
}
/**
* Get The Image resource ID
* @param s
* @return
*/
private int getImageId(String s){
if(imagemap == null){
return 0;
}
else if(imagemap.containsKey(s)){
return imagemap.get(s);
}
else if(imagemap.containsKey(sEmpty)){
return imagemap.get(sEmpty);
}
else {
return 0;
}
}
/**
* Refresh the file list in Window
* @return
*/
private int refreshFileList()
{
File[] files = null;
try{
files = new File(path).listFiles();
}
catch(Exception e){
files = null;
}
if(files==null){
Toast.makeText(getContext(), sOnErrorMsg,Toast.LENGTH_SHORT).show();
return -1;
}
if(list != null){
list.clear();
}
else{
list = new ArrayList<Map<String, Object>>(files.length);
}
ArrayList<Map<String, Object>> lfolders = new ArrayList<Map<String, Object>>();
ArrayList<Map<String, Object>> lfiles = new ArrayList<Map<String, Object>>();
if(!this.path.equals(sRoot)){
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", sRoot);
map.put("path", sRoot);
map.put("img", getImageId(sRoot));
list.add(map);
map = new HashMap<String, Object>();
map.put("name", sParent);
map.put("path", path);
map.put("img", getImageId(sParent));
list.add(map);
}
for(File file: files)
{
if(file.isDirectory() && file.listFiles()!=null){
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", file.getName());
map.put("path", file.getPath());
map.put("img", getImageId(sFolder));
lfolders.add(map);
}
else if(file.isFile()){
String sf = getSuffix(file.getName()).toLowerCase(Locale.getDefault());
if(suffix == null || suffix.length()==0 || (sf.length()>0 && suffix.indexOf("."+sf+";")>=0)){
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", file.getName());
map.put("path", file.getPath());
map.put("img", getImageId(sf));
lfiles.add(map);
}
}
}
list.addAll(lfolders);
list.addAll(lfiles);
SimpleAdapter adapter = new SimpleAdapter(getContext(), list, R.layout.filedialogitem, new String[]{"img", "name", "path"}, new int[]{R.id.filedialogitem_img, R.id.filedialogitem_name, R.id.filedialogitem_path});
this.setAdapter(adapter);
return files.length;
}
/**
* OnItemClick action
*
* @see ListView#onItemClick(AdapterView<?> parent, View v, int position, long id)
*/
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String pt = (String) list.get(position).get("path");
String fn = (String) list.get(position).get("name");
if(fn.equals(sRoot) || fn.equals(sParent)){
File fl = new File(pt);
String ppt = fl.getParent();
if(ppt != null){
path = ppt;
}
else{
path = sRoot;
}
}
else{
File fl = new File(pt);
if(fl.isFile()){
((Activity)getContext()).dismissDialog(this.dialogid);
Bundle bundle = new Bundle();
bundle.putString("path", pt);
bundle.putString("name", fn);
this.callback.callback(bundle);
return;
}
else if(fl.isDirectory()){
path = pt;
}
}
this.refreshFileList();
}
}
}