/android/export-contacts

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

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/ContactsContractBackend.java

  • Committer: edam
  • Date: 2012-12-21 15:53:59 UTC
  • Revision ID: tim@ed.am-20121221155359-p8h3n0xhtulmwva0
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsContractContactAccessor.java
3
3
 *
4
 
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Export Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
 
7
 * to as "this program"). For more information, see
8
8
 * http://ed.am/dev/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
48
48
        public int getNumContacts()
49
49
        {
50
50
                // get number of aggregate contacts
51
 
                Cursor cur = _activity.getContentResolver().query(
 
51
                Cursor cursor = _activity.managedQuery(
52
52
                        ContactsContract.Contacts.CONTENT_URI,
53
53
                        new String[] {
54
54
                                ContactsContract.Contacts._ID,
55
55
                        }, null, null, null );
56
 
                int ret = cur.getCount();
57
 
                cur.close();
58
 
                return ret;
 
56
                return cursor.getCount();
59
57
        }
60
58
 
61
59
        private int convertBackendTypeToType( Class< ? > cls, int type )
109
107
                if( _cur == null )
110
108
                {
111
109
                        // get all aggregate contacts
112
 
                        _cur = _activity.getContentResolver().query(
 
110
                        _cur = _activity.managedQuery(
113
111
                                ContactsContract.Contacts.CONTENT_URI,
114
112
                                new String[] {
115
113
                                        ContactsContract.Contacts._ID,
118
116
                }
119
117
 
120
118
                // if there are no more aggregate contacts, abort
121
 
                if( _cur == null ) return false;
122
 
                if( !_cur.moveToNext() ) {
123
 
                        _cur.close();
 
119
                if( _cur == null || !_cur.moveToNext() ) {
124
120
                        _cur = null;
125
121
                        return false;
126
122
                }
144
140
                                ContactsContract.Data.DATA4,
145
141
                        },
146
142
                        ContactsContract.Data.CONTACT_ID + " = ? AND " +
147
 
// column DELETED not found!?
148
 
//                              ContactsContract.Data.DELETED + " = 0 AND " +
149
 
                                ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ?, ? ) ",
 
143
                                ContactsContract.Data.DELETED + " = 0 AND " +
 
144
                                ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ? )",
150
145
                        new String[] {
151
146
                                String.valueOf( id ),
152
147
                                CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
154
149
                                CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
155
150
                                CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
156
151
                                CommonDataKinds.Note.CONTENT_ITEM_TYPE,
157
 
                                CommonDataKinds.Event.CONTENT_ITEM_TYPE,
158
152
                        },
159
153
                        ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
160
154
                                ContactsContract.Data.RAW_CONTACT_ID + ", " +
176
170
                        // add email addresses
177
171
                        else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
178
172
                                contact.addEmail( contact.new EmailDetail(
179
 
                                        convertBackendTypeToType( CommonDataKinds.Email.class,
 
173
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
180
174
                                                cur.getInt( cur.getColumnIndex(
181
175
                                                        CommonDataKinds.Email.TYPE ) ) ),
182
176
                                        cur.getString( cur.getColumnIndex(
185
179
                        // add postal addresses
186
180
                        else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
187
181
                                contact.addAddress( contact.new AddressDetail(
188
 
                                        convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
 
182
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
189
183
                                                cur.getInt( cur.getColumnIndex(
190
184
                                                        CommonDataKinds.StructuredPostal.TYPE ) ) ),
191
185
                                        cur.getString( cur.getColumnIndex(
203
197
                        else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
204
198
                                contact.addNote( cur.getString( cur.getColumnIndex(
205
199
                                        CommonDataKinds.Note.NOTE ) ) );
206
 
 
207
 
                        // add birthday
208
 
                        else if( type.equals( CommonDataKinds.Event.CONTENT_ITEM_TYPE ) ) {
209
 
                                int event = cur.getInt( cur.getColumnIndex(
210
 
                                        CommonDataKinds.Event.TYPE ) );
211
 
                                if( event == CommonDataKinds.Event.TYPE_BIRTHDAY )
212
 
                                        contact.setBirthday( cur.getString( cur.getColumnIndex(
213
 
                                                CommonDataKinds.Event.START_DATE ) ) );
214
 
                        }
215
200
                }
216
201
                cur.close();
217
202