1 /*
2  * Copyright (C) 2019 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 
17 package com.android.car.dialer.ui.common.entity;
18 
19 import android.content.Context;
20 import android.util.Pair;
21 
22 import com.android.car.dialer.R;
23 import com.android.car.dialer.livedata.SharedPreferencesLiveData;
24 import com.android.car.telephony.common.Contact;
25 
26 import java.util.Comparator;
27 
28 /**
29  * Information about how Contacts are sorted.
30  */
31 public class ContactSortingInfo {
32     public static final int SORT_BY_FIRST_NAME = 1;
33     public static final int SORT_BY_LAST_NAME = 2;
34     /**
35      * Sort by the default display order of a name. For western names it will be "Given Family".
36      * For unstructured names like east asian this will be the only order.
37      * Phone Dialer uses the same method for sorting given names.
38      *
39      * @see android.provider.ContactsContract.Contacts#DISPLAY_NAME_PRIMARY
40      */
41     private static final Comparator<Contact> sFirstNameComparator =
42             (o1, o2) -> o1.compareBySortKeyPrimary(o2);
43 
44     /**
45      * Sort by the alternative display order of a name. For western names it will be "Family,
46      * Given". For unstructured names like east asian this order will be ignored and treated as
47      * primary.
48      * Phone Dialer uses the same method for sorting family names.
49      *
50      * @see android.provider.ContactsContract.Contacts#DISPLAY_NAME_ALTERNATIVE
51      */
52     private static final Comparator<Contact> sLastNameComparator =
53             (o1, o2) -> o1.compareBySortKeyAlt(o2);
54 
55     /**
56      * A static method that return how Contacts are sorted
57      * The first parameter is the comparator that is used for sorting Contacts
58      * The second parameter is a reference to keep track of the soring method.
59      */
getSortingInfo(Context context, SharedPreferencesLiveData preferencesLiveData)60     public static Pair<Comparator<Contact>, Integer> getSortingInfo(Context context,
61             SharedPreferencesLiveData preferencesLiveData) {
62         String key = preferencesLiveData.getKey();
63         String defaultValue = context.getResources().getStringArray(
64                 R.array.contact_order_entry_values)[0];
65         String firstNameSort = context.getResources().getString(
66                 R.string.given_name_first_key);
67 
68         Comparator<Contact> comparator;
69         Integer sortMethod;
70         if (preferencesLiveData.getValue() == null
71                 || firstNameSort.equals(
72                 preferencesLiveData.getValue().getString(key, defaultValue))) {
73             comparator = sFirstNameComparator;
74             sortMethod = SORT_BY_FIRST_NAME;
75         } else {
76             comparator = sLastNameComparator;
77             sortMethod = SORT_BY_LAST_NAME;
78         }
79 
80         return new Pair<>(comparator, sortMethod);
81     }
82 }
83