/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
/*
 * ConfigureVCF.java
 *
 * Copyright (C) 2010 Tim Marston <tim@ed.am>
 *
 * This file is part of the Export Contacts program (hereafter referred
 * to as "this program").  For more information, see
 * http://ed.am/dev/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 am.ed.exportcontacts;

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.View;
import android.widget.Button;
import android.widget.EditText;

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

	private FileChooser _file_chooser = null;

	// the sdcard path prefix
	private String _sdcard_prefix;

	// the save path
	private String _path;

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

		setNextActivity( Doit.class );

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

		// create file chooser
		_file_chooser = new FileChooser( this );
		_file_chooser.setMode( FileChooser.MODE_DIR );
//		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 path_button = (Button)findViewById( R.id.path );
		path_button.setOnClickListener( new View.OnClickListener() {
			public void onClick( View view ) {
				onBrowse();
			}
		} );
	}

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

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

		// path and filename
		editor.putString( "path", _path );
		EditText filename = (EditText)findViewById( R.id.filename );
		editor.putString( "filename", filename.getText().toString() );

		editor.commit();
	}

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

		SharedPreferences prefs = getSharedPreferences();

/*		// default filename
		Calendar now = Calendar.getInstance();
		NumberFormat formatter = new DecimalFormat( "00" );
		String date = now.get( Calendar.YEAR ) + "-" +
			formatter.format( now.get( Calendar.MONTH ) ) + "-" +
			formatter.format( now.get( Calendar.DAY_OF_MONTH ) );
*/
		// path and filename
		_path = prefs.getString( "path", "/" );
		updatePathButton();
		EditText filename = (EditText)findViewById( R.id.filename );
		filename.setText( prefs.getString( "filename",
			"android-contacts.vcf" ) );
	}

	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 ) );
	}

	protected void onBrowse()
	{
		// get path
		Button path_button = (Button)findViewById( R.id.path );

		// set a path for this incantation
		_file_chooser.setPath( path_button.getText().toString() );

		showDialog( DIALOG_FILECHOOSER );
	}

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

		switch( id )
		{
		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_FILECHOOSER:
			_file_chooser.onPrepareDialog( this, dialog );
			break;
		}

		super.onPrepareDialog( id, dialog );
	}
}