summaryrefslogtreecommitdiff
path: root/src/com/android/providers/contacts/ContactsSyncAdapter.java
blob: cd33590092152fa1801d357210494ac3fd482b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
/*
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** See the License for the specific language governing permissions and
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** limitations under the License.
*/

package com.android.providers.contacts;

import com.google.android.collect.Sets;
import com.google.android.gdata.client.AndroidGDataClient;
import com.google.android.gdata.client.AndroidXmlParserFactory;
import com.google.android.providers.AbstractGDataSyncAdapter;
import com.google.wireless.gdata.client.GDataServiceClient;
import com.google.wireless.gdata.client.QueryParams;
import com.google.wireless.gdata.client.HttpException;
import com.google.wireless.gdata.contacts.client.ContactsClient;
import com.google.wireless.gdata.contacts.data.ContactEntry;
import com.google.wireless.gdata.contacts.data.ContactsElement;
import com.google.wireless.gdata.contacts.data.EmailAddress;
import com.google.wireless.gdata.contacts.data.GroupEntry;
import com.google.wireless.gdata.contacts.data.GroupMembershipInfo;
import com.google.wireless.gdata.contacts.data.ImAddress;
import com.google.wireless.gdata.contacts.data.Organization;
import com.google.wireless.gdata.contacts.data.PhoneNumber;
import com.google.wireless.gdata.contacts.data.PostalAddress;
import com.google.wireless.gdata.contacts.parser.xml.XmlContactsGDataParserFactory;
import com.google.wireless.gdata.data.Entry;
import com.google.wireless.gdata.data.ExtendedProperty;
import com.google.wireless.gdata.data.Feed;
import com.google.wireless.gdata.data.MediaEntry;
import com.google.wireless.gdata.parser.ParseException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SyncContext;
import android.content.SyncResult;
import android.content.SyncableContentProvider;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemProperties;
import android.provider.Contacts;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.Extensions;
import android.provider.Contacts.GroupMembership;
import android.provider.Contacts.Groups;
import android.provider.Contacts.Organizations;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.provider.Contacts.Photos;
import android.provider.SubscribedFeeds;
import android.provider.SyncConstValue;
import android.text.TextUtils;
import android.util.Config;
import android.util.Log;
import android.accounts.AccountManager;
import android.accounts.Account;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Implements a SyncAdapter for Contacts
 */
public class ContactsSyncAdapter extends AbstractGDataSyncAdapter {

    private static final String USER_AGENT_APP_VERSION = "Android-GData-Contacts/1.1"; 

    private static final String CONTACTS_FEED_URL = "http://www.google.com/m8/feeds/contacts/";
    private static final String GROUPS_FEED_URL = "http://www.google.com/m8/feeds/groups/";
    private static final String PHOTO_FEED_URL = "http://www.google.com/m8/feeds/photos/media/";

    private final ContactsClient mContactsClient;

    private static final String[] sSubscriptionProjection =
            new String[] {
                    SubscribedFeeds.Feeds._SYNC_ACCOUNT,
                    SubscribedFeeds.Feeds.FEED,
                    SubscribedFeeds.Feeds._ID};

    private static final HashMap<Byte, Integer> ENTRY_TYPE_TO_PROVIDER_PHONE;
    private static final HashMap<Byte, Integer> ENTRY_TYPE_TO_PROVIDER_EMAIL;
    private static final HashMap<Byte, Integer> ENTRY_TYPE_TO_PROVIDER_IM;
    private static final HashMap<Byte, Integer> ENTRY_TYPE_TO_PROVIDER_POSTAL;
    private static final HashMap<Byte, Integer> ENTRY_TYPE_TO_PROVIDER_ORGANIZATION;
    private static final HashMap<Integer, Byte> PROVIDER_TYPE_TO_ENTRY_PHONE;
    private static final HashMap<Integer, Byte> PROVIDER_TYPE_TO_ENTRY_EMAIL;
    private static final HashMap<Integer, Byte> PROVIDER_TYPE_TO_ENTRY_IM;
    private static final HashMap<Integer, Byte> PROVIDER_TYPE_TO_ENTRY_POSTAL;
    private static final HashMap<Integer, Byte> PROVIDER_TYPE_TO_ENTRY_ORGANIZATION;

    private static final HashMap<Byte, Integer> ENTRY_IM_PROTOCOL_TO_PROVIDER_PROTOCOL;
    private static final HashMap<Integer, Byte> PROVIDER_IM_PROTOCOL_TO_ENTRY_PROTOCOL;

    private static final int MAX_MEDIA_ENTRIES_PER_SYNC = 10;

    // Only valid during a sync operation.
    // If set then a getServerDiffs() was performed during this sync.
    private boolean mPerformedGetServerDiffs;

    // Only valid during a sync. If set then this sync was a forced sync request
    private boolean mIsManualSync;

    private int mPhotoDownloads;
    private int mPhotoUploads;

    private static final String IMAGE_MIME_TYPE = "image/*";

    static {
        HashMap<Byte, Integer> map;

        map = new HashMap<Byte, Integer>();
        map.put(ImAddress.PROTOCOL_AIM, ContactMethods.PROTOCOL_AIM);
        map.put(ImAddress.PROTOCOL_GOOGLE_TALK, ContactMethods.PROTOCOL_GOOGLE_TALK);
        map.put(ImAddress.PROTOCOL_ICQ, ContactMethods.PROTOCOL_ICQ);
        map.put(ImAddress.PROTOCOL_JABBER, ContactMethods.PROTOCOL_JABBER);
        map.put(ImAddress.PROTOCOL_MSN, ContactMethods.PROTOCOL_MSN);
        map.put(ImAddress.PROTOCOL_QQ, ContactMethods.PROTOCOL_QQ);
        map.put(ImAddress.PROTOCOL_SKYPE, ContactMethods.PROTOCOL_SKYPE);
        map.put(ImAddress.PROTOCOL_YAHOO, ContactMethods.PROTOCOL_YAHOO);
        ENTRY_IM_PROTOCOL_TO_PROVIDER_PROTOCOL = map;
        PROVIDER_IM_PROTOCOL_TO_ENTRY_PROTOCOL = swapMap(map);

        map = new HashMap<Byte, Integer>();
        map.put(EmailAddress.TYPE_HOME, ContactMethods.TYPE_HOME);
        map.put(EmailAddress.TYPE_WORK, ContactMethods.TYPE_WORK);
        map.put(EmailAddress.TYPE_OTHER, ContactMethods.TYPE_OTHER);
        map.put(EmailAddress.TYPE_NONE, ContactMethods.TYPE_CUSTOM);
        ENTRY_TYPE_TO_PROVIDER_EMAIL = map;
        PROVIDER_TYPE_TO_ENTRY_EMAIL = swapMap(map);

        map = new HashMap<Byte, Integer>();
        map.put(PhoneNumber.TYPE_HOME, Phones.TYPE_HOME);
        map.put(PhoneNumber.TYPE_MOBILE, Phones.TYPE_MOBILE);
        map.put(PhoneNumber.TYPE_PAGER, Phones.TYPE_PAGER);
        map.put(PhoneNumber.TYPE_WORK, Phones.TYPE_WORK);
        map.put(PhoneNumber.TYPE_HOME_FAX, Phones.TYPE_FAX_HOME);
        map.put(PhoneNumber.TYPE_WORK_FAX, Phones.TYPE_FAX_WORK);
        map.put(PhoneNumber.TYPE_OTHER, Phones.TYPE_OTHER);
        map.put(PhoneNumber.TYPE_NONE, Phones.TYPE_CUSTOM);
        ENTRY_TYPE_TO_PROVIDER_PHONE = map;
        PROVIDER_TYPE_TO_ENTRY_PHONE = swapMap(map);

        map = new HashMap<Byte, Integer>();
        map.put(PostalAddress.TYPE_HOME, ContactMethods.TYPE_HOME);
        map.put(PostalAddress.TYPE_WORK, ContactMethods.TYPE_WORK);
        map.put(PostalAddress.TYPE_OTHER, ContactMethods.TYPE_OTHER);
        map.put(PostalAddress.TYPE_NONE, ContactMethods.TYPE_CUSTOM);
        ENTRY_TYPE_TO_PROVIDER_POSTAL = map;
        PROVIDER_TYPE_TO_ENTRY_POSTAL = swapMap(map);

        map = new HashMap<Byte, Integer>();
        map.put(ImAddress.TYPE_HOME, ContactMethods.TYPE_HOME);
        map.put(ImAddress.TYPE_WORK, ContactMethods.TYPE_WORK);
        map.put(ImAddress.TYPE_OTHER, ContactMethods.TYPE_OTHER);
        map.put(ImAddress.TYPE_NONE, ContactMethods.TYPE_CUSTOM);
        ENTRY_TYPE_TO_PROVIDER_IM = map;
        PROVIDER_TYPE_TO_ENTRY_IM = swapMap(map);

        map = new HashMap<Byte, Integer>();
        map.put(Organization.TYPE_WORK, Organizations.TYPE_WORK);
        map.put(Organization.TYPE_OTHER, Organizations.TYPE_OTHER);
        map.put(Organization.TYPE_NONE, Organizations.TYPE_CUSTOM);
        ENTRY_TYPE_TO_PROVIDER_ORGANIZATION = map;
        PROVIDER_TYPE_TO_ENTRY_ORGANIZATION = swapMap(map);
    }

    private static <A, B> HashMap<B, A> swapMap(HashMap<A, B> originalMap) {
        HashMap<B, A> newMap = new HashMap<B,A>();
        for (Map.Entry<A, B> entry : originalMap.entrySet()) {
            final B originalValue = entry.getValue();
            if (newMap.containsKey(originalValue)) {
                throw new IllegalArgumentException("value " + originalValue
                        + " was already encountered");
            }
            newMap.put(originalValue, entry.getKey());
        }
        return newMap;
    }

    protected ContactsSyncAdapter(Context context, SyncableContentProvider provider) {
        super(context, provider);
        mContactsClient = new ContactsClient(
                new AndroidGDataClient(context, USER_AGENT_APP_VERSION),
                new XmlContactsGDataParserFactory(new AndroidXmlParserFactory()));
    }

    protected GDataServiceClient getGDataServiceClient() {
        return mContactsClient;
    }

    @Override
    protected Entry newEntry() {
        throw new UnsupportedOperationException("this should never be used");
    }

    protected String getFeedUrl(Account account) {
        throw new UnsupportedOperationException("this should never be used");
    }

    protected Class getFeedEntryClass() {
        throw new UnsupportedOperationException("this should never be used");
    }

    protected Class getFeedEntryClass(String feed) {
        if (feed.startsWith(rewriteUrlforAccount(getAccount(), GROUPS_FEED_URL))) {
            return GroupEntry.class;
        }
        if (feed.startsWith(rewriteUrlforAccount(getAccount(), CONTACTS_FEED_URL))) {
            return ContactEntry.class;
        }
        return null;
    }

    @Override
    public void getServerDiffs(SyncContext context, SyncData baseSyncData,
            SyncableContentProvider tempProvider,
            Bundle extras, Object syncInfo, SyncResult syncResult) {
        mPerformedGetServerDiffs = true;
        GDataSyncData syncData = (GDataSyncData)baseSyncData;

        ArrayList<String> feedsToSync = new ArrayList<String>();

        if (extras != null && extras.containsKey("feed")) {
            feedsToSync.add((String) extras.get("feed"));
        } else {
            feedsToSync.add(getGroupsFeedForAccount(getAccount()));
            addContactsFeedsToSync(getContext().getContentResolver(), getAccount(), feedsToSync);
            feedsToSync.add(getPhotosFeedForAccount(getAccount()));
        }

        for (String feed : feedsToSync) {
            context.setStatusText("Downloading\u2026");
            if (getPhotosFeedForAccount(getAccount()).equals(feed)) {
                getServerPhotos(context, feed, MAX_MEDIA_ENTRIES_PER_SYNC, syncData, syncResult);
            } else {
                final Class feedEntryClass = getFeedEntryClass(feed);
                if (feedEntryClass != null) {
                    getServerDiffsImpl(context, tempProvider, feedEntryClass,
                            feed, null, getMaxEntriesPerSync(), syncData, syncResult);
                } else {
                    if (Config.LOGD) {
                        Log.d(TAG, "ignoring sync request for unknown feed " + feed);
                    }
                }
            }
            if (syncResult.hasError()) {
                break;
            }
        }
    }

    /**
     * Look at the groups sync settings and the overall sync preference to determine which
     * feeds to sync and add them to the feedsToSync list.
     */
    public static void addContactsFeedsToSync(ContentResolver cr, Account account,
            Collection<String> feedsToSync) {
        boolean shouldSyncEverything = getShouldSyncEverything(cr, account);
        if (shouldSyncEverything) {
            feedsToSync.add(getContactsFeedForAccount(account));
            return;
        }

        Cursor cursor = cr.query(Contacts.Groups.CONTENT_URI, new String[]{Groups._SYNC_ID},
                "_sync_account=? AND _sync_account_type=? AND should_sync>0",
                new String[]{account.mName, account.mType}, null);
        try {
            while (cursor.moveToNext()) {
                feedsToSync.add(getContactsFeedForGroup(account, cursor.getString(0)));
            }
        } finally {
            cursor.close();
        }
    }

    private static boolean getShouldSyncEverything(ContentResolver cr, Account account) {
        // TODO(fredq) should be using account instead of null
        String value = Contacts.Settings.getSetting(cr, null, Contacts.Settings.SYNC_EVERYTHING);
        return !TextUtils.isEmpty(value) && !"0".equals(value);
    }

    private void getServerPhotos(SyncContext context, String feedUrl, int maxDownloads,
            GDataSyncData syncData, SyncResult syncResult) {
        final ContentResolver cr = getContext().getContentResolver();
        final Account account = getAccount();
        Cursor cursor = cr.query(
                Photos.CONTENT_URI,
                new String[]{Photos._SYNC_ID, Photos._SYNC_VERSION, Photos.PERSON_ID,
                        Photos.DOWNLOAD_REQUIRED, Photos._ID}, ""
                + "_sync_account=? AND _sync_account_type=? AND download_required != 0",
                new String[]{account.mName, account.mType}, null);
        try {
            int numFetched = 0;
            while (cursor.moveToNext()) {
                if (numFetched >= maxDownloads) {
                    break;
                }
                String photoSyncId = cursor.getString(0);
                String photoVersion = cursor.getString(1);
                long person = cursor.getLong(2);
                String photoUrl = feedUrl + "/" + photoSyncId;
                long photoId = cursor.getLong(4);

                try {
                    context.setStatusText("Downloading photo " + photoSyncId);
                    ++numFetched;
                    ++mPhotoDownloads;
                    InputStream inputStream = mContactsClient.getMediaEntryAsStream(
                            photoUrl, getAuthToken());
                    savePhoto(person, inputStream, photoVersion);
                    syncResult.stats.numUpdates++;
                } catch (IOException e) {
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.d(TAG, "error downloading " + photoUrl, e);
                    }
                    syncResult.stats.numIoExceptions++;
                    return;
                } catch (HttpException e) {
                    switch (e.getStatusCode()) {
                        case HttpException.SC_UNAUTHORIZED:
                            if (Config.LOGD) {
                                Log.d(TAG, "not authorized to download " + photoUrl, e);
                            }
                            syncResult.stats.numAuthExceptions++;
                            return;
                        case HttpException.SC_FORBIDDEN:
                        case HttpException.SC_NOT_FOUND:
                            final String exceptionMessage = e.getMessage();
                            if (Config.LOGD) {
                                Log.d(TAG, "unable to download photo " + photoUrl + ", "
                                        + exceptionMessage + ", ignoring");
                            }
                            ContentValues values = new ContentValues();
                            values.put(Photos.SYNC_ERROR, exceptionMessage);
                            Uri photoUri = Uri.withAppendedPath(
                                    ContentUris.withAppendedId(People.CONTENT_URI, photoId),
                                    Photos.CONTENT_DIRECTORY);
                            cr.update(photoUri, values, null /* where */, null /* where args */);
                            break;
                        default:
                            if (Config.LOGD) {
                                Log.d(TAG, "error downloading " + photoUrl, e);
                            }
                            syncResult.stats.numIoExceptions++;
                            return;
                    }
                }
            }
            final boolean hasMoreToSync = numFetched < cursor.getCount();
            GDataSyncData.FeedData feedData =
                    new GDataSyncData.FeedData(0  /* no update time */,
                            numFetched, hasMoreToSync, null /* no lastId */,
                            0 /* no feed index */);
            syncData.feedData.put(feedUrl, feedData);
        } finally {
            cursor.close();
        }
    }

    @Override
    protected void getStatsString(StringBuffer sb, SyncResult result) {
        super.getStatsString(sb, result);
        if (mPhotoUploads > 0) {
            sb.append("p").append(mPhotoUploads);
        }
        if (mPhotoDownloads > 0) {
            sb.append("P").append(mPhotoDownloads);
        }
    }

    @Override
    public void sendClientDiffs(SyncContext context, SyncableContentProvider clientDiffs,
            SyncableContentProvider serverDiffs, SyncResult syncResult,
            boolean dontSendDeletes) {
        initTempProvider(clientDiffs);

        sendClientDiffsImpl(context, clientDiffs, new GroupEntry(), null /* no syncInfo */,
                serverDiffs, syncResult, dontSendDeletes);

        // lets go ahead and commit what we have if we successfully made a change
        if (syncResult.madeSomeProgress()) {
            return;
        }

        sendClientPhotos(context, clientDiffs, null /* no syncInfo */, syncResult);

        // lets go ahead and commit what we have if we successfully made a change
        if (syncResult.madeSomeProgress()) {
            return;
        }

        sendClientDiffsImpl(context, clientDiffs, new ContactEntry(), null /* no syncInfo */,
                serverDiffs, syncResult, dontSendDeletes);
    }

    protected void sendClientPhotos(SyncContext context, ContentProvider clientDiffs,
            Object syncInfo, SyncResult syncResult) {
        Entry entry = new MediaEntry();

        GDataServiceClient client = getGDataServiceClient();
        String authToken = getAuthToken();
        ContentResolver cr = getContext().getContentResolver();
        final Account account = getAccount();

        Cursor c = clientDiffs.query(Photos.CONTENT_URI, null /* all columns */,
                null /* no where */, null /* no where args */, null /* default sort order */);
        try {
            int personColumn = c.getColumnIndexOrThrow(Photos.PERSON_ID);
            int dataColumn = c.getColumnIndexOrThrow(Photos.DATA);
            int numRows = c.getCount();
            while (c.moveToNext()) {
                if (mSyncCanceled) {
                    if (Config.LOGD) Log.d(TAG, "stopping since the sync was canceled");
                    break;
                }

                entry.clear();
                context.setStatusText("Updating, " + (numRows - 1) + " to go");

                cursorToBaseEntry(entry, account, c);
                String editUrl = entry.getEditUri();

                if (TextUtils.isEmpty(editUrl)) {
                    if (Config.LOGD) {
                        Log.d(TAG, "skipping photo edit for unsynced contact");
                    }
                    continue;
                }

                // Send the request and receive the response
                InputStream inputStream = null;
                byte[] imageData = c.getBlob(dataColumn);
                if (imageData != null) {
                    inputStream = new ByteArrayInputStream(imageData);
                }
                Uri photoUri = Uri.withAppendedPath(People.CONTENT_URI,
                        c.getString(personColumn) + "/" + Photos.CONTENT_DIRECTORY);
                try {
                    if (inputStream != null) {
                        if (Log.isLoggable(TAG, Log.VERBOSE)) {
                            Log.v(TAG, "Updating photo " + entry.toString());
                        }
                        ++mPhotoUploads;
                        client.updateMediaEntry(editUrl, inputStream, IMAGE_MIME_TYPE, authToken);
                    } else {
                        if (Log.isLoggable(TAG, Log.VERBOSE)) {
                            Log.v(TAG, "Deleting photo " + entry.toString());
                        }
                        client.deleteEntry(editUrl, authToken);
                    }

                    // Mark that this photo is no longer dirty. The next time we sync (which
                    // should be soon), we will get the new version of the photo and whether
                    // or not there is a new one to download (e.g. if we deleted our version
                    // yet there is an evergreen version present).
                    ContentValues values = new ContentValues();
                    values.put(Photos.EXISTS_ON_SERVER, inputStream == null ? 0 : 1);
                    values.put(Photos._SYNC_DIRTY, 0);
                    if (cr.update(photoUri, values,
                            null /* no where */, null /* no where args */) != 1) {
                        Log.e(TAG, "error updating photo " + photoUri + " with values " + values);
                        syncResult.stats.numParseExceptions++;
                    } else {
                        syncResult.stats.numUpdates++;
                    }
                    continue;
                } catch (ParseException e) {
                    Log.e(TAG, "parse error during update of " + ", skipping");
                    syncResult.stats.numParseExceptions++;
                } catch (IOException e) {
                    if (Config.LOGD) {
                        Log.d(TAG, "io error during update of " + entry.toString()
                                + ", skipping");
                    }
                    syncResult.stats.numIoExceptions++;
                } catch (HttpException e) {
                    switch (e.getStatusCode()) {
                        case HttpException.SC_UNAUTHORIZED:
                            if (syncResult.stats.numAuthExceptions == 0) {
                                if (Config.LOGD) {
                                   Log.d(TAG, "auth error during update of " + entry
                                           + ", skipping");
                                }
                            }
                            syncResult.stats.numAuthExceptions++;
                            AccountManager.get(getContext()).blockingInvalidateAuthToken(
                                    "com.google.GAIA", authToken);
                            return;

                        case HttpException.SC_CONFLICT:
                            if (Config.LOGD) {
                                Log.d(TAG, "conflict detected during update of " + entry
                                        + ", skipping");
                            }
                            syncResult.stats.numConflictDetectedExceptions++;
                            break;
                        case HttpException.SC_BAD_REQUEST:
                        case HttpException.SC_FORBIDDEN:
                        case HttpException.SC_NOT_FOUND:
                        case HttpException.SC_INTERNAL_SERVER_ERROR:
                        default:
                            if (Config.LOGD) {
                                Log.d(TAG, "error " + e.getMessage() + " during update of "
                                        + entry.toString() + ", skipping");
                            }
                            syncResult.stats.numIoExceptions++;
                    }
                }
            }
        } finally {
            c.close();
        }
    }

    @Override
    protected Cursor getCursorForTable(ContentProvider cp, Class entryClass) {
        return getCursorForTableImpl(cp, entryClass);
    }

    protected static Cursor getCursorForTableImpl(ContentProvider cp, Class entryClass) {
        if (entryClass == ContactEntry.class) {
            return cp.query(People.CONTENT_URI, null, null, null, null);
        }
        if (entryClass == GroupEntry.class) {
            return cp.query(Groups.CONTENT_URI, null, null, null, null);
        }
        throw new IllegalArgumentException("unexpected entry class, " + entryClass.getName());
    }

    @Override
    protected Cursor getCursorForDeletedTable(ContentProvider cp, Class entryClass) {
        return getCursorForDeletedTableImpl(cp, entryClass);
    }

    protected static Cursor getCursorForDeletedTableImpl(ContentProvider cp, Class entryClass) {
        if (entryClass == ContactEntry.class) {
            return cp.query(People.DELETED_CONTENT_URI, null, null, null, null);
        }
        if (entryClass == GroupEntry.class) {
            return cp.query(Groups.DELETED_CONTENT_URI, null, null, null, null);
        }
        throw new IllegalArgumentException("unexpected entry class, " + entryClass.getName());
    }

    @Override
    protected String cursorToEntry(SyncContext context, Cursor c, Entry baseEntry,
            Object syncInfo) throws ParseException {
        return cursorToEntryImpl(getContext().getContentResolver(), c, baseEntry, getAccount());
    }

    static protected String cursorToEntryImpl(ContentResolver cr, Cursor c, Entry entry,
            Account account) throws ParseException {
        cursorToBaseEntry(entry, account, c);
        String createUrl = null;
        if (entry instanceof ContactEntry) {
            cursorToContactEntry(account, cr, c, (ContactEntry) entry);
            if (entry.getEditUri() == null) {
                createUrl = getContactsFeedForAccount(account);
            }
        } else if (entry instanceof MediaEntry) {
            if (entry.getEditUri() == null) {
                createUrl = getPhotosFeedForAccount(account);
            }
        } else {
            cursorToGroupEntry(c, (GroupEntry) entry);
            if (entry.getEditUri() == null) {
                createUrl = getGroupsFeedForAccount(account);
            }
        }

        return createUrl;
    }

    private static void cursorToGroupEntry(Cursor c, GroupEntry entry) throws ParseException {
        if (!TextUtils.isEmpty(c.getString(c.getColumnIndexOrThrow(Groups.SYSTEM_ID)))) {
            throw new ParseException("unable to modify system groups");
        }
        entry.setTitle(c.getString(c.getColumnIndexOrThrow(Groups.NAME)));
        entry.setContent(c.getString(c.getColumnIndexOrThrow(Groups.NOTES)));
        entry.setSystemGroup(null);
    }

    private static void cursorToContactEntry(Account account, ContentResolver cr, Cursor c,
            ContactEntry entry)
            throws ParseException {
        entry.setTitle(c.getString(c.getColumnIndexOrThrow(People.NAME)));
        entry.setContent(c.getString(c.getColumnIndexOrThrow(People.NOTES)));
        entry.setYomiName(c.getString(c.getColumnIndexOrThrow(People.PHONETIC_NAME)));

        long syncLocalId = c.getLong(c.getColumnIndexOrThrow(SyncConstValue._SYNC_LOCAL_ID));
        addContactMethodsToContactEntry(cr, syncLocalId, entry);
        addPhonesToContactEntry(cr, syncLocalId, entry);
        addOrganizationsToContactEntry(cr, syncLocalId, entry);
        addGroupMembershipToContactEntry(account, cr, syncLocalId, entry);
        addExtensionsToContactEntry(cr, syncLocalId, entry);
    }

    @Override
    protected void deletedCursorToEntry(SyncContext context, Cursor c, Entry entry) {
        deletedCursorToEntryImpl(c, entry, getAccount());
    }

    protected boolean handleAllDeletedUnavailable(GDataSyncData syncData, String feed) {
        // Contacts has no way to clear the contacts for just a given feed so it is unable
        // to handle this condition itself. Instead it returns false, which tell the
        // sync framework that it must handle it.
        return false;
    }

    protected static void deletedCursorToEntryImpl(Cursor c, Entry entry, Account account) {
        cursorToBaseEntry(entry, account, c);
    }

    private static void cursorToBaseEntry(Entry entry, Account account, Cursor c) {
        String feedUrl;
        if (entry instanceof ContactEntry) {
            feedUrl = getContactsFeedForAccount(account);
        } else if (entry instanceof GroupEntry) {
            feedUrl = getGroupsFeedForAccount(account);
        } else if (entry instanceof MediaEntry) {
            feedUrl = getPhotosFeedForAccount(account);
        } else {
            throw new IllegalArgumentException("bad entry type: " + entry.getClass().getName());
        }

        String syncId = c.getString(c.getColumnIndexOrThrow(SyncConstValue._SYNC_ID));
        if (syncId != null) {
            String syncVersion = c.getString(c.getColumnIndexOrThrow(SyncConstValue._SYNC_VERSION));
            entry.setId(feedUrl + "/" + syncId);
            entry.setEditUri(entry.getId() + "/" + syncVersion);
        }
    }

    private static void addPhonesToContactEntry(ContentResolver cr, long personId, ContactEntry entry)
            throws ParseException {
        Cursor c = cr.query(Phones.CONTENT_URI, null, "person=" + personId, null, null);
        int numberIndex = c.getColumnIndexOrThrow(People.Phones.NUMBER);
        try {
            while (c.moveToNext()) {
                PhoneNumber phoneNumber = new PhoneNumber();
                cursorToContactsElement(phoneNumber, c, PROVIDER_TYPE_TO_ENTRY_PHONE);
                phoneNumber.setPhoneNumber(c.getString(numberIndex));
                entry.addPhoneNumber(phoneNumber);
            }
        } finally {
            if (c != null) c.close();
        }
    }


    static private void addContactMethodsToContactEntry(ContentResolver cr, long personId,
            ContactEntry entry) throws ParseException {
        Cursor c = cr.query(ContactMethods.CONTENT_URI, null,
                "person=" + personId, null, null);
        int kindIndex = c.getColumnIndexOrThrow(ContactMethods.KIND);
        int dataIndex = c.getColumnIndexOrThrow(ContactMethods.DATA);
        int auxDataIndex = c.getColumnIndexOrThrow(ContactMethods.AUX_DATA);
        try {
            while (c.moveToNext()) {
                int kind = c.getInt(kindIndex);
                switch (kind) {
                    case Contacts.KIND_IM: {
                        ImAddress address = new ImAddress();
                        cursorToContactsElement(address, c, PROVIDER_TYPE_TO_ENTRY_IM);
                        address.setAddress(c.getString(dataIndex));
                        Object object = ContactMethods.decodeImProtocol(c.getString(auxDataIndex));
                        if (object == null) {
                            address.setProtocolPredefined(ImAddress.PROTOCOL_NONE);
                        } else if (object instanceof Integer) {
                            address.setProtocolPredefined(
                                    PROVIDER_IM_PROTOCOL_TO_ENTRY_PROTOCOL.get((Integer)object));
                        } else {
                            if (!(object instanceof String)) {
                                throw new IllegalArgumentException("expected an String, " + object);
                            }
                            address.setProtocolPredefined(ImAddress.PROTOCOL_CUSTOM);
                            address.setProtocolCustom((String)object);
                        }
                        entry.addImAddress(address);
                        break;
                    }
                    case Contacts.KIND_POSTAL: {
                        PostalAddress address = new PostalAddress();
                        cursorToContactsElement(address, c, PROVIDER_TYPE_TO_ENTRY_POSTAL);
                        address.setValue(c.getString(dataIndex));
                        entry.addPostalAddress(address);
                        break;
                    }
                    case Contacts.KIND_EMAIL: {
                        EmailAddress address = new EmailAddress();
                        cursorToContactsElement(address, c, PROVIDER_TYPE_TO_ENTRY_EMAIL);
                        address.setAddress(c.getString(dataIndex));
                        entry.addEmailAddress(address);
                        break;
                    }
                }
            }
        } finally {
            if (c != null) c.close();
        }
    }

    private static void addOrganizationsToContactEntry(ContentResolver cr, long personId,
            ContactEntry entry) throws ParseException {
        Cursor c = cr.query(Organizations.CONTENT_URI, null,
                "person=" + personId, null, null);
        try {
            int companyIndex = c.getColumnIndexOrThrow(Organizations.COMPANY);
            int titleIndex = c.getColumnIndexOrThrow(Organizations.TITLE);
            while (c.moveToNext()) {
                Organization organization = new Organization();
                cursorToContactsElement(organization, c, PROVIDER_TYPE_TO_ENTRY_ORGANIZATION);
                organization.setName(c.getString(companyIndex));
                organization.setTitle(c.getString(titleIndex));
                entry.addOrganization(organization);
            }
        } finally {
            if (c != null) c.close();
        }
    }

    private static void addGroupMembershipToContactEntry(Account account, ContentResolver cr,
            long personId, ContactEntry entry) throws ParseException {
        Cursor c = cr.query(GroupMembership.RAW_CONTENT_URI, null,
                "person=" + personId, null, null);
        try {
            int serverIdIndex = c.getColumnIndexOrThrow(GroupMembership.GROUP_SYNC_ID);
            int localIdIndex = c.getColumnIndexOrThrow(GroupMembership.GROUP_ID);
            while (c.moveToNext()) {
                String serverId = c.getString(serverIdIndex);
                if (serverId == null) {
                    final Uri groupUri = ContentUris
                            .withAppendedId(Groups.CONTENT_URI, c.getLong(localIdIndex));
                    Cursor groupCursor = cr.query(groupUri, new String[]{Groups._SYNC_ID},
                            null, null, null);
                    try {
                        if (groupCursor.moveToNext()) {
                            serverId = groupCursor.getString(0);
                        }
                    } finally {
                        groupCursor.close();
                    }
                }
                if (serverId == null) {
                    // the group hasn't been synced yet, we can't complete this operation since
                    // we don't know what server id to use for the group
                    throw new ParseException("unable to construct GroupMembershipInfo since the "
                            + "group _sync_id isn't known yet, will retry later");
                }
                GroupMembershipInfo groupMembershipInfo = new GroupMembershipInfo();
                String groupId = getCanonicalGroupsFeedForAccount(account) + "/" + serverId;
                groupMembershipInfo.setGroup(groupId);
                groupMembershipInfo.setDeleted(false);
                entry.addGroup(groupMembershipInfo);
            }
        } finally {
            if (c != null) c.close();
        }
    }

    private static void addExtensionsToContactEntry(ContentResolver cr, long personId,
            ContactEntry entry) throws ParseException {
        Cursor c = cr.query(Extensions.CONTENT_URI, null, "person=" + personId, null, null);
        try {
            JSONObject jsonObject = new JSONObject();
            int nameIndex = c.getColumnIndexOrThrow(Extensions.NAME);
            int valueIndex = c.getColumnIndexOrThrow(Extensions.VALUE);
            if (c.getCount() == 0) return;
            while (c.moveToNext()) {
                try {
                    jsonObject.put(c.getString(nameIndex), c.getString(valueIndex));
                } catch (JSONException e) {
                    throw new ParseException("bad key or value", e);
                }
            }
            ExtendedProperty extendedProperty = new ExtendedProperty();
            extendedProperty.setName("android");
            final String jsonString = jsonObject.toString();
            if (jsonString == null) {
                throw new ParseException("unable to convert cursor into a JSON string, "
                        + DatabaseUtils.dumpCursorToString(c));
            }
            extendedProperty.setXmlBlob(jsonString);
            entry.addExtendedProperty(extendedProperty);
        } finally {
            if (c != null) c.close();
        }
    }

    private static void cursorToContactsElement(ContactsElement element,
            Cursor c, HashMap<Integer, Byte> map) {
        final int typeIndex = c.getColumnIndexOrThrow("type");
        final int labelIndex = c.getColumnIndexOrThrow("label");
        final int isPrimaryIndex = c.getColumnIndexOrThrow("isprimary");

        element.setLabel(c.getString(labelIndex));
        element.setType(map.get(c.getInt(typeIndex)));
        element.setIsPrimary(c.getInt(isPrimaryIndex) != 0);
    }

    private static void contactsElementToValues(ContentValues values, ContactsElement element,
            HashMap<Byte, Integer> map) {
        values.put("type", map.get(element.getType()));
        values.put("label", element.getLabel());
        values.put("isprimary", element.isPrimary() ? 1 : 0);
    }

    /*
     * Takes the entry, casts it to a ContactEntry and executes the appropriate
     * actions on the ContentProvider to represent the entry.
     */
    protected void updateProvider(Feed feed, Long syncLocalId,
            Entry baseEntry, ContentProvider provider, Object syncInfo) throws ParseException {

        // This is a hack to delete these incorrectly created contacts named "Starred in Android"
        if (baseEntry instanceof ContactEntry
                && "Starred in Android".equals(baseEntry.getTitle())) {
            Log.i(TAG, "Deleting incorrectly created contact from the server: " + baseEntry);
            GDataServiceClient client = getGDataServiceClient();
            try {
                client.deleteEntry(baseEntry.getEditUri(), getAuthToken());
            } catch (IOException e) {
                Log.i(TAG, "  exception while deleting contact: " + baseEntry, e);
            } catch (com.google.wireless.gdata.client.HttpException e) {
                Log.i(TAG, "  exception while deleting contact: " + baseEntry, e);
            }
        }

        updateProviderImpl(getAccount(), syncLocalId, baseEntry, provider);
    }

    protected static void updateProviderImpl(Account account, Long syncLocalId,
            Entry entry, ContentProvider provider) throws ParseException {
        // If this is a deleted entry then add it to the DELETED_CONTENT_URI
        ContentValues deletedValues = null;
        if (entry.isDeleted()) {
            deletedValues = new ContentValues();
            deletedValues.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
            final String id = entry.getId();
            final String editUri = entry.getEditUri();
            if (!TextUtils.isEmpty(id)) {
                deletedValues.put(SyncConstValue._SYNC_ID, lastItemFromUri(id));
            }
            if (!TextUtils.isEmpty(editUri)) {
                deletedValues.put(SyncConstValue._SYNC_VERSION, lastItemFromUri(editUri));
            }
            deletedValues.put(SyncConstValue._SYNC_ACCOUNT, account.mName);
            deletedValues.put(SyncConstValue._SYNC_ACCOUNT_TYPE, account.mType);
        }

        if (entry instanceof ContactEntry) {
            if (deletedValues != null) {
                provider.insert(People.DELETED_CONTENT_URI, deletedValues);
                return;
            }
            updateProviderWithContactEntry(account, syncLocalId, (ContactEntry) entry, provider);
            return;
        }
        if (entry instanceof GroupEntry) {
            if (deletedValues != null) {
                provider.insert(Groups.DELETED_CONTENT_URI, deletedValues);
                return;
            }
            updateProviderWithGroupEntry(account, syncLocalId, (GroupEntry) entry, provider);
            return;
        }
        throw new IllegalArgumentException("unknown entry type, " + entry.getClass().getName());
    }

    protected static void updateProviderWithContactEntry(Account account, Long syncLocalId,
            ContactEntry entry, ContentProvider provider) throws ParseException {
        final String name = entry.getTitle();
        final String notes = entry.getContent();
        final String yomiName = entry.getYomiName();
        final String personSyncId = lastItemFromUri(entry.getId());
        final String personSyncVersion = lastItemFromUri(entry.getEditUri());

        // Store the info about the person
        ContentValues values = new ContentValues();
        values.put(People.NAME, name);
        values.put(People.NOTES, notes);
        values.put(People.PHONETIC_NAME, yomiName);
        values.put(SyncConstValue._SYNC_ACCOUNT, account.mName);
        values.put(SyncConstValue._SYNC_ACCOUNT_TYPE, account.mType);
        values.put(SyncConstValue._SYNC_ID, personSyncId);
        values.put(SyncConstValue._SYNC_DIRTY, "0");
        values.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
        values.put(SyncConstValue._SYNC_TIME, personSyncVersion);
        values.put(SyncConstValue._SYNC_VERSION, personSyncVersion);
        Uri personUri = provider.insert(People.CONTENT_URI, values);

        // Store the photo information
        final boolean photoExistsOnServer = !TextUtils.isEmpty(entry.getLinkPhotoHref());
        final String photoVersion = lastItemFromUri(entry.getLinkEditPhotoHref());
        values.clear();
        values.put(Photos.PERSON_ID, ContentUris.parseId(personUri));
        values.put(Photos.EXISTS_ON_SERVER, photoExistsOnServer ? 1 : 0);
        values.put(SyncConstValue._SYNC_ACCOUNT, account.mName);
        values.put(SyncConstValue._SYNC_ACCOUNT_TYPE, account.mType);
        values.put(SyncConstValue._SYNC_ID, personSyncId);
        values.put(SyncConstValue._SYNC_DIRTY, 0);
        values.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
        values.put(SyncConstValue._SYNC_TIME, photoVersion);
        values.put(SyncConstValue._SYNC_VERSION, photoVersion);
        if (provider.insert(Photos.CONTENT_URI, values) == null) {
            Log.e(TAG, "error inserting photo row, " + values);
        }

        // Store each email address
        for (Object object : entry.getEmailAddresses()) {
            EmailAddress email = (EmailAddress) object;
            values.clear();
            contactsElementToValues(values, email, ENTRY_TYPE_TO_PROVIDER_EMAIL);
            values.put(ContactMethods.DATA, email.getAddress());
            values.put(ContactMethods.KIND, Contacts.KIND_EMAIL);
            Uri uri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
            provider.insert(uri, values);
        }

        // Store each postal address
        for (Object object : entry.getPostalAddresses()) {
            PostalAddress address = (PostalAddress) object;
            values.clear();
            contactsElementToValues(values, address, ENTRY_TYPE_TO_PROVIDER_POSTAL);
            values.put(ContactMethods.DATA, address.getValue());
            values.put(ContactMethods.KIND, Contacts.KIND_POSTAL);
            Uri uri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
            provider.insert(uri, values);
        }

        // Store each im address
        for (Object object : entry.getImAddresses()) {
            ImAddress address = (ImAddress) object;
            values.clear();
            contactsElementToValues(values, address, ENTRY_TYPE_TO_PROVIDER_IM);
            values.put(ContactMethods.DATA, address.getAddress());
            values.put(ContactMethods.KIND, Contacts.KIND_IM);
            final byte protocolType = address.getProtocolPredefined();
            if (protocolType == ImAddress.PROTOCOL_NONE) {
                // don't add anything
            } else if (protocolType == ImAddress.PROTOCOL_CUSTOM) {
                values.put(ContactMethods.AUX_DATA,
                        ContactMethods.encodeCustomImProtocol(address.getProtocolCustom()));
            } else {
                Integer providerProtocolType =
                        ENTRY_IM_PROTOCOL_TO_PROVIDER_PROTOCOL .get(protocolType);
                if (providerProtocolType == null) {
                    throw new IllegalArgumentException("unknown protocol type, " + protocolType);
                }
                values.put(ContactMethods.AUX_DATA,
                        ContactMethods.encodePredefinedImProtocol(providerProtocolType));
            }
            Uri uri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
            provider.insert(uri, values);
        }

        // Store each organization
        for (Object object : entry.getOrganizations()) {
            Organization organization = (Organization) object;
            values.clear();
            contactsElementToValues(values, organization, ENTRY_TYPE_TO_PROVIDER_ORGANIZATION);
            values.put(Organizations.COMPANY, organization.getName());
            values.put(Organizations.TITLE, organization.getTitle());
            values.put(Organizations.COMPANY, organization.getName());
            Uri uri = Uri.withAppendedPath(personUri, Organizations.CONTENT_DIRECTORY);
            provider.insert(uri, values);
        }

        // Store each group
        for (Object object : entry.getGroups()) {
            GroupMembershipInfo groupMembershipInfo = (GroupMembershipInfo) object;
            if (groupMembershipInfo.isDeleted()) {
                continue;
            }
            values.clear();
            values.put(GroupMembership.GROUP_SYNC_ACCOUNT, account.mName);
            values.put(GroupMembership.GROUP_SYNC_ACCOUNT_TYPE, account.mType);
            values.put(GroupMembership.GROUP_SYNC_ID,
                    lastItemFromUri(groupMembershipInfo.getGroup()));
            Uri uri = Uri.withAppendedPath(personUri, GroupMembership.CONTENT_DIRECTORY);
            provider.insert(uri, values);
        }

        // Store each phone number
        for (Object object : entry.getPhoneNumbers()) {
            PhoneNumber phone = (PhoneNumber) object;
            values.clear();
            contactsElementToValues(values, phone, ENTRY_TYPE_TO_PROVIDER_PHONE);
            values.put(People.Phones.NUMBER, phone.getPhoneNumber());
            values.put(People.Phones.LABEL, phone.getLabel());
            Uri uri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
            provider.insert(uri, values);
        }

        // Store the extended properties
        for (Object object : entry.getExtendedProperties()) {
            ExtendedProperty extendedProperty = (ExtendedProperty) object;
            if (!"android".equals(extendedProperty.getName())) {
                continue;
            }
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(extendedProperty.getXmlBlob());
            } catch (JSONException e) {
                Log.w(TAG, "error parsing the android extended property, dropping, entry is "
                        + entry.toString());
                continue;
            }
            Iterator jsonIterator = jsonObject.keys();
            while (jsonIterator.hasNext()) {
                String key = (String)jsonIterator.next();
                values.clear();
                values.put(Extensions.NAME, key);
                try {
                    values.put(Extensions.VALUE, jsonObject.getString(key));
                } catch (JSONException e) {
                    // this should never happen, since we just got the key from the iterator
                }
                Uri uri = Uri.withAppendedPath(personUri, People.Extensions.CONTENT_DIRECTORY);
                if (null == provider.insert(uri, values)) {
                    Log.e(TAG, "Error inserting extension into provider, uri "
                            + uri + ", values " + values);
                }
            }
            break;
        }
    }

    protected static void updateProviderWithGroupEntry(Account account, Long syncLocalId,
            GroupEntry entry, ContentProvider provider) throws ParseException {
        ContentValues values = new ContentValues();
        values.put(Groups.NAME, entry.getTitle());
        values.put(Groups.NOTES, entry.getContent());
        values.put(Groups.SYSTEM_ID, entry.getSystemGroup());
        values.put(Groups._SYNC_ACCOUNT, account.mName);
        values.put(Groups._SYNC_ACCOUNT_TYPE, account.mType);
        values.put(Groups._SYNC_ID, lastItemFromUri(entry.getId()));
        values.put(Groups._SYNC_DIRTY, 0);
        values.put(Groups._SYNC_LOCAL_ID, syncLocalId);
        final String editUri = entry.getEditUri();
        final String syncVersion = editUri == null ? null : lastItemFromUri(editUri);
        values.put(Groups._SYNC_TIME, syncVersion);
        values.put(Groups._SYNC_VERSION, syncVersion);
        provider.insert(Groups.CONTENT_URI, values);
    }

    private static String lastItemFromUri(String url) {
        return url.substring(url.lastIndexOf('/') + 1);
    }

    protected void savePhoto(long person, InputStream photoInput, String photoVersion)
            throws IOException {
        try {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            while(true) {
                int bytesRead = photoInput.read(data);
                if (bytesRead < 0) break;
                byteStream.write(data, 0, bytesRead);
            }

            ContentValues values = new ContentValues();
            // we have to include this here otherwise the provider will set it to 1
            values.put(Photos._SYNC_DIRTY, 0);
            values.put(Photos.LOCAL_VERSION, photoVersion);
            values.put(Photos.DATA, byteStream.toByteArray());
            Uri photoUri = Uri.withAppendedPath(People.CONTENT_URI,
                    "" + person + "/" + Photos.CONTENT_DIRECTORY);
            if (getContext().getContentResolver().update(photoUri, values,
                    "_sync_dirty=0", null) > 0) {
                if (Log.isLoggable(TAG, Log.VERBOSE)) {
                    Log.v(TAG, "savePhoto: updated " + photoUri + " with values " + values);
                }
            } else {
                Log.e(TAG, "savePhoto: update of " + photoUri + " with values " + values
                        + " affected no rows");
            }
        } finally {
            try {
                if (photoInput != null) photoInput.close();
            } catch (IOException e) {
                // we don't care about exceptions here
            }
        }
    }

    /**
     * Make sure the contacts subscriptions we expect based on the current
     * accounts are present and that there aren't any extra subscriptions
     * that we don't expect.
     */
    @Override
    public void onAccountsChanged(Account[] accountsArray) {
        if (!"yes".equals(SystemProperties.get("ro.config.sync"))) {
            return;
        }

        ContentResolver cr = getContext().getContentResolver();
        for (Account account : accountsArray) {
            // TODO(fredq) should be using account instead of null
            String value = Contacts.Settings.getSetting(cr, null,
                    Contacts.Settings.SYNC_EVERYTHING);
            if (value == null) {
                // TODO(fredq) should be using account instead of null
                Contacts.Settings.setSetting(cr, null, Contacts.Settings.SYNC_EVERYTHING, "1");
            }
            updateSubscribedFeeds(cr, account);
        }
    }

    /**
     *  Returns the contacts feed url for a specific account.
     *  @param account The account
     *  @return The contacts feed url for a specific account.
     */
    public static String getContactsFeedForAccount(Account account) {
        String url = CONTACTS_FEED_URL + account.mName + "/base2_property-android";
        return rewriteUrlforAccount(account, url);
    }

    /**
     *  Returns the contacts group feed url for a specific account.
     *  @param account The account
     *  @param groupSyncId The group id
     *  @return The contacts feed url for a specific account and group.
     */
    public static String getContactsFeedForGroup(Account account, String groupSyncId) {
        String groupId = getCanonicalGroupsFeedForAccount(account);
        try {
            groupId = URLEncoder.encode(groupId, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException("unable to url encode group: " + groupId);
        }
        return getContactsFeedForAccount(account) + "?group=" + groupId + "/" + groupSyncId;
    }

    /**
     *  Returns the groups feed url for a specific account.
     *  @param account The account
     *  @return The groups feed url for a specific account.
     */
    public static String getGroupsFeedForAccount(Account account) {
        String url = GROUPS_FEED_URL + account.mName + "/base2_property-android";
        return rewriteUrlforAccount(account, url);
    }

    /**
     *  Returns the groups feed url for a specific account that should be
     *  used as the foreign reference to this group, e.g. in the
     *  group membership element of the ContactEntry. The canonical groups
     *  feed always uses http (so it doesn't need to be rewritten) and it always
     *  uses the base projection. 
     *  @param account The account
     *  @return The groups feed url for a specific account.
     */
    public static String getCanonicalGroupsFeedForAccount(Account account) {
        return GROUPS_FEED_URL + account.mName + "/base";
    }

    /**
     *  Returns the photo feed url for a specific account.
     *  @param account The account
     *  @return The photo feed url for a specific account.
     */
    public static String getPhotosFeedForAccount(Account account) {
        String url = PHOTO_FEED_URL + account.mName;
        return rewriteUrlforAccount(account, url);
    }

    protected static boolean getFeedReturnsPartialDiffs() {
        return true;
    }

    @Override
    protected void updateQueryParameters(QueryParams params) {
        // we want to get the events ordered by last modified, so we can
        // recover in case we cannot process the entire feed.
        params.setParamValue("orderby", "lastmodified");
        params.setParamValue("sortorder", "ascending");

        // set showdeleted so that we get tombstones, only do this when we
        // are doing an incremental sync
        if (params.getUpdatedMin() != null) {
            params.setParamValue("showdeleted", "true");
        }
    }

    @Override
    public void onSyncStarting(SyncContext context, Account account, boolean manualSync,
            SyncResult result) {
        mPerformedGetServerDiffs = false;
        mIsManualSync = manualSync;
        mPhotoDownloads = 0;
        mPhotoUploads = 0;
        super.onSyncStarting(context, account, manualSync, result);
    }

    @Override
    public void onSyncEnding(SyncContext context, boolean success) {
        final ContentResolver cr = getContext().getContentResolver();

        if (success && mPerformedGetServerDiffs && !mSyncCanceled) {
            final Account account = getAccount();
            Cursor cursor = cr.query(
                    Photos.CONTENT_URI,
                    new String[]{Photos._SYNC_ID, Photos._SYNC_VERSION, Photos.PERSON_ID,
                            Photos.DOWNLOAD_REQUIRED}, ""
                    + "_sync_account=? AND _sync_account_type=? AND download_required != 0",
                    new String[]{account.mName, account.mType}, null);
            try {
                if (cursor.getCount() != 0) {
                    Bundle extras = new Bundle();
                    extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, mIsManualSync);
                    extras.putString("feed", ContactsSyncAdapter.getPhotosFeedForAccount(account));
                    ContentResolver.requestSync(account, Contacts.AUTHORITY, extras);
                }
            } finally {
                cursor.close();
            }
        }

        super.onSyncEnding(context, success);
    }

    public static void updateSubscribedFeeds(ContentResolver cr, Account account) {
        Set<String> feedsToSync = Sets.newHashSet();
        feedsToSync.add(getGroupsFeedForAccount(account));
        addContactsFeedsToSync(cr, account, feedsToSync);

        Cursor c = SubscribedFeeds.Feeds.query(cr, sSubscriptionProjection,
                SubscribedFeeds.Feeds.AUTHORITY + "=?"
                        + " AND " + SubscribedFeeds.Feeds._SYNC_ACCOUNT + "=?"
                        + " AND " + SubscribedFeeds.Feeds._SYNC_ACCOUNT_TYPE + "=?",
                new String[]{Contacts.AUTHORITY, account.mName, account.mType}, null);
        try {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "scanning over subscriptions with authority "
                        + Contacts.AUTHORITY + " and account " + account);
            }
            c.moveToNext();
            while (!c.isAfterLast()) {
                String feedInCursor = c.getString(1);
                if (feedsToSync.contains(feedInCursor)) {
                    feedsToSync.remove(feedInCursor);
                    c.moveToNext();
                } else {
                    c.deleteRow();
                }
            }
            c.commitUpdates();
        } finally {
            c.close();
        }

        // any feeds remaining in feedsToSync need a subscription
        for (String feed : feedsToSync) {
            SubscribedFeeds.addFeed(cr, feed, account, Contacts.AUTHORITY, ContactsClient.SERVICE);

            // request a sync of this feed
            Bundle extras = new Bundle();
            extras.putString("feed", feed);
            ContentResolver.requestSync(account, Contacts.AUTHORITY, extras);
        }
    }
}