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