/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: 2010-10-28 15:49:21 UTC
  • Revision ID: edam@waxworlds.org-20101028154921-98svlxlpno3cpzb8
- added file chooser
- changed file/dir entry box for a button that opens the file chooser
- added dialog to ask if you want to select a dir to scan or a file
- fixed bug where you could abort as dialog opened and the dialog wasn't cancelled
- fixed crash where you could abort as the merge prompt opened and it would try to use the just-destroyed importer
- fixed bug where closing the application at the end would show "aborted!" erroniously

Show diffs side-by-side

added added

removed removed

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