4
* Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/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 org.waxworlds.edam.importcontacts;
27
import java.io.FileFilter;
28
import java.util.ArrayList;
29
import java.util.Collections;
30
import java.util.Comparator;
32
import android.app.AlertDialog;
33
import android.app.Dialog;
34
import android.content.Context;
35
import android.content.DialogInterface;
36
import android.view.LayoutInflater;
37
import android.view.View;
38
import android.view.View.OnClickListener;
39
import android.view.ViewGroup;
40
import android.widget.AdapterView;
41
import android.widget.AdapterView.OnItemClickListener;
42
import android.widget.ArrayAdapter;
43
import android.widget.Button;
44
import android.widget.ImageView;
45
import android.widget.ListView;
46
import android.widget.TextView;
48
public class FileChooser
50
// pick an existing directory
51
public final static int MODE_DIR = 1;
53
// pick an existing file
54
public final static int MODE_FILE = 2;
57
private Dialog _dialog;
60
private int _mode = MODE_DIR;
69
private String _filename;
71
// enforce extension (in file-mode)
72
private String[] _extensions;
74
// path to secretly prefix all paths with
75
private String _path_prefix = "";
77
private Context _context;
78
private ArrayList< RowItem > _items;
79
private DialogInterface.OnDismissListener _on_dismiss_listener;
81
// class that represents a row in the list
82
private class RowItem implements Comparable< RowItem >
85
private boolean _directory;
87
public RowItem( String name, boolean directory )
90
_directory = directory;
93
public String getName()
98
public boolean isDirectory()
104
public int compareTo( RowItem that )
106
if( this._directory && !that._directory )
108
else if( !this._directory && that._directory )
111
return this._name.compareToIgnoreCase( that._name );
115
// class to manage our list of RowItems
116
private class RowItemAdapter extends ArrayAdapter< RowItem >
118
private ArrayList< RowItem > _items;
120
public RowItemAdapter( Context context, int textview_resource_id,
121
ArrayList< RowItem > items )
123
super( context, textview_resource_id, items );
128
public View getView( int position, View convert_view, ViewGroup parent )
130
View view = convert_view;
132
LayoutInflater factory = LayoutInflater.from( _context );
133
view = factory.inflate( R.layout.filechooser_row, null );
135
RowItem rowitem = _items.get( position );
136
if( rowitem != null ) {
137
( (TextView)view.findViewById( R.id.name ) )
138
.setText( rowitem.getName() );
139
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
140
rowitem.isDirectory()? View.VISIBLE : View.GONE );
146
@SuppressWarnings( "serial" )
147
class InvalidPathPrefixException extends RuntimeException
154
public FileChooser( Context context )
159
public void setMode( int mode )
164
public void setPath( String path )
166
_path = cleanUpPath( path );
167
File file = new File( _path_prefix + path.trim() );
170
if( file.isFile() ) {
171
_path = _path.substring( 0, _path.length() - 1 );
172
_filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
173
_path = _path.substring( 0, _path.length() - _filename.length() );
176
// else, treat as just a path
181
public void setExtensions( String[] extensions )
183
_extensions = extensions;
186
// set dismiss listener
187
public void setDismissListener(
188
DialogInterface.OnDismissListener on_dismiss_listener )
190
_on_dismiss_listener = on_dismiss_listener;
193
// set the path prefix
194
public void setPathPrefix( String path_prefix )
196
// set to cleaned-up path, with trailing '/' removed so that it can be
197
// trivially pre-pended to a cleaned-up path
198
_path_prefix = cleanUpPath( path_prefix );
199
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
202
public boolean getOk()
207
public String getPath()
209
return _path + _filename;
212
public Dialog onCreateDialog()
214
// custom layout in an AlertDialog
215
LayoutInflater factory = LayoutInflater.from( _context );
216
final View dialogView = factory.inflate(
217
R.layout.filechooser, null );
220
( (Button)dialogView.findViewById( R.id.ok ) )
221
.setOnClickListener( _fileChooserButtonListener );
222
( (ListView)dialogView.findViewById( R.id.list ) )
223
.setOnItemClickListener( _fileChooserItemClickListener );
226
Dialog dialog = new AlertDialog.Builder( _context )
228
.setView( dialogView )
230
dialog.setOnDismissListener( _on_dismiss_listener );
234
private OnClickListener _fileChooserButtonListener = new OnClickListener() {
235
public void onClick( View view )
237
switch( view.getId() )
240
// close dialog and free (don't keep a reference)
248
private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
249
public void onItemClick( AdapterView< ? > adapter_view, View view, int position, long id )
251
RowItem rowitem = _items.get( position );
253
// handle directory changes
254
if( rowitem.isDirectory() )
256
String dirname = rowitem.getName();
257
if( dirname.equals( ".." ) )
258
strtipLastFilepartFromPath();
260
_path += dirname + "/";
266
// handle file selections
269
_filename = rowitem.getName();
270
updateCurrentSelection();
275
public void onPrepareDialog( Context context, Dialog dialog )
277
// set up reference to dialog
284
// pick text based on mode
285
int title = 0, current = 0;
288
title = R.string.filechooser_title_dir;
289
current = R.string.filechooser_current_dir;
292
title = R.string.filechooser_title_file;
293
current = R.string.filechooser_current_file;
296
dialog.setTitle( title );
297
( (TextView)dialog.findViewById( R.id.current ) )
298
.setText( _context.getString( current ) );
300
// clear filename in directory mode
301
if( _mode == MODE_DIR )
304
// set root path icon
305
( (ImageView)_dialog.findViewById( R.id.icon ) )
306
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
308
// setup current-path-specific stuff
312
public static String cleanUpPath( String path )
316
// ensure it starts and ends in a '/'
317
if( !path.startsWith( "/" ) ) path = "/" + path;
318
if( !path.endsWith( "/" ) ) path += "/";
323
public static int pathIcon( String path )
325
if( path.equals( "/sdcard/" ) )
326
return R.drawable.sdcard;
328
return R.drawable.directory;
331
public String prettyPrint( String full_path, boolean return_full )
333
String path = full_path;
336
if( path.equals( "/sdcard/" ) )
337
return " " + _context.getString( R.string.filechooser_path_sdcard );
339
// remove prefix, if present
340
if( path.startsWith( _path_prefix + "/" ) )
341
path = path.substring( _path_prefix.length() );
343
// unless path is "/", strip trailing "/".
344
if( path.length() > 1 && path.endsWith( "/" ) )
345
path = path.substring( 0, path.length() - 1 );
347
// if full path not required, strip off preceding directories
349
int idx = path.lastIndexOf( "/" );
350
if( idx != -1 ) path = path.substring( idx + 1 );
356
protected void strtipLastFilepartFromPath()
358
int at = _path.lastIndexOf( '/', _path.length() - 2 );
359
if( at != -1 ) _path = _path.substring( 0, at + 1 );
362
protected void updateList()
365
_items = new ArrayList< RowItem >();
367
// open directory (and ensure _path is a directory)
368
File dir = new File( _path_prefix + _path );
369
while( !dir.isDirectory() ) {
371
throw new InvalidPathPrefixException();
372
strtipLastFilepartFromPath();
373
dir = new File( _path_prefix + _path );
377
if( !_path.equals( "/" ) )
378
_items.add( new RowItem( "..", true ) );
381
class DirFilter implements FileFilter {
382
public boolean accept( File file ) {
383
return file.isDirectory() && file.getName().charAt( 0 ) != '.';
386
File[] files = dir.listFiles( new DirFilter() );
387
for( int i = 0; i < files.length; i++ )
388
_items.add( new RowItem( files[ i ].getName(), true ) );
391
if( _mode == MODE_FILE )
393
class VCardFilter implements FileFilter {
394
public boolean accept( File file ) {
395
if( file.isDirectory() || file.getName().startsWith( "." ) )
397
String filename = file.getName().toLowerCase();
398
for( int i = 0; i < _extensions.length; i++ )
399
if( filename.endsWith( "." + _extensions[ i ] ) )
404
files = dir.listFiles( new VCardFilter() );
405
for( int i = 0; i < files.length; i++ )
406
_items.add( new RowItem( files[ i ].getName(), false ) );
410
class RowItemSorter implements Comparator< RowItem > {
412
public int compare( RowItem lhs, RowItem rhs ) {
413
return lhs.compareTo( rhs );
416
Collections.sort( _items, new RowItemSorter() );
418
// setup directory list
419
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
420
new RowItemAdapter( _context, R.layout.filechooser_row,
423
updateCurrentSelection();
426
private void updateCurrentSelection()
429
( (TextView)_dialog.findViewById( R.id.path ) ).setText(
430
prettyPrint( _path_prefix + _path + _filename, true ) );
432
// enable/disable ok button
433
if( _mode == MODE_FILE )
434
_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
436
_dialog.findViewById( R.id.ok ).setEnabled( true );