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

  • Committer: edam
  • Date: 2012-04-24 11:17:41 UTC
  • Revision ID: tim@ed.am-20120424111741-gbzn6qr4i574d9s4
renamed contact readers to accessors

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 <edam@waxworlds.org>
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://ed.am/dev/android/export-contacts
 
8
 * http://www.waxworlds.org/edam/software/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 am.ed.exportcontacts;
 
24
package org.waxworlds.edam.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.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
 
 
89
53
        @Override
90
54
        public boolean getNextContact( Exporter.ContactData contact )
91
55
        {
97
61
                                new String[] {
98
62
                                        Contacts.People._ID,
99
63
                                        Contacts.People.NAME,
100
 
                                        Contacts.People.NOTES,
101
64
                                }, null, null, null );
102
65
                }
103
66
 
104
67
                // if there are no more contacts, abort
105
 
                if( _cur == null || !_cur.moveToNext() ) {
 
68
                if( !_cur.moveToNext() ) {
106
69
                        _cur = null;
107
70
                        return false;
108
71
                }
110
73
                // get this contact's id
111
74
                Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
112
75
 
113
 
                // set name
 
76
                // create contact
114
77
                contact.setName(
115
78
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
116
79
 
117
 
                // add notes
118
 
                contact.addNote(
119
 
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NOTES ) ) );
120
 
 
121
 
                // add the organisations
 
80
                // get the organisations
122
81
                Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
123
82
                        new String[] {
124
83
                                Contacts.Organizations.COMPANY,
135
94
                                        Contacts.Organizations.TITLE ) ) ) );
136
95
                cur.close();
137
96
 
138
 
                // add the phone numbers
 
97
                // get the phone numbers
139
98
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
140
99
                        new String[] {
141
100
                                Contacts.Phones.NUMBER,
146
105
                                Contacts.Phones.PERSON_ID + " ASC" );
147
106
                while( cur.moveToNext() )
148
107
                        contact.addNumber( contact.new NumberDetail(
149
 
                                convertBackendTypeToType( Contacts.Phones.class,
150
 
                                        cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
 
108
                                cur.getInt( cur.getColumnIndex(
 
109
                                        Contacts.Phones.TYPE ) ),
151
110
                                cur.getString( cur.getColumnIndex(
152
111
                                        Contacts.Phones.NUMBER ) ) ) );
153
112
                cur.close();
154
113
 
155
 
                // add the email and postal addresses
 
114
                // get the email and postal addresses
156
115
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
157
116
                        new String[] {
158
117
                                Contacts.ContactMethods.KIND,
173
132
                                Contacts.ContactMethods.KIND ) );
174
133
                        if( kind == Contacts.KIND_EMAIL )
175
134
                                contact.addEmail( contact.new EmailDetail(
176
 
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
177
 
                                                cur.getInt( cur.getColumnIndex(
178
 
                                                        Contacts.ContactMethods.TYPE ) ) ),
 
135
                                        cur.getInt( cur.getColumnIndex(
 
136
                                                Contacts.ContactMethods.TYPE ) ),
179
137
                                        cur.getString( cur.getColumnIndex(
180
138
                                                Contacts.ContactMethods.DATA ) ) ) );
181
139
                        else
182
140
                                contact.addAddress( contact.new AddressDetail(
183
 
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
184
 
                                                cur.getInt( cur.getColumnIndex(
185
 
                                                        Contacts.ContactMethods.TYPE ) ) ),
 
141
                                        cur.getInt( cur.getColumnIndex(
 
142
                                                Contacts.ContactMethods.TYPE ) ),
186
143
                                        cur.getString( cur.getColumnIndex(
187
144
                                                Contacts.ContactMethods.DATA ) ) ) );
188
145
                }