/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/am/ed/exportcontacts/FileChooser.java

  • Committer: Tim Marston
  • Date: 2014-03-02 11:01:06 UTC
  • Revision ID: tim@ed.am-20140302110106-xa3a8z3e8jwih9tm
updated NEWS

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