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

  • Committer: edam
  • Date: 2010-12-16 00:16:01 UTC
  • Revision ID: edam@waxworlds.org-20101216001601-tb8g7tsw55v9qe18
- determine path to SD card by querying android
- added error when no SD card can be found

Show diffs side-by-side

added added

removed removed

23
23
 
24
24
package org.waxworlds.edam.importcontacts;
25
25
 
 
26
import java.io.IOException;
 
27
 
26
28
import android.app.AlertDialog;
27
29
import android.app.Dialog;
28
30
import android.content.DialogInterface;
29
31
import android.content.SharedPreferences;
30
32
import android.os.Bundle;
 
33
import android.os.Environment;
31
34
import android.view.LayoutInflater;
32
35
import android.view.View;
33
36
import android.widget.Button;
37
40
{
38
41
        public final static int DIALOG_FILEORDIR = 1;
39
42
        public final static int DIALOG_FILECHOOSER = 2;
 
43
        public final static int DIALOG_NOSDCARD = 3;
40
44
 
41
45
        private FileChooser _file_chooser = null;
42
46
 
 
47
        // the sdcard path prefix
 
48
        private String _sdcard_prefix;
 
49
 
43
50
        // the save path
44
51
        private String _path;
45
52
 
60
67
 
61
68
                setNextActivity( Merge.class );
62
69
 
 
70
                // get sdcard prefix
 
71
                _sdcard_prefix = getSdCardPathPrefix();
 
72
                if( _sdcard_prefix == null )
 
73
                        showDialog( DIALOG_NOSDCARD );
 
74
 
63
75
                // create file chooser
64
76
                _file_chooser = new FileChooser( this );
65
77
                String[] extensions = { "vcf" };
74
86
                                        }
75
87
                                }
76
88
                        } );
77
 
                _file_chooser.setPathPrefix( "/sdcard" );
 
89
                if( _sdcard_prefix != null )
 
90
                        _file_chooser.setPathPrefix( _sdcard_prefix );
78
91
 
79
92
                // set up browser button
80
93
                Button button = (Button)findViewById( R.id.path );
101
114
                showDialog( DIALOG_FILECHOOSER );
102
115
        }
103
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
 
104
144
        protected void updatePathButton()
105
145
        {
106
146
                Button path_button = (Button)findViewById( R.id.path );
107
 
                path_button.setText(
108
 
                        _file_chooser.prettyPrint( "/sdcard" + _path, true ) );
 
147
                if( _sdcard_prefix != null )
 
148
                        path_button.setText(
 
149
                                _file_chooser.prettyPrint( _sdcard_prefix + _path, true ) );
109
150
 
110
151
                TextView location_text = (TextView)findViewById( R.id.location );
111
152
                location_text.setText( getString( _path.endsWith( "/" )?
181
222
                case DIALOG_FILECHOOSER:
182
223
                        ret = _file_chooser.onCreateDialog();
183
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;
184
243
                }
185
244
 
186
245
                return ret;