/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/am/ed/importcontacts/ConfigureVCF.java

  • Committer: Tim Marston
  • Date: 2013-10-20 17:51:59 UTC
  • Revision ID: tim@ed.am-20131020175159-xbei6qwxo8jxejdm
removed TODO from project

Show diffs side-by-side

added added

removed removed

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