/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/ContactsBackend.java

  • Committer: Tim Marston
  • Date: 2015-02-25 00:19:50 UTC
  • Revision ID: tim@ed.am-20150225001950-prsbdjr5nov3mrvs
fixed date in NEWS

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * ContactsContactAccessor.java
 
3
 *
 
4
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
 
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.app.Activity;
 
28
import android.database.Cursor;
 
29
import android.provider.Contacts;
 
30
 
 
31
@SuppressWarnings( "deprecation" )
 
32
public class ContactsBackend implements Backend
 
33
{
 
34
        Activity _activity = null;
 
35
        Exporter _exporter = null;
 
36
        Cursor _cur = null;
 
37
 
 
38
        public ContactsBackend( Activity activity, Exporter exporter )
 
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
 
 
55
        private int convertBackendTypeToType( Class< ? > cls, int type )
 
56
        {
 
57
                if( cls == Contacts.Phones.class )
 
58
                {
 
59
                        switch( type )
 
60
                        {
 
61
                        case Contacts.PhonesColumns.TYPE_MOBILE:
 
62
                                return ContactData.TYPE_MOBILE;
 
63
                        case Contacts.PhonesColumns.TYPE_FAX_HOME:
 
64
                                return ContactData.TYPE_FAX_HOME;
 
65
                        case Contacts.PhonesColumns.TYPE_FAX_WORK:
 
66
                                return ContactData.TYPE_FAX_WORK;
 
67
                        case Contacts.PhonesColumns.TYPE_PAGER:
 
68
                                return ContactData.TYPE_PAGER;
 
69
                        case Contacts.PhonesColumns.TYPE_WORK:
 
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
                        {
 
79
                        case Contacts.ContactMethodsColumns.TYPE_WORK:
 
80
                                return ContactData.TYPE_WORK;
 
81
                        default:
 
82
                                return ContactData.TYPE_HOME;
 
83
                        }
 
84
                }
 
85
 
 
86
                return ContactData.TYPE_HOME;
 
87
        }
 
88
 
 
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.getContentResolver().query(
 
97
                                Contacts.People.CONTENT_URI,
 
98
                                new String[] {
 
99
                                        Contacts.People._ID,
 
100
                                        Contacts.People.NAME,
 
101
                                        Contacts.People.NOTES,
 
102
                                }, null, null, null );
 
103
                }
 
104
 
 
105
                // if there are no more contacts, abort
 
106
                if( _cur == null ) return false;
 
107
                if( !_cur.moveToNext() ) {
 
108
                        _cur.close();
 
109
                        _cur = null;
 
110
                        return false;
 
111
                }
 
112
 
 
113
                // get this contact's id
 
114
                Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
 
115
 
 
116
                // set name
 
117
                contact.setName(
 
118
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
 
119
 
 
120
                // add notes
 
121
                String note = _cur.getString(
 
122
                        _cur.getColumnIndex( Contacts.People.NOTES ) );
 
123
                if( note != null && note.length() > 0 )
 
124
                        contact.addNote( note );
 
125
 
 
126
                // add the organisations
 
127
                Cursor cur = _activity.getContentResolver().query(
 
128
                        Contacts.Organizations.CONTENT_URI,
 
129
                        new String[] {
 
130
                                Contacts.Organizations.COMPANY,
 
131
                                Contacts.Organizations.TITLE,
 
132
                        }, Contacts.Organizations.PERSON_ID + " = ?",
 
133
                        new String[] { id.toString() },
 
134
                        Contacts.Organizations.ISPRIMARY + " DESC, " +
 
135
                                Contacts.Organizations.PERSON_ID + " ASC" );
 
136
                while( cur.moveToNext() )
 
137
                        contact.addOrganisation( contact.new OrganisationDetail(
 
138
                                cur.getString( cur.getColumnIndex(
 
139
                                        Contacts.Organizations.COMPANY ) ),
 
140
                                cur.getString( cur.getColumnIndex(
 
141
                                        Contacts.Organizations.TITLE ) ) ) );
 
142
                cur.close();
 
143
 
 
144
                // add the phone numbers
 
145
                cur = _activity.getContentResolver().query(
 
146
                        Contacts.Phones.CONTENT_URI,
 
147
                        new String[] {
 
148
                                Contacts.Phones.NUMBER,
 
149
                                Contacts.Phones.TYPE,
 
150
                        }, Contacts.Phones.PERSON_ID + " = ?",
 
151
                        new String[] { id.toString() },
 
152
                        Contacts.Phones.ISPRIMARY + " DESC," +
 
153
                                Contacts.Phones.PERSON_ID + " ASC" );
 
154
                while( cur.moveToNext() )
 
155
                        contact.addNumber( contact.new NumberDetail(
 
156
                                convertBackendTypeToType( Contacts.Phones.class,
 
157
                                        cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
 
158
                                cur.getString( cur.getColumnIndex(
 
159
                                        Contacts.Phones.NUMBER ) ) ) );
 
160
                cur.close();
 
161
 
 
162
                // add the email and postal addresses
 
163
                cur = _activity.getContentResolver().query(
 
164
                        Contacts.ContactMethods.CONTENT_URI,
 
165
                        new String[] {
 
166
                                Contacts.ContactMethods.KIND,
 
167
                                Contacts.ContactMethods.TYPE,
 
168
                                Contacts.ContactMethods.DATA,
 
169
                        },
 
170
                        Contacts.ContactMethods.PERSON_ID + " = ? AND " +
 
171
                                Contacts.ContactMethods.KIND + " IN( ?, ? )",
 
172
                        new String[] {
 
173
                                id.toString(),
 
174
                                "" + Contacts.KIND_EMAIL,
 
175
                                "" + Contacts.KIND_POSTAL,
 
176
                        },
 
177
                        Contacts.ContactMethods.ISPRIMARY + " DESC," +
 
178
                                Contacts.ContactMethods.PERSON_ID + " ASC" );
 
179
                while( cur.moveToNext() ) {
 
180
                        int kind = cur.getInt( cur.getColumnIndex(
 
181
                                Contacts.ContactMethods.KIND ) );
 
182
                        if( kind == Contacts.KIND_EMAIL )
 
183
                                contact.addEmail( contact.new EmailDetail(
 
184
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
 
185
                                                cur.getInt( cur.getColumnIndex(
 
186
                                                        Contacts.ContactMethods.TYPE ) ) ),
 
187
                                        cur.getString( cur.getColumnIndex(
 
188
                                                Contacts.ContactMethods.DATA ) ) ) );
 
189
                        else
 
190
                                contact.addAddress( contact.new AddressDetail(
 
191
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
 
192
                                                cur.getInt( cur.getColumnIndex(
 
193
                                                        Contacts.ContactMethods.TYPE ) ) ),
 
194
                                        cur.getString( cur.getColumnIndex(
 
195
                                                Contacts.ContactMethods.DATA ) ) ) );
 
196
                }
 
197
                cur.close();
 
198
 
 
199
                return true;
 
200
        }
 
201
}