4
* Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Export 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.exportcontacts;
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;
63
// enforce extension (in file-mode)
64
private String[] _extensions;
66
// path to secretly prefix all paths with
67
private String _path_prefix = "";
69
private Context _context;
70
private ArrayList< RowItem > _items;
71
private DialogInterface.OnDismissListener _on_dismiss_listener;
73
// class that represents a row in the list
77
private boolean _directory;
79
public RowItem( String name, boolean directory )
82
_directory = directory;
85
public String getName()
90
public boolean isDirectory()
96
// class to manage our list of RowItems
97
private class RowItemAdapter extends ArrayAdapter< RowItem >
99
private ArrayList< RowItem > _items;
101
public RowItemAdapter( Context context, int textview_resource_id,
102
ArrayList< RowItem > items )
104
super( context, textview_resource_id, items );
109
public View getView( int position, View convert_view, ViewGroup parent )
111
View view = convert_view;
113
LayoutInflater factory = LayoutInflater.from( _context );
114
view = factory.inflate( R.layout.filechooser_row, null );
116
RowItem rowitem = _items.get( position );
117
if( rowitem != null ) {
118
( (TextView)view.findViewById( R.id.path ) )
119
.setText( rowitem.getName() );
120
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
121
rowitem.isDirectory()? View.VISIBLE : View.GONE );
127
class InvalidPathPrefixException extends RuntimeException
134
public FileChooser( int mode )
139
public void setPath( String path )
141
_path = cleanUpPath( path );
144
public void setExtensions( String[] extensions )
146
_extensions = extensions;
149
// set dismiss listener
150
public void setDismissListener(
151
DialogInterface.OnDismissListener on_dismiss_listener )
153
_on_dismiss_listener = on_dismiss_listener;
156
// set the path prefix
157
public void setPathPrefix( String path_prefix )
159
// set to cleaned-up path, with trailaing '/' removed so that it can be
160
// trivially pre-pended to a cleaned-up path
161
_path_prefix = cleanUpPath( path_prefix );
162
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
165
public String getPath()
170
public Dialog onCreateDialog( Context context )
174
// custom layout in an AlertDialog
175
LayoutInflater factory = LayoutInflater.from( context );
176
final View dialogView = factory.inflate(
177
R.layout.filechooser, null );
180
( (Button)dialogView.findViewById( R.id.ok ) )
181
.setOnClickListener( _fileChooserButtonListener );
182
( (ListView)dialogView.findViewById( R.id.list ) )
183
.setOnItemClickListener( _fileChooserItemClickListener );
186
Dialog dialog = new AlertDialog.Builder( context )
188
.setView( dialogView )
190
dialog.setOnDismissListener( _on_dismiss_listener );
194
private OnClickListener _fileChooserButtonListener = new OnClickListener() {
195
public void onClick( View view )
197
switch( view.getId() )
200
// close dialog and free (don't keep a reference)
207
private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
208
public void onItemClick( AdapterView adapterView, View view, int position, long id )
210
RowItem rowitem = _items.get( position );
212
// handle directory changes
213
if( rowitem.isDirectory() )
215
String dirname = rowitem.getName();
216
if( dirname.equals( ".." ) )
217
strtipLastFilepartFromPath();
219
_path += dirname + "/";
230
public void onPrepareDialog( Context context, Dialog dialog )
232
// set up reference to dialog
239
case MODE_DIR: title = R.string.filechooser_title_dir; break;
240
case MODE_FILE: title = R.string.filechooser_title_file; break;
242
dialog.setTitle( title );
244
// set root path icon
245
( (ImageView)_dialog.findViewById( R.id.icon ) )
246
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
248
// setup current-path-specific stuff
252
public static String cleanUpPath( String path )
256
// ensure it starts and ends in a '/'
257
if( !path.startsWith( "/" ) ) path = "/" + path;
258
if( !path.endsWith( "/" ) ) path += "/";
263
public static int pathIcon( String path )
265
if( path.equals( "/sdcard/" ) )
266
return R.drawable.sdcard;
268
return R.drawable.directory;
271
public static String prettyPrint( Context context, String path,
274
if( path.equals( "/sdcard/" ) )
275
return context.getString( R.string.filechooser_path_sdcard );
277
// unless path is "/", strip trailing "/".
278
if( path.length() > 1 && path.endsWith( "/" ) )
279
path = path.substring( 0, path.length() - 1 );
281
// if full path not required, strip off preceding directories
283
int idx = path.lastIndexOf( "/" );
284
if( idx != -1 ) path = path.substring( idx + 1 );
290
protected void strtipLastFilepartFromPath()
292
int at = _path.lastIndexOf( '/', _path.length() - 2 );
293
if( at != -1 ) _path = _path.substring( 0, at + 1 );
296
protected void populateList()
299
_items = new ArrayList< RowItem >();
301
// open directory (and ensure _path is a directory)
302
File dir = new File( _path_prefix + _path );
303
while( !dir.isDirectory() ) {
305
throw new InvalidPathPrefixException();
306
strtipLastFilepartFromPath();
307
dir = new File( _path_prefix + _path );
311
if( !_path.equals( "/" ) )
312
_items.add( new RowItem( "..", true ) );
315
class DirFilter implements FileFilter {
316
public boolean accept( File file ) {
317
return file.isDirectory() && file.getName().charAt( 0 ) != '.';
320
File[] files = dir.listFiles( new DirFilter() );
321
for( int i = 0; i < files.length; i++ )
322
_items.add( new RowItem( files[ i ].getName(), true ) );
325
if( _mode == MODE_FILE )
327
class VCardFilter implements FileFilter {
328
public boolean accept( File file ) {
329
if( file.isDirectory() || file.getName().startsWith( "." ) )
331
String filename = file.getName().toLowerCase();
332
for( int i = 0; i < _extensions.length; i++ )
333
if( filename.endsWith( "." + _extensions[ i ] ) )
338
files = dir.listFiles( new VCardFilter() );
339
for( int i = 0; i < files.length; i++ )
340
_items.add( new RowItem( files[ i ].getName(), false ) );
343
// setup directory list
344
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
345
new RowItemAdapter( _context, R.layout.filechooser_row,
350
prettyPrint( _context, _path_prefix + _path, true );
351
if( pretty_path.startsWith( _path_prefix ) )
352
pretty_path = pretty_path.substring( _path_prefix.length() );
353
if( !pretty_path.startsWith( "/" ) )
354
pretty_path = " " + pretty_path;
355
( (TextView)_dialog.findViewById( R.id.path ) ).setText( pretty_path );