/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
/*
 * ContactsContactAccessor.java
 *
 * Copyright (C) 2011 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.app.Activity;
import android.database.Cursor;
import android.provider.Contacts;

@SuppressWarnings( "deprecation" )
public class ContactsBackend implements Backend
{
	Activity _activity = null;
	Exporter _exporter = null;
	Cursor _cur = null;

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

	@Override
	public int getNumContacts()
	{
		Cursor cursor = _activity.managedQuery(
			Contacts.People.CONTENT_URI,
			new String[] {
				Contacts.People._ID,
			}, null, null, null );
		return cursor.getCount();
	}

	private int convertBackendTypeToType( Class< ? > cls, int type )
	{
		if( cls == Contacts.Phones.class )
		{
			switch( type )
			{
			case Contacts.Phones.TYPE_MOBILE:
				return ContactData.TYPE_MOBILE;
			case Contacts.Phones.TYPE_FAX_HOME:
				return ContactData.TYPE_FAX_HOME;
			case Contacts.Phones.TYPE_FAX_WORK:
				return ContactData.TYPE_FAX_WORK;
			case Contacts.Phones.TYPE_PAGER:
				return ContactData.TYPE_PAGER;
			case Contacts.Phones.TYPE_WORK:
				return ContactData.TYPE_WORK;
			default:
				return ContactData.TYPE_HOME;
			}
		}
		else if( cls == Contacts.ContactMethods.class )
		{
			switch( type )
			{
			case Contacts.Phones.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 contacts
			_cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
				new String[] {
					Contacts.People._ID,
					Contacts.People.NAME,
					Contacts.People.NOTES,
				}, null, null, null );
		}

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

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

		// set name
		contact.setName(
			_cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );

		// add notes
		contact.addNote(
			_cur.getString( _cur.getColumnIndex( Contacts.People.NOTES ) ) );

		// add the organisations
		Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
			new String[] {
				Contacts.Organizations.COMPANY,
				Contacts.Organizations.TITLE,
			}, Contacts.Organizations.PERSON_ID + " = ?",
			new String[] { id.toString() },
			Contacts.Organizations.ISPRIMARY + " DESC, " +
				Contacts.Organizations.PERSON_ID + " ASC" );
		while( cur.moveToNext() )
			contact.addOrganisation( contact.new OrganisationDetail(
				cur.getString( cur.getColumnIndex(
					Contacts.Organizations.COMPANY ) ),
				cur.getString( cur.getColumnIndex(
					Contacts.Organizations.TITLE ) ) ) );
		cur.close();

		// add the phone numbers
		cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
			new String[] {
				Contacts.Phones.NUMBER,
				Contacts.Phones.TYPE,
			}, Contacts.Phones.PERSON_ID + " = ?",
			new String[] { id.toString() },
			Contacts.Phones.ISPRIMARY + " DESC," +
				Contacts.Phones.PERSON_ID + " ASC" );
		while( cur.moveToNext() )
			contact.addNumber( contact.new NumberDetail(
				convertBackendTypeToType( Contacts.Phones.class,
					cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
				cur.getString( cur.getColumnIndex(
					Contacts.Phones.NUMBER ) ) ) );
		cur.close();

		// add the email and postal addresses
		cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
			new String[] {
				Contacts.ContactMethods.KIND,
				Contacts.ContactMethods.TYPE,
				Contacts.ContactMethods.DATA,
			},
			Contacts.ContactMethods.PERSON_ID + " = ? AND " +
				Contacts.ContactMethods.KIND + " IN( ?, ? )",
			new String[] {
				id.toString(),
				"" + Contacts.KIND_EMAIL,
				"" + Contacts.KIND_POSTAL,
			},
			Contacts.ContactMethods.ISPRIMARY + " DESC," +
				Contacts.ContactMethods.PERSON_ID + " ASC" );
		while( cur.moveToNext() ) {
			int kind = cur.getInt( cur.getColumnIndex(
				Contacts.ContactMethods.KIND ) );
			if( kind == Contacts.KIND_EMAIL )
				contact.addEmail( contact.new EmailDetail(
					convertBackendTypeToType( Contacts.ContactMethods.class,
						cur.getInt( cur.getColumnIndex(
							Contacts.ContactMethods.TYPE ) ) ),
					cur.getString( cur.getColumnIndex(
						Contacts.ContactMethods.DATA ) ) ) );
			else
				contact.addAddress( contact.new AddressDetail(
					convertBackendTypeToType( Contacts.ContactMethods.class,
						cur.getInt( cur.getColumnIndex(
							Contacts.ContactMethods.TYPE ) ) ),
					cur.getString( cur.getColumnIndex(
						Contacts.ContactMethods.DATA ) ) ) );
		}
		cur.close();

		return true;
	}
}