/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-10 16:14:11 UTC
  • Revision ID: edam@waxworlds.org-20090110161411-4d17l9hs9d6j277q
Initial import

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * ConfigureVCF.java
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
 
 *
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
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 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;
 
1
package org.waxworlds.importcontacts;
 
2
 
31
3
import android.content.SharedPreferences;
32
4
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;
 
5
import android.widget.EditText;
38
6
 
39
 
public class ConfigureVCF extends WizardActivity
 
7
public class ImportVCF extends WizardActivity
40
8
{
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
9
        @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 ) );
 
10
        protected void onCreate( Bundle savedInstanceState )
 
11
        {
 
12
                setContentView( R.layout.import_vcf );
 
13
                super.onCreate( savedInstanceState );
 
14
 
 
15
                setNextState( ImportContacts.STATE_MERGE );
154
16
        }
155
17
 
156
18
        @Override
159
21
 
160
22
                SharedPreferences.Editor editor = getSharedPreferences().edit();
161
23
 
162
 
                editor.putString( "location", _path );
 
24
                // location
 
25
                EditText location = (EditText)findViewById( R.id.location );
 
26
                editor.putString( "location", location.getText().toString() );
163
27
 
164
28
                editor.commit();
165
29
        }
171
35
                SharedPreferences prefs = getSharedPreferences();
172
36
 
173
37
                // 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 );
 
38
                EditText location = (EditText)findViewById( R.id.location );
 
39
                location.setText( prefs.getString( "location", "/sdcard/contacts" ) );
263
40
        }
264
41
}