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 |
|
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 " + |
|
22
by edam
removed check for deleted contacts (that seems to be broken); fixed conversion on types for ContactsContract backend |
143 |
// column DELTED not found!? |
144 |
// 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 |
145 |
ContactsContract.Data.MIMETYPE + " IN ( ?, ?, ?, ?, ? )", |
146 |
new String[] { |
|
147 |
String.valueOf( id ), |
|
148 |
CommonDataKinds.Phone.CONTENT_ITEM_TYPE, |
|
149 |
CommonDataKinds.Email.CONTENT_ITEM_TYPE, |
|
150 |
CommonDataKinds.Organization.CONTENT_ITEM_TYPE, |
|
151 |
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, |
|
152 |
CommonDataKinds.Note.CONTENT_ITEM_TYPE, |
|
153 |
}, |
|
154 |
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC, " + |
|
155 |
ContactsContract.Data.RAW_CONTACT_ID + ", " + |
|
156 |
ContactsContract.Data.IS_PRIMARY + " DESC" ); |
|
157 |
while( cur.moveToNext() ) |
|
158 |
{ |
|
159 |
String type = cur.getString( cur.getColumnIndex( |
|
160 |
ContactsContract.Data.MIMETYPE ) ); |
|
161 |
||
162 |
// add phone numbers |
|
163 |
if( type.equals( CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) ) |
|
164 |
contact.addNumber( contact.new NumberDetail( |
|
165 |
convertBackendTypeToType( CommonDataKinds.Phone.class, |
|
166 |
cur.getInt( cur.getColumnIndex( |
|
167 |
CommonDataKinds.Phone.TYPE ) ) ), |
|
168 |
cur.getString( cur.getColumnIndex( |
|
169 |
CommonDataKinds.Phone.NUMBER ) ) ) ); |
|
170 |
||
171 |
// add email addresses |
|
172 |
else if( type.equals( CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) |
|
173 |
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 |
174 |
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 |
175 |
cur.getInt( cur.getColumnIndex( |
176 |
CommonDataKinds.Email.TYPE ) ) ), |
|
177 |
cur.getString( cur.getColumnIndex( |
|
178 |
CommonDataKinds.Email.DATA ) ) ) ); |
|
179 |
||
180 |
// add postal addresses |
|
181 |
else if( type.equals( CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE ) ) |
|
182 |
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 |
183 |
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 |
184 |
cur.getInt( cur.getColumnIndex( |
185 |
CommonDataKinds.StructuredPostal.TYPE ) ) ), |
|
186 |
cur.getString( cur.getColumnIndex( |
|
187 |
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS ) ) ) ); |
|
188 |
||
189 |
// add organisations/titles |
|
190 |
else if( type.equals( CommonDataKinds.Organization.CONTENT_ITEM_TYPE ) ) |
|
191 |
contact.addOrganisation( contact.new OrganisationDetail( |
|
192 |
cur.getString( cur.getColumnIndex( |
|
193 |
CommonDataKinds.Organization.COMPANY ) ), |
|
194 |
cur.getString( cur.getColumnIndex( |
|
195 |
CommonDataKinds.Organization.TITLE ) ) ) ); |
|
196 |
||
197 |
// add notes |
|
198 |
else if( type.equals( CommonDataKinds.Note.CONTENT_ITEM_TYPE ) ) |
|
199 |
contact.addNote( cur.getString( cur.getColumnIndex( |
|
200 |
CommonDataKinds.Note.NOTE ) ) ); |
|
201 |
} |
|
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) |
202 |
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 |
203 |
|
204 |
return true; |
|
205 |
} |
|
206 |
} |