/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: 2015-12-05 01:58:53 UTC
  • Revision ID: tim@ed.am-20151205015853-zn2dpz56nn9zcego
updated family pic

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