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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
package cgeo.geocaching.files;
import butterknife.ButterKnife;
import cgeo.geocaching.Intents;
import cgeo.geocaching.R;
import cgeo.geocaching.activity.AbstractListActivity;
import cgeo.geocaching.activity.ActivityMixin;
import org.apache.commons.lang3.StringUtils;
import android.app.AlertDialog;
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.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Dialog for choosing a file or directory.
*/
public class SimpleDirChooser extends AbstractListActivity {
public static final String EXTRA_CHOOSE_FOR_WRITING = "chooseForWriting";
private static final String PARENT_DIR = ".. ";
private File currentDir;
private FileArrayAdapter adapter;
private Button okButton = null;
private int lastPosition = -1;
private boolean chooseForWriting = false;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Bundle extras = getIntent().getExtras();
currentDir = dirContaining(extras.getString(Intents.EXTRA_START_DIR));
chooseForWriting = extras.getBoolean(SimpleDirChooser.EXTRA_CHOOSE_FOR_WRITING, false);
ActivityMixin.setTheme(this);
setContentView(R.layout.simple_dir_chooser);
fill(currentDir);
okButton = (Button) findViewById(R.id.simple_dir_chooser_ok);
resetOkButton();
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
setResult(RESULT_OK, new Intent()
.setData(Uri.fromFile(new File(currentDir, adapter.getItem(lastPosition).getName()))));
finish();
}
});
final Button cancelButton = (Button) findViewById(R.id.simple_dir_chooser_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final Intent intent = new Intent();
setResult(RESULT_CANCELED, intent);
finish();
}
});
final EditText pathField = (EditText) findViewById(R.id.simple_dir_chooser_path);
pathField.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
editPath();
}
});
}
public void editPath() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.simple_dir_chooser_current_path);
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(currentDir.getPath());
builder.setView(input);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
final String pathText = input.getText().toString();
final File newPathDir = new File(pathText);
if (newPathDir.exists() && newPathDir.isDirectory()) {
currentDir = newPathDir;
fill(currentDir);
} else {
showToast(SimpleDirChooser.this.getResources().getString(R.string.simple_dir_chooser_invalid_path));
}
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
dialog.cancel();
}
});
builder.show();
}
/**
* Return the directory containing a given path, or a sensible default.
*
* @param path the path to get the enclosing directory from, can be null or empty
* @return the directory containing <code>path</code>, or a sensible default if none
*/
private static File dirContaining(final String path) {
return StringUtils.contains(path, File.separatorChar) ?
new File(StringUtils.substringBeforeLast(path, Character.toString(File.separatorChar))) :
Environment.getExternalStorageDirectory();
}
private void fill(final File dir) {
lastPosition = -1;
resetOkButton();
final EditText path = (EditText) findViewById(R.id.simple_dir_chooser_path);
path.setText(this.getResources().getString(R.string.simple_dir_chooser_current_path) + " " + dir.getAbsolutePath());
final File[] dirs = dir.listFiles(new DirOnlyFilenameFilter());
final List<Option> listDirs = new ArrayList<>();
try {
for (final File currentDir : dirs) {
listDirs.add(new Option(currentDir.getName(), currentDir.getAbsolutePath(), currentDir.canWrite()));
}
} catch (final RuntimeException e) {
}
Collections.sort(listDirs, Option.NAME_COMPARATOR);
if (dir.getParent() != null) {
listDirs.add(0, new Option(PARENT_DIR, dir.getParent(), false));
}
this.adapter = new FileArrayAdapter(this, R.layout.simple_dir_item, listDirs);
this.setListAdapter(adapter);
}
private void resetOkButton() {
if (okButton != null) {
okButton.setEnabled(false);
okButton.setVisibility(View.INVISIBLE);
}
}
public class FileArrayAdapter extends ArrayAdapter<Option> {
private final Context context;
private final int id;
private final List<Option> items;
public FileArrayAdapter(final Context context, final int simpleDirItemResId, final List<Option> objects) {
super(context, simpleDirItemResId, objects);
this.context = context;
this.id = simpleDirItemResId;
this.items = objects;
}
@Override
public Option getItem(final int index) {
return items.get(index);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View v = convertView;
if (v == null) {
final LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
final Option option = items.get(position);
if (option != null) {
final TextView t1 = ButterKnife.findById(v, R.id.TextView01);
if (t1 != null) {
t1.setOnClickListener(new OnTextViewClickListener(position));
t1.setText(option.getName());
}
final CheckBox check = ButterKnife.findById(v, R.id.CheckBox);
if (check != null) {
if (!chooseForWriting || option.isWriteable()) {
check.setOnClickListener(new OnCheckBoxClickListener(position));
check.setChecked(option.isChecked());
check.setEnabled(true);
} else {
check.setEnabled(false);
}
}
}
return v;
}
}
public class OnTextViewClickListener implements OnClickListener {
private final int position;
OnTextViewClickListener(final int position) {
this.position = position;
}
@Override
public void onClick(final View arg0) {
final Option option = adapter.getItem(position);
if (option.getName().equals(PARENT_DIR)) {
currentDir = new File(option.getPath());
fill(currentDir);
} else {
final File dir = new File(option.getPath());
if (dir.list(new DirOnlyFilenameFilter()).length > 0) {
currentDir = dir;
fill(currentDir);
}
}
}
}
public class OnCheckBoxClickListener implements OnClickListener {
private final int position;
OnCheckBoxClickListener(final int position) {
this.position = position;
}
@Override
public void onClick(final View arg0) {
final Option lastOption = (lastPosition > -1) ? adapter.getItem(lastPosition) : null;
final Option currentOption = adapter.getItem(position);
if (lastOption != null) {
lastOption.setChecked(false);
}
if (currentOption != lastOption) {
currentOption.setChecked(true);
lastPosition = position;
} else {
lastPosition = -1;
}
final boolean enabled = currentOption.isChecked() && !currentOption.getName().equals(PARENT_DIR);
okButton.setEnabled(enabled);
okButton.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
adapter.notifyDataSetChanged();
}
}
public static class Option {
private final String name;
private final String path;
private boolean checked = false;
private boolean writeable = false;
private final static Comparator<Option> NAME_COMPARATOR = new Comparator<SimpleDirChooser.Option>() {
@Override
public int compare(final Option lhs, final Option rhs) {
return String.CASE_INSENSITIVE_ORDER.compare(lhs.name, rhs.name);
}
};
public Option(final String name, final String path, final boolean writeable) {
this.name = name;
this.path = path;
this.writeable = writeable;
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
public boolean isChecked() {
return this.checked;
}
public void setChecked(final boolean checked) {
this.checked = checked;
}
public boolean isWriteable() {
return writeable;
}
}
public static class DirOnlyFilenameFilter implements FilenameFilter {
@Override
public boolean accept(final File dir, final String filename) {
final File file = new File(dir, filename);
return file.isDirectory() && file.canRead();
}
}
}
|