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;
32
import java.util.Locale;
34
import android.app.AlertDialog;
35
import android.app.Dialog;
36
import android.content.Context;
37
import android.content.DialogInterface;
38
import android.os.Environment;
39
import android.view.LayoutInflater;
40
import android.view.View;
41
import android.view.View.OnClickListener;
42
import android.view.ViewGroup;
43
import android.widget.AdapterView;
44
import android.widget.AdapterView.OnItemClickListener;
45
import android.widget.ArrayAdapter;
46
import android.widget.Button;
47
import android.widget.ImageView;
48
import android.widget.ListView;
49
import android.widget.TextView;
51
public class FileChooser
53
// pick an existing directory
54
public final static int MODE_DIR = 1;
56
// pick an existing file
57
public final static int MODE_FILE = 2;
60
private Dialog _dialog;
63
private int _mode = MODE_DIR;
72
private String _filename;
74
// enforce extension (in file-mode)
75
private String[] _extensions;
77
// path to secretly prefix all paths with
78
private String _path_prefix = "";
80
private Context _context;
81
private ArrayList< RowItem > _items;
82
private DialogInterface.OnDismissListener _on_dismiss_listener;
84
// class that represents a row in the list
85
private class RowItem implements Comparable< RowItem >
88
private boolean _directory;
90
public RowItem( String name, boolean directory )
93
_directory = directory;
96
public String getName()
101
public boolean isDirectory()
107
public int compareTo( RowItem that )
109
if( this._directory && !that._directory )
111
else if( !this._directory && that._directory )
114
return this._name.compareToIgnoreCase( that._name );
118
// class to manage our list of RowItems
119
private class RowItemAdapter extends ArrayAdapter< RowItem >
121
private ArrayList< RowItem > _items;
123
public RowItemAdapter( Context context, int textview_resource_id,
124
ArrayList< RowItem > items )
126
super( context, textview_resource_id, items );
131
public View getView( int position, View convert_view, ViewGroup parent )
133
View view = convert_view;
135
LayoutInflater factory = LayoutInflater.from( _context );
136
view = factory.inflate( R.layout.filechooser_row, null );
138
RowItem rowitem = _items.get( position );
139
if( rowitem != null ) {
140
( (TextView)view.findViewById( R.id.name ) )
141
.setText( rowitem.getName() );
142
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
143
rowitem.isDirectory()? View.VISIBLE : View.GONE );
149
@SuppressWarnings( "serial" )
150
class InvalidPathPrefixException extends RuntimeException
157
public FileChooser( Context context )
162
public void setMode( int mode )
167
public void setPath( String path )
169
_path = cleanUpPath( path );
170
File file = new File( _path_prefix + path.trim() );
173
if( file.isFile() ) {
174
_path = _path.substring( 0, _path.length() - 1 );
175
_filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
176
_path = _path.substring( 0, _path.length() - _filename.length() );
179
// else, treat as just a path
184
public void setExtensions( String[] extensions )
186
_extensions = extensions;
189
// set dismiss listener
190
public void setDismissListener(
191
DialogInterface.OnDismissListener on_dismiss_listener )
193
_on_dismiss_listener = on_dismiss_listener;
196
// set the path prefix
197
public void setPathPrefix( String path_prefix )
199
// set to cleaned-up path, with trailing '/' removed so that it can be
200
// trivially pre-pended to a cleaned-up path
201
_path_prefix = cleanUpPath( path_prefix );
202
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
205
public boolean getOk()
210
public String getPath()
212
return _path + _filename;
215
public Dialog onCreateDialog()
217
// custom layout in an AlertDialog
218
LayoutInflater factory = LayoutInflater.from( _context );
219
final View dialogView = factory.inflate(
220
R.layout.filechooser, null );
223
( (Button)dialogView.findViewById( R.id.ok ) )
224
.setOnClickListener( _fileChooserButtonListener );
225
( (ListView)dialogView.findViewById( R.id.list ) )
226
.setOnItemClickListener( _fileChooserItemClickListener );
229
Dialog dialog = new AlertDialog.Builder( _context )
231
.setView( dialogView )
233
dialog.setOnDismissListener( _on_dismiss_listener );
237
private OnClickListener _fileChooserButtonListener = new OnClickListener() {
238
public void onClick( View view )
240
switch( view.getId() )
243
// close dialog and free (don't keep a reference)
251
private OnItemClickListener _fileChooserItemClickListener =
252
new OnItemClickListener() {
253
public void onItemClick( AdapterView< ? > adapter_view, View view,
254
int position, long id )
256
RowItem rowitem = _items.get( position );
258
// handle directory changes
259
if( rowitem.isDirectory() )
261
String dirname = rowitem.getName();
262
if( dirname.equals( ".." ) )
263
strtipLastFilepartFromPath();
265
_path += dirname + "/";
271
// handle file selections
274
_filename = rowitem.getName();
275
updateCurrentSelection();
280
public void onPrepareDialog( Context context, Dialog dialog )
282
// set up reference to dialog
289
// pick text based on mode
290
int title = 0, current = 0;
293
title = R.string.filechooser_title_dir;
294
current = R.string.filechooser_current_dir;
297
title = R.string.filechooser_title_file;
298
current = R.string.filechooser_current_file;
301
dialog.setTitle( title );
302
( (TextView)dialog.findViewById( R.id.current ) )
303
.setText( _context.getString( current ) );
305
// clear filename in directory mode
306
if( _mode == MODE_DIR )
309
// set root path icon
310
( (ImageView)_dialog.findViewById( R.id.icon ) )
311
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
313
// setup current-path-specific stuff
317
public static String cleanUpPath( String path )
321
// ensure it starts and ends in a '/'
322
if( !path.startsWith( "/" ) ) path = "/" + path;
323
if( !path.endsWith( "/" ) ) path += "/";
328
public static int pathIcon( String path )
333
sdcard_path = Environment.getExternalStorageDirectory()
335
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
338
catch( IOException e ) {
343
if( sdcard_path != null && path.equals( sdcard_path ) )
344
return R.drawable.sdcard;
347
return R.drawable.directory;
350
public String prettyPrint( String full_path, boolean return_full )
352
String path = full_path;
357
sdcard_path = Environment.getExternalStorageDirectory()
359
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
362
catch( IOException e ) {
367
if( sdcard_path != null && path.equals( sdcard_path ) )
368
return " " + _context.getString( R.string.filechooser_path_sdcard );
370
// remove prefix, if present
371
if( path.startsWith( _path_prefix + "/" ) )
372
path = path.substring( _path_prefix.length() );
374
// unless path is "/", strip trailing "/"
375
if( path.length() > 1 && path.endsWith( "/" ) )
376
path = path.substring( 0, path.length() - 1 );
378
// if full path not required, strip off preceding directories
380
int idx = path.lastIndexOf( "/" );
381
if( idx != -1 ) path = path.substring( idx + 1 );
387
protected void strtipLastFilepartFromPath()
389
int at = _path.lastIndexOf( '/', _path.length() - 2 );
390
if( at != -1 ) _path = _path.substring( 0, at + 1 );
393
protected void updateList()
396
_items = new ArrayList< RowItem >();
398
// open directory (and ensure _path is a directory)
399
File dir = new File( _path_prefix + _path );
400
while( !dir.isDirectory() ) {
402
throw new InvalidPathPrefixException();
403
strtipLastFilepartFromPath();
404
dir = new File( _path_prefix + _path );
408
if( !_path.equals( "/" ) )
409
_items.add( new RowItem( "..", true ) );
412
class DirFilter implements FileFilter {
413
public boolean accept( File file ) {
414
return file.isDirectory() && file.getName().charAt( 0 ) != '.';
417
File[] files = dir.listFiles( new DirFilter() );
419
for( int i = 0; i < files.length; i++ )
420
_items.add( new RowItem( files[ i ].getName(), true ) );
423
if( _mode == MODE_FILE )
425
class VCardFilter implements FileFilter {
426
public boolean accept( File file ) {
427
if( file.isDirectory() || file.getName().startsWith( "." ) )
430
file.getName().toLowerCase( Locale.ENGLISH );
431
for( int i = 0; i < _extensions.length; i++ )
432
if( filename.endsWith( "." + _extensions[ i ] ) )
437
files = dir.listFiles( new VCardFilter() );
439
for( int i = 0; i < files.length; i++ )
440
_items.add( new RowItem( files[ i ].getName(), false ) );
444
class RowItemSorter implements Comparator< RowItem > {
446
public int compare( RowItem lhs, RowItem rhs ) {
447
return lhs.compareTo( rhs );
450
Collections.sort( _items, new RowItemSorter() );
452
// setup directory list
453
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
454
new RowItemAdapter( _context, R.layout.filechooser_row,
457
updateCurrentSelection();
460
private void updateCurrentSelection()
463
( (TextView)_dialog.findViewById( R.id.path ) ).setText(
464
prettyPrint( _path_prefix + _path + _filename, true ) );
466
// enable/disable ok button
467
if( _mode == MODE_FILE )
468
_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
470
_dialog.findViewById( R.id.ok ).setEnabled( true );