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
|
/*
* 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 android.app.Activity;
import android.database.Cursor;
import android.provider.Contacts;
public class ContactsContactAccessor implements ContactAccessor
{
Activity _activity = null;
Exporter _exporter = null;
Cursor _cur = null;
public ContactsContactAccessor( 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();
}
@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,
}, null, null, null );
}
// if there are no more contacts, abort
if( !_cur.moveToNext() ) {
_cur = null;
return false;
}
// get this contact's id
Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
// create contact
contact.setName(
_cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
// get 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();
// get 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(
cur.getInt( cur.getColumnIndex(
Contacts.Phones.TYPE ) ),
cur.getString( cur.getColumnIndex(
Contacts.Phones.NUMBER ) ) ) );
cur.close();
// get 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(
cur.getInt( cur.getColumnIndex(
Contacts.ContactMethods.TYPE ) ),
cur.getString( cur.getColumnIndex(
Contacts.ContactMethods.DATA ) ) ) );
else
contact.addAddress( contact.new AddressDetail(
cur.getInt( cur.getColumnIndex(
Contacts.ContactMethods.TYPE ) ),
cur.getString( cur.getColumnIndex(
Contacts.ContactMethods.DATA ) ) ) );
}
cur.close();
return true;
}
}
|