/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * FileChooser.java
 *
 * Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
 *
 * This file is part of the Export Contacts program (hereafter referred
 * to as "this program"). For more information, see
 * http://www.waxworlds.org/edam/software/android/export-contacts
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.waxworlds.edam.exportcontacts;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FileChooser
{
	// pick an existing directory
	public final static int MODE_DIR = 1;

	// pick an existing file
	public final static int MODE_FILE = 2;


	private Dialog _dialog;

	// mode
	private int _mode = MODE_DIR;

	// ok was pressed
	boolean _ok = false;

	// working path
	private String _path;

	// selected filename
	private String _filename;

	// enforce extension (in file-mode)
	private String[] _extensions;

	// path to secretly prefix all paths with
	private String _path_prefix = "";

	private Context _context;
	private ArrayList< RowItem > _items;
	private DialogInterface.OnDismissListener _on_dismiss_listener;

	// class that represents a row in the list
	private class RowItem
	{
		private String _name;
		private boolean _directory;

		public RowItem( String name, boolean directory )
		{
			_name = name;
			_directory = directory;
		}

		public String getName()
		{
			return _name;
		}

		public boolean isDirectory()
		{
			return _directory;
		}
	}

	// class to manage our list of RowItems
	private class RowItemAdapter extends ArrayAdapter< RowItem >
	{
		private ArrayList< RowItem > _items;

		public RowItemAdapter( Context context, int textview_resource_id,
			ArrayList< RowItem > items )
		{
			super( context, textview_resource_id, items );
			_items = items;
		}

		@Override
		public View getView( int position, View convert_view, ViewGroup parent )
		{
			View view = convert_view;
			if( view == null ) {
				LayoutInflater factory = LayoutInflater.from( _context );
				view = factory.inflate(  R.layout.filechooser_row, null );
			}
			RowItem rowitem = _items.get( position );
			if( rowitem != null ) {
				( (TextView)view.findViewById( R.id.name ) )
					.setText( rowitem.getName() );
				( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
					rowitem.isDirectory()? View.VISIBLE : View.GONE );
			}
			return view;
		}
	}

	class InvalidPathPrefixException extends RuntimeException
	{
	}



	// constructor
	public FileChooser( Context context )
	{
		_context = context;
	}

	public void setMode( int mode )
	{
		_mode = mode;
	}

	public void setPath( String path )
	{
		_path = cleanUpPath( path );
		File file = new File( _path_prefix + path.trim() );

		// path and filename
		if( file.isFile() ) {
			_path = _path.substring( 0, _path.length() - 1 );
			_filename = _path.substring( _path.lastIndexOf( '/' ) + 1 );
			_path = _path.substring( 0, _path.length() - _filename.length() );
		}

		// else, treat as just a path
		else
			_filename = "";
	}

	public void setExtensions( String[] extensions )
	{
		_extensions = extensions;
	}

	// set dismiss listener
	public void setDismissListener(
		DialogInterface.OnDismissListener on_dismiss_listener )
	{
		_on_dismiss_listener = on_dismiss_listener;
	}

	// set the path prefix
	public void setPathPrefix( String path_prefix )
	{
		// set to cleaned-up path, with trailaing '/' removed so that it can be
		// trivially pre-pended to a cleaned-up path
		_path_prefix = cleanUpPath( path_prefix );
		_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
	}

	public boolean getOk()
	{
		return _ok;
	}

	public String getPath()
	{
		return _path + _filename;
	}

	public Dialog onCreateDialog()
	{
		// custom layout in an AlertDialog
		LayoutInflater factory = LayoutInflater.from( _context );
		final View dialogView = factory.inflate(
			R.layout.filechooser, null );

		// wire up buttons
		( (Button)dialogView.findViewById( R.id.ok ) )
			.setOnClickListener( _fileChooserButtonListener );
		( (ListView)dialogView.findViewById( R.id.list ) )
			.setOnItemClickListener( _fileChooserItemClickListener );

		// return dialog
		Dialog dialog = new AlertDialog.Builder( _context )
			.setTitle( " " )
			.setView( dialogView )
			.create();
		dialog.setOnDismissListener( _on_dismiss_listener );
		return dialog;
	}

	private OnClickListener _fileChooserButtonListener = new OnClickListener() {
		public void onClick( View view )
		{
			switch( view.getId() )
			{
			case R.id.ok:
				// close dialog and free (don't keep a reference)
				_ok = true;
				_dialog.dismiss();
				break;
			}
		}
	};

	private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
		public void onItemClick( AdapterView adapterView, View view, int position, long id )
		{
			RowItem rowitem = _items.get( position );

			// handle directory changes
			if( rowitem.isDirectory() )
			{
				String dirname = rowitem.getName();
				if( dirname.equals( ".." ) )
					strtipLastFilepartFromPath();
				else
					_path += dirname + "/";
				_filename = "";

				updateList();
			}

			// handle file selections
			else
			{
				_filename = rowitem.getName();
				updateCurrentSelection();
			}
		}
	};

	public void onPrepareDialog( Context context, Dialog dialog )
	{
		// set up reference to dialog
		_dialog = dialog;
		_context = context;

		// reset "ok"
		_ok = false;

		// pick text based on mode
		int title = 0, current = 0;
		switch( _mode ) {
		case MODE_DIR:
			title = R.string.filechooser_title_dir;
			current = R.string.filechooser_current_dir;
			break;
		case MODE_FILE:
			title = R.string.filechooser_title_file;
			current = R.string.filechooser_current_file;
			break;
		}
		dialog.setTitle( title );
		( (TextView)dialog.findViewById( R.id.current ) )
			.setText( _context.getString(  current ) );

		// clear filename in directory mode
		if( _mode == MODE_DIR )
			_filename = "";

		// set root path icon
		( (ImageView)_dialog.findViewById( R.id.icon ) )
			.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );

		// setup current-path-specific stuff
		updateList();
	}

	public static String cleanUpPath( String path )
	{
		path = path.trim();

		// ensure it starts and ends in a '/'
		if( !path.startsWith( "/" ) ) path = "/" + path;
		if( !path.endsWith( "/" ) ) path += "/";

		return path;
	}

	public static int pathIcon( String path )
	{
		if( path.equals( "/sdcard/" ) )
			return R.drawable.sdcard;

		return R.drawable.directory;
	}

	public String prettyPrint( String full_path, boolean return_full )
	{
		String path = full_path;

		// special names
		if( path.equals( "/sdcard/" ) )
			return " " + _context.getString( R.string.filechooser_path_sdcard );

		// remove prefix, if present
		if( path.startsWith( _path_prefix + "/" ) )
			path = path.substring( _path_prefix.length() );

		// unless path is "/", strip trailing "/".
		if( path.length() > 1 && path.endsWith( "/" ) )
			path = path.substring( 0, path.length() - 1 );

		// if full path not required, strip off preceding directories
		if( !return_full ) {
			int idx = path.lastIndexOf( "/" );
			if( idx != -1 ) path = path.substring( idx + 1 );
		}

		return path;
	}

	protected void strtipLastFilepartFromPath()
	{
		int at = _path.lastIndexOf( '/', _path.length() - 2 );
		if( at != -1 ) _path = _path.substring( 0, at + 1 );
	}

	protected void updateList()
	{
		// reset item list
		_items = new ArrayList< RowItem >();

		// open directory (and ensure _path is a directory)
		File dir = new File( _path_prefix + _path );
		while( !dir.isDirectory() ) {
			if( _path == "/" )
				throw new InvalidPathPrefixException();
			strtipLastFilepartFromPath();
			dir = new File( _path_prefix + _path );
		}

		// add ".."?
		if( !_path.equals( "/" ) )
			_items.add( new RowItem( "..", true ) );

		// get directories
		class DirFilter implements FileFilter {
			public boolean accept( File file ) {
				return file.isDirectory() && file.getName().charAt( 0 ) != '.';
			}
		}
		File[] files = dir.listFiles( new DirFilter() );
		for( int i = 0; i < files.length; i++ )
			_items.add( new RowItem( files[ i ].getName(), true ) );

		// get files
		if( _mode == MODE_FILE )
		{
			class VCardFilter implements FileFilter {
				public boolean accept( File file ) {
					if( file.isDirectory() || file.getName().startsWith( "." ) )
						return false;
					String filename = file.getName().toLowerCase();
					for( int i = 0; i < _extensions.length; i++ )
						if( filename.endsWith( "." + _extensions[ i ] ) )
							return true;
					return false;
				}
			}
			files = dir.listFiles( new VCardFilter() );
			for( int i = 0; i < files.length; i++ )
				_items.add( new RowItem( files[ i ].getName(), false ) );
		}

		// setup directory list
		( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
			new RowItemAdapter( _context, R.layout.filechooser_row,
				_items ) );

		updateCurrentSelection();
	}

	private void updateCurrentSelection()
	{
		// set current path
		( (TextView)_dialog.findViewById( R.id.path ) ).setText(
			prettyPrint( _path_prefix + _path + _filename, true ) );

		// enable/disable ok button
		if( _mode == MODE_FILE )
			_dialog.findViewById( R.id.ok ).setEnabled( _filename != "" );
		else
			_dialog.findViewById( R.id.ok ).setEnabled( true );
	}

}