/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

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/import-contacts
 
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
26
26
import java.io.File;
27
27
import java.io.FileFilter;
28
28
import java.util.ArrayList;
29
 
import java.util.Collections;
30
 
import java.util.Comparator;
31
29
 
32
30
import android.app.AlertDialog;
33
31
import android.app.Dialog;
79
77
        private DialogInterface.OnDismissListener _on_dismiss_listener;
80
78
 
81
79
        // class that represents a row in the list
82
 
        private class RowItem implements Comparable< RowItem >
 
80
        private class RowItem
83
81
        {
84
82
                private String _name;
85
83
                private boolean _directory;
99
97
                {
100
98
                        return _directory;
101
99
                }
102
 
 
103
 
                @Override
104
 
                public int compareTo( RowItem that )
105
 
                {
106
 
                        if( this._directory && !that._directory )
107
 
                                return -1;
108
 
                        else if( !this._directory && that._directory )
109
 
                                return 1;
110
 
                        else
111
 
                                return this._name.compareToIgnoreCase( that._name );
112
 
                }
113
100
        }
114
101
 
115
102
        // class to manage our list of RowItems
143
130
                }
144
131
        }
145
132
 
146
 
        @SuppressWarnings( "serial" )
147
133
        class InvalidPathPrefixException extends RuntimeException
148
134
        {
149
135
        }
151
137
 
152
138
 
153
139
        // constructor
154
 
        public FileChooser( Context context )
 
140
        public FileChooser()
155
141
        {
156
 
                _context = context;
157
142
        }
158
143
 
159
144
        public void setMode( int mode )
193
178
        // set the path prefix
194
179
        public void setPathPrefix( String path_prefix )
195
180
        {
196
 
                // 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
197
182
                // trivially pre-pended to a cleaned-up path
198
183
                _path_prefix = cleanUpPath( path_prefix );
199
184
                _path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
209
194
                return _path + _filename;
210
195
        }
211
196
 
212
 
        public Dialog onCreateDialog()
 
197
        public Dialog onCreateDialog( Context context )
213
198
        {
 
199
                _context = context;
 
200
 
214
201
                // custom layout in an AlertDialog
215
 
                LayoutInflater factory = LayoutInflater.from( _context );
 
202
                LayoutInflater factory = LayoutInflater.from( context );
216
203
                final View dialogView = factory.inflate(
217
204
                        R.layout.filechooser, null );
218
205
 
223
210
                        .setOnItemClickListener( _fileChooserItemClickListener );
224
211
 
225
212
                // return dialog
226
 
                Dialog dialog = new AlertDialog.Builder( _context )
 
213
                Dialog dialog = new AlertDialog.Builder( context )
227
214
                        .setTitle( " " )
228
215
                        .setView( dialogView )
229
216
                        .create();
246
233
        };
247
234
 
248
235
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
249
 
                public void onItemClick( AdapterView< ? > adapter_view, View view, int position, long id )
 
236
                public void onItemClick( AdapterView adapterView, View view, int position, long id )
250
237
                {
251
238
                        RowItem rowitem = _items.get( position );
252
239
 
384
371
                        }
385
372
                }
386
373
                File[] files = dir.listFiles( new DirFilter() );
387
 
                if( files != null )
388
 
                        for( int i = 0; i < files.length; i++ )
389
 
                                _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 ) );
390
376
 
391
377
                // get files
392
378
                if( _mode == MODE_FILE )
403
389
                                }
404
390
                        }
405
391
                        files = dir.listFiles( new VCardFilter() );
406
 
                        if( files != null )
407
 
                                for( int i = 0; i < files.length; i++ )
408
 
                                        _items.add( new RowItem( files[ i ].getName(), false ) );
409
 
                }
410
 
 
411
 
                // sort
412
 
                class RowItemSorter implements Comparator< RowItem > {
413
 
                        @Override
414
 
                        public int compare( RowItem lhs, RowItem rhs ) {
415
 
                                return lhs.compareTo( rhs );
416
 
                        }
417
 
                }
418
 
                Collections.sort( _items, new RowItemSorter() );
 
392
                        for( int i = 0; i < files.length; i++ )
 
393
                                _items.add( new RowItem( files[ i ].getName(), false ) );
 
394
                }
419
395
 
420
396
                // setup directory list
421
397
                ( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
434
410
                // enable/disable ok button
435
411
                if( _mode == MODE_FILE )
436
412
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
437
 
                else
438
 
                        _dialog.findViewById( R.id.ok ).setEnabled( true );
439
413
        }
440
414
 
441
415
}