/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: Tim Marston
  • Date: 2014-03-02 11:04:33 UTC
  • Revision ID: tim@ed.am-20140302110433-9crgerrfbh30bs39
bump version to 1.0.3

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * ContactsContactAccessor.java
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 to 2012 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;
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.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
 
53
89
        @Override
54
90
        public boolean getNextContact( Exporter.ContactData contact )
55
91
        {
57
93
                if( _cur == null )
58
94
                {
59
95
                        // get all contacts
60
 
                        _cur = _activity.managedQuery( Contacts.People.CONTENT_URI,
 
96
                        _cur = _activity.getContentResolver().query(
 
97
                                Contacts.People.CONTENT_URI,
61
98
                                new String[] {
62
99
                                        Contacts.People._ID,
63
100
                                        Contacts.People.NAME,
 
101
                                        Contacts.People.NOTES,
64
102
                                }, null, null, null );
65
103
                }
66
104
 
67
105
                // if there are no more contacts, abort
 
106
                if( _cur == null ) return false;
68
107
                if( !_cur.moveToNext() ) {
 
108
                        _cur.close();
69
109
                        _cur = null;
70
110
                        return false;
71
111
                }
73
113
                // get this contact's id
74
114
                Long id = _cur.getLong( _cur.getColumnIndex( Contacts.People._ID ) );
75
115
 
76
 
                // create contact
 
116
                // set name
77
117
                contact.setName(
78
118
                        _cur.getString( _cur.getColumnIndex( Contacts.People.NAME ) ) );
79
119
 
80
 
                // get the organisations
81
 
                Cursor cur = _activity.managedQuery( Contacts.Organizations.CONTENT_URI,
 
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,
82
129
                        new String[] {
83
130
                                Contacts.Organizations.COMPANY,
84
131
                                Contacts.Organizations.TITLE,
94
141
                                        Contacts.Organizations.TITLE ) ) ) );
95
142
                cur.close();
96
143
 
97
 
                // get the phone numbers
98
 
                cur = _activity.managedQuery( Contacts.Phones.CONTENT_URI,
 
144
                // add the phone numbers
 
145
                cur = _activity.getContentResolver().query(
 
146
                        Contacts.Phones.CONTENT_URI,
99
147
                        new String[] {
100
148
                                Contacts.Phones.NUMBER,
101
149
                                Contacts.Phones.TYPE,
105
153
                                Contacts.Phones.PERSON_ID + " ASC" );
106
154
                while( cur.moveToNext() )
107
155
                        contact.addNumber( contact.new NumberDetail(
108
 
                                cur.getInt( cur.getColumnIndex(
109
 
                                        Contacts.Phones.TYPE ) ),
 
156
                                convertBackendTypeToType( Contacts.Phones.class,
 
157
                                        cur.getInt( cur.getColumnIndex( Contacts.Phones.TYPE ) ) ),
110
158
                                cur.getString( cur.getColumnIndex(
111
159
                                        Contacts.Phones.NUMBER ) ) ) );
112
160
                cur.close();
113
161
 
114
 
                // get the email and postal addresses
115
 
                cur = _activity.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
162
                // add the email and postal addresses
 
163
                cur = _activity.getContentResolver().query(
 
164
                        Contacts.ContactMethods.CONTENT_URI,
116
165
                        new String[] {
117
166
                                Contacts.ContactMethods.KIND,
118
167
                                Contacts.ContactMethods.TYPE,
132
181
                                Contacts.ContactMethods.KIND ) );
133
182
                        if( kind == Contacts.KIND_EMAIL )
134
183
                                contact.addEmail( contact.new EmailDetail(
135
 
                                        cur.getInt( cur.getColumnIndex(
136
 
                                                Contacts.ContactMethods.TYPE ) ),
 
184
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
 
185
                                                cur.getInt( cur.getColumnIndex(
 
186
                                                        Contacts.ContactMethods.TYPE ) ) ),
137
187
                                        cur.getString( cur.getColumnIndex(
138
188
                                                Contacts.ContactMethods.DATA ) ) ) );
139
189
                        else
140
190
                                contact.addAddress( contact.new AddressDetail(
141
 
                                        cur.getInt( cur.getColumnIndex(
142
 
                                                Contacts.ContactMethods.TYPE ) ),
 
191
                                        convertBackendTypeToType( Contacts.ContactMethods.class,
 
192
                                                cur.getInt( cur.getColumnIndex(
 
193
                                                        Contacts.ContactMethods.TYPE ) ) ),
143
194
                                        cur.getString( cur.getColumnIndex(
144
195
                                                Contacts.ContactMethods.DATA ) ) ) );
145
196
                }