/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/ContactsContactAccessor.java

  • Committer: edam
  • Date: 2012-04-24 11:29:44 UTC
  • Revision ID: tim@ed.am-20120424112944-6fic236bul6r9cqi
changed all the URLs to ed.am, including copyrights, package names and project
site

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsContactAccessor.java
3
3
 *
4
 
 * Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
 
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
 
 * to as "this program").  For more information, see
 
7
 * to as "this program"). For more information, see
8
8
 * http://ed.am/dev/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
23
23
 
24
24
package am.ed.exportcontacts;
25
25
 
26
 
import am.ed.exportcontacts.Exporter.ContactData;
27
26
import android.app.Activity;
28
27
import android.database.Cursor;
29
28
import android.provider.Contacts;
30
29
 
31
 
@SuppressWarnings( "deprecation" )
32
 
public class ContactsBackend implements Backend
 
30
public class ContactsContactAccessor implements ContactAccessor
33
31
{
34
32
        Activity _activity = null;
35
33
        Exporter _exporter = null;
36
34
        Cursor _cur = null;
37
35
 
38
 
        public ContactsBackend( Activity activity, Exporter exporter )
 
36
        public ContactsContactAccessor( Activity activity, Exporter exporter )
39
37
        {
40
38
                _activity = activity;
41
39
                _exporter = exporter;
52
50
                return cursor.getCount();
53
51
        }
54
52
 
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
53
        @Override
90
54
        public boolean getNextContact( Exporter.ContactData contact )
91
55
        {
93
57
                if( _cur == null )
94
58
                {
95
59
                        // get all contacts
96
 
                        _cur = _activity.getContentResolver().query(
97
 
                                Contacts.People.CONTENT_URI,
 
60
                        _cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
98
61
                                new String[] {
99
62
                                        Contacts.People._ID,
100
63
                                        Contacts.People.NAME,
101
 
                                        Contacts.People.NOTES,
102
64
                                }, null, null, null );
103
65
                }
104
66
 
105
67
                // if there are no more contacts, abort
106
 
                if( _cur == null ) return false;
107
68
                if( !_cur.moveToNext() ) {
108
 
                        _cur.close();
109
69
                        _cur = null;
110
70
                        return false;
111
71
                }
113
73
                // get this contact's id
114
74
                Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
115
75
 
116
 
                // set name
 
76
                // create contact
117
77
                contact.setName(
118
78
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
119
79
 
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,
 
80
                // get the organisations
 
81
                Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
129
82
                        new String[] {
130
83
                                Contacts.Organizations.COMPANY,
131
84
                                Contacts.Organizations.TITLE,
141
94
                                        Contacts.Organizations.TITLE ) ) ) );
142
95
                cur.close();
143
96
 
144
 
                // add the phone numbers
145
 
                cur = _activity.getContentResolver().query(
146
 
                        Contacts.Phones.CONTENT_URI,
 
97
                // get the phone numbers
 
98
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
147
99
                        new String[] {
148
100
                                Contacts.Phones.NUMBER,
149
101
                                Contacts.Phones.TYPE,
153
105
                                Contacts.Phones.PERSON_ID + " ASC" );
154
106
                while( cur.moveToNext() )
155
107
                        contact.addNumber( contact.new NumberDetail(
156
 
                                convertBackendTypeToType( Contacts.Phones.class,
157
 
                                        cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
 
108
                                cur.getInt( cur.getColumnIndex(
 
109
                                        Contacts.Phones.TYPE ) ),
158
110
                                cur.getString( cur.getColumnIndex(
159
111
                                        Contacts.Phones.NUMBER ) ) ) );
160
112
                cur.close();
161
113
 
162
 
                // add the email and postal addresses
163
 
                cur = _activity.getContentResolver().query(
164
 
                        Contacts.ContactMethods.CONTENT_URI,
 
114
                // get the email and postal addresses
 
115
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
165
116
                        new String[] {
166
117
                                Contacts.ContactMethods.KIND,
167
118
                                Contacts.ContactMethods.TYPE,
181
132
                                Contacts.ContactMethods.KIND ) );
182
133
                        if( kind == Contacts.KIND_EMAIL )
183
134
                                contact.addEmail( contact.new EmailDetail(
184
 
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
185
 
                                                cur.getInt( cur.getColumnIndex(
186
 
                                                        Contacts.ContactMethods.TYPE ) ) ),
 
135
                                        cur.getInt( cur.getColumnIndex(
 
136
                                                Contacts.ContactMethods.TYPE ) ),
187
137
                                        cur.getString( cur.getColumnIndex(
188
138
                                                Contacts.ContactMethods.DATA ) ) ) );
189
139
                        else
190
140
                                contact.addAddress( contact.new AddressDetail(
191
 
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
192
 
                                                cur.getInt( cur.getColumnIndex(
193
 
                                                        Contacts.ContactMethods.TYPE ) ) ),
 
141
                                        cur.getInt( cur.getColumnIndex(
 
142
                                                Contacts.ContactMethods.TYPE ) ),
194
143
                                        cur.getString( cur.getColumnIndex(
195
144
                                                Contacts.ContactMethods.DATA ) ) ) );
196
145
                }