/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: 2016-03-28 18:18:11 UTC
  • Revision ID: tim@ed.am-20160328181811-as61lllmvnv8e5os
handle vcards with missing vesion lines (treat as v2.1)

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/export-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;
 
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
        }
137
154
 
138
155
 
139
156
        // constructor
140
 
        public FileChooser()
 
157
        public FileChooser( Context context )
141
158
        {
 
159
                _context = context;
142
160
        }
143
161
 
144
162
        public void setMode( int mode )
178
196
        // set the path prefix
179
197
        public void setPathPrefix( String path_prefix )
180
198
        {
181
 
                // 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
182
200
                // trivially pre-pended to a cleaned-up path
183
201
                _path_prefix = cleanUpPath( path_prefix );
184
202
                _path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
194
212
                return _path + _filename;
195
213
        }
196
214
 
197
 
        public Dialog onCreateDialog( Context context )
 
215
        public Dialog onCreateDialog()
198
216
        {
199
 
                _context = context;
200
 
 
201
217
                // custom layout in an AlertDialog
202
 
                LayoutInflater factory = LayoutInflater.from( context );
 
218
                LayoutInflater factory = LayoutInflater.from( _context );
203
219
                final View dialogView = factory.inflate(
204
220
                        R.layout.filechooser, null );
205
221
 
210
226
                        .setOnItemClickListener( _fileChooserItemClickListener );
211
227
 
212
228
                // return dialog
213
 
                Dialog dialog = new AlertDialog.Builder( context )
 
229
                Dialog dialog = new AlertDialog.Builder( _context )
214
230
                        .setTitle( " " )
215
231
                        .setView( dialogView )
216
232
                        .create();
232
248
                }
233
249
        };
234
250
 
235
 
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
236
 
                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 )
237
255
                {
238
256
                        RowItem rowitem = _items.get( position );
239
257
 
309
327
 
310
328
        public static int pathIcon( String path )
311
329
        {
312
 
                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 ) )
313
344
                        return R.drawable.sdcard;
314
345
 
 
346
                // default
315
347
                return R.drawable.directory;
316
348
        }
317
349
 
319
351
        {
320
352
                String path = full_path;
321
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
 
322
366
                // special names
323
 
                if( path.equals( "/sdcard/" ) )
 
367
                if( sdcard_path != null && path.equals( sdcard_path ) )
324
368
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
325
369
 
326
370
                // remove prefix, if present
327
371
                if( path.startsWith( _path_prefix + "/" ) )
328
372
                        path = path.substring( _path_prefix.length() );
329
373
 
330
 
                // unless path is "/", strip trailing "/".
 
374
                // unless path is "/", strip trailing "/"
331
375
                if( path.length() > 1 && path.endsWith( "/" ) )
332
376
                        path = path.substring( 0, path.length() - 1 );
333
377
 
371
415
                        }
372
416
                }
373
417
                File[] files = dir.listFiles( new DirFilter() );
374
 
                for( int i = 0; i < files.length; i++ )
375
 
                        _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 ) );
376
421
 
377
422
                // get files
378
423
                if( _mode == MODE_FILE )
381
426
                                public boolean accept( File file ) {
382
427
                                        if( file.isDirectory() || file.getName().startsWith( "." ) )
383
428
                                                return false;
384
 
                                        String filename = file.getName().toLowerCase();
 
429
                                        String filename =
 
430
                                                file.getName().toLowerCase( Locale.ENGLISH );
385
431
                                        for( int i = 0; i < _extensions.length; i++ )
386
432
                                                if( filename.endsWith( "." + _extensions[ i ] ) )
387
433
                                                        return true;
389
435
                                }
390
436
                        }
391
437
                        files = dir.listFiles( new VCardFilter() );
392
 
                        for( int i = 0; i < files.length; i++ )
393
 
                                _items.add( new RowItem( files[ i ].getName(), false ) );
394
 
                }
 
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() );
395
451
 
396
452
                // setup directory list
397
453
                ( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
410
466
                // enable/disable ok button
411
467
                if( _mode == MODE_FILE )
412
468
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
 
469
                else
 
470
                        _dialog.findViewById( R.id.ok ).setEnabled( true );
413
471
        }
414
472
 
415
473
}