4
* Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2010 Tim Marston <tim@ed.am>
6
6
* This file is part of the Export Contacts program (hereafter referred
7
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/export-contacts
8
* http://ed.am/dev/android/export-contacts
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/>.
24
package org.waxworlds.edam.exportcontacts;
24
package am.ed.exportcontacts;
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;
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;
92
102
return _directory;
106
public int compareTo( RowItem that )
108
if( this._directory && !that._directory )
110
else if( !this._directory && that._directory )
113
return this._name.compareToIgnoreCase( that._name );
96
117
// class to manage our list of RowItems
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 );
139
166
public void setPath( String path )
141
168
_path = cleanUpPath( path );
169
File file = new File( _path_prefix + path.trim() );
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() );
178
// else, treat as just a path
144
183
public void setExtensions( String[] extensions )
162
201
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
204
public boolean getOk()
165
209
public String getPath()
211
return _path + _filename;
170
public Dialog onCreateDialog( Context context )
214
public Dialog onCreateDialog()
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 );
207
250
private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
208
public void onItemClick( AdapterView adapterView, View view, int position, long id )
251
public void onItemClick( AdapterView< ? > adapterView, View view, int position, long id )
210
253
RowItem rowitem = _items.get( position );
233
280
_dialog = dialog;
234
281
_context = context;
286
// pick text based on mode
287
int title = 0, current = 0;
238
288
switch( _mode ) {
239
case MODE_DIR: title = R.string.filechooser_title_dir; break;
240
case MODE_FILE: title = R.string.filechooser_title_file; break;
290
title = R.string.filechooser_title_dir;
291
current = R.string.filechooser_current_dir;
294
title = R.string.filechooser_title_file;
295
current = R.string.filechooser_current_file;
242
298
dialog.setTitle( title );
299
( (TextView)dialog.findViewById( R.id.current ) )
300
.setText( _context.getString( current ) );
302
// clear filename in directory mode
303
if( _mode == MODE_DIR )
244
306
// set root path icon
245
307
( (ImageView)_dialog.findViewById( R.id.icon ) )
246
308
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
248
310
// setup current-path-specific stuff
252
314
public static String cleanUpPath( String path )
263
325
public static int pathIcon( String path )
265
if( path.equals( "/sdcard/" ) )
330
sdcard_path = Environment.getExternalStorageDirectory()
332
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
335
catch( IOException e ) {
340
if( sdcard_path != null && path.equals( sdcard_path ) )
266
341
return R.drawable.sdcard;
268
344
return R.drawable.directory;
271
public static String prettyPrint( Context context, String path,
347
public String prettyPrint( String full_path, boolean return_full )
274
if( path.equals( "/sdcard/" ) )
275
return context.getString( R.string.filechooser_path_sdcard );
349
String path = full_path;
354
sdcard_path = Environment.getExternalStorageDirectory()
356
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
359
catch( IOException e ) {
364
if( sdcard_path != null && path.equals( sdcard_path ) )
365
return " " + _context.getString( R.string.filechooser_path_sdcard );
367
// remove prefix, if present
368
if( path.startsWith( _path_prefix + "/" ) )
369
path = path.substring( _path_prefix.length() );
277
371
// unless path is "/", strip trailing "/".
278
372
if( path.length() > 1 && path.endsWith( "/" ) )
279
373
path = path.substring( 0, path.length() - 1 );
281
375
// if full path not required, strip off preceding directories
283
377
int idx = path.lastIndexOf( "/" );
284
378
if( idx != -1 ) path = path.substring( idx + 1 );
293
387
if( at != -1 ) _path = _path.substring( 0, at + 1 );
296
protected void populateList()
390
protected void updateList()
298
392
// reset item list
299
393
_items = new ArrayList< RowItem >();
320
414
File[] files = dir.listFiles( new DirFilter() );
321
for( int i = 0; i < files.length; i++ )
322
_items.add( new RowItem( files[ i ].getName(), true ) );
416
for( int i = 0; i < files.length; i++ )
417
_items.add( new RowItem( files[ i ].getName(), true ) );
325
420
if( _mode == MODE_FILE )
338
433
files = dir.listFiles( new VCardFilter() );
339
for( int i = 0; i < files.length; i++ )
340
_items.add( new RowItem( files[ i ].getName(), false ) );
435
for( int i = 0; i < files.length; i++ )
436
_items.add( new RowItem( files[ i ].getName(), false ) );
440
class RowItemSorter implements Comparator< RowItem > {
442
public int compare( RowItem lhs, RowItem rhs ) {
443
return lhs.compareTo( rhs );
446
Collections.sort( _items, new RowItemSorter() );
343
448
// setup directory list
344
449
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
345
450
new RowItemAdapter( _context, R.layout.filechooser_row,
453
updateCurrentSelection();
456
private void updateCurrentSelection()
348
458
// set current 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 );
459
( (TextView)_dialog.findViewById( R.id.path ) ).setText(
460
prettyPrint( _path_prefix + _path + _filename, true ) );
462
// enable/disable ok button
463
if( _mode == MODE_FILE )
464
_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
466
_dialog.findViewById( R.id.ok ).setEnabled( true );