/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-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
/*
 * ConfigureVCF.java
 *
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 *
 * This file is part of the Import Contacts program (hereafter referred
 * to as "this program"). For more information, see
 * http://ed.am/dev/android/import-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 am.ed.importcontacts;

import java.io.IOException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ConfigureVCF extends WizardActivity
{
	public final static int DIALOG_FILEORDIR = 1;
	public final static int DIALOG_FILECHOOSER = 2;
	public final static int DIALOG_NOSDCARD = 3;

	private FileChooser _file_chooser = null;

	// the sdcard path prefix
	private String _sdcard_prefix;

	// the save path
	private String _path;

	// was the dialog closed normally?
	private boolean _ok = false;

	// for the fileordir dialog, was file selected?
	boolean _ok_file;

	// reference to the dialog
	Dialog _dialog;

	@Override
	protected void onCreate( Bundle saved_instance_state )
	{
		setContentView( R.layout.configure_vcf );
		super.onCreate( saved_instance_state );

		setNextActivity( Merge.class );

		// get sdcard prefix
		_sdcard_prefix = getSdCardPathPrefix();
		if( _sdcard_prefix == null )
			showDialog( DIALOG_NOSDCARD );

		// create file chooser
		_file_chooser = new FileChooser( this );
		String[] extensions = { "vcf" };
		_file_chooser.setExtensions( extensions );
		_file_chooser.setDismissListener(
			new DialogInterface.OnDismissListener() {
				public void onDismiss( DialogInterface dialog )
				{
					if( _file_chooser.getOk() ) {
						_path = _file_chooser.getPath();
						updatePathButton();
					}
				}
			} );
		if( _sdcard_prefix != null )
			_file_chooser.setPathPrefix( _sdcard_prefix );

		// set up browser button
		Button button = (Button)findViewById( R.id.path );
		button.setOnClickListener( new View.OnClickListener() {
			public void onClick( View view ) {
				onBrowse();
			}
		} );
	}

	private void onBrowse()
	{
		showDialog( DIALOG_FILEORDIR );
	}

	private void showBrowseDialog()
	{
		// set a path for this incantation
		_file_chooser.setMode(
			_ok_file? FileChooser.MODE_FILE : FileChooser.MODE_DIR );
		_file_chooser.setPath( _path );

		// show dialog
		showDialog( DIALOG_FILECHOOSER );
	}

	static protected String getSdCardPathPrefix()
	{
		// check sdcard status
		String state = Environment.getExternalStorageState();
		if( !Environment.MEDIA_MOUNTED.equals( state ) &&
			!Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) )
		{
			// no sdcard mounted
			return null;
		}

		// get sdcard path
		String sdcard_path;
		try {
			sdcard_path = Environment.getExternalStorageDirectory()
				.getCanonicalPath();
			if( sdcard_path.charAt( sdcard_path.length() - 1 ) == '/' )
				sdcard_path =
					sdcard_path.substring( 0, sdcard_path.length() - 1 );
		}
		catch( IOException e ) {
			sdcard_path = null;
		}

		return sdcard_path;
	}

	protected void updatePathButton()
	{
		Button path_button = (Button)findViewById( R.id.path );
		if( _sdcard_prefix != null )
			path_button.setText(
				_file_chooser.prettyPrint( _sdcard_prefix + _path, true ) );

		TextView location_text = (TextView)findViewById( R.id.location );
		location_text.setText( getString( _path.endsWith( "/" )?
			R.string.vcf_location_dir : R.string.vcf_location_file ) );
	}

	@Override
	protected void onPause() {
		super.onPause();

		SharedPreferences.Editor editor = getSharedPreferences().edit();

		editor.putString( "location", _path );

		editor.commit();
	}

	@Override
	protected void onResume() {
		super.onResume();

		SharedPreferences prefs = getSharedPreferences();

		// location
		_path = prefs.getString( "location", "/" );
		updatePathButton();
	}

	@Override
	protected Dialog onCreateDialog( int id )
	{
		Dialog ret = null;

		switch( id )
		{
		case DIALOG_FILEORDIR:
			// custom layout in an AlertDialog
			LayoutInflater factory = LayoutInflater.from( this );
			final View dialogView = factory.inflate(
				R.layout.fileordir, null );

			// wire up buttons
			( (Button)dialogView.findViewById(  R.id.file ) )
				.setOnClickListener( new View.OnClickListener() {
					public void onClick( View view ) {
						_ok = true;
						_ok_file = true;
						_dialog.dismiss();
					}
				} );
			( (Button)dialogView.findViewById(  R.id.dir ) )
			.setOnClickListener( new View.OnClickListener() {
				public void onClick( View view ) {
					_ok = true;
					_ok_file = false;
					_dialog.dismiss();
				}
			} );

			_dialog = ret = new AlertDialog.Builder( this )
				.setTitle( "Do you want to?" )
				.setView( dialogView )
				.create();
			ret.setOnDismissListener(
				new DialogInterface.OnDismissListener() {
					public void onDismiss( DialogInterface dialog )
					{
						if( _ok ) showBrowseDialog();
					}
				} );
			break;

		case DIALOG_FILECHOOSER:
			ret = _file_chooser.onCreateDialog();
			break;

		case DIALOG_NOSDCARD:
			ret = new AlertDialog.Builder( this )
			.setIcon( R.drawable.alert_dialog_icon )
			.setTitle( R.string.error_title )
			.setMessage( R.string.error_nosdcard )
			.setPositiveButton( R.string.error_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
						int whichButton)
					{
						// close the whole app!
						setResult( RESULT_OK );
						finish();
					}
				} )
			.create();
			break;
		}

		return ret;
	}

	@Override
	protected void onPrepareDialog( int id, Dialog dialog )
	{
		switch( id )
		{
		case DIALOG_FILEORDIR:
			_ok = false;
			break;

		case DIALOG_FILECHOOSER:
			_file_chooser.onPrepareDialog( this, dialog );
			break;
		}

		super.onPrepareDialog( id, dialog );
	}
}