/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-06-22 17:29:31 UTC
  • Revision ID: tim@ed.am-20130622172931-ujydoni23t3a543b
minor style tweaks

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;
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();
232
247
                }
233
248
        };
234
249
 
235
 
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
236
 
                public void onItemClick( AdapterView adapterView, 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 )
237
254
                {
238
255
                        RowItem rowitem = _items.get( position );
239
256
 
309
326
 
310
327
        public static int pathIcon( String path )
311
328
        {
312
 
                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 ) )
313
343
                        return R.drawable.sdcard;
314
344
 
 
345
                // default
315
346
                return R.drawable.directory;
316
347
        }
317
348
 
319
350
        {
320
351
                String path = full_path;
321
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
 
322
365
                // special names
323
 
                if( path.equals( "/sdcard/" ) )
 
366
                if( sdcard_path != null && path.equals( sdcard_path ) )
324
367
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
325
368
 
326
369
                // remove prefix, if present
327
370
                if( path.startsWith( _path_prefix + "/" ) )
328
371
                        path = path.substring( _path_prefix.length() );
329
372
 
330
 
                // unless path is "/", strip trailing "/".
 
373
                // unless path is "/", strip trailing "/"
331
374
                if( path.length() > 1 && path.endsWith( "/" ) )
332
375
                        path = path.substring( 0, path.length() - 1 );
333
376
 
371
414
                        }
372
415
                }
373
416
                File[] files = dir.listFiles( new DirFilter() );
374
 
                for( int i = 0; i < files.length; i++ )
375
 
                        _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 ) );
376
420
 
377
421
                // get files
378
422
                if( _mode == MODE_FILE )
389
433
                                }
390
434
                        }
391
435
                        files = dir.listFiles( new VCardFilter() );
392
 
                        for( int i = 0; i < files.length; i++ )
393
 
                                _items.add( new RowItem( files[ i ].getName(), false ) );
394
 
                }
 
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() );
395
449
 
396
450
                // setup directory list
397
451
                ( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
410
464
                // enable/disable ok button
411
465
                if( _mode == MODE_FILE )
412
466
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
 
467
                else
 
468
                        _dialog.findViewById( R.id.ok ).setEnabled( true );
413
469
        }
414
470
 
415
471
}