/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * ContactsContractContactAccessor.java
 *
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
 *
 * This file is part of the Export Contacts program (hereafter referred
 * to as "this program").  For more information, see
 * http://ed.am/dev/android/export-contacts
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package am.ed.exportcontacts;

import am.ed.exportcontacts.Exporter.ContactData;
import android.annotation.TargetApi;
import android.app.Activity;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;

@TargetApi(5)
public class ContactsContractBackend implements Backend
{
	Activity _activity = null;
	Exporter _exporter = null;
	Cursor _cur = null;

	public ContactsContractBackend( Activity activity,
		Exporter exporter )
	{
		_activity = activity;
		_exporter = exporter;
	}

	@Override
	public int getNumContacts()
	{
		// get number of aggregate contacts
		Cursor cur = _activity.getContentResolver().query(
			ContactsContract.Contacts.CONTENT_URI,
			new String[] {
				ContactsContract.Contacts._ID,
			}, null, null, null );
		int ret = cur.getCount();
		cur.close();
		return ret;
	}

	private int convertBackendTypeToType( Class< ? > cls, int type )
	{
		if( cls == CommonDataKinds.Phone.class )
		{
			switch( type )
			{
			case CommonDataKinds.Phone.TYPE_MOBILE:
				return ContactData.TYPE_MOBILE;
			case CommonDataKinds.Phone.TYPE_FAX_HOME:
				return ContactData.TYPE_FAX_HOME;
			case CommonDataKinds.Phone.TYPE_FAX_WORK:
				return ContactData.TYPE_FAX_WORK;
			case CommonDataKinds.Phone.TYPE_PAGER:
				return ContactData.TYPE_PAGER;
			case CommonDataKinds.Phone.TYPE_WORK:
				return ContactData.TYPE_WORK;
			default:
				return ContactData.TYPE_HOME;
			}
		}
		else if( cls == CommonDataKinds.Email.class )
		{
			switch( type )
			{
			case CommonDataKinds.Email.TYPE_WORK:
				return ContactData.TYPE_WORK;
			default:
				return ContactData.TYPE_HOME;
			}
		}
		else if( cls == CommonDataKinds.StructuredPostal.class )
		{
			switch( type )
			{
			case CommonDataKinds.StructuredPostal.TYPE_WORK:
				return ContactData.TYPE_WORK;
			default:
				return ContactData.TYPE_HOME;
			}
		}

		return ContactData.TYPE_HOME;
	}

	@Override
	public boolean getNextContact( Exporter.ContactData contact )
	{
		// set up cursor
		if( _cur == null )
		{
			// get all aggregate contacts
			_cur = _activity.getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI,
				new String[] {
					ContactsContract.Contacts._ID,
					ContactsContract.Contacts.DISPLAY_NAME,
				}, null, null, null );
		}

		// if there are no more aggregate contacts, abort
		if( _cur == null ) return false;
		if( !_cur.moveToNext() ) {
			_cur.close();
			_cur = null;
			return false;
		}

		// get this aggregate contact's id
		Long id = _cur.getLong( _cur.getColumnIndex(
			ContactsContract.Contacts._ID ) );

		// create contact
		contact.setName( _cur.getString( _cur.getColumnIndex(
			ContactsContract.Contacts.DISPLAY_NAME ) ) );

		// get all contact data pertaining to the aggregate contact
		Cursor cur = _activity.getContentResolver().query(
			ContactsContract.Data.CONTENT_URI,
			new String[]{
				ContactsContract.Data.MIMETYPE,
				ContactsContract.Data.IS_PRIMARY,
				ContactsContract.Data.DATA1,
				ContactsContract.Data.DATA2,
				ContactsContract.Data.DATA4,
			},
			ContactsContract.Data.CONTACT_ID + " = ? AND " +
// column DELETED not found!?
//				ContactsContract.Data.DELETED + " = 0 AND " +
				ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ?, ? ) ",
			new String[] {
				String.valueOf( id ),
				CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
				CommonDataKinds.Email.CONTENT_ITEM_TYPE,
				CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
				CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,
				CommonDataKinds.Note.CONTENT_ITEM_TYPE,
				CommonDataKinds.Event.CONTENT_ITEM_TYPE,
			},
			ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " +
				ContactsContract.Data.RAW_CONTACT_ID + ", " +
				ContactsContract.Data.IS_PRIMARY + " DESC" );
		while( cur.moveToNext() )
		{
			String type = cur.getString( cur.getColumnIndex(
				ContactsContract.Data.MIMETYPE ) );

			// add phone numbers
			if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) )
				contact.addNumber( contact.new NumberDetail(
					convertBackendTypeToType( CommonDataKinds.Phone.class,
						cur.getInt( cur.getColumnIndex(
							CommonDataKinds.Phone.TYPE ) ) ),
					cur.getString( cur.getColumnIndex(
						CommonDataKinds.Phone.NUMBER ) ) ) );

			// add email addresses
			else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) )
				contact.addEmail( contact.new EmailDetail(
					convertBackendTypeToType( CommonDataKinds.Email.class,
						cur.getInt( cur.getColumnIndex(
							CommonDataKinds.Email.TYPE ) ) ),
					cur.getString( cur.getColumnIndex(
						CommonDataKinds.Email.DATA ) ) ) );

			// add postal addresses
			else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) )
				contact.addAddress( contact.new AddressDetail(
					convertBackendTypeToType( CommonDataKinds.StructuredPostal.class,
						cur.getInt( cur.getColumnIndex(
							CommonDataKinds.StructuredPostal.TYPE ) ) ),
					cur.getString( cur.getColumnIndex(
						CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) );

			// add organisations/titles
			else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) )
				contact.addOrganisation( contact.new OrganisationDetail(
					cur.getString( cur.getColumnIndex(
						CommonDataKinds.Organization.COMPANY ) ),
					cur.getString( cur.getColumnIndex(
						CommonDataKinds.Organization.TITLE ) ) ) );

			// add notes
			else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) )
				contact.addNote( cur.getString( cur.getColumnIndex(
					CommonDataKinds.Note.NOTE ) ) );

			// add birthday
			else if( type.equals( CommonDataKinds.Event.CONTENT_ITEM_TYPE ) ) {
				int event = cur.getInt( cur.getColumnIndex(
					CommonDataKinds.Event.TYPE ) );
				if( event == CommonDataKinds.Event.TYPE_BIRTHDAY )
					contact.setBirthday( cur.getString( cur.getColumnIndex(
						CommonDataKinds.Event.START_DATE ) ) );
			}
		}
		cur.close();

		return true;
	}
}