/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-04-24 11:17:41 UTC
  • Revision ID: tim@ed.am-20120424111741-gbzn6qr4i574d9s4
renamed contact readers to accessors

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * ContactsContractContactAccessor.java
3
 
 *
4
 
 * Copyright (C) 2011 to 2012 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 cursor = _activity.managedQuery(
52
 
                        ContactsContract.Contacts.CONTENT_URI,
53
 
                        new String[] {
54
 
                                ContactsContract.Contacts._ID,
55
 
                        }, null, null, null );
56
 
                return cursor.getCount();
57
 
        }
58
 
 
59
 
        private int convertBackendTypeToType( Class< ? > cls, int type )
60
 
        {
61
 
                if( cls == CommonDataKinds.Phone.class )
62
 
                {
63
 
                        switch( type )
64
 
                        {
65
 
                        case CommonDataKinds.Phone.TYPE_MOBILE:
66
 
                                return ContactData.TYPE_MOBILE;
67
 
                        case CommonDataKinds.Phone.TYPE_FAX_HOME:
68
 
                                return ContactData.TYPE_FAX_HOME;
69
 
                        case CommonDataKinds.Phone.TYPE_FAX_WORK:
70
 
                                return ContactData.TYPE_FAX_WORK;
71
 
                        case CommonDataKinds.Phone.TYPE_PAGER:
72
 
                                return ContactData.TYPE_PAGER;
73
 
                        case CommonDataKinds.Phone.TYPE_WORK:
74
 
                                return ContactData.TYPE_WORK;
75
 
                        default:
76
 
                                return ContactData.TYPE_HOME;
77
 
                        }
78
 
                }
79
 
                else if( cls == CommonDataKinds.Email.class )
80
 
                {
81
 
                        switch( type )
82
 
                        {
83
 
                        case CommonDataKinds.Email.TYPE_WORK:
84
 
                                return ContactData.TYPE_WORK;
85
 
                        default:
86
 
                                return ContactData.TYPE_HOME;
87
 
                        }
88
 
                }
89
 
                else if( cls == CommonDataKinds.StructuredPostal.class )
90
 
                {
91
 
                        switch( type )
92
 
                        {
93
 
                        case CommonDataKinds.StructuredPostal.TYPE_WORK:
94
 
                                return ContactData.TYPE_WORK;
95
 
                        default:
96
 
                                return ContactData.TYPE_HOME;
97
 
                        }
98
 
                }
99
 
 
100
 
                return ContactData.TYPE_HOME;
101
 
        }
102
 
 
103
 
        @Override
104
 
        public boolean getNextContact( Exporter.ContactData contact )
105
 
        {
106
 
                // set up cursor
107
 
                if( _cur == null )
108
 
                {
109
 
                        // get all aggregate contacts
110
 
                        _cur = _activity.managedQuery(
111
 
                                ContactsContract.Contacts.CONTENT_URI,
112
 
                                new String[] {
113
 
                                        ContactsContract.Contacts._ID,
114
 
                                        ContactsContract.Contacts.DISPLAY_NAME,
115
 
                                }, null, null, null );
116
 
                }
117
 
 
118
 
                // if there are no more aggregate contacts, abort
119
 
                if( _cur == null || !_cur.moveToNext() ) {
120
 
                        _cur = null;
121
 
                        return false;
122
 
                }
123
 
 
124
 
                // get this aggregate contact's id
125
 
                Long id = _cur.getLong( _cur.getColumnIndex(
126
 
                        ContactsContract.Contacts._ID ) );
127
 
 
128
 
                // create contact
129
 
                contact.setName( _cur.getString( _cur.getColumnIndex(
130
 
                        ContactsContract.Contacts.DISPLAY_NAME ) ) );
131
 
 
132
 
                // get all contact data pertaining to the aggregate contact
133
 
                Cursor cur = _activity.getContentResolver().query(
134
 
                        ContactsContract.Data.CONTENT_URI,
135
 
                        new String[]{
136
 
                                ContactsContract.Data.MIMETYPE,
137
 
                                ContactsContract.Data.IS_PRIMARY,
138
 
                                ContactsContract.Data.DATA1,
139
 
                                ContactsContract.Data.DATA2,
140
 
                                ContactsContract.Data.DATA4,
141
 
                        },
142
 
                        ContactsContract.Data.CONTACT_ID + " = ? AND " +
143
 
                                ContactsContract.Data.DELETED + " = 0 AND " +
144
 
                                ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ? )",
145
 
                        new String[] {
146
 
                                String.valueOf( id ),
147
 
                                CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
148
 
                                CommonDataKinds.Email.CONTENT_ITEM_TYPE,
149
 
                                CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
150
 
                                CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
151
 
                                CommonDataKinds.Note.CONTENT_ITEM_TYPE,
152
 
                        },
153
 
                        ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
154
 
                                ContactsContract.Data.RAW_CONTACT_ID + ", " +
155
 
                                ContactsContract.Data.IS_PRIMARY + " DESC" );
156
 
                while( cur.moveToNext() )
157
 
                {
158
 
                        String type = cur.getString( cur.getColumnIndex(
159
 
                                ContactsContract.Data.MIMETYPE ) );
160
 
 
161
 
                        // add phone numbers
162
 
                        if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
163
 
                                contact.addNumber( contact.new NumberDetail(
164
 
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
165
 
                                                cur.getInt( cur.getColumnIndex(
166
 
                                                        CommonDataKinds.Phone.TYPE ) ) ),
167
 
                                        cur.getString( cur.getColumnIndex(
168
 
                                                CommonDataKinds.Phone.NUMBER ) ) ) );
169
 
 
170
 
                        // add email addresses
171
 
                        else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
172
 
                                contact.addEmail( contact.new EmailDetail(
173
 
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
174
 
                                                cur.getInt( cur.getColumnIndex(
175
 
                                                        CommonDataKinds.Email.TYPE ) ) ),
176
 
                                        cur.getString( cur.getColumnIndex(
177
 
                                                CommonDataKinds.Email.DATA ) ) ) );
178
 
 
179
 
                        // add postal addresses
180
 
                        else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
181
 
                                contact.addAddress( contact.new AddressDetail(
182
 
                                        convertBackendTypeToType( CommonDataKinds.Phone.class,
183
 
                                                cur.getInt( cur.getColumnIndex(
184
 
                                                        CommonDataKinds.StructuredPostal.TYPE ) ) ),
185
 
                                        cur.getString( cur.getColumnIndex(
186
 
                                                CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );
187
 
 
188
 
                        // add organisations/titles
189
 
                        else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
190
 
                                contact.addOrganisation( contact.new OrganisationDetail(
191
 
                                        cur.getString( cur.getColumnIndex(
192
 
                                                CommonDataKinds.Organization.COMPANY ) ),
193
 
                                        cur.getString( cur.getColumnIndex(
194
 
                                                CommonDataKinds.Organization.TITLE ) ) ) );
195
 
 
196
 
                        // add notes
197
 
                        else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
198
 
                                contact.addNote( cur.getString( cur.getColumnIndex(
199
 
                                        CommonDataKinds.Note.NOTE ) ) );
200
 
                }
201
 
 
202
 
                return true;
203
 
        }
204
 
}