/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: edam
  • Date: 2010-10-22 18:56:17 UTC
  • Revision ID: edam@waxworlds.org-20101022185617-6p6ivjqt28avwlzo
- added file chooser
- changed directory edit box for a directory "browse" button
- wired file chooser in to configure vcf page
- fixed intro
- made app debuggable
- added licence and changelog

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * ContactsContactAccessor.java
3
 
 *
4
 
 * Copyright (C) 2011 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 android.app.Activity;
27
 
import android.database.Cursor;
28
 
import android.provider.Contacts;
29
 
 
30
 
public class ContactsBackend implements ContactAccessor
31
 
{
32
 
        Activity _activity = null;
33
 
        Exporter _exporter = null;
34
 
        Cursor _cur = null;
35
 
 
36
 
        public ContactsBackend( 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 == null || !_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
 
                cur.close();
96
 
 
97
 
                // get the phone numbers
98
 
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
99
 
                        new String[] {
100
 
                                Contacts.Phones.NUMBER,
101
 
                                Contacts.Phones.TYPE,
102
 
                        }, Contacts.Phones.PERSON_ID + " = ?",
103
 
                        new String[] { id.toString() },
104
 
                        Contacts.Phones.ISPRIMARY + " DESC," +
105
 
                                Contacts.Phones.PERSON_ID + " ASC" );
106
 
                while( cur.moveToNext() )
107
 
                        contact.addNumber( contact.new NumberDetail(
108
 
                                cur.getInt( cur.getColumnIndex(
109
 
                                        Contacts.Phones.TYPE ) ),
110
 
                                cur.getString( cur.getColumnIndex(
111
 
                                        Contacts.Phones.NUMBER ) ) ) );
112
 
                cur.close();
113
 
 
114
 
                // get the email and postal addresses
115
 
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
116
 
                        new String[] {
117
 
                                Contacts.ContactMethods.KIND,
118
 
                                Contacts.ContactMethods.TYPE,
119
 
                                Contacts.ContactMethods.DATA,
120
 
                        },
121
 
                        Contacts.ContactMethods.PERSON_ID + " = ? AND " +
122
 
                                Contacts.ContactMethods.KIND + " IN( ?, ? )",
123
 
                        new String[] {
124
 
                                id.toString(),
125
 
                                "" + Contacts.KIND_EMAIL,
126
 
                                "" + Contacts.KIND_POSTAL,
127
 
                        },
128
 
                        Contacts.ContactMethods.ISPRIMARY + " DESC," +
129
 
                                Contacts.ContactMethods.PERSON_ID + " ASC" );
130
 
                while( cur.moveToNext() ) {
131
 
                        int kind = cur.getInt( cur.getColumnIndex(
132
 
                                Contacts.ContactMethods.KIND ) );
133
 
                        if( kind == Contacts.KIND_EMAIL )
134
 
                                contact.addEmail( contact.new EmailDetail(
135
 
                                        cur.getInt( cur.getColumnIndex(
136
 
                                                Contacts.ContactMethods.TYPE ) ),
137
 
                                        cur.getString( cur.getColumnIndex(
138
 
                                                Contacts.ContactMethods.DATA ) ) ) );
139
 
                        else
140
 
                                contact.addAddress( contact.new AddressDetail(
141
 
                                        cur.getInt( cur.getColumnIndex(
142
 
                                                Contacts.ContactMethods.TYPE ) ),
143
 
                                        cur.getString( cur.getColumnIndex(
144
 
                                                Contacts.ContactMethods.DATA ) ) ) );
145
 
                }
146
 
                cur.close();
147
 
 
148
 
                return true;
149
 
        }
150
 
}