bzr branch
http://bzr.ed.am/android/export-contacts
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 |
1 |
/* |
2 |
* ContactsContractContactAccessor.java |
|
3 |
* |
|
19
by edam
updated some copyright dates |
4 |
* Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am> |
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 |
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 |
|
25
by edam
stop using managedQuery(), which isn't cleaning up my queries |
51 |
Cursor cur = _activity.getContentResolver().query( |
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 |
52 |
ContactsContract.Contacts.CONTENT_URI, |
53 |
new String[] { |
|
54 |
ContactsContract.Contacts._ID, |
|
55 |
}, null, null, null ); |
|
25
by edam
stop using managedQuery(), which isn't cleaning up my queries |
56 |
int ret = cur.getCount(); |
57 |
cur.close(); |
|
58 |
return ret; |
|
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 |
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 |
|
25
by edam
stop using managedQuery(), which isn't cleaning up my queries |
112 |
_cur = _activity.getContentResolver().query( |
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 |
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 |
|
25
by edam
stop using managedQuery(), which isn't cleaning up my queries |
121 |
if( _cur == null ) return false; |
122 |
if( !_cur.moveToNext() ) { |
|
123 |
_cur.close(); |
|
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 |
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 " + |
|
22
by edam
removed check for deleted contacts (that seems to be broken); fixed conversion on types for ContactsContract backend |
147 |
// column DELTED not found!? |
148 |
// ContactsContract.Data.DELETED + " = 0 AND " + |
|
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 |
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 |
}, |
|
158 |
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " + |
|
159 |
ContactsContract.Data.RAW_CONTACT_ID + ", " + |
|
160 |
ContactsContract.Data.IS_PRIMARY + " DESC" ); |
|
161 |
while( cur.moveToNext() ) |
|
162 |
{ |
|
163 |
String type = cur.getString( cur.getColumnIndex( |
|
164 |
ContactsContract.Data.MIMETYPE ) ); |
|
165 |
||
166 |
// add phone numbers |
|
167 |
if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) ) |
|
168 |
contact.addNumber( contact.new NumberDetail( |
|
169 |
convertBackendTypeToType( CommonDataKinds.Phone.class, |
|
170 |
cur.getInt( cur.getColumnIndex( |
|
171 |
CommonDataKinds.Phone.TYPE ) ) ), |
|
172 |
cur.getString( cur.getColumnIndex( |
|
173 |
CommonDataKinds.Phone.NUMBER ) ) ) ); |
|
174 |
||
175 |
// add email addresses |
|
176 |
else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) |
|
177 |
contact.addEmail( contact.new EmailDetail( |
|
22
by edam
removed check for deleted contacts (that seems to be broken); fixed conversion on types for ContactsContract backend |
178 |
convertBackendTypeToType( CommonDataKinds.Email.class, |
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 |
179 |
cur.getInt( cur.getColumnIndex( |
180 |
CommonDataKinds.Email.TYPE ) ) ), |
|
181 |
cur.getString( cur.getColumnIndex( |
|
182 |
CommonDataKinds.Email.DATA ) ) ) ); |
|
183 |
||
184 |
// add postal addresses |
|
185 |
else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) ) |
|
186 |
contact.addAddress( contact.new AddressDetail( |
|
22
by edam
removed check for deleted contacts (that seems to be broken); fixed conversion on types for ContactsContract backend |
187 |
convertBackendTypeToType( CommonDataKinds.StructuredPostal.class, |
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 |
188 |
cur.getInt( cur.getColumnIndex( |
189 |
CommonDataKinds.StructuredPostal.TYPE ) ) ), |
|
190 |
cur.getString( cur.getColumnIndex( |
|
191 |
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) ); |
|
192 |
||
193 |
// add organisations/titles |
|
194 |
else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) ) |
|
195 |
contact.addOrganisation( contact.new OrganisationDetail( |
|
196 |
cur.getString( cur.getColumnIndex( |
|
197 |
CommonDataKinds.Organization.COMPANY ) ), |
|
198 |
cur.getString( cur.getColumnIndex( |
|
199 |
CommonDataKinds.Organization.TITLE ) ) ) ); |
|
200 |
||
201 |
// add notes |
|
202 |
else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) ) |
|
203 |
contact.addNote( cur.getString( cur.getColumnIndex( |
|
204 |
CommonDataKinds.Note.NOTE ) ) ); |
|
205 |
} |
|
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) |
206 |
cur.close(); |
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 |
207 |
|
208 |
return true; |
|
209 |
} |
|
210 |
} |