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