/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/org/waxworlds/edam/importcontacts/FileChooser.java

  • Committer: edam
  • Date: 2011-03-13 18:58:52 UTC
  • Revision ID: edam@waxworlds.org-20110313185852-s74kvkbv8k0v0lwc
- accept parameters that are quoted (this doesn't appear to be part of the standards AFAICT, but Evolution apparently quotes parameter values)

Show diffs side-by-side

added added

removed removed

5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/export-contacts
 
8
 * http://www.waxworlds.org/edam/software/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
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;
29
32
 
30
33
import android.app.AlertDialog;
31
34
import android.app.Dialog;
32
35
import android.content.Context;
33
36
import android.content.DialogInterface;
 
37
import android.os.Environment;
34
38
import android.view.LayoutInflater;
35
39
import android.view.View;
36
40
import android.view.View.OnClickListener;
77
81
        private DialogInterface.OnDismissListener _on_dismiss_listener;
78
82
 
79
83
        // class that represents a row in the list
80
 
        private class RowItem
 
84
        private class RowItem implements Comparable< RowItem >
81
85
        {
82
86
                private String _name;
83
87
                private boolean _directory;
97
101
                {
98
102
                        return _directory;
99
103
                }
 
104
 
 
105
                @Override
 
106
                public int compareTo( RowItem that )
 
107
                {
 
108
                        if( this._directory && !that._directory )
 
109
                                return -1;
 
110
                        else if( !this._directory && that._directory )
 
111
                                return 1;
 
112
                        else
 
113
                                return this._name.compareToIgnoreCase( that._name );
 
114
                }
100
115
        }
101
116
 
102
117
        // class to manage our list of RowItems
130
145
                }
131
146
        }
132
147
 
 
148
        @SuppressWarnings( "serial" )
133
149
        class InvalidPathPrefixException extends RuntimeException
134
150
        {
135
151
        }
137
153
 
138
154
 
139
155
        // constructor
140
 
        public FileChooser()
 
156
        public FileChooser( Context context )
141
157
        {
 
158
                _context = context;
142
159
        }
143
160
 
144
161
        public void setMode( int mode )
178
195
        // set the path prefix
179
196
        public void setPathPrefix( String path_prefix )
180
197
        {
181
 
                // set to cleaned-up path, with trailaing '/' removed so that it can be
 
198
                // set to cleaned-up path, with trailing '/' removed so that it can be
182
199
                // trivially pre-pended to a cleaned-up path
183
200
                _path_prefix = cleanUpPath( path_prefix );
184
201
                _path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
194
211
                return _path + _filename;
195
212
        }
196
213
 
197
 
        public Dialog onCreateDialog( Context context )
 
214
        public Dialog onCreateDialog()
198
215
        {
199
 
                _context = context;
200
 
 
201
216
                // custom layout in an AlertDialog
202
 
                LayoutInflater factory = LayoutInflater.from( context );
 
217
                LayoutInflater factory = LayoutInflater.from( _context );
203
218
                final View dialogView = factory.inflate(
204
219
                        R.layout.filechooser, null );
205
220
 
210
225
                        .setOnItemClickListener( _fileChooserItemClickListener );
211
226
 
212
227
                // return dialog
213
 
                Dialog dialog = new AlertDialog.Builder( context )
 
228
                Dialog dialog = new AlertDialog.Builder( _context )
214
229
                        .setTitle( " " )
215
230
                        .setView( dialogView )
216
231
                        .create();
233
248
        };
234
249
 
235
250
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
236
 
                public void onItemClick( AdapterView adapterView, View view, int position, long id )
 
251
                public void onItemClick( AdapterView< ? > adapter_view, View view, int position, long id )
237
252
                {
238
253
                        RowItem rowitem = _items.get( position );
239
254
 
309
324
 
310
325
        public static int pathIcon( String path )
311
326
        {
312
 
                if( path.equals( "/sdcard/" ) )
 
327
                // get sdcard path
 
328
                String sdcard_path;
 
329
                try {
 
330
                        sdcard_path = Environment.getExternalStorageDirectory()
 
331
                                .getCanonicalPath();
 
332
                        if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
 
333
                                sdcard_path += "/";
 
334
                }
 
335
                catch( IOException e ) {
 
336
                        sdcard_path = null;
 
337
                }
 
338
 
 
339
                // special paths
 
340
                if( sdcard_path != null && path.equals( sdcard_path ) )
313
341
                        return R.drawable.sdcard;
314
342
 
 
343
                // default
315
344
                return R.drawable.directory;
316
345
        }
317
346
 
319
348
        {
320
349
                String path = full_path;
321
350
 
 
351
                // get sdcard path
 
352
                String sdcard_path;
 
353
                try {
 
354
                        sdcard_path = Environment.getExternalStorageDirectory()
 
355
                                .getCanonicalPath();
 
356
                        if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
 
357
                                sdcard_path += "/";
 
358
                }
 
359
                catch( IOException e ) {
 
360
                        sdcard_path = null;
 
361
                }
 
362
 
322
363
                // special names
323
 
                if( path.equals( "/sdcard/" ) )
 
364
                if( sdcard_path != null && path.equals( sdcard_path ) )
324
365
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
325
366
 
326
367
                // remove prefix, if present
371
412
                        }
372
413
                }
373
414
                File[] files = dir.listFiles( new DirFilter() );
374
 
                for( int i = 0; i < files.length; i++ )
375
 
                        _items.add( new RowItem( files[ i ].getName(), true ) );
 
415
                if( files != null )
 
416
                        for( int i = 0; i < files.length; i++ )
 
417
                                _items.add( new RowItem( files[ i ].getName(), true ) );
376
418
 
377
419
                // get files
378
420
                if( _mode == MODE_FILE )
389
431
                                }
390
432
                        }
391
433
                        files = dir.listFiles( new VCardFilter() );
392
 
                        for( int i = 0; i < files.length; i++ )
393
 
                                _items.add( new RowItem( files[ i ].getName(), false ) );
394
 
                }
 
434
                        if( files != null )
 
435
                                for( int i = 0; i < files.length; i++ )
 
436
                                        _items.add( new RowItem( files[ i ].getName(), false ) );
 
437
                }
 
438
 
 
439
                // sort
 
440
                class RowItemSorter implements Comparator< RowItem > {
 
441
                        @Override
 
442
                        public int compare( RowItem lhs, RowItem rhs ) {
 
443
                                return lhs.compareTo( rhs );
 
444
                        }
 
445
                }
 
446
                Collections.sort( _items, new RowItemSorter() );
395
447
 
396
448
                // setup directory list
397
449
                ( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
410
462
                // enable/disable ok button
411
463
                if( _mode == MODE_FILE )
412
464
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
 
465
                else
 
466
                        _dialog.findViewById( R.id.ok ).setEnabled( true );
413
467
        }
414
468
 
415
469
}