/android/import-contacts

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

« back to all changes in this revision

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

  • Committer: Tim Marston
  • Date: 2013-09-16 20:15:38 UTC
  • Revision ID: tim@ed.am-20130916201538-w2s54x86ml6kvjft
prevent NPE

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 Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
7
 * to as "this program").  For more information, see
 
8
 * http://ed.am/dev/android/import-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.importcontacts;
 
24
package am.ed.importcontacts;
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;
29
30
import java.util.Collections;
30
31
import java.util.Comparator;
33
34
import android.app.Dialog;
34
35
import android.content.Context;
35
36
import android.content.DialogInterface;
 
37
import android.os.Environment;
36
38
import android.view.LayoutInflater;
37
39
import android.view.View;
38
40
import android.view.View.OnClickListener;
245
247
                }
246
248
        };
247
249
 
248
 
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
249
 
                public void onItemClick( AdapterView< ? > adapter_view, View view, int position, long id )
 
250
        private OnItemClickListener _fileChooserItemClickListener =
 
251
                        new OnItemClickListener() {
 
252
                public void onItemClick( AdapterView< ? > adapter_view, View view,
 
253
                        int position, long id )
250
254
                {
251
255
                        RowItem rowitem = _items.get( position );
252
256
 
322
326
 
323
327
        public static int pathIcon( String path )
324
328
        {
325
 
                if( path.equals( "/sdcard/" ) )
 
329
                // get sdcard path
 
330
                String sdcard_path;
 
331
                try {
 
332
                        sdcard_path = Environment.getExternalStorageDirectory()
 
333
                                .getCanonicalPath();
 
334
                        if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
 
335
                                sdcard_path += "/";
 
336
                }
 
337
                catch( IOException e ) {
 
338
                        sdcard_path = null;
 
339
                }
 
340
 
 
341
                // special paths
 
342
                if( sdcard_path != null && path.equals( sdcard_path ) )
326
343
                        return R.drawable.sdcard;
327
344
 
 
345
                // default
328
346
                return R.drawable.directory;
329
347
        }
330
348
 
332
350
        {
333
351
                String path = full_path;
334
352
 
 
353
                // get sdcard path
 
354
                String sdcard_path;
 
355
                try {
 
356
                        sdcard_path = Environment.getExternalStorageDirectory()
 
357
                                .getCanonicalPath();
 
358
                        if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
 
359
                                sdcard_path += "/";
 
360
                }
 
361
                catch( IOException e ) {
 
362
                        sdcard_path = null;
 
363
                }
 
364
 
335
365
                // special names
336
 
                if( path.equals( "/sdcard/" ) )
 
366
                if( sdcard_path != null && path.equals( sdcard_path ) )
337
367
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
338
368
 
339
369
                // remove prefix, if present
340
370
                if( path.startsWith( _path_prefix + "/" ) )
341
371
                        path = path.substring( _path_prefix.length() );
342
372
 
343
 
                // unless path is "/", strip trailing "/".
 
373
                // unless path is "/", strip trailing "/"
344
374
                if( path.length() > 1 && path.endsWith( "/" ) )
345
375
                        path = path.substring( 0, path.length() - 1 );
346
376
 
384
414
                        }
385
415
                }
386
416
                File[] files = dir.listFiles( new DirFilter() );
387
 
                for( int i = 0; i < files.length; i++ )
388
 
                        _items.add( new RowItem( files[ i ].getName(), true ) );
 
417
                if( files != null )
 
418
                        for( int i = 0; i < files.length; i++ )
 
419
                                _items.add( new RowItem( files[ i ].getName(), true ) );
389
420
 
390
421
                // get files
391
422
                if( _mode == MODE_FILE )
402
433
                                }
403
434
                        }
404
435
                        files = dir.listFiles( new VCardFilter() );
405
 
                        for( int i = 0; i < files.length; i++ )
406
 
                                _items.add( new RowItem( files[ i ].getName(), false ) );
 
436
                        if( files != null )
 
437
                                for( int i = 0; i < files.length; i++ )
 
438
                                        _items.add( new RowItem( files[ i ].getName(), false ) );
407
439
                }
408
440
 
409
441
                // sort