/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: 2012-12-21 13:26:31 UTC
  • Revision ID: tim@ed.am-20121221132631-ofj30d60lvuxp4a3
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsContactAccessor.java
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Export Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/export-contacts
 
8
 * http://ed.am/dev/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.exportcontacts;
 
24
package am.ed.exportcontacts;
25
25
 
 
26
import am.ed.exportcontacts.Exporter.ContactData;
26
27
import android.app.Activity;
27
28
import android.database.Cursor;
28
29
import android.provider.Contacts;
29
30
 
30
 
public class ContactsContactAccessor implements ContactAccessor
 
31
@SuppressWarnings( "deprecation" )
 
32
public class ContactsBackend implements Backend
31
33
{
32
34
        Activity _activity = null;
33
35
        Exporter _exporter = null;
34
36
        Cursor _cur = null;
35
37
 
36
 
        public ContactsContactAccessor( Activity activity, Exporter exporter )
 
38
        public ContactsBackend( Activity activity, Exporter exporter )
37
39
        {
38
40
                _activity = activity;
39
41
                _exporter = exporter;
50
52
                return cursor.getCount();
51
53
        }
52
54
 
 
55
        private int convertBackendTypeToType( Class< ? > cls, int type )
 
56
        {
 
57
                if( cls == Contacts.Phones.class )
 
58
                {
 
59
                        switch( type )
 
60
                        {
 
61
                        case Contacts.Phones.TYPE_MOBILE:
 
62
                                return ContactData.TYPE_MOBILE;
 
63
                        case Contacts.Phones.TYPE_FAX_HOME:
 
64
                                return ContactData.TYPE_FAX_HOME;
 
65
                        case Contacts.Phones.TYPE_FAX_WORK:
 
66
                                return ContactData.TYPE_FAX_WORK;
 
67
                        case Contacts.Phones.TYPE_PAGER:
 
68
                                return ContactData.TYPE_PAGER;
 
69
                        case Contacts.Phones.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.Phones.TYPE_WORK:
 
80
                                return ContactData.TYPE_WORK;
 
81
                        default:
 
82
                                return ContactData.TYPE_HOME;
 
83
                        }
 
84
                }
 
85
 
 
86
                return ContactData.TYPE_HOME;
 
87
        }
 
88
 
53
89
        @Override
54
90
        public boolean getNextContact( Exporter.ContactData contact )
55
91
        {
61
97
                                new String[] {
62
98
                                        Contacts.People._ID,
63
99
                                        Contacts.People.NAME,
 
100
                                        Contacts.People.NOTES,
64
101
                                }, null, null, null );
65
102
                }
66
103
 
67
104
                // if there are no more contacts, abort
68
 
                if( !_cur.moveToNext() ) {
 
105
                if( _cur == null || !_cur.moveToNext() ) {
69
106
                        _cur = null;
70
107
                        return false;
71
108
                }
73
110
                // get this contact's id
74
111
                Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
75
112
 
76
 
                // create contact
 
113
                // set name
77
114
                contact.setName(
78
115
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
79
116
 
80
 
                // get the organisations
 
117
                // add notes
 
118
                contact.addNote(
 
119
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NOTES ) ) );
 
120
 
 
121
                // add the organisations
81
122
                Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
82
123
                        new String[] {
83
124
                                Contacts.Organizations.COMPANY,
94
135
                                        Contacts.Organizations.TITLE ) ) ) );
95
136
                cur.close();
96
137
 
97
 
                // get the phone numbers
 
138
                // add the phone numbers
98
139
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
99
140
                        new String[] {
100
141
                                Contacts.Phones.NUMBER,
105
146
                                Contacts.Phones.PERSON_ID + " ASC" );
106
147
                while( cur.moveToNext() )
107
148
                        contact.addNumber( contact.new NumberDetail(
108
 
                                cur.getInt( cur.getColumnIndex(
109
 
                                        Contacts.Phones.TYPE ) ),
 
149
                                convertBackendTypeToType( Contacts.Phones.class,
 
150
                                        cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
110
151
                                cur.getString( cur.getColumnIndex(
111
152
                                        Contacts.Phones.NUMBER ) ) ) );
112
153
                cur.close();
113
154
 
114
 
                // get the email and postal addresses
 
155
                // add the email and postal addresses
115
156
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
116
157
                        new String[] {
117
158
                                Contacts.ContactMethods.KIND,
132
173
                                Contacts.ContactMethods.KIND ) );
133
174
                        if( kind == Contacts.KIND_EMAIL )
134
175
                                contact.addEmail( contact.new EmailDetail(
135
 
                                        cur.getInt( cur.getColumnIndex(
136
 
                                                Contacts.ContactMethods.TYPE ) ),
 
176
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
 
177
                                                cur.getInt( cur.getColumnIndex(
 
178
                                                        Contacts.ContactMethods.TYPE ) ) ),
137
179
                                        cur.getString( cur.getColumnIndex(
138
180
                                                Contacts.ContactMethods.DATA ) ) ) );
139
181
                        else
140
182
                                contact.addAddress( contact.new AddressDetail(
141
 
                                        cur.getInt( cur.getColumnIndex(
142
 
                                                Contacts.ContactMethods.TYPE ) ),
 
183
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
 
184
                                                cur.getInt( cur.getColumnIndex(
 
185
                                                        Contacts.ContactMethods.TYPE ) ) ),
143
186
                                        cur.getString( cur.getColumnIndex(
144
187
                                                Contacts.ContactMethods.DATA ) ) ) );
145
188
                }