4
* Copyright (C) 2010 Tim Marston <tim@ed.am>
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/import-contacts
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package am.ed.importcontacts;
27
import java.io.FileFilter;
28
import java.io.IOException;
29
import java.util.ArrayList;
30
import java.util.Collections;
31
import java.util.Comparator;
33
import android.app.AlertDialog;
34
import android.app.Dialog;
35
import android.content.Context;
36
import android.content.DialogInterface;
37
import android.os.Environment;
38
import android.view.LayoutInflater;
39
import android.view.View;
40
import android.view.View.OnClickListener;
41
import android.view.ViewGroup;
42
import android.widget.AdapterView;
43
import android.widget.AdapterView.OnItemClickListener;
44
import android.widget.ArrayAdapter;
45
import android.widget.Button;
46
import android.widget.ImageView;
47
import android.widget.ListView;
48
import android.widget.TextView;
50
public class FileChooser
52
// pick an existing directory
53
public final static int MODE_DIR = 1;
55
// pick an existing file
56
public final static int MODE_FILE = 2;
59
private Dialog _dialog;
62
private int _mode = MODE_DIR;
71
private String _filename;
73
// enforce extension (in file-mode)
74
private String[] _extensions;
76
// path to secretly prefix all paths with
77
private String _path_prefix = "";
79
private Context _context;
80
private ArrayList< RowItem > _items;
81
private DialogInterface.OnDismissListener _on_dismiss_listener;
83
// class that represents a row in the list
84
private class RowItem implements Comparable< RowItem >
87
private boolean _directory;
89
public RowItem( String name, boolean directory )
92
_directory = directory;
95
public String getName()
100
public boolean isDirectory()
106
public int compareTo( RowItem that )
108
if( this._directory && !that._directory )
110
else if( !this._directory && that._directory )
113
return this._name.compareToIgnoreCase( that._name );
117
// class to manage our list of RowItems
118
private class RowItemAdapter extends ArrayAdapter< RowItem >
120
private ArrayList< RowItem > _items;
122
public RowItemAdapter( Context context, int textview_resource_id,
123
ArrayList< RowItem > items )
125
super( context, textview_resource_id, items );
130
public View getView( int position, View convert_view, ViewGroup parent )
132
View view = convert_view;
134
LayoutInflater factory = LayoutInflater.from( _context );
135
view = factory.inflate( R.layout.filechooser_row, null );
137
RowItem rowitem = _items.get( position );
138
if( rowitem != null ) {
139
( (TextView)view.findViewById( R.id.name ) )
140
.setText( rowitem.getName() );
141
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
142
rowitem.isDirectory()? View.VISIBLE : View.GONE );
148
@SuppressWarnings( "serial" )
149
class InvalidPathPrefixException extends RuntimeException
156
public FileChooser( Context context )
161
public void setMode( int mode )
166
public void setPath( String path )
168
_path = cleanUpPath( path );
169
File file = new File( _path_prefix + path.trim() );
172
if( file.isFile() ) {
173
_path = _path.substring( 0, _path.length() - 1 );
174
_filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
175
_path = _path.substring( 0, _path.length() - _filename.length() );
178
// else, treat as just a path
183
public void setExtensions( String[] extensions )
185
_extensions = extensions;
188
// set dismiss listener
189
public void setDismissListener(
190
DialogInterface.OnDismissListener on_dismiss_listener )
192
_on_dismiss_listener = on_dismiss_listener;
195
// set the path prefix
196
public void setPathPrefix( String path_prefix )
198
// set to cleaned-up path, with trailing '/' removed so that it can be
199
// trivially pre-pended to a cleaned-up path
200
_path_prefix = cleanUpPath( path_prefix );
201
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
204
public boolean getOk()
209
public String getPath()
211
return _path + _filename;
214
public Dialog onCreateDialog()
216
// custom layout in an AlertDialog
217
LayoutInflater factory = LayoutInflater.from( _context );
218
final View dialogView = factory.inflate(
219
R.layout.filechooser, null );
222
( (Button)dialogView.findViewById( R.id.ok ) )
223
.setOnClickListener( _fileChooserButtonListener );
224
( (ListView)dialogView.findViewById( R.id.list ) )
225
.setOnItemClickListener( _fileChooserItemClickListener );
228
Dialog dialog = new AlertDialog.Builder( _context )
230
.setView( dialogView )
232
dialog.setOnDismissListener( _on_dismiss_listener );
236
private OnClickListener _fileChooserButtonListener = new OnClickListener() {
237
public void onClick( View view )
239
switch( view.getId() )
242
// close dialog and free (don't keep a reference)
250
private OnItemClickListener _fileChooserItemClickListener =
251
new OnItemClickListener() {
252
public void onItemClick( AdapterView< ? > adapter_view, View view,
253
int position, long id )
255
RowItem rowitem = _items.get( position );
257
// handle directory changes
258
if( rowitem.isDirectory() )
260
String dirname = rowitem.getName();
261
if( dirname.equals( ".." ) )
262
strtipLastFilepartFromPath();
264
_path += dirname + "/";
270
// handle file selections
273
_filename = rowitem.getName();
274
updateCurrentSelection();
279
public void onPrepareDialog( Context context, Dialog dialog )
281
// set up reference to dialog
288
// pick text based on mode
289
int title = 0, current = 0;
292
title = R.string.filechooser_title_dir;
293
current = R.string.filechooser_current_dir;
296
title = R.string.filechooser_title_file;
297
current = R.string.filechooser_current_file;
300
dialog.setTitle( title );
301
( (TextView)dialog.findViewById( R.id.current ) )
302
.setText( _context.getString( current ) );
304
// clear filename in directory mode
305
if( _mode == MODE_DIR )
308
// set root path icon
309
( (ImageView)_dialog.findViewById( R.id.icon ) )
310
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
312
// setup current-path-specific stuff
316
public static String cleanUpPath( String path )
320
// ensure it starts and ends in a '/'
321
if( !path.startsWith( "/" ) ) path = "/" + path;
322
if( !path.endsWith( "/" ) ) path += "/";
327
public static int pathIcon( String path )
332
sdcard_path = Environment.getExternalStorageDirectory()
334
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
337
catch( IOException e ) {
342
if( sdcard_path != null && path.equals( sdcard_path ) )
343
return R.drawable.sdcard;
346
return R.drawable.directory;
349
public String prettyPrint( String full_path, boolean return_full )
351
String path = full_path;
356
sdcard_path = Environment.getExternalStorageDirectory()
358
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
361
catch( IOException e ) {
366
if( sdcard_path != null && path.equals( sdcard_path ) )
367
return " " + _context.getString( R.string.filechooser_path_sdcard );
369
// remove prefix, if present
370
if( path.startsWith( _path_prefix + "/" ) )
371
path = path.substring( _path_prefix.length() );
373
// unless path is "/", strip trailing "/"
374
if( path.length() > 1 && path.endsWith( "/" ) )
375
path = path.substring( 0, path.length() - 1 );
377
// if full path not required, strip off preceding directories
379
int idx = path.lastIndexOf( "/" );
380
if( idx != -1 ) path = path.substring( idx + 1 );
386
protected void strtipLastFilepartFromPath()
388
int at = _path.lastIndexOf( '/', _path.length() - 2 );
389
if( at != -1 ) _path = _path.substring( 0, at + 1 );
392
protected void updateList()
395
_items = new ArrayList< RowItem >();
397
// open directory (and ensure _path is a directory)
398
File dir = new File( _path_prefix + _path );
399
while( !dir.isDirectory() ) {
401
throw new InvalidPathPrefixException();
402
strtipLastFilepartFromPath();
403
dir = new File( _path_prefix + _path );
407
if( !_path.equals( "/" ) )
408
_items.add( new RowItem( "..", true ) );
411
class DirFilter implements FileFilter {
412
public boolean accept( File file ) {
413
return file.isDirectory() && file.getName().charAt( 0 ) != '.';
416
File[] files = dir.listFiles( new DirFilter() );
418
for( int i = 0; i < files.length; i++ )
419
_items.add( new RowItem( files[ i ].getName(), true ) );
422
if( _mode == MODE_FILE )
424
class VCardFilter implements FileFilter {
425
public boolean accept( File file ) {
426
if( file.isDirectory() || file.getName().startsWith( "." ) )
428
String filename = file.getName().toLowerCase();
429
for( int i = 0; i < _extensions.length; i++ )
430
if( filename.endsWith( "." + _extensions[ i ] ) )
435
files = dir.listFiles( new VCardFilter() );
437
for( int i = 0; i < files.length; i++ )
438
_items.add( new RowItem( files[ i ].getName(), false ) );
442
class RowItemSorter implements Comparator< RowItem > {
444
public int compare( RowItem lhs, RowItem rhs ) {
445
return lhs.compareTo( rhs );
448
Collections.sort( _items, new RowItemSorter() );
450
// setup directory list
451
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
452
new RowItemAdapter( _context, R.layout.filechooser_row,
455
updateCurrentSelection();
458
private void updateCurrentSelection()
461
( (TextView)_dialog.findViewById( R.id.path ) ).setText(
462
prettyPrint( _path_prefix + _path + _filename, true ) );
464
// enable/disable ok button
465
if( _mode == MODE_FILE )
466
_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
468
_dialog.findViewById( R.id.ok ).setEnabled( true );