/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/exportcontacts/FileChooser.java

  • Committer: edam
  • Date: 2010-10-22 18:56:17 UTC
  • Revision ID: edam@waxworlds.org-20101022185617-6p6ivjqt28avwlzo
- added file chooser
- changed directory edit box for a directory "browse" button
- wired file chooser in to configure vcf page
- fixed intro
- made app debuggable
- added licence and changelog

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 Export Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/export-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.exportcontacts;
 
24
package org.waxworlds.edam.exportcontacts;
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
 
import java.util.Locale;
33
29
 
34
30
import android.app.AlertDialog;
35
31
import android.app.Dialog;
36
32
import android.content.Context;
37
33
import android.content.DialogInterface;
38
 
import android.os.Environment;
39
34
import android.view.LayoutInflater;
40
35
import android.view.View;
41
36
import android.view.View.OnClickListener;
60
55
        private Dialog _dialog;
61
56
 
62
57
        // mode
63
 
        private int _mode = MODE_DIR;
64
 
 
65
 
        // ok was pressed
66
 
        boolean _ok = false;
 
58
        private int _mode;
67
59
 
68
60
        // working path
69
61
        private String _path;
70
62
 
71
 
        // selected filename
72
 
        private String _filename;
73
 
 
74
63
        // enforce extension (in file-mode)
75
64
        private String[] _extensions;
76
65
 
82
71
        private DialogInterface.OnDismissListener _on_dismiss_listener;
83
72
 
84
73
        // class that represents a row in the list
85
 
        private class RowItem implements Comparable< RowItem >
 
74
        private class RowItem
86
75
        {
87
76
                private String _name;
88
77
                private boolean _directory;
102
91
                {
103
92
                        return _directory;
104
93
                }
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
 
                }
116
94
        }
117
95
 
118
96
        // class to manage our list of RowItems
137
115
                        }
138
116
                        RowItem rowitem = _items.get( position );
139
117
                        if( rowitem != null ) {
140
 
                                ( (TextView)view.findViewById( R.id.name ) )
 
118
                                ( (TextView)view.findViewById( R.id.path ) )
141
119
                                        .setText( rowitem.getName() );
142
120
                                ( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
143
121
                                        rowitem.isDirectory()? View.VISIBLE : View.GONE );
146
124
                }
147
125
        }
148
126
 
149
 
        @SuppressWarnings( "serial" )
150
127
        class InvalidPathPrefixException extends RuntimeException
151
128
        {
152
129
        }
154
131
 
155
132
 
156
133
        // constructor
157
 
        public FileChooser( Context context )
158
 
        {
159
 
                _context = context;
160
 
        }
161
 
 
162
 
        public void setMode( int mode )
 
134
        public FileChooser( int mode )
163
135
        {
164
136
                _mode = mode;
165
137
        }
167
139
        public void setPath( String path )
168
140
        {
169
141
                _path = cleanUpPath( path );
170
 
                File file = new File( _path_prefix + path.trim() );
171
 
 
172
 
                // path and filename
173
 
                if( file.isFile() ) {
174
 
                        _path = _path.substring( 0, _path.length() - 1 );
175
 
                        _filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
176
 
                        _path = _path.substring( 0, _path.length() - _filename.length() );
177
 
                }
178
 
 
179
 
                // else, treat as just a path
180
 
                else
181
 
                        _filename = "";
182
142
        }
183
143
 
184
144
        public void setExtensions( String[] extensions )
196
156
        // set the path prefix
197
157
        public void setPathPrefix( String path_prefix )
198
158
        {
199
 
                // set to cleaned-up path, with trailing '/' removed so that it can be
 
159
                // set to cleaned-up path, with trailaing '/' removed so that it can be
200
160
                // trivially pre-pended to a cleaned-up path
201
161
                _path_prefix = cleanUpPath( path_prefix );
202
162
                _path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
203
163
        }
204
164
 
205
 
        public boolean getOk()
206
 
        {
207
 
                return _ok;
208
 
        }
209
 
 
210
165
        public String getPath()
211
166
        {
212
 
                return _path + _filename;
 
167
                return _path;
213
168
        }
214
169
 
215
 
        public Dialog onCreateDialog()
 
170
        public Dialog onCreateDialog( Context context )
216
171
        {
 
172
                _context = context;
 
173
 
217
174
                // custom layout in an AlertDialog
218
 
                LayoutInflater factory = LayoutInflater.from( _context );
 
175
                LayoutInflater factory = LayoutInflater.from( context );
219
176
                final View dialogView = factory.inflate(
220
177
                        R.layout.filechooser, null );
221
178
 
226
183
                        .setOnItemClickListener( _fileChooserItemClickListener );
227
184
 
228
185
                // return dialog
229
 
                Dialog dialog = new AlertDialog.Builder( _context )
 
186
                Dialog dialog = new AlertDialog.Builder( context )
230
187
                        .setTitle( " " )
231
188
                        .setView( dialogView )
232
189
                        .create();
241
198
                        {
242
199
                        case R.id.ok:
243
200
                                // close dialog and free (don't keep a reference)
244
 
                                _ok = true;
245
201
                                _dialog.dismiss();
246
202
                                break;
247
203
                        }
248
204
                }
249
205
        };
250
206
 
251
 
        private OnItemClickListener _fileChooserItemClickListener =
252
 
                        new OnItemClickListener() {
253
 
                public void onItemClick( AdapterView< ? > adapter_view, View view,
254
 
                        int position, long id )
 
207
        private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
 
208
                public void onItemClick( AdapterView adapterView, View view, int position, long id )
255
209
                {
256
210
                        RowItem rowitem = _items.get( position );
257
211
 
263
217
                                        strtipLastFilepartFromPath();
264
218
                                else
265
219
                                        _path += dirname + "/";
266
 
                                _filename = "";
267
220
 
268
 
                                updateList();
 
221
                                populateList();
269
222
                        }
270
 
 
271
 
                        // handle file selections
272
223
                        else
273
224
                        {
274
 
                                _filename = rowitem.getName();
275
 
                                updateCurrentSelection();
 
225
 
276
226
                        }
277
227
                }
278
228
        };
283
233
                _dialog = dialog;
284
234
                _context = context;
285
235
 
286
 
                // reset "ok"
287
 
                _ok = false;
288
 
 
289
 
                // pick text based on mode
290
 
                int title = 0, current = 0;
 
236
                // pick title
 
237
                int title = 0;
291
238
                switch( _mode ) {
292
 
                case MODE_DIR:
293
 
                        title = R.string.filechooser_title_dir;
294
 
                        current = R.string.filechooser_current_dir;
295
 
                        break;
296
 
                case MODE_FILE:
297
 
                        title = R.string.filechooser_title_file;
298
 
                        current = R.string.filechooser_current_file;
299
 
                        break;
 
239
                case MODE_DIR: title = R.string.filechooser_title_dir; break;
 
240
                case MODE_FILE: title = R.string.filechooser_title_file; break;
300
241
                }
301
242
                dialog.setTitle( title );
302
 
                ( (TextView)dialog.findViewById( R.id.current ) )
303
 
                        .setText( _context.getString(  current ) );
304
 
 
305
 
                // clear filename in directory mode
306
 
                if( _mode == MODE_DIR )
307
 
                        _filename = "";
308
243
 
309
244
                // set root path icon
310
245
                ( (ImageView)_dialog.findViewById( R.id.icon ) )
311
246
                        .setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
312
247
 
313
248
                // setup current-path-specific stuff
314
 
                updateList();
 
249
                populateList();
315
250
        }
316
251
 
317
252
        public static String cleanUpPath( String path )
327
262
 
328
263
        public static int pathIcon( String path )
329
264
        {
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 ) )
 
265
                if( path.equals( "/sdcard/" ) )
344
266
                        return R.drawable.sdcard;
345
267
 
346
 
                // default
347
268
                return R.drawable.directory;
348
269
        }
349
270
 
350
 
        public String prettyPrint( String full_path, boolean return_full )
 
271
        public static String prettyPrint( Context context, String path,
 
272
                boolean full_path )
351
273
        {
352
 
                String path = full_path;
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
 
 
366
 
                // special names
367
 
                if( sdcard_path != null && path.equals( sdcard_path ) )
368
 
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
369
 
 
370
 
                // remove prefix, if present
371
 
                if( path.startsWith( _path_prefix + "/" ) )
372
 
                        path = path.substring( _path_prefix.length() );
373
 
 
374
 
                // unless path is "/", strip trailing "/"
 
274
                if( path.equals( "/sdcard/" ) )
 
275
                        return context.getString( R.string.filechooser_path_sdcard );
 
276
 
 
277
                // unless path is "/", strip trailing "/".
375
278
                if( path.length() > 1 && path.endsWith( "/" ) )
376
279
                        path = path.substring( 0, path.length() - 1 );
377
280
 
378
281
                // if full path not required, strip off preceding directories
379
 
                if( !return_full ) {
 
282
                if( !full_path ) {
380
283
                        int idx = path.lastIndexOf( "/" );
381
284
                        if( idx != -1 ) path = path.substring( idx + 1 );
382
285
                }
390
293
                if( at != -1 ) _path = _path.substring( 0, at + 1 );
391
294
        }
392
295
 
393
 
        protected void updateList()
 
296
        protected void populateList()
394
297
        {
395
298
                // reset item list
396
299
                _items = new ArrayList< RowItem >();
415
318
                        }
416
319
                }
417
320
                File[] files = dir.listFiles( new DirFilter() );
418
 
                if( files != null )
419
 
                        for( int i = 0; i < files.length; i++ )
420
 
                                _items.add( new RowItem( files[ i ].getName(), true ) );
 
321
                for( int i = 0; i < files.length; i++ )
 
322
                        _items.add( new RowItem( files[ i ].getName(), true ) );
421
323
 
422
324
                // get files
423
325
                if( _mode == MODE_FILE )
426
328
                                public boolean accept( File file ) {
427
329
                                        if( file.isDirectory() || file.getName().startsWith( "." ) )
428
330
                                                return false;
429
 
                                        String filename =
430
 
                                                file.getName().toLowerCase( Locale.ENGLISH );
 
331
                                        String filename = file.getName().toLowerCase();
431
332
                                        for( int i = 0; i < _extensions.length; i++ )
432
333
                                                if( filename.endsWith( "." + _extensions[ i ] ) )
433
334
                                                        return true;
435
336
                                }
436
337
                        }
437
338
                        files = dir.listFiles( new VCardFilter() );
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() );
 
339
                        for( int i = 0; i < files.length; i++ )
 
340
                                _items.add( new RowItem( files[ i ].getName(), false ) );
 
341
                }
451
342
 
452
343
                // setup directory list
453
344
                ( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
454
345
                        new RowItemAdapter( _context, R.layout.filechooser_row,
455
346
                                _items ) );
456
347
 
457
 
                updateCurrentSelection();
458
 
        }
459
 
 
460
 
        private void updateCurrentSelection()
461
 
        {
462
348
                // set current path
463
 
                ( (TextView)_dialog.findViewById( R.id.path ) ).setText(
464
 
                        prettyPrint( _path_prefix + _path + _filename, true ) );
465
 
 
466
 
                // enable/disable ok button
467
 
                if( _mode == MODE_FILE )
468
 
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
469
 
                else
470
 
                        _dialog.findViewById( R.id.ok ).setEnabled( true );
 
349
                String pretty_path =
 
350
                        prettyPrint( _context, _path_prefix + _path, true );
 
351
                if( pretty_path.startsWith( _path_prefix ) )
 
352
                        pretty_path = pretty_path.substring( _path_prefix.length() );
 
353
                if( !pretty_path.startsWith( "/" ) )
 
354
                        pretty_path = " " + pretty_path;
 
355
                ( (TextView)_dialog.findViewById( R.id.path ) ).setText( pretty_path );
471
356
        }
472
357
 
473
358
}