blob: 70ccf494fb78694222497e7e2d1827d3f1e87f96 [file] [log] [blame]
Marcus Hagerott04be88c2016-12-07 09:55:03 -08001/*
2 * Copyright (C) 2016 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 */
16package com.android.contacts.model.account;
17
18import com.google.common.base.Objects;
19
20import java.util.Comparator;
21
22/**
23 * Orders accounts for display such that the default account is first
24 */
25public class AccountComparator implements Comparator<AccountWithDataSet> {
26 private AccountWithDataSet mDefaultAccount;
27
28 public AccountComparator(AccountWithDataSet defaultAccount) {
29 mDefaultAccount = defaultAccount;
30 }
31
32 @Override
33 public int compare(AccountWithDataSet a, AccountWithDataSet b) {
34 if (Objects.equal(a.name, b.name) && Objects.equal(a.type, b.type)
35 && Objects.equal(a.dataSet, b.dataSet)) {
36 return 0;
37 } else if (b.name == null || b.type == null) {
38 return -1;
39 } else if (a.name == null || a.type == null) {
40 return 1;
41 } else if (isWritableGoogleAccount(a) && a.equals(mDefaultAccount)) {
42 return -1;
43 } else if (isWritableGoogleAccount(b) && b.equals(mDefaultAccount)) {
44 return 1;
45 } else if (isWritableGoogleAccount(a) && !isWritableGoogleAccount(b)) {
46 return -1;
47 } else if (isWritableGoogleAccount(b) && !isWritableGoogleAccount(a)) {
48 return 1;
49 } else {
50 int diff = a.name.compareToIgnoreCase(b.name);
51 if (diff != 0) {
52 return diff;
53 }
54 diff = a.type.compareToIgnoreCase(b.type);
55 if (diff != 0) {
56 return diff;
57 }
58
59 // Accounts without data sets get sorted before those that have them.
60 if (a.dataSet != null) {
61 return b.dataSet == null ? 1 : a.dataSet.compareToIgnoreCase(b.dataSet);
62 } else {
63 return -1;
64 }
65 }
66 }
67
68 private static boolean isWritableGoogleAccount(AccountWithDataSet account) {
69 return GoogleAccountType.ACCOUNT_TYPE.equals(account.type) && account.dataSet == null;
70 }
71}