/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-28 16:11:51 UTC
  • Revision ID: edam@waxworlds.org-20101028161151-rvtpjt1efsxmyfe5
- merged changes to file chooser from import contacts
- changed the browse button to show a full path

Show diffs side-by-side

added added

removed removed

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