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/export-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;
30
import android.app.AlertDialog;
31
import android.app.Dialog;
32
import android.content.Context;
33
import android.content.DialogInterface;
34
import android.view.LayoutInflater;
35
import android.view.View;
36
import android.view.View.OnClickListener;
37
import android.view.ViewGroup;
38
import android.widget.AdapterView;
39
import android.widget.AdapterView.OnItemClickListener;
40
import android.widget.ArrayAdapter;
41
import android.widget.Button;
42
import android.widget.ImageView;
43
import android.widget.ListView;
44
import android.widget.TextView;
46
public class FileChooser
48
// pick an existing directory
49
public final static int MODE_DIR = 1;
51
// pick an existing file
52
public final static int MODE_FILE = 2;
55
private Dialog _dialog;
58
private int _mode = MODE_DIR;
67
private String _filename;
69
// enforce extension (in file-mode)
70
private String[] _extensions;
72
// path to secretly prefix all paths with
73
private String _path_prefix = "";
75
private Context _context;
76
private ArrayList< RowItem > _items;
77
private DialogInterface.OnDismissListener _on_dismiss_listener;
79
// class that represents a row in the list
83
private boolean _directory;
85
public RowItem( String name, boolean directory )
88
_directory = directory;
91
public String getName()
96
public boolean isDirectory()
102
// class to manage our list of RowItems
103
private class RowItemAdapter extends ArrayAdapter< RowItem >
105
private ArrayList< RowItem > _items;
107
public RowItemAdapter( Context context, int textview_resource_id,
108
ArrayList< RowItem > items )
110
super( context, textview_resource_id, items );
115
public View getView( int position, View convert_view, ViewGroup parent )
117
View view = convert_view;
119
LayoutInflater factory = LayoutInflater.from( _context );
120
view = factory.inflate( R.layout.filechooser_row, null );
122
RowItem rowitem = _items.get( position );
123
if( rowitem != null ) {
124
( (TextView)view.findViewById( R.id.name ) )
125
.setText( rowitem.getName() );
126
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
127
rowitem.isDirectory()? View.VISIBLE : View.GONE );
133
class InvalidPathPrefixException extends RuntimeException
144
public void setMode( int mode )
149
public void setPath( String path )
151
_path = cleanUpPath( path );
152
File file = new File( _path_prefix + path.trim() );
155
if( file.isFile() ) {
156
_path = _path.substring( 0, _path.length() - 1 );
157
_filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
158
_path = _path.substring( 0, _path.length() - _filename.length() );
161
// else, treat as just a path
166
public void setExtensions( String[] extensions )
168
_extensions = extensions;
171
// set dismiss listener
172
public void setDismissListener(
173
DialogInterface.OnDismissListener on_dismiss_listener )
175
_on_dismiss_listener = on_dismiss_listener;
178
// set the path prefix
179
public void setPathPrefix( String path_prefix )
181
// set to cleaned-up path, with trailaing '/' removed so that it can be
182
// trivially pre-pended to a cleaned-up path
183
_path_prefix = cleanUpPath( path_prefix );
184
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
187
public boolean getOk()
192
public String getPath()
194
return _path + _filename;
197
public Dialog onCreateDialog( Context context )
201
// custom layout in an AlertDialog
202
LayoutInflater factory = LayoutInflater.from( context );
203
final View dialogView = factory.inflate(
204
R.layout.filechooser, null );
207
( (Button)dialogView.findViewById( R.id.ok ) )
208
.setOnClickListener( _fileChooserButtonListener );
209
( (ListView)dialogView.findViewById( R.id.list ) )
210
.setOnItemClickListener( _fileChooserItemClickListener );
213
Dialog dialog = new AlertDialog.Builder( context )
215
.setView( dialogView )
217
dialog.setOnDismissListener( _on_dismiss_listener );
221
private OnClickListener _fileChooserButtonListener = new OnClickListener() {
222
public void onClick( View view )
224
switch( view.getId() )
227
// close dialog and free (don't keep a reference)
235
private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
236
public void onItemClick( AdapterView adapterView, View view, int position, long id )
238
RowItem rowitem = _items.get( position );
240
// handle directory changes
241
if( rowitem.isDirectory() )
243
String dirname = rowitem.getName();
244
if( dirname.equals( ".." ) )
245
strtipLastFilepartFromPath();
247
_path += dirname + "/";
253
// handle file selections
256
_filename = rowitem.getName();
257
updateCurrentSelection();
262
public void onPrepareDialog( Context context, Dialog dialog )
264
// set up reference to dialog
271
// pick text based on mode
272
int title = 0, current = 0;
275
title = R.string.filechooser_title_dir;
276
current = R.string.filechooser_current_dir;
279
title = R.string.filechooser_title_file;
280
current = R.string.filechooser_current_file;
283
dialog.setTitle( title );
284
( (TextView)dialog.findViewById( R.id.current ) )
285
.setText( _context.getString( current ) );
287
// clear filename in directory mode
288
if( _mode == MODE_DIR )
291
// set root path icon
292
( (ImageView)_dialog.findViewById( R.id.icon ) )
293
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
295
// setup current-path-specific stuff
299
public static String cleanUpPath( String path )
303
// ensure it starts and ends in a '/'
304
if( !path.startsWith( "/" ) ) path = "/" + path;
305
if( !path.endsWith( "/" ) ) path += "/";
310
public static int pathIcon( String path )
312
if( path.equals( "/sdcard/" ) )
313
return R.drawable.sdcard;
315
return R.drawable.directory;
318
public String prettyPrint( String full_path, boolean return_full )
320
String path = full_path;
323
if( path.equals( "/sdcard/" ) )
324
return " " + _context.getString( R.string.filechooser_path_sdcard );
326
// remove prefix, if present
327
if( path.startsWith( _path_prefix + "/" ) )
328
path = path.substring( _path_prefix.length() );
330
// unless path is "/", strip trailing "/".
331
if( path.length() > 1 && path.endsWith( "/" ) )
332
path = path.substring( 0, path.length() - 1 );
334
// if full path not required, strip off preceding directories
336
int idx = path.lastIndexOf( "/" );
337
if( idx != -1 ) path = path.substring( idx + 1 );
343
protected void strtipLastFilepartFromPath()
345
int at = _path.lastIndexOf( '/', _path.length() - 2 );
346
if( at != -1 ) _path = _path.substring( 0, at + 1 );
349
protected void updateList()
352
_items = new ArrayList< RowItem >();
354
// open directory (and ensure _path is a directory)
355
File dir = new File( _path_prefix + _path );
356
while( !dir.isDirectory() ) {
358
throw new InvalidPathPrefixException();
359
strtipLastFilepartFromPath();
360
dir = new File( _path_prefix + _path );
364
if( !_path.equals( "/" ) )
365
_items.add( new RowItem( "..", true ) );
368
class DirFilter implements FileFilter {
369
public boolean accept( File file ) {
370
return file.isDirectory() && file.getName().charAt( 0 ) != '.';
373
File[] files = dir.listFiles( new DirFilter() );
374
for( int i = 0; i < files.length; i++ )
375
_items.add( new RowItem( files[ i ].getName(), true ) );
378
if( _mode == MODE_FILE )
380
class VCardFilter implements FileFilter {
381
public boolean accept( File file ) {
382
if( file.isDirectory() || file.getName().startsWith( "." ) )
384
String filename = file.getName().toLowerCase();
385
for( int i = 0; i < _extensions.length; i++ )
386
if( filename.endsWith( "." + _extensions[ i ] ) )
391
files = dir.listFiles( new VCardFilter() );
392
for( int i = 0; i < files.length; i++ )
393
_items.add( new RowItem( files[ i ].getName(), false ) );
396
// setup directory list
397
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
398
new RowItemAdapter( _context, R.layout.filechooser_row,
401
updateCurrentSelection();
404
private void updateCurrentSelection()
407
( (TextView)_dialog.findViewById( R.id.path ) ).setText(
408
prettyPrint( _path_prefix + _path + _filename, true ) );
410
// enable/disable ok button
411
if( _mode == MODE_FILE )
412
_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );