blob: 8b410aaf98e9607ea6117c845fb580f956312e42 [file] [log] [blame]
Walter Jang92101e72015-01-28 09:25:55 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts.editor;
18
Gary Mai0a49afa2016-12-05 15:53:58 -080019import android.content.Context;
20import android.provider.ContactsContract.RawContacts;
21
Gary Mai69c182a2016-12-05 13:07:03 -080022import com.android.contacts.model.AccountTypeManager;
23import com.android.contacts.model.RawContactDelta;
24import com.android.contacts.model.account.AccountType;
25import com.android.contacts.model.account.GoogleAccountType;
Walter Jang92101e72015-01-28 09:25:55 -080026
Walter Jang92101e72015-01-28 09:25:55 -080027import java.util.Comparator;
28
29/**
30 * Compares {@link RawContactDelta}s
31 */
32class RawContactDeltaComparator implements Comparator<RawContactDelta> {
33
34 private Context mContext;
35
36 public RawContactDeltaComparator(Context context) {
37 mContext = context;
38 }
39
40 @Override
41 public int compare(RawContactDelta one, RawContactDelta two) {
42 // Check direct equality
43 if (one.equals(two)) {
44 return 0;
45 }
46
47 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
48 String accountType1 = one.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
49 String dataSet1 = one.getValues().getAsString(RawContacts.DATA_SET);
50 final AccountType type1 = accountTypes.getAccountType(accountType1, dataSet1);
51 String accountType2 = two.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
52 String dataSet2 = two.getValues().getAsString(RawContacts.DATA_SET);
53 final AccountType type2 = accountTypes.getAccountType(accountType2, dataSet2);
54
55 // Check read-only. Sort read/write before read-only.
56 if (!type1.areContactsWritable() && type2.areContactsWritable()) {
57 return 1;
58 } else if (type1.areContactsWritable() && !type2.areContactsWritable()) {
59 return -1;
60 }
61
62 // Check account type. Sort Google before non-Google.
63 boolean skipAccountTypeCheck = false;
64 boolean isGoogleAccount1 = type1 instanceof GoogleAccountType;
65 boolean isGoogleAccount2 = type2 instanceof GoogleAccountType;
66 if (isGoogleAccount1 && !isGoogleAccount2) {
67 return -1;
68 } else if (!isGoogleAccount1 && isGoogleAccount2) {
69 return 1;
70 } else if (isGoogleAccount1 && isGoogleAccount2) {
71 skipAccountTypeCheck = true;
72 }
73
74 int value;
75 if (!skipAccountTypeCheck) {
76 // Sort accounts with type before accounts without types.
77 if (type1.accountType != null && type2.accountType == null) {
78 return -1;
79 } else if (type1.accountType == null && type2.accountType != null) {
80 return 1;
81 }
82
83 if (type1.accountType != null && type2.accountType != null) {
84 value = type1.accountType.compareTo(type2.accountType);
85 if (value != 0) {
86 return value;
87 }
88 }
89
90 // Fall back to data set. Sort accounts with data sets before
91 // those without.
92 if (type1.dataSet != null && type2.dataSet == null) {
93 return -1;
94 } else if (type1.dataSet == null && type2.dataSet != null) {
95 return 1;
96 }
97
98 if (type1.dataSet != null && type2.dataSet != null) {
99 value = type1.dataSet.compareTo(type2.dataSet);
100 if (value != 0) {
101 return value;
102 }
103 }
104 }
105
106 // Check account name
107 String oneAccount = one.getAccountName();
108 if (oneAccount == null) {
109 oneAccount = "";
110 }
111 String twoAccount = two.getAccountName();
112 if (twoAccount == null) {
113 twoAccount = "";
114 }
115 value = oneAccount.compareTo(twoAccount);
116 if (value != 0) {
117 return value;
118 }
119
120 // Both are in the same account, fall back to contact ID
121 Long oneId = one.getRawContactId();
122 Long twoId = two.getRawContactId();
Jay Shraunere11e06a2015-09-16 10:53:48 -0700123 if (oneId == null && twoId == null) {
124 return 0;
125 } else if (oneId == null) {
Walter Jang92101e72015-01-28 09:25:55 -0800126 return -1;
127 } else if (twoId == null) {
128 return 1;
129 }
130
Jay Shraunere11e06a2015-09-16 10:53:48 -0700131 return Long.compare(oneId, twoId);
Walter Jang92101e72015-01-28 09:25:55 -0800132 }
Jay Shraunere11e06a2015-09-16 10:53:48 -0700133}