/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

55
55
        private Dialog _dialog;
56
56
 
57
57
        // mode
58
 
        private int _mode = MODE_DIR;
59
 
 
60
 
        // ok was pressed
61
 
        boolean _ok = false;
 
58
        private int _mode;
62
59
 
63
60
        // working path
64
61
        private String _path;
65
62
 
66
 
        // selected filename
67
 
        private String _filename;
68
 
 
69
63
        // enforce extension (in file-mode)
70
64
        private String[] _extensions;
71
65
 
121
115
                        }
122
116
                        RowItem rowitem = _items.get( position );
123
117
                        if( rowitem != null ) {
124
 
                                ( (TextView)view.findViewById( R.id.name ) )
 
118
                                ( (TextView)view.findViewById( R.id.path ) )
125
119
                                        .setText( rowitem.getName() );
126
120
                                ( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
127
121
                                        rowitem.isDirectory()? View.VISIBLE : View.GONE );
137
131
 
138
132
 
139
133
        // constructor
140
 
        public FileChooser( Context context )
141
 
        {
142
 
                _context = context;
143
 
        }
144
 
 
145
 
        public void setMode( int mode )
 
134
        public FileChooser( int mode )
146
135
        {
147
136
                _mode = mode;
148
137
        }
150
139
        public void setPath( String path )
151
140
        {
152
141
                _path = cleanUpPath( path );
153
 
                File file = new File( _path_prefix + path.trim() );
154
 
 
155
 
                // path and filename
156
 
                if( file.isFile() ) {
157
 
                        _path = _path.substring( 0, _path.length() - 1 );
158
 
                        _filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
159
 
                        _path = _path.substring( 0, _path.length() - _filename.length() );
160
 
                }
161
 
 
162
 
                // else, treat as just a path
163
 
                else
164
 
                        _filename = "";
165
142
        }
166
143
 
167
144
        public void setExtensions( String[] extensions )
185
162
                _path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
186
163
        }
187
164
 
188
 
        public boolean getOk()
189
 
        {
190
 
                return _ok;
191
 
        }
192
 
 
193
165
        public String getPath()
194
166
        {
195
 
                return _path + _filename;
 
167
                return _path;
196
168
        }
197
169
 
198
 
        public Dialog onCreateDialog()
 
170
        public Dialog onCreateDialog( Context context )
199
171
        {
 
172
                _context = context;
 
173
 
200
174
                // custom layout in an AlertDialog
201
 
                LayoutInflater factory = LayoutInflater.from( _context );
 
175
                LayoutInflater factory = LayoutInflater.from( context );
202
176
                final View dialogView = factory.inflate(
203
177
                        R.layout.filechooser, null );
204
178
 
209
183
                        .setOnItemClickListener( _fileChooserItemClickListener );
210
184
 
211
185
                // return dialog
212
 
                Dialog dialog = new AlertDialog.Builder( _context )
 
186
                Dialog dialog = new AlertDialog.Builder( context )
213
187
                        .setTitle( " " )
214
188
                        .setView( dialogView )
215
189
                        .create();
224
198
                        {
225
199
                        case R.id.ok:
226
200
                                // close dialog and free (don't keep a reference)
227
 
                                _ok = true;
228
201
                                _dialog.dismiss();
229
202
                                break;
230
203
                        }
244
217
                                        strtipLastFilepartFromPath();
245
218
                                else
246
219
                                        _path += dirname + "/";
247
 
                                _filename = "";
248
220
 
249
 
                                updateList();
 
221
                                populateList();
250
222
                        }
251
 
 
252
 
                        // handle file selections
253
223
                        else
254
224
                        {
255
 
                                _filename = rowitem.getName();
256
 
                                updateCurrentSelection();
 
225
 
257
226
                        }
258
227
                }
259
228
        };
264
233
                _dialog = dialog;
265
234
                _context = context;
266
235
 
267
 
                // reset "ok"
268
 
                _ok = false;
269
 
 
270
 
                // pick text based on mode
271
 
                int title = 0, current = 0;
 
236
                // pick title
 
237
                int title = 0;
272
238
                switch( _mode ) {
273
 
                case MODE_DIR:
274
 
                        title = R.string.filechooser_title_dir;
275
 
                        current = R.string.filechooser_current_dir;
276
 
                        break;
277
 
                case MODE_FILE:
278
 
                        title = R.string.filechooser_title_file;
279
 
                        current = R.string.filechooser_current_file;
280
 
                        break;
 
239
                case MODE_DIR: title = R.string.filechooser_title_dir; break;
 
240
                case MODE_FILE: title = R.string.filechooser_title_file; break;
281
241
                }
282
242
                dialog.setTitle( title );
283
 
                ( (TextView)dialog.findViewById( R.id.current ) )
284
 
                        .setText( _context.getString(  current ) );
285
 
 
286
 
                // clear filename in directory mode
287
 
                if( _mode == MODE_DIR )
288
 
                        _filename = "";
289
243
 
290
244
                // set root path icon
291
245
                ( (ImageView)_dialog.findViewById( R.id.icon ) )
292
246
                        .setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
293
247
 
294
248
                // setup current-path-specific stuff
295
 
                updateList();
 
249
                populateList();
296
250
        }
297
251
 
298
252
        public static String cleanUpPath( String path )
314
268
                return R.drawable.directory;
315
269
        }
316
270
 
317
 
        public String prettyPrint( String full_path, boolean return_full )
 
271
        public static String prettyPrint( Context context, String path,
 
272
                boolean full_path )
318
273
        {
319
 
                String path = full_path;
320
 
 
321
 
                // special names
322
274
                if( path.equals( "/sdcard/" ) )
323
 
                        return " " + _context.getString( R.string.filechooser_path_sdcard );
324
 
 
325
 
                // remove prefix, if present
326
 
                if( path.startsWith( _path_prefix + "/" ) )
327
 
                        path = path.substring( _path_prefix.length() );
 
275
                        return context.getString( R.string.filechooser_path_sdcard );
328
276
 
329
277
                // unless path is "/", strip trailing "/".
330
278
                if( path.length() > 1 && path.endsWith( "/" ) )
331
279
                        path = path.substring( 0, path.length() - 1 );
332
280
 
333
281
                // if full path not required, strip off preceding directories
334
 
                if( !return_full ) {
 
282
                if( !full_path ) {
335
283
                        int idx = path.lastIndexOf( "/" );
336
284
                        if( idx != -1 ) path = path.substring( idx + 1 );
337
285
                }
345
293
                if( at != -1 ) _path = _path.substring( 0, at + 1 );
346
294
        }
347
295
 
348
 
        protected void updateList()
 
296
        protected void populateList()
349
297
        {
350
298
                // reset item list
351
299
                _items = new ArrayList< RowItem >();
397
345
                        new RowItemAdapter( _context, R.layout.filechooser_row,
398
346
                                _items ) );
399
347
 
400
 
                updateCurrentSelection();
401
 
        }
402
 
 
403
 
        private void updateCurrentSelection()
404
 
        {
405
348
                // set current path
406
 
                ( (TextView)_dialog.findViewById( R.id.path ) ).setText(
407
 
                        prettyPrint( _path_prefix + _path + _filename, true ) );
408
 
 
409
 
                // enable/disable ok button
410
 
                if( _mode == MODE_FILE )
411
 
                        _dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
412
 
                else
413
 
                        _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 );
414
356
        }
415
357
 
416
358
}