/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/FileChooser.java

  • Committer: Tim Marston
  • Date: 2015-02-25 00:19:50 UTC
  • Revision ID: tim@ed.am-20150225001950-prsbdjr5nov3mrvs
fixed date in NEWS

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * FileChooser.java
3
3
 *
4
 
 * Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2010 Tim Marston <tim@ed.am>
5
5
 *
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
9
9
 *
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/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.exportcontacts;
 
24
package am.ed.exportcontacts;
25
25
 
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;
29
33
 
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;
77
82
        private DialogInterface.OnDismissListener _on_dismiss_listener;
78
83
 
79
84
        // class that represents a row in the list
80
 
        private class RowItem
 
85
        private class RowItem implements Comparable< RowItem >
81
86
        {
82
87
                private String _name;
83
88
                private boolean _directory;
97
102
                {
98
103
                        return _directory;
99
104
                }
 
105
 
 
106
                @Override
 
107
                public int compareTo( RowItem that )
 
108
                {
 
109
                        if( this._directory && !that._directory )
 
110
                                return -1;
 
111
                        else if( !this._directory && that._directory )
 
112
                                return 1;
 
113
                        else
 
114
                                return this._name.compareToIgnoreCase( that._name );
 
115
                }
100
116
        }
101
117
 
102
118
        // class to manage our list of RowItems
130
146
                }
131
147
        }
132
148
 
 
149
        @SuppressWarnings( "serial" )
133
150
        class InvalidPathPrefixException extends RuntimeException
134
151
        {
135
152
        }
179
196
        // set the path prefix
180
197
        public void setPathPrefix( String path_prefix )
181
198
        {
182
 
                // 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
183
200
                // trivially pre-pended to a cleaned-up path
184
201
                _path_prefix = cleanUpPath( path_prefix );
185
202
                _path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
231
248
                }
232
249
        };
233
250
 
234
 
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
235
 
                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 )
236
255
                {
237
256
                        RowItem rowitem = _items.get( position );
238
257
 
308
327
 
309
328
        public static int pathIcon( String path )
310
329
        {
311
 
                if( path.equals( "/sdcard/" ) )
 
330
                // get sdcard path
 
331
                String sdcard_path;
 
332
                try {
 
333
                        sdcard_path = Environment.getExternalStorageDirectory()
 
334
                                .getCanonicalPath();
 
335
                        if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
 
336
                                sdcard_path += "/";
 
337
                }
 
338
                catch( IOException e ) {
 
339
                        sdcard_path = null;
 
340
                }
 
341
 
 
342
                // special paths
 
343
                if( sdcard_path != null && path.equals( sdcard_path ) )
312
344
                        return R.drawable.sdcard;
313
345
 
 
346
                // default
314
347
                return R.drawable.directory;
315
348
        }
316
349
 
318
351
        {
319
352
                String path = full_path;
320
353
 
 
354
                // get sdcard path
 
355
                String sdcard_path;
 
356
                try {
 
357
                        sdcard_path = Environment.getExternalStorageDirectory()
 
358
                                .getCanonicalPath();
 
359
                        if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
 
360
                                sdcard_path += "/";
 
361
                }
 
362
                catch( IOException e ) {
 
363
                        sdcard_path = null;
 
364
                }
 
365
 
321
366
                // special names
322
 
                if( path.equals( "/sdcard/" ) )
 
367
                if( sdcard_path != null && path.equals( sdcard_path ) )
323
368
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
324
369
 
325
370
                // remove prefix, if present
326
371
                if( path.startsWith( _path_prefix + "/" ) )
327
372
                        path = path.substring( _path_prefix.length() );
328
373
 
329
 
                // unless path is "/", strip trailing "/".
 
374
                // unless path is "/", strip trailing "/"
330
375
                if( path.length() > 1 && path.endsWith( "/" ) )
331
376
                        path = path.substring( 0, path.length() - 1 );
332
377
 
370
415
                        }
371
416
                }
372
417
                File[] files = dir.listFiles( new DirFilter() );
373
 
                for( int i = 0; i < files.length; i++ )
374
 
                        _items.add( new RowItem( files[ i ].getName(), true ) );
 
418
                if( files != null )
 
419
                        for( int i = 0; i < files.length; i++ )
 
420
                                _items.add( new RowItem( files[ i ].getName(), true ) );
375
421
 
376
422
                // get files
377
423
                if( _mode == MODE_FILE )
380
426
                                public boolean accept( File file ) {
381
427
                                        if( file.isDirectory() || file.getName().startsWith( "." ) )
382
428
                                                return false;
383
 
                                        String filename = file.getName().toLowerCase();
 
429
                                        String filename =
 
430
                                                file.getName().toLowerCase( Locale.ENGLISH );
384
431
                                        for( int i = 0; i < _extensions.length; i++ )
385
432
                                                if( filename.endsWith( "." + _extensions[ i ] ) )
386
433
                                                        return true;
388
435
                                }
389
436
                        }
390
437
                        files = dir.listFiles( new VCardFilter() );
391
 
                        for( int i = 0; i < files.length; i++ )
392
 
                                _items.add( new RowItem( files[ i ].getName(), false ) );
393
 
                }
 
438
                        if( files != null )
 
439
                                for( int i = 0; i < files.length; i++ )
 
440
                                        _items.add( new RowItem( files[ i ].getName(), false ) );
 
441
                }
 
442
 
 
443
                // sort
 
444
                class RowItemSorter implements Comparator< RowItem > {
 
445
                        @Override
 
446
                        public int compare( RowItem lhs, RowItem rhs ) {
 
447
                                return lhs.compareTo( rhs );
 
448
                        }
 
449
                }
 
450
                Collections.sort( _items, new RowItemSorter() );
394
451
 
395
452
                // setup directory list
396
453
                ( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
409
466
                // enable/disable ok button
410
467
                if( _mode == MODE_FILE )
411
468
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
 
469
                else
 
470
                        _dialog.findViewById( R.id.ok ).setEnabled( true );
412
471
        }
413
472
 
414
473
}