4
* Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2010 Tim Marston <tim@ed.am>
6
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
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/export-contacts
10
10
* This program is free software: you can redistribute it and/or modify
11
11
* it under the terms of the GNU General Public License as published by
21
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package org.waxworlds.edam.exportcontacts;
24
package am.ed.exportcontacts;
26
26
import java.io.File;
27
27
import java.io.FileFilter;
28
import java.io.IOException;
28
29
import java.util.ArrayList;
30
import java.util.Collections;
31
import java.util.Comparator;
32
import java.util.Locale;
30
34
import android.app.AlertDialog;
31
35
import android.app.Dialog;
32
36
import android.content.Context;
33
37
import android.content.DialogInterface;
38
import android.os.Environment;
34
39
import android.view.LayoutInflater;
35
40
import android.view.View;
36
41
import android.view.View.OnClickListener;
92
103
return _directory;
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 );
96
118
// class to manage our list of RowItems
116
138
RowItem rowitem = _items.get( position );
117
139
if( rowitem != null ) {
118
( (TextView)view.findViewById( R.id.path ) )
140
( (TextView)view.findViewById( R.id.name ) )
119
141
.setText( rowitem.getName() );
120
142
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
121
143
rowitem.isDirectory()? View.VISIBLE : View.GONE );
139
167
public void setPath( String path )
141
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
144
184
public void setExtensions( String[] extensions )
156
196
// set the path prefix
157
197
public void setPathPrefix( String path_prefix )
159
// set to cleaned-up path, with trailaing '/' removed so that it can be
199
// set to cleaned-up path, with trailing '/' removed so that it can be
160
200
// trivially pre-pended to a cleaned-up path
161
201
_path_prefix = cleanUpPath( path_prefix );
162
202
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
205
public boolean getOk()
165
210
public String getPath()
212
return _path + _filename;
170
public Dialog onCreateDialog( Context context )
215
public Dialog onCreateDialog()
174
217
// custom layout in an AlertDialog
175
LayoutInflater factory = LayoutInflater.from( context );
218
LayoutInflater factory = LayoutInflater.from( _context );
176
219
final View dialogView = factory.inflate(
177
220
R.layout.filechooser, null );
200
243
// close dialog and free (don't keep a reference)
201
245
_dialog.dismiss();
207
private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
208
public void onItemClick( AdapterView adapterView, View view, int position, long id )
251
private OnItemClickListener _fileChooserItemClickListener =
252
new OnItemClickListener() {
253
public void onItemClick( AdapterView< ? > adapter_view, View view,
254
int position, long id )
210
256
RowItem rowitem = _items.get( position );
233
283
_dialog = dialog;
234
284
_context = context;
289
// pick text based on mode
290
int title = 0, current = 0;
238
291
switch( _mode ) {
239
case MODE_DIR: title = R.string.filechooser_title_dir; break;
240
case MODE_FILE: title = R.string.filechooser_title_file; break;
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;
242
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 )
244
309
// set root path icon
245
310
( (ImageView)_dialog.findViewById( R.id.icon ) )
246
311
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
248
313
// setup current-path-specific stuff
252
317
public static String cleanUpPath( String path )
263
328
public static int pathIcon( String path )
265
if( path.equals( "/sdcard/" ) )
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 ) )
266
344
return R.drawable.sdcard;
268
347
return R.drawable.directory;
271
public static String prettyPrint( Context context, String path,
350
public String prettyPrint( String full_path, boolean return_full )
274
if( path.equals( "/sdcard/" ) )
275
return context.getString( R.string.filechooser_path_sdcard );
277
// unless path is "/", strip trailing "/".
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 "/"
278
375
if( path.length() > 1 && path.endsWith( "/" ) )
279
376
path = path.substring( 0, path.length() - 1 );
281
378
// if full path not required, strip off preceding directories
283
380
int idx = path.lastIndexOf( "/" );
284
381
if( idx != -1 ) path = path.substring( idx + 1 );
293
390
if( at != -1 ) _path = _path.substring( 0, at + 1 );
296
protected void populateList()
393
protected void updateList()
298
395
// reset item list
299
396
_items = new ArrayList< RowItem >();
320
417
File[] files = dir.listFiles( new DirFilter() );
321
for( int i = 0; i < files.length; i++ )
322
_items.add( new RowItem( files[ i ].getName(), true ) );
419
for( int i = 0; i < files.length; i++ )
420
_items.add( new RowItem( files[ i ].getName(), true ) );
325
423
if( _mode == MODE_FILE )
328
426
public boolean accept( File file ) {
329
427
if( file.isDirectory() || file.getName().startsWith( "." ) )
331
String filename = file.getName().toLowerCase();
430
file.getName().toLowerCase( Locale.ENGLISH );
332
431
for( int i = 0; i < _extensions.length; i++ )
333
432
if( filename.endsWith( "." + _extensions[ i ] ) )
338
437
files = dir.listFiles( new VCardFilter() );
339
for( int i = 0; i < files.length; i++ )
340
_items.add( new RowItem( files[ i ].getName(), false ) );
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() );
343
452
// setup directory list
344
453
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
345
454
new RowItemAdapter( _context, R.layout.filechooser_row,
457
updateCurrentSelection();
460
private void updateCurrentSelection()
348
462
// set current path
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 );
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 );