4
* Copyright (C) 2010 Tim Marston <tim@ed.am>
4
* Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
6
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/export-contacts
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/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 am.ed.exportcontacts;
24
package org.waxworlds.edam.exportcontacts;
26
26
import java.io.File;
27
27
import java.io.FileFilter;
28
import java.io.IOException;
29
28
import java.util.ArrayList;
30
import java.util.Collections;
31
import java.util.Comparator;
32
import java.util.Locale;
34
30
import android.app.AlertDialog;
35
31
import android.app.Dialog;
36
32
import android.content.Context;
37
33
import android.content.DialogInterface;
38
import android.os.Environment;
39
34
import android.view.LayoutInflater;
40
35
import android.view.View;
41
36
import android.view.View.OnClickListener;
103
92
return _directory;
107
public int compareTo( RowItem that )
109
if( this._directory && !that._directory )
111
else if( !this._directory && that._directory )
114
return this._name.compareToIgnoreCase( that._name );
118
96
// class to manage our list of RowItems
138
116
RowItem rowitem = _items.get( position );
139
117
if( rowitem != null ) {
140
( (TextView)view.findViewById( R.id.name ) )
118
( (TextView)view.findViewById( R.id.path ) )
141
119
.setText( rowitem.getName() );
142
120
( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
143
121
rowitem.isDirectory()? View.VISIBLE : View.GONE );
167
139
public void setPath( String path )
169
141
_path = cleanUpPath( path );
170
File file = new File( _path_prefix + path.trim() );
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() );
179
// else, treat as just a path
184
144
public void setExtensions( String[] extensions )
196
156
// set the path prefix
197
157
public void setPathPrefix( String path_prefix )
199
// set to cleaned-up path, with trailing '/' removed so that it can be
159
// set to cleaned-up path, with trailaing '/' removed so that it can be
200
160
// trivially pre-pended to a cleaned-up path
201
161
_path_prefix = cleanUpPath( path_prefix );
202
162
_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
205
public boolean getOk()
210
165
public String getPath()
212
return _path + _filename;
215
public Dialog onCreateDialog()
170
public Dialog onCreateDialog( Context context )
217
174
// custom layout in an AlertDialog
218
LayoutInflater factory = LayoutInflater.from( _context );
175
LayoutInflater factory = LayoutInflater.from( context );
219
176
final View dialogView = factory.inflate(
220
177
R.layout.filechooser, null );
243
200
// close dialog and free (don't keep a reference)
245
201
_dialog.dismiss();
251
private OnItemClickListener _fileChooserItemClickListener =
252
new OnItemClickListener() {
253
public void onItemClick( AdapterView< ? > adapter_view, View view,
254
int position, long id )
207
private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
208
public void onItemClick( AdapterView adapterView, View view, int position, long id )
256
210
RowItem rowitem = _items.get( position );
283
233
_dialog = dialog;
284
234
_context = context;
289
// pick text based on mode
290
int title = 0, current = 0;
291
238
switch( _mode ) {
293
title = R.string.filechooser_title_dir;
294
current = R.string.filechooser_current_dir;
297
title = R.string.filechooser_title_file;
298
current = R.string.filechooser_current_file;
239
case MODE_DIR: title = R.string.filechooser_title_dir; break;
240
case MODE_FILE: title = R.string.filechooser_title_file; break;
301
242
dialog.setTitle( title );
302
( (TextView)dialog.findViewById( R.id.current ) )
303
.setText( _context.getString( current ) );
305
// clear filename in directory mode
306
if( _mode == MODE_DIR )
309
244
// set root path icon
310
245
( (ImageView)_dialog.findViewById( R.id.icon ) )
311
246
.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
313
248
// setup current-path-specific stuff
317
252
public static String cleanUpPath( String path )
328
263
public static int pathIcon( String path )
333
sdcard_path = Environment.getExternalStorageDirectory()
335
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
338
catch( IOException e ) {
343
if( sdcard_path != null && path.equals( sdcard_path ) )
265
if( path.equals( "/sdcard/" ) )
344
266
return R.drawable.sdcard;
347
268
return R.drawable.directory;
350
public String prettyPrint( String full_path, boolean return_full )
271
public static String prettyPrint( Context context, String path,
352
String path = full_path;
357
sdcard_path = Environment.getExternalStorageDirectory()
359
if( sdcard_path.charAt( sdcard_path.length() - 1 ) != '/' )
362
catch( IOException e ) {
367
if( sdcard_path != null && path.equals( sdcard_path ) )
368
return " " + _context.getString( R.string.filechooser_path_sdcard );
370
// remove prefix, if present
371
if( path.startsWith( _path_prefix + "/" ) )
372
path = path.substring( _path_prefix.length() );
374
// unless path is "/", strip trailing "/"
274
if( path.equals( "/sdcard/" ) )
275
return context.getString( R.string.filechooser_path_sdcard );
277
// unless path is "/", strip trailing "/".
375
278
if( path.length() > 1 && path.endsWith( "/" ) )
376
279
path = path.substring( 0, path.length() - 1 );
378
281
// if full path not required, strip off preceding directories
380
283
int idx = path.lastIndexOf( "/" );
381
284
if( idx != -1 ) path = path.substring( idx + 1 );
390
293
if( at != -1 ) _path = _path.substring( 0, at + 1 );
393
protected void updateList()
296
protected void populateList()
395
298
// reset item list
396
299
_items = new ArrayList< RowItem >();
417
320
File[] files = dir.listFiles( new DirFilter() );
419
for( int i = 0; i < files.length; i++ )
420
_items.add( new RowItem( files[ i ].getName(), true ) );
321
for( int i = 0; i < files.length; i++ )
322
_items.add( new RowItem( files[ i ].getName(), true ) );
423
325
if( _mode == MODE_FILE )
426
328
public boolean accept( File file ) {
427
329
if( file.isDirectory() || file.getName().startsWith( "." ) )
430
file.getName().toLowerCase( Locale.ENGLISH );
331
String filename = file.getName().toLowerCase();
431
332
for( int i = 0; i < _extensions.length; i++ )
432
333
if( filename.endsWith( "." + _extensions[ i ] ) )
437
338
files = dir.listFiles( new VCardFilter() );
439
for( int i = 0; i < files.length; i++ )
440
_items.add( new RowItem( files[ i ].getName(), false ) );
444
class RowItemSorter implements Comparator< RowItem > {
446
public int compare( RowItem lhs, RowItem rhs ) {
447
return lhs.compareTo( rhs );
450
Collections.sort( _items, new RowItemSorter() );
339
for( int i = 0; i < files.length; i++ )
340
_items.add( new RowItem( files[ i ].getName(), false ) );
452
343
// setup directory list
453
344
( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
454
345
new RowItemAdapter( _context, R.layout.filechooser_row,
457
updateCurrentSelection();
460
private void updateCurrentSelection()
462
348
// set current path
463
( (TextView)_dialog.findViewById( R.id.path ) ).setText(
464
prettyPrint( _path_prefix + _path + _filename, true ) );
466
// enable/disable ok button
467
if( _mode == MODE_FILE )
468
_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
470
_dialog.findViewById( R.id.ok ).setEnabled( true );
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 );