/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts
2 by edam
- added file chooser
1
/*
2
 * FileChooser.java
3
 *
4
 * Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
5
 *
6
 * This file is part of the Export Contacts program (hereafter referred
7
 * to as "this program"). For more information, see
8
 * http://www.waxworlds.org/edam/software/android/export-contacts
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
package org.waxworlds.edam.exportcontacts;
25
26
import java.io.File;
27
import java.io.FileFilter;
28
import java.util.ArrayList;
29
30
import android.app.AlertDialog;
31
import android.app.Dialog;
32
import android.content.Context;
33
import android.content.DialogInterface;
34
import android.view.LayoutInflater;
35
import android.view.View;
36
import android.view.View.OnClickListener;
37
import android.view.ViewGroup;
38
import android.widget.AdapterView;
39
import android.widget.AdapterView.OnItemClickListener;
40
import android.widget.ArrayAdapter;
41
import android.widget.Button;
42
import android.widget.ImageView;
43
import android.widget.ListView;
44
import android.widget.TextView;
45
46
public class FileChooser
47
{
48
	// pick an existing directory
49
	public final static int MODE_DIR = 1;
50
51
	// pick an existing file
52
	public final static int MODE_FILE = 2;
53
54
55
	private Dialog _dialog;
56
57
	// mode
58
	private int _mode;
59
60
	// working path
61
	private String _path;
62
63
	// enforce extension (in file-mode)
64
	private String[] _extensions;
65
66
	// path to secretly prefix all paths with
67
	private String _path_prefix = "";
68
69
	private Context _context;
70
	private ArrayList< RowItem > _items;
71
	private DialogInterface.OnDismissListener _on_dismiss_listener;
72
73
	// class that represents a row in the list
74
	private class RowItem
75
	{
76
		private String _name;
77
		private boolean _directory;
78
79
		public RowItem( String name, boolean directory )
80
		{
81
			_name = name;
82
			_directory = directory;
83
		}
84
85
		public String getName()
86
		{
87
			return _name;
88
		}
89
90
		public boolean isDirectory()
91
		{
92
			return _directory;
93
		}
94
	}
95
96
	// class to manage our list of RowItems
97
	private class RowItemAdapter extends ArrayAdapter< RowItem >
98
	{
99
		private ArrayList< RowItem > _items;
100
101
		public RowItemAdapter( Context context, int textview_resource_id,
102
			ArrayList< RowItem > items )
103
		{
104
			super( context, textview_resource_id, items );
105
			_items = items;
106
		}
107
108
		@Override
109
		public View getView( int position, View convert_view, ViewGroup parent )
110
		{
111
			View view = convert_view;
112
			if( view == null ) {
113
				LayoutInflater factory = LayoutInflater.from( _context );
114
				view = factory.inflate(  R.layout.filechooser_row, null );
115
			}
116
			RowItem rowitem = _items.get( position );
117
			if( rowitem != null ) {
118
				( (TextView)view.findViewById( R.id.path ) )
119
					.setText( rowitem.getName() );
120
				( (ImageView)view.findViewById( R.id.icon ) ).setVisibility(
121
					rowitem.isDirectory()? View.VISIBLE : View.GONE );
122
			}
123
			return view;
124
		}
125
	}
126
127
	class InvalidPathPrefixException extends RuntimeException
128
	{
129
	}
130
131
132
133
	// constructor
134
	public FileChooser( int mode )
135
	{
136
		_mode = mode;
137
	}
138
139
	public void setPath( String path )
140
	{
141
		_path = cleanUpPath( path );
142
	}
143
144
	public void setExtensions( String[] extensions )
145
	{
146
		_extensions = extensions;
147
	}
148
149
	// set dismiss listener
150
	public void setDismissListener(
151
		DialogInterface.OnDismissListener on_dismiss_listener )
152
	{
153
		_on_dismiss_listener = on_dismiss_listener;
154
	}
155
156
	// set the path prefix
157
	public void setPathPrefix( String path_prefix )
158
	{
159
		// set to cleaned-up path, with trailaing '/' removed so that it can be
160
		// trivially pre-pended to a cleaned-up path
161
		_path_prefix = cleanUpPath( path_prefix );
162
		_path_prefix = _path_prefix.substring( 0, _path_prefix.length() - 1 );
163
	}
164
165
	public String getPath()
166
	{
167
		return _path;
168
	}
169
170
	public Dialog onCreateDialog( Context context )
171
	{
172
		_context = context;
173
174
		// custom layout in an AlertDialog
175
		LayoutInflater factory = LayoutInflater.from( context );
176
		final View dialogView = factory.inflate(
177
			R.layout.filechooser, null );
178
179
		// wire up buttons
180
		( (Button)dialogView.findViewById( R.id.ok ) )
181
			.setOnClickListener( _fileChooserButtonListener );
182
		( (ListView)dialogView.findViewById( R.id.list ) )
183
			.setOnItemClickListener( _fileChooserItemClickListener );
184
185
		// return dialog
186
		Dialog dialog = new AlertDialog.Builder( context )
187
			.setTitle( " " )
188
			.setView( dialogView )
189
			.create();
190
		dialog.setOnDismissListener( _on_dismiss_listener );
191
		return dialog;
192
	}
193
194
	private OnClickListener _fileChooserButtonListener = new OnClickListener() {
195
		public void onClick( View view )
196
		{
197
			switch( view.getId() )
198
			{
199
			case R.id.ok:
200
				// close dialog and free (don't keep a reference)
201
				_dialog.dismiss();
202
				break;
203
			}
204
		}
205
	};
206
207
	private OnItemClickListener _fileChooserItemClickListener = new OnItemClickListener() {
208
		public void onItemClick( AdapterView adapterView, View view, int position, long id )
209
		{
210
			RowItem rowitem = _items.get( position );
211
212
			// handle directory changes
213
			if( rowitem.isDirectory() )
214
			{
215
				String dirname = rowitem.getName();
216
				if( dirname.equals( ".." ) )
217
					strtipLastFilepartFromPath();
218
				else
219
					_path += dirname + "/";
220
221
				populateList();
222
			}
223
			else
224
			{
225
226
			}
227
		}
228
	};
229
230
	public void onPrepareDialog( Context context, Dialog dialog )
231
	{
232
		// set up reference to dialog
233
		_dialog = dialog;
234
		_context = context;
235
236
		// pick title
237
		int title = 0;
238
		switch( _mode ) {
239
		case MODE_DIR: title = R.string.filechooser_title_dir; break;
240
		case MODE_FILE: title = R.string.filechooser_title_file; break;
241
		}
242
		dialog.setTitle( title );
243
244
		// set root path icon
245
		( (ImageView)_dialog.findViewById( R.id.icon ) )
246
			.setImageResource( pathIcon( cleanUpPath( _path_prefix ) ) );
247
248
		// setup current-path-specific stuff
249
		populateList();
250
	}
251
252
	public static String cleanUpPath( String path )
253
	{
254
		path = path.trim();
255
256
		// ensure it starts and ends in a '/'
257
		if( !path.startsWith( "/" ) ) path = "/" + path;
258
		if( !path.endsWith( "/" ) ) path += "/";
259
260
		return path;
261
	}
262
263
	public static int pathIcon( String path )
264
	{
265
		if( path.equals( "/sdcard/" ) )
266
			return R.drawable.sdcard;
267
268
		return R.drawable.directory;
269
	}
270
271
	public static String prettyPrint( Context context, String path,
272
		boolean full_path )
273
	{
274
		if( path.equals( "/sdcard/" ) )
275
			return context.getString( R.string.filechooser_path_sdcard );
276
277
		// unless path is "/", strip trailing "/".
278
		if( path.length() > 1 && path.endsWith( "/" ) )
279
			path = path.substring( 0, path.length() - 1 );
280
281
		// if full path not required, strip off preceding directories
282
		if( !full_path ) {
283
			int idx = path.lastIndexOf( "/" );
284
			if( idx != -1 ) path = path.substring( idx + 1 );
285
		}
286
287
		return path;
288
	}
289
290
	protected void strtipLastFilepartFromPath()
291
	{
292
		int at = _path.lastIndexOf( '/', _path.length() - 2 );
293
		if( at != -1 ) _path = _path.substring( 0, at + 1 );
294
	}
295
296
	protected void populateList()
297
	{
298
		// reset item list
299
		_items = new ArrayList< RowItem >();
300
301
		// open directory (and ensure _path is a directory)
302
		File dir = new File( _path_prefix + _path );
303
		while( !dir.isDirectory() ) {
304
			if( _path == "/" )
305
				throw new InvalidPathPrefixException();
306
			strtipLastFilepartFromPath();
307
			dir = new File( _path_prefix + _path );
308
		}
309
310
		// add ".."?
311
		if( !_path.equals( "/" ) )
312
			_items.add( new RowItem( "..", true ) );
313
314
		// get directories
315
		class DirFilter implements FileFilter {
316
			public boolean accept( File file ) {
317
				return file.isDirectory() && file.getName().charAt( 0 ) != '.';
318
			}
319
		}
320
		File[] files = dir.listFiles( new DirFilter() );
321
		for( int i = 0; i < files.length; i++ )
322
			_items.add( new RowItem( files[ i ].getName(), true ) );
323
324
		// get files
325
		if( _mode == MODE_FILE )
326
		{
327
			class VCardFilter implements FileFilter {
328
				public boolean accept( File file ) {
329
					if( file.isDirectory() || file.getName().startsWith( "." ) )
330
						return false;
331
					String filename = file.getName().toLowerCase();
332
					for( int i = 0; i < _extensions.length; i++ )
333
						if( filename.endsWith( "." + _extensions[ i ] ) )
334
							return true;
335
					return false;
336
				}
337
			}
338
			files = dir.listFiles( new VCardFilter() );
339
			for( int i = 0; i < files.length; i++ )
340
				_items.add( new RowItem( files[ i ].getName(), false ) );
341
		}
342
343
		// setup directory list
344
		( (ListView)_dialog.findViewById( R.id.list ) ).setAdapter(
345
			new RowItemAdapter( _context, R.layout.filechooser_row,
346
				_items ) );
347
348
		// 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 );
356
	}
357
358
}