/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: 2010-07-04 14:54:18 UTC
  • Revision ID: edam@waxworlds.org-20100704145418-vcpya2sxsop0dlrq
- initial checkin
- copied intro and basic vcf-configure activities from import-contacts
- copied WizzardActivity class from import-contacts

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * ContactsContractContactAccessor.java
3
 
 *
4
 
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
5
 
 *
6
 
 * This file is part of the Export Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/export-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.exportcontacts;
25
 
 
26
 
import am.ed.exportcontacts.Exporter.ContactData;
27
 
import android.annotation.TargetApi;
28
 
import android.app.Activity;
29
 
import android.database.Cursor;
30
 
import android.provider.ContactsContract;
31
 
import android.provider.ContactsContract.CommonDataKinds;
32
 
 
33
 
@TargetApi(5)
34
 
public class ContactsContractBackend implements Backend
35
 
{
36
 
        Activity _activity = null;
37
 
        Exporter _exporter = null;
38
 
        Cursor _cur = null;
39
 
 
40
 
        public ContactsContractBackend( Activity activity,
41
 
                Exporter exporter )
42
 
        {
43
 
                _activity = activity;
44
 
                _exporter = exporter;
45
 
        }
46
 
 
47
 
        @Override
48
 
        public int getNumContacts()
49
 
        {
50
 
                // get number of aggregate contacts
51
 
                Cursor cur = _activity.getContentResolver().query(
52
 
                        ContactsContract.Contacts.CONTENT_URI,
53
 
                        new String[] {
54
 
                                ContactsContract.Contacts._ID,
55
 
                        }, null, null, null );
56
 
                int ret = cur.getCount();
57
 
                cur.close();
58
 
                return ret;
59
 
        }
60
 
 
61
 
        private int convertBackendTypeToType( Class< ? > cls, int type )
62
 
        {
63
 
                if( cls == CommonDataKinds.Phone.class )
64
 
                {
65
 
                        switch( type )
66
 
                        {
67
 
                        case CommonDataKinds.Phone.TYPE_MOBILE:
68
 
                                return ContactData.TYPE_MOBILE;
69
 
                        case CommonDataKinds.Phone.TYPE_FAX_HOME:
70
 
                                return ContactData.TYPE_FAX_HOME;
71
 
                        case CommonDataKinds.Phone.TYPE_FAX_WORK:
72
 
                                return ContactData.TYPE_FAX_WORK;
73
 
                        case CommonDataKinds.Phone.TYPE_PAGER:
74
 
                                return ContactData.TYPE_PAGER;
75
 
                        case CommonDataKinds.Phone.TYPE_WORK:
76
 
                                return ContactData.TYPE_WORK;
77
 
                        default:
78
 
                                return ContactData.TYPE_HOME;
79
 
                        }
80
 
                }
81
 
                else if( cls == CommonDataKinds.Email.class )
82
 
                {
83
 
                        switch( type )
84
 
                        {
85
 
                        case CommonDataKinds.Email.TYPE_WORK:
86
 
                                return ContactData.TYPE_WORK;
87
 
                        default:
88
 
                                return ContactData.TYPE_HOME;
89
 
                        }
90
 
                }
91
 
                else if( cls == CommonDataKinds.StructuredPostal.class )
92
 
                {
93
 
                        switch( type )
94
 
                        {
95
 
                        case CommonDataKinds.StructuredPostal.TYPE_WORK:
96
 
                                return ContactData.TYPE_WORK;
97
 
                        default:
98
 
                                return ContactData.TYPE_HOME;
99
 
                        }
100
 
                }
101
 
 
102
 
                return ContactData.TYPE_HOME;
103
 
        }
104
 
 
105
 
        @Override
106
 
        public boolean getNextContact( Exporter.ContactData contact )
107
 
        {
108
 
                // set up cursor
109
 
                if( _cur == null )
110
 
                {
111
 
                        // get all aggregate contacts
112
 
                        _cur = _activity.getContentResolver().query(
113
 
                                ContactsContract.Contacts.CONTENT_URI,
114
 
                                new String[] {
115
 
                                        ContactsContract.Contacts._ID,
116
 
                                        ContactsContract.Contacts.DISPLAY_NAME,
117
 
                                }, null, null, null );
118
 
                }
119
 
 
120
 
                // if there are no more aggregate contacts, abort
121
 
                if( _cur == null ) return false;
122
 
                if( !_cur.moveToNext() ) {
123
 
                        _cur.close();
124
 
                        _cur = null;
125
 
                        return false;
126
 
                }
127
 
 
128
 
                // get this aggregate contact's id
129
 
                Long id = _cur.getLong( _cur.getColumnIndex(
130
 
                        ContactsContract.Contacts._ID ) );
131
 
 
132
 
                // create contact
133
 
                contact.setName( _cur.getString( _cur.getColumnIndex(
134
 
                        ContactsContract.Contacts.DISPLAY_NAME ) ) );
135
 
 
136
 
                // get all contact data pertaining to the aggregate contact
137
 
                Cursor cur = _activity.getContentResolver().query(
138
 
                        ContactsContract.Data.CONTENT_URI,
139
 
                        new String[]{
140
 
                                ContactsContract.Data.MIMETYPE,
141
 
                                ContactsContract.Data.IS_PRIMARY,
142
 
                                ContactsContract.Data.DATA1,
143
 
                                ContactsContract.Data.DATA2,
144
 
                                ContactsContract.Data.DATA4,
145
 
                        },
146
 
                        ContactsContract.Data.CONTACT_ID + " = ? AND " +
147
 
// column DELETED not found!?
148
 
//                              ContactsContract.Data.DELETED + " = 0 AND " +
149
 
                                ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ?, ? ) ",
150
 
                        new String[] {
151
 
                                String.valueOf( id ),
152
 
                                CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
153
 
                                CommonDataKinds.Email.CONTENT_ITEM_TYPE,
154
 
                                CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
155
 
                                CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
156
 
                                CommonDataKinds.Note.CONTENT_ITEM_TYPE,
157
 
                                CommonDataKinds.Event.CONTENT_ITEM_TYPE,
158
 
                        },
159
 
                        ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
160
 
                                ContactsContract.Data.RAW_CONTACT_ID + ", " +
161
 
                                ContactsContract.Data.IS_PRIMARY + " DESC" );
162
 
                while( cur.moveToNext() )
163
 
                {
164
 
                        String type = cur.getString( cur.getColumnIndex(
165
 
                                ContactsContract.Data.MIMETYPE ) );
166
 
 
167
 
                        // add phone numbers
168
 
                        if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
169
 
                                contact.addNumber( contact.new NumberDetail(
170
 
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
171
 
                                                cur.getInt( cur.getColumnIndex(
172
 
                                                        CommonDataKinds.Phone.TYPE ) ) ),
173
 
                                        cur.getString( cur.getColumnIndex(
174
 
                                                CommonDataKinds.Phone.NUMBER ) ) ) );
175
 
 
176
 
                        // add email addresses
177
 
                        else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
178
 
                                contact.addEmail( contact.new EmailDetail(
179
 
                                        convertBackendTypeToType( CommonDataKinds.Email.class,
180
 
                                                cur.getInt( cur.getColumnIndex(
181
 
                                                        CommonDataKinds.Email.TYPE ) ) ),
182
 
                                        cur.getString( cur.getColumnIndex(
183
 
                                                CommonDataKinds.Email.DATA ) ) ) );
184
 
 
185
 
                        // add postal addresses
186
 
                        else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
187
 
                                contact.addAddress( contact.new AddressDetail(
188
 
                                        convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
189
 
                                                cur.getInt( cur.getColumnIndex(
190
 
                                                        CommonDataKinds.StructuredPostal.TYPE ) ) ),
191
 
                                        cur.getString( cur.getColumnIndex(
192
 
                                                CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );
193
 
 
194
 
                        // add organisations/titles
195
 
                        else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
196
 
                                contact.addOrganisation( contact.new OrganisationDetail(
197
 
                                        cur.getString( cur.getColumnIndex(
198
 
                                                CommonDataKinds.Organization.COMPANY ) ),
199
 
                                        cur.getString( cur.getColumnIndex(
200
 
                                                CommonDataKinds.Organization.TITLE ) ) ) );
201
 
 
202
 
                        // add notes
203
 
                        else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
204
 
                                contact.addNote( cur.getString( cur.getColumnIndex(
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
 
                        }
215
 
                }
216
 
                cur.close();
217
 
 
218
 
                return true;
219
 
        }
220
 
}