/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts
5 by edam
- added ContactReader interface
1
/*
10 by edam
renamed contact readers to accessors
2
 * ContactsContactAccessor.java
5 by edam
- added ContactReader interface
3
 *
19 by edam
updated some copyright dates
4
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
5 by edam
- added ContactReader interface
5
 *
6
 * This file is part of the Export Contacts program (hereafter referred
7
 * to as "this program"). For more information, see
12 by edam
changed all the URLs to ed.am, including copyrights, package names and project
8
 * http://ed.am/dev/android/export-contacts
5 by edam
- added ContactReader interface
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
12 by edam
changed all the URLs to ed.am, including copyrights, package names and project
24
package am.ed.exportcontacts;
5 by edam
- added ContactReader interface
25
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
26
import am.ed.exportcontacts.Exporter.ContactData;
5 by edam
- added ContactReader interface
27
import android.app.Activity;
28
import android.database.Cursor;
29
import android.provider.Contacts;
30
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
31
@SuppressWarnings( "deprecation" )
32
public class ContactsBackend implements Backend
5 by edam
- added ContactReader interface
33
{
34
	Activity _activity = null;
35
	Exporter _exporter = null;
36
	Cursor _cur = null;
37
17 by edam
renamed ContactAccessor to Backend
38
	public ContactsBackend( Activity activity, Exporter exporter )
5 by edam
- added ContactReader interface
39
	{
40
		_activity = activity;
41
		_exporter = exporter;
42
	}
43
44
	@Override
45
	public int getNumContacts()
46
	{
47
		Cursor cursor = _activity.managedQuery(
48
			Contacts.People.CONTENT_URI,
49
			new String[] {
50
				Contacts.People._ID,
51
			}, null, null, null );
52
		return cursor.getCount();
53
	}
54
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
55
	private int convertBackendTypeToType( Class< ? > cls, int type )
56
	{
57
		if( cls == Contacts.Phones.class )
58
		{
59
			switch( type )
60
			{
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
61
			case Contacts.PhonesColumns.TYPE_MOBILE:
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
62
				return ContactData.TYPE_MOBILE;
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
63
			case Contacts.PhonesColumns.TYPE_FAX_HOME:
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
64
				return ContactData.TYPE_FAX_HOME;
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
65
			case Contacts.PhonesColumns.TYPE_FAX_WORK:
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
66
				return ContactData.TYPE_FAX_WORK;
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
67
			case Contacts.PhonesColumns.TYPE_PAGER:
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
68
				return ContactData.TYPE_PAGER;
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
69
			case Contacts.PhonesColumns.TYPE_WORK:
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
70
				return ContactData.TYPE_WORK;
71
			default:
72
				return ContactData.TYPE_HOME;
73
			}
74
		}
75
		else if( cls == Contacts.ContactMethods.class )
76
		{
77
			switch( type )
78
			{
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
79
			case Contacts.ContactMethodsColumns.TYPE_WORK:
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
80
				return ContactData.TYPE_WORK;
81
			default:
82
				return ContactData.TYPE_HOME;
83
			}
84
		}
85
86
		return ContactData.TYPE_HOME;
87
	}
88
5 by edam
- added ContactReader interface
89
	@Override
90
	public boolean getNextContact( Exporter.ContactData contact )
91
	{
92
		// set up cursor
93
		if( _cur == null )
94
		{
95
			// get all contacts
96
			_cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
97
				new String[] {
98
					Contacts.People._ID,
99
					Contacts.People.NAME,
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
100
					Contacts.People.NOTES,
5 by edam
- added ContactReader interface
101
				}, null, null, null );
102
		}
103
104
		// if there are no more contacts, abort
16 by edam
fixed NPE bug
105
		if( _cur == null || !_cur.moveToNext() ) {
5 by edam
- added ContactReader interface
106
			_cur = null;
107
			return false;
108
		}
109
110
		// get this contact's id
111
		Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
112
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
113
		// set name
5 by edam
- added ContactReader interface
114
		contact.setName(
115
			_cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
116
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
117
		// add notes
21 by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n)
118
		String note = _cur.getString(
119
			_cur.getColumnIndex( Contacts.People.NOTES ) );
120
		if( note != null && note.length() > 0 )
121
			contact.addNote( note );
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
122
123
		// add the organisations
5 by edam
- added ContactReader interface
124
		Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
125
			new String[] {
126
				Contacts.Organizations.COMPANY,
127
				Contacts.Organizations.TITLE,
128
			}, Contacts.Organizations.PERSON_ID + " = ?",
129
			new String[] { id.toString() },
130
			Contacts.Organizations.ISPRIMARY + " DESC, " +
131
				Contacts.Organizations.PERSON_ID + " ASC" );
132
		while( cur.moveToNext() )
133
			contact.addOrganisation( contact.new OrganisationDetail(
134
				cur.getString( cur.getColumnIndex(
135
					Contacts.Organizations.COMPANY ) ),
136
				cur.getString( cur.getColumnIndex(
137
					Contacts.Organizations.TITLE ) ) ) );
6 by edam
- fixed a couple of comment headers
138
		cur.close();
5 by edam
- added ContactReader interface
139
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
140
		// add the phone numbers
5 by edam
- added ContactReader interface
141
		cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
142
			new String[] {
143
				Contacts.Phones.NUMBER,
144
				Contacts.Phones.TYPE,
145
			}, Contacts.Phones.PERSON_ID + " = ?",
146
			new String[] { id.toString() },
147
			Contacts.Phones.ISPRIMARY + " DESC," +
148
				Contacts.Phones.PERSON_ID + " ASC" );
149
		while( cur.moveToNext() )
150
			contact.addNumber( contact.new NumberDetail(
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
151
				convertBackendTypeToType( Contacts.Phones.class,
152
					cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
5 by edam
- added ContactReader interface
153
				cur.getString( cur.getColumnIndex(
154
					Contacts.Phones.NUMBER ) ) ) );
6 by edam
- fixed a couple of comment headers
155
		cur.close();
5 by edam
- added ContactReader interface
156
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
157
		// add the email and postal addresses
5 by edam
- added ContactReader interface
158
		cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
159
			new String[] {
160
				Contacts.ContactMethods.KIND,
161
				Contacts.ContactMethods.TYPE,
162
				Contacts.ContactMethods.DATA,
163
			},
164
			Contacts.ContactMethods.PERSON_ID + " = ? AND " +
165
				Contacts.ContactMethods.KIND + " IN( ?, ? )",
166
			new String[] {
167
				id.toString(),
168
				"" + Contacts.KIND_EMAIL,
169
				"" + Contacts.KIND_POSTAL,
170
			},
171
			Contacts.ContactMethods.ISPRIMARY + " DESC," +
172
				Contacts.ContactMethods.PERSON_ID + " ASC" );
173
		while( cur.moveToNext() ) {
174
			int kind = cur.getInt( cur.getColumnIndex(
175
				Contacts.ContactMethods.KIND ) );
176
			if( kind == Contacts.KIND_EMAIL )
177
				contact.addEmail( contact.new EmailDetail(
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
178
					convertBackendTypeToType( Contacts.ContactMethods.class,
179
						cur.getInt( cur.getColumnIndex(
180
							Contacts.ContactMethods.TYPE ) ) ),
5 by edam
- added ContactReader interface
181
					cur.getString( cur.getColumnIndex(
182
						Contacts.ContactMethods.DATA ) ) ) );
183
			else
184
				contact.addAddress( contact.new AddressDetail(
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
185
					convertBackendTypeToType( Contacts.ContactMethods.class,
186
						cur.getInt( cur.getColumnIndex(
187
							Contacts.ContactMethods.TYPE ) ) ),
5 by edam
- added ContactReader interface
188
					cur.getString( cur.getColumnIndex(
189
						Contacts.ContactMethods.DATA ) ) ) );
190
		}
6 by edam
- fixed a couple of comment headers
191
		cur.close();
5 by edam
- added ContactReader interface
192
193
		return true;
194
	}
195
}