0
|
1 package net.bgreco;
|
1
|
2 /**
|
|
3 Copyright (C) 2011 by Brad Greco <brad@bgreco.net>
|
0
|
4
|
1
|
5 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6 of this software and associated documentation files (the "Software"), to deal
|
|
7 in the Software without restriction, including without limitation the rights
|
|
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9 copies of the Software, and to permit persons to whom the Software is
|
|
10 furnished to do so, subject to the following conditions:
|
|
11
|
|
12 The above copyright notice and this permission notice shall be included in
|
|
13 all copies or substantial portions of the Software.
|
|
14
|
|
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21 THE SOFTWARE.
|
|
22 */
|
0
|
23 import java.io.File;
|
|
24 import java.util.ArrayList;
|
|
25 import java.util.Collections;
|
|
26
|
|
27 import android.app.ListActivity;
|
|
28 import android.content.Context;
|
|
29 import android.content.Intent;
|
|
30 import android.os.Bundle;
|
|
31 import android.os.Environment;
|
|
32 import android.view.View;
|
|
33 import android.widget.AdapterView;
|
|
34 import android.widget.AdapterView.OnItemClickListener;
|
|
35 import android.widget.ArrayAdapter;
|
|
36 import android.widget.Button;
|
|
37 import android.widget.ListView;
|
|
38 import android.widget.Toast;
|
|
39
|
|
40 public class DirectoryPicker extends ListActivity {
|
|
41
|
|
42 public static final String START_DIR = "startDir";
|
|
43 public static final String ONLY_DIRS = "onlyDirs";
|
|
44 public static final String SHOW_HIDDEN = "showHidden";
|
|
45 public static final String CHOSEN_DIRECTORY = "chosenDir";
|
|
46 public static final int PICK_DIRECTORY = 43522432;
|
|
47 private File dir;
|
|
48
|
|
49 @Override
|
|
50 public void onCreate(Bundle savedInstanceState) {
|
|
51 super.onCreate(savedInstanceState);
|
|
52 Bundle extras = getIntent().getExtras();
|
|
53 dir = Environment.getExternalStorageDirectory();
|
|
54 String preferredStartDir = extras.getString(START_DIR);
|
|
55 final boolean showHidden = extras.getBoolean(SHOW_HIDDEN, false);
|
|
56 final boolean onlyDirs = extras.getBoolean(ONLY_DIRS, true);
|
|
57 if(preferredStartDir != null) {
|
|
58 File startDir = new File(preferredStartDir);
|
|
59 if(startDir.isDirectory()) {
|
|
60 dir = startDir;
|
|
61 }
|
|
62 }
|
|
63
|
|
64 setContentView(R.layout.chooser_list);
|
|
65 setTitle(dir.getAbsolutePath());
|
|
66 Button btnChoose = (Button) findViewById(R.id.btnChoose);
|
|
67 String name = dir.getName();
|
|
68 if(name.length() == 0)
|
|
69 name = "/";
|
|
70 btnChoose.setText("Choose " + "'" + name + "'");
|
|
71 btnChoose.setOnClickListener(new View.OnClickListener() {
|
|
72 public void onClick(View v) {
|
|
73 returnDir(dir.getAbsolutePath());
|
|
74 }
|
|
75 });
|
|
76
|
|
77 ListView lv = getListView();
|
|
78 lv.setTextFilterEnabled(true);
|
|
79
|
|
80 if(!dir.canRead()) {
|
|
81 Context context = getApplicationContext();
|
|
82 String msg = "Could not read folder contents.";
|
|
83 Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
|
|
84 toast.show();
|
|
85 return;
|
|
86 }
|
|
87
|
|
88 final ArrayList<File> files = filter(dir.listFiles(), onlyDirs, showHidden);
|
|
89 String[] names = names(files);
|
|
90 setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, names));
|
|
91
|
|
92
|
|
93 lv.setOnItemClickListener(new OnItemClickListener() {
|
|
94 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
95 if(!files.get(position).isDirectory())
|
|
96 return;
|
|
97 String path = files.get(position).getAbsolutePath();
|
|
98 Intent intent = new Intent(DirectoryPicker.this, DirectoryPicker.class);
|
|
99 intent.putExtra(DirectoryPicker.START_DIR, path);
|
|
100 intent.putExtra(DirectoryPicker.SHOW_HIDDEN, showHidden);
|
|
101 intent.putExtra(DirectoryPicker.ONLY_DIRS, onlyDirs);
|
|
102 startActivityForResult(intent, PICK_DIRECTORY);
|
|
103 }
|
|
104 });
|
|
105 }
|
|
106
|
|
107 @Override
|
|
108 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
109 if(requestCode == PICK_DIRECTORY && resultCode == RESULT_OK) {
|
|
110 Bundle extras = data.getExtras();
|
|
111 String path = (String) extras.get(DirectoryPicker.CHOSEN_DIRECTORY);
|
|
112 returnDir(path);
|
|
113 }
|
|
114 }
|
|
115
|
|
116 private void returnDir(String path) {
|
|
117 Intent result = new Intent();
|
|
118 result.putExtra(CHOSEN_DIRECTORY, path);
|
|
119 setResult(RESULT_OK, result);
|
|
120 finish();
|
|
121 }
|
|
122
|
|
123 public ArrayList<File> filter(File[] file_list, boolean onlyDirs, boolean showHidden) {
|
|
124 ArrayList<File> files = new ArrayList<File>();
|
|
125 for(File file: file_list) {
|
|
126 if(onlyDirs && !file.isDirectory())
|
|
127 continue;
|
|
128 if(!showHidden && file.isHidden())
|
|
129 continue;
|
|
130 files.add(file);
|
|
131 }
|
|
132 Collections.sort(files);
|
|
133 return files;
|
|
134 }
|
|
135
|
|
136 public String[] names(ArrayList<File> files) {
|
|
137 String[] names = new String[files.size()];
|
|
138 int i = 0;
|
|
139 for(File file: files) {
|
|
140 names[i] = file.getName();
|
|
141 i++;
|
|
142 }
|
|
143 return names;
|
|
144 }
|
|
145 }
|
|
146
|