/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/org/waxworlds/importcontacts/ImportVCF.java

  • Committer: edam
  • Date: 2009-01-11 16:47:01 UTC
  • Revision ID: edam@waxworlds.org-20090111164701-tvnnv5axo6jd7ck0
- added GPL header comments to all files
- added GPLv3 to project as COPYING

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * ConfigureVCF.java
 
2
 * ImportVCF.java
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/import-contacts
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
25
 
 
26
 
import java.io.IOException;
27
 
 
28
 
import android.app.AlertDialog;
29
 
import android.app.Dialog;
30
 
import android.content.DialogInterface;
 
24
package org.waxworlds.importcontacts;
 
25
 
31
26
import android.content.SharedPreferences;
32
27
import android.os.Bundle;
33
 
import android.os.Environment;
34
 
import android.view.LayoutInflater;
35
 
import android.view.View;
36
 
import android.widget.Button;
37
 
import android.widget.TextView;
 
28
import android.widget.EditText;
38
29
 
39
 
public class ConfigureVCF extends WizardActivity
 
30
public class ImportVCF extends WizardActivity
40
31
{
41
 
        public final static int DIALOG_FILEORDIR = 1;
42
 
        public final static int DIALOG_FILECHOOSER = 2;
43
 
        public final static int DIALOG_NOSDCARD = 3;
44
 
 
45
 
        private FileChooser _file_chooser = null;
46
 
 
47
 
        // the sdcard path prefix
48
 
        private String _sdcard_prefix;
49
 
 
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
 
 
62
32
        @Override
63
 
        protected void onCreate( Bundle saved_instance_state )
64
 
        {
65
 
                setContentView( R.layout.configure_vcf );
66
 
                super.onCreate( saved_instance_state );
67
 
 
68
 
                setNextActivity( Merge.class );
69
 
 
70
 
                // get sdcard prefix
71
 
                _sdcard_prefix = getSdCardPathPrefix();
72
 
                if( _sdcard_prefix == null )
73
 
                        showDialog( DIALOG_NOSDCARD );
74
 
 
75
 
                // create file chooser
76
 
                _file_chooser = new FileChooser( this );
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
 
                        } );
89
 
                if( _sdcard_prefix != null )
90
 
                        _file_chooser.setPathPrefix( _sdcard_prefix );
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
 
 
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
 
 
144
 
        protected void updatePathButton()
145
 
        {
146
 
                Button path_button = (Button)findViewById( R.id.path );
147
 
                if( _sdcard_prefix != null )
148
 
                        path_button.setText(
149
 
                                _file_chooser.prettyPrint( _sdcard_prefix + _path, true ) );
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 ) );
 
33
        protected void onCreate( Bundle savedInstanceState )
 
34
        {
 
35
                setContentView( R.layout.import_vcf );
 
36
                super.onCreate( savedInstanceState );
 
37
 
 
38
                setNextState( ImportContacts.STATE_MERGE );
154
39
        }
155
40
 
156
41
        @Override
159
44
 
160
45
                SharedPreferences.Editor editor = getSharedPreferences().edit();
161
46
 
162
 
                editor.putString( "location", _path );
 
47
                // location
 
48
                EditText location = (EditText)findViewById( R.id.location );
 
49
                editor.putString( "location", location.getText().toString() );
163
50
 
164
51
                editor.commit();
165
52
        }
171
58
                SharedPreferences prefs = getSharedPreferences();
172
59
 
173
60
                // location
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:
223
 
                        ret = _file_chooser.onCreateDialog();
224
 
                        break;
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;
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 );
 
61
                EditText location = (EditText)findViewById( R.id.location );
 
62
                location.setText( prefs.getString( "location", "/sdcard/contacts" ) );
263
63
        }
264
64
}