/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: Tim Marston
  • Date: 2014-03-01 18:03:39 UTC
  • Revision ID: tim@ed.am-20140301180339-rmfh8x7wys2inc65
new family pic

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsContractContactAccessor.java
3
3
 *
4
 
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 to 2013 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 cursor = _activity.managedQuery(
 
51
                Cursor cur = _activity.getContentResolver().query(
52
52
                        ContactsContract.Contacts.CONTENT_URI,
53
53
                        new String[] {
54
54
                                ContactsContract.Contacts._ID,
55
55
                        }, null, null, null );
56
 
                return cursor.getCount();
 
56
                int ret = cur.getCount();
 
57
                cur.close();
 
58
                return ret;
57
59
        }
58
60
 
59
61
        private int convertBackendTypeToType( Class< ? > cls, int type )
107
109
                if( _cur == null )
108
110
                {
109
111
                        // get all aggregate contacts
110
 
                        _cur = _activity.managedQuery(
 
112
                        _cur = _activity.getContentResolver().query(
111
113
                                ContactsContract.Contacts.CONTENT_URI,
112
114
                                new String[] {
113
115
                                        ContactsContract.Contacts._ID,
116
118
                }
117
119
 
118
120
                // if there are no more aggregate contacts, abort
119
 
                if( _cur == null || !_cur.moveToNext() ) {
 
121
                if( _cur == null ) return false;
 
122
                if( !_cur.moveToNext() ) {
 
123
                        _cur.close();
120
124
                        _cur = null;
121
125
                        return false;
122
126
                }
140
144
                                ContactsContract.Data.DATA4,
141
145
                        },
142
146
                        ContactsContract.Data.CONTACT_ID + " = ? AND " +
143
 
                                ContactsContract.Data.DELETED + " = 0 AND " +
144
 
                                ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ? )",
 
147
// column DELETED not found!?
 
148
//                              ContactsContract.Data.DELETED + " = 0 AND " +
 
149
                                ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ?, ? ) ",
145
150
                        new String[] {
146
151
                                String.valueOf( id ),
147
152
                                CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
149
154
                                CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
150
155
                                CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
151
156
                                CommonDataKinds.Note.CONTENT_ITEM_TYPE,
 
157
                                CommonDataKinds.Event.CONTENT_ITEM_TYPE,
152
158
                        },
153
159
                        ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
154
160
                                ContactsContract.Data.RAW_CONTACT_ID + ", " +
170
176
                        // add email addresses
171
177
                        else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
172
178
                                contact.addEmail( contact.new EmailDetail(
173
 
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
 
179
                                        convertBackendTypeToType( CommonDataKinds.Email.class,
174
180
                                                cur.getInt( cur.getColumnIndex(
175
181
                                                        CommonDataKinds.Email.TYPE ) ) ),
176
182
                                        cur.getString( cur.getColumnIndex(
179
185
                        // add postal addresses
180
186
                        else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
181
187
                                contact.addAddress( contact.new AddressDetail(
182
 
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
 
188
                                        convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
183
189
                                                cur.getInt( cur.getColumnIndex(
184
190
                                                        CommonDataKinds.StructuredPostal.TYPE ) ) ),
185
191
                                        cur.getString( cur.getColumnIndex(
197
203
                        else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
198
204
                                contact.addNote( cur.getString( cur.getColumnIndex(
199
205
                                        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
                        }
200
215
                }
 
216
                cur.close();
201
217
 
202
218
                return true;
203
219
        }