summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/editor/RawContactDeltaComparator.java
blob: 17a4ddad0912aad3283623e6725eec7657546b54 (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
/*
 * Copyright (C) 2009 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,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.editor;

import com.android.contacts.common.model.AccountTypeManager;
import com.android.contacts.common.model.RawContactDelta;
import com.android.contacts.common.model.account.AccountType;
import com.android.contacts.common.model.account.GoogleAccountType;

import android.content.Context;
import android.provider.ContactsContract.RawContacts;

import java.util.Comparator;

/**
 * Compares {@link RawContactDelta}s
 */
class RawContactDeltaComparator implements Comparator<RawContactDelta> {

    private Context mContext;

    public RawContactDeltaComparator(Context context) {
        mContext = context;
    }

    @Override
    public int compare(RawContactDelta one, RawContactDelta two) {
        // Check direct equality
        if (one.equals(two)) {
            return 0;
        }

        final AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
        String accountType1 = one.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
        String dataSet1 = one.getValues().getAsString(RawContacts.DATA_SET);
        final AccountType type1 = accountTypes.getAccountType(accountType1, dataSet1);
        String accountType2 = two.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
        String dataSet2 = two.getValues().getAsString(RawContacts.DATA_SET);
        final AccountType type2 = accountTypes.getAccountType(accountType2, dataSet2);

        // Check read-only. Sort read/write before read-only.
        if (!type1.areContactsWritable() && type2.areContactsWritable()) {
            return 1;
        } else if (type1.areContactsWritable() && !type2.areContactsWritable()) {
            return -1;
        }

        // Check account type. Sort Google before non-Google.
        boolean skipAccountTypeCheck = false;
        boolean isGoogleAccount1 = type1 instanceof GoogleAccountType;
        boolean isGoogleAccount2 = type2 instanceof GoogleAccountType;
        if (isGoogleAccount1 && !isGoogleAccount2) {
            return -1;
        } else if (!isGoogleAccount1 && isGoogleAccount2) {
            return 1;
        } else if (isGoogleAccount1 && isGoogleAccount2) {
            skipAccountTypeCheck = true;
        }

        int value;
        if (!skipAccountTypeCheck) {
            // Sort accounts with type before accounts without types.
            if (type1.accountType != null && type2.accountType == null) {
                return -1;
            } else if (type1.accountType == null && type2.accountType != null) {
                return 1;
            }

            if (type1.accountType != null && type2.accountType != null) {
                value = type1.accountType.compareTo(type2.accountType);
                if (value != 0) {
                    return value;
                }
            }

            // Fall back to data set. Sort accounts with data sets before
            // those without.
            if (type1.dataSet != null && type2.dataSet == null) {
                return -1;
            } else if (type1.dataSet == null && type2.dataSet != null) {
                return 1;
            }

            if (type1.dataSet != null && type2.dataSet != null) {
                value = type1.dataSet.compareTo(type2.dataSet);
                if (value != 0) {
                    return value;
                }
            }
        }

        // Check account name
        String oneAccount = one.getAccountName();
        if (oneAccount == null) {
            oneAccount = "";
        }
        String twoAccount = two.getAccountName();
        if (twoAccount == null) {
            twoAccount = "";
        }
        value = oneAccount.compareTo(twoAccount);
        if (value != 0) {
            return value;
        }

        // Both are in the same account, fall back to contact ID
        Long oneId = one.getRawContactId();
        Long twoId = two.getRawContactId();
        if (oneId == null) {
            return -1;
        } else if (twoId == null) {
            return 1;
        }

        return (int) (oneId - twoId);
    }
}