/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts
6 by edam
- added GPL header comments to all files
1
/*
13 by edam
- converted project to use Android 1.5 SDK
2
 * ConfigureVCF.java
6 by edam
- added GPL header comments to all files
3
 *
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 *
6
 * This file is part of the Import Contacts program (hereafter referred
7
 * to as "this program"). For more information, see
8
 * http://www.waxworlds.org/edam/software/android/import-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
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
24
package org.waxworlds.edam.importcontacts;
1 by edam
Initial import
25
30 by edam
- determine path to SD card by querying android
26
import java.io.IOException;
27
19 by edam
- added file chooser
28
import android.app.AlertDialog;
29
import android.app.Dialog;
30
import android.content.DialogInterface;
1 by edam
Initial import
31
import android.content.SharedPreferences;
32
import android.os.Bundle;
30 by edam
- determine path to SD card by querying android
33
import android.os.Environment;
19 by edam
- added file chooser
34
import android.view.LayoutInflater;
35
import android.view.View;
36
import android.widget.Button;
37
import android.widget.TextView;
1 by edam
Initial import
38
11 by edam
- renamed ImportVCF class to ConfigureVCF
39
public class ConfigureVCF extends WizardActivity
1 by edam
Initial import
40
{
19 by edam
- added file chooser
41
	public final static int DIALOG_FILEORDIR = 1;
42
	public final static int DIALOG_FILECHOOSER = 2;
30 by edam
- determine path to SD card by querying android
43
	public final static int DIALOG_NOSDCARD = 3;
19 by edam
- added file chooser
44
45
	private FileChooser _file_chooser = null;
46
30 by edam
- determine path to SD card by querying android
47
	// the sdcard path prefix
48
	private String _sdcard_prefix;
49
19 by edam
- added file chooser
50
	// the save path
51
	private String _path;
52
53
	// was the dialog closed normally?
54
	private boolean _ok = false;
55
56
	// for the fileordir dialog, was file selected?
57
	boolean _ok_file;
58
59
	// reference to the dialog
60
	Dialog _dialog;
61
1 by edam
Initial import
62
	@Override
41 by edam
- updated TODO
63
	protected void onCreate( Bundle saved_instance_state )
1 by edam
Initial import
64
	{
11 by edam
- renamed ImportVCF class to ConfigureVCF
65
		setContentView( R.layout.configure_vcf );
41 by edam
- updated TODO
66
		super.onCreate( saved_instance_state );
1 by edam
Initial import
67
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
68
		setNextActivity( Merge.class );
19 by edam
- added file chooser
69
30 by edam
- determine path to SD card by querying android
70
		// get sdcard prefix
71
		_sdcard_prefix = getSdCardPathPrefix();
72
		if( _sdcard_prefix == null )
73
			showDialog( DIALOG_NOSDCARD );
74
19 by edam
- added file chooser
75
		// create file chooser
20 by edam
- fixed bug where a file chooser wouldn't know it's context when the context was used
76
		_file_chooser = new FileChooser( this );
19 by edam
- added file chooser
77
		String[] extensions = { "vcf" };
78
		_file_chooser.setExtensions( extensions );
79
		_file_chooser.setDismissListener(
80
			new DialogInterface.OnDismissListener() {
81
				public void onDismiss( DialogInterface dialog )
82
				{
83
					if( _file_chooser.getOk() ) {
84
						_path = _file_chooser.getPath();
85
						updatePathButton();
86
					}
87
				}
88
			} );
30 by edam
- determine path to SD card by querying android
89
		if( _sdcard_prefix != null )
90
			_file_chooser.setPathPrefix( _sdcard_prefix );
19 by edam
- added file chooser
91
92
		// set up browser button
93
		Button button = (Button)findViewById( R.id.path );
94
		button.setOnClickListener( new View.OnClickListener() {
95
			public void onClick( View view ) {
96
				onBrowse();
97
			}
98
		} );
99
	}
100
101
	private void onBrowse()
102
	{
103
		showDialog( DIALOG_FILEORDIR );
104
	}
105
106
	private void showBrowseDialog()
107
	{
108
		// set a path for this incantation
109
		_file_chooser.setMode(
110
			_ok_file? FileChooser.MODE_FILE : FileChooser.MODE_DIR );
111
		_file_chooser.setPath( _path );
112
113
		// show dialog
114
		showDialog( DIALOG_FILECHOOSER );
115
	}
116
30 by edam
- determine path to SD card by querying android
117
	static protected String getSdCardPathPrefix()
118
	{
119
		// check sdcard status
120
		String state = Environment.getExternalStorageState();
121
		if( !Environment.MEDIA_MOUNTED.equals( state ) &&
122
			!Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) )
123
		{
124
			// no sdcard mounted
125
			return null;
126
		}
127
128
		// get sdcard path
129
		String sdcard_path;
130
		try {
131
			sdcard_path = Environment.getExternalStorageDirectory()
132
				.getCanonicalPath();
133
			if( sdcard_path.charAt( sdcard_path.length() - 1 ) == '/' )
134
				sdcard_path =
135
					sdcard_path.substring( 0, sdcard_path.length() - 1 );
136
		}
137
		catch( IOException e ) {
138
			sdcard_path = null;
139
		}
140
141
		return sdcard_path;
142
	}
143
19 by edam
- added file chooser
144
	protected void updatePathButton()
145
	{
146
		Button path_button = (Button)findViewById( R.id.path );
30 by edam
- determine path to SD card by querying android
147
		if( _sdcard_prefix != null )
148
			path_button.setText(
149
				_file_chooser.prettyPrint( _sdcard_prefix + _path, true ) );
19 by edam
- added file chooser
150
151
		TextView location_text = (TextView)findViewById( R.id.location );
152
		location_text.setText( getString( _path.endsWith( "/" )?
153
			R.string.vcf_location_dir : R.string.vcf_location_file ) );
1 by edam
Initial import
154
	}
155
156
	@Override
157
	protected void onPause() {
158
		super.onPause();
159
160
		SharedPreferences.Editor editor = getSharedPreferences().edit();
161
19 by edam
- added file chooser
162
		editor.putString( "location", _path );
1 by edam
Initial import
163
164
		editor.commit();
165
	}
166
167
	@Override
168
	protected void onResume() {
169
		super.onResume();
170
171
		SharedPreferences prefs = getSharedPreferences();
172
173
		// location
19 by edam
- added file chooser
174
		_path = prefs.getString( "location", "/" );
175
		updatePathButton();
176
	}
177
178
	@Override
179
	protected Dialog onCreateDialog( int id )
180
	{
181
		Dialog ret = null;
182
183
		switch( id )
184
		{
185
		case DIALOG_FILEORDIR:
186
			// custom layout in an AlertDialog
187
			LayoutInflater factory = LayoutInflater.from( this );
188
			final View dialogView = factory.inflate(
189
				R.layout.fileordir, null );
190
191
			// wire up buttons
192
			( (Button)dialogView.findViewById(  R.id.file ) )
193
				.setOnClickListener( new View.OnClickListener() {
194
					public void onClick( View view ) {
195
						_ok = true;
196
						_ok_file = true;
197
						_dialog.dismiss();
198
					}
199
				} );
200
			( (Button)dialogView.findViewById(  R.id.dir ) )
201
			.setOnClickListener( new View.OnClickListener() {
202
				public void onClick( View view ) {
203
					_ok = true;
204
					_ok_file = false;
205
					_dialog.dismiss();
206
				}
207
			} );
208
209
			_dialog = ret = new AlertDialog.Builder( this )
210
				.setTitle( "Do you want to?" )
211
				.setView( dialogView )
212
				.create();
213
			ret.setOnDismissListener(
214
				new DialogInterface.OnDismissListener() {
215
					public void onDismiss( DialogInterface dialog )
216
					{
217
						if( _ok ) showBrowseDialog();
218
					}
219
				} );
220
			break;
221
222
		case DIALOG_FILECHOOSER:
20 by edam
- fixed bug where a file chooser wouldn't know it's context when the context was used
223
			ret = _file_chooser.onCreateDialog();
19 by edam
- added file chooser
224
			break;
30 by edam
- determine path to SD card by querying android
225
226
		case DIALOG_NOSDCARD:
227
			ret = new AlertDialog.Builder( this )
228
			.setIcon( R.drawable.alert_dialog_icon )
229
			.setTitle( R.string.error_title )
230
			.setMessage( R.string.error_nosdcard )
231
			.setPositiveButton( R.string.error_ok,
232
				new DialogInterface.OnClickListener() {
233
					public void onClick(DialogInterface dialog,
234
						int whichButton)
235
					{
236
						// close the whole app!
237
						setResult( RESULT_OK );
238
						finish();
239
					}
240
				} )
241
			.create();
242
			break;
19 by edam
- added file chooser
243
		}
244
245
		return ret;
246
	}
247
248
	@Override
249
	protected void onPrepareDialog( int id, Dialog dialog )
250
	{
251
		switch( id )
252
		{
253
		case DIALOG_FILEORDIR:
254
			_ok = false;
255
			break;
256
257
		case DIALOG_FILECHOOSER:
258
			_file_chooser.onPrepareDialog( this, dialog );
259
			break;
260
		}
261
262
		super.onPrepareDialog( id, dialog );
1 by edam
Initial import
263
	}
264
}