/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/org/waxworlds/edam/exportcontacts/ContactsContactReader.java

  • Committer: edam
  • Date: 2011-06-11 08:22:04 UTC
  • Revision ID: edam@waxworlds.org-20110611082204-u2v1ri3a8iayq9b4
- added ContactReader interface
- added ContactsContactReader class to read old-style android.Contacts data
- updated FileChooser from import contacts app
- updated TODO
- added Doit activity
- added Exporter
- added VcardExporter that writes vCards

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * ContactsContactReader.java
 
3
 *
 
4
 * Copyright (C) 2010 Tim Marston <edam@waxworlds.org>
 
5
 *
 
6
 * This file is part of the Export Contacts program (hereafter referred
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/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 org.waxworlds.edam.exportcontacts;
 
25
 
 
26
import android.app.Activity;
 
27
import android.database.Cursor;
 
28
import android.provider.Contacts;
 
29
 
 
30
public class ContactsContactReader implements ContactReader
 
31
{
 
32
        Activity _activity = null;
 
33
        Exporter _exporter = null;
 
34
        Cursor _cur = null;
 
35
 
 
36
        public ContactsContactReader( Activity activity, Exporter exporter )
 
37
        {
 
38
                _activity = activity;
 
39
                _exporter = exporter;
 
40
        }
 
41
 
 
42
        @Override
 
43
        public int getNumContacts()
 
44
        {
 
45
                Cursor cursor = _activity.managedQuery(
 
46
                        Contacts.People.CONTENT_URI,
 
47
                        new String[] {
 
48
                                Contacts.People._ID,
 
49
                        }, null, null, null );
 
50
                return cursor.getCount();
 
51
        }
 
52
 
 
53
        @Override
 
54
        public boolean getNextContact( Exporter.ContactData contact )
 
55
        {
 
56
                // set up cursor
 
57
                if( _cur == null )
 
58
                {
 
59
                        // get all contacts
 
60
                        _cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
 
61
                                new String[] {
 
62
                                        Contacts.People._ID,
 
63
                                        Contacts.People.NAME,
 
64
                                }, null, null, null );
 
65
                }
 
66
 
 
67
                // if there are no more contacts, abort
 
68
                if( !_cur.moveToNext() ) {
 
69
                        _cur = null;
 
70
                        return false;
 
71
                }
 
72
 
 
73
                // get this contact's id
 
74
                Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
 
75
 
 
76
                // create contact
 
77
                contact.setName(
 
78
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
 
79
 
 
80
                // get the organisations
 
81
                Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
 
82
                        new String[] {
 
83
                                Contacts.Organizations.COMPANY,
 
84
                                Contacts.Organizations.TITLE,
 
85
                        }, Contacts.Organizations.PERSON_ID + " = ?",
 
86
                        new String[] { id.toString() },
 
87
                        Contacts.Organizations.ISPRIMARY + " DESC, " +
 
88
                                Contacts.Organizations.PERSON_ID + " ASC" );
 
89
                while( cur.moveToNext() )
 
90
                        contact.addOrganisation( contact.new OrganisationDetail(
 
91
                                cur.getString( cur.getColumnIndex(
 
92
                                        Contacts.Organizations.COMPANY ) ),
 
93
                                cur.getString( cur.getColumnIndex(
 
94
                                        Contacts.Organizations.TITLE ) ) ) );
 
95
 
 
96
                // get the phone numbers
 
97
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
 
98
                        new String[] {
 
99
                                Contacts.Phones.NUMBER,
 
100
                                Contacts.Phones.TYPE,
 
101
                        }, Contacts.Phones.PERSON_ID + " = ?",
 
102
                        new String[] { id.toString() },
 
103
                        Contacts.Phones.ISPRIMARY + " DESC," +
 
104
                                Contacts.Phones.PERSON_ID + " ASC" );
 
105
                while( cur.moveToNext() )
 
106
                        contact.addNumber( contact.new NumberDetail(
 
107
                                cur.getInt( cur.getColumnIndex(
 
108
                                        Contacts.Phones.TYPE ) ),
 
109
                                cur.getString( cur.getColumnIndex(
 
110
                                        Contacts.Phones.NUMBER ) ) ) );
 
111
 
 
112
                // get the email and postal addresses
 
113
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
114
                        new String[] {
 
115
                                Contacts.ContactMethods.KIND,
 
116
                                Contacts.ContactMethods.TYPE,
 
117
                                Contacts.ContactMethods.DATA,
 
118
                        },
 
119
                        Contacts.ContactMethods.PERSON_ID + " = ? AND " +
 
120
                                Contacts.ContactMethods.KIND + " IN( ?, ? )",
 
121
                        new String[] {
 
122
                                id.toString(),
 
123
                                "" + Contacts.KIND_EMAIL,
 
124
                                "" + Contacts.KIND_POSTAL,
 
125
                        },
 
126
                        Contacts.ContactMethods.ISPRIMARY + " DESC," +
 
127
                                Contacts.ContactMethods.PERSON_ID + " ASC" );
 
128
                while( cur.moveToNext() ) {
 
129
                        int kind = cur.getInt( cur.getColumnIndex(
 
130
                                Contacts.ContactMethods.KIND ) );
 
131
                        if( kind == Contacts.KIND_EMAIL )
 
132
                                contact.addEmail( contact.new EmailDetail(
 
133
                                        cur.getInt( cur.getColumnIndex(
 
134
                                                Contacts.ContactMethods.TYPE ) ),
 
135
                                        cur.getString( cur.getColumnIndex(
 
136
                                                Contacts.ContactMethods.DATA ) ) ) );
 
137
                        else
 
138
                                contact.addAddress( contact.new AddressDetail(
 
139
                                        cur.getInt( cur.getColumnIndex(
 
140
                                                Contacts.ContactMethods.TYPE ) ),
 
141
                                        cur.getString( cur.getColumnIndex(
 
142
                                                Contacts.ContactMethods.DATA ) ) ) );
 
143
                }
 
144
 
 
145
                return true;
 
146
        }
 
147
}