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