summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/contacts/displaypreference/ContactDisplayPreferences.java
blob: dca466ebf59d4806a7a3396e67926382bcf759ed (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
/*
 * Copyright (C) 2018 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.dialer.contacts.displaypreference;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import java.util.Arrays;

/** Handles name ordering of a contact (Given name first or family name first.) */
public interface ContactDisplayPreferences {

  /**
   * A enum whose value is a String from a Android string resource which can only be resolved at run
   * time.
   */
  interface StringResEnum {

    @StringRes
    int getStringRes();

    default String getValue(Context context) {
      return context.getString(getStringRes());
    }

    static <T extends Enum<T> & StringResEnum> T fromValue(
        Context context, T[] values, String value) {
      return Arrays.stream(values)
          .filter(enumValue -> TextUtils.equals(enumValue.getValue(context), value))
          // MoreCollectors.onlyElement() is not available to android guava.
          .reduce(
              (a, b) -> {
                throw new AssertionError("multiple result");
              })
          .get();
    }
  }

  /** Order when displaying the name; */
  enum DisplayOrder implements StringResEnum {

    /**
     * The default display order of a name. For western names it will be "Given Family". For
     * unstructured names like east asian this will be the only order.
     *
     * @see android.provider.ContactsContract.Contacts#DISPLAY_NAME_PRIMARY
     */
    PRIMARY(R.string.display_options_view_given_name_first_value),
    /**
     * The alternative display order of a name. For western names it will be "Family, Given". For
     * unstructured names like east asian this order will be ignored and treated as primary.
     *
     * @see android.provider.ContactsContract.Contacts#DISPLAY_NAME_ALTERNATIVE
     */
    ALTERNATIVE(R.string.display_options_view_family_name_first_value);

    @StringRes private final int value;

    DisplayOrder(@StringRes int value) {
      this.value = value;
    }

    @Override
    @StringRes
    public int getStringRes() {
      return value;
    }

    static DisplayOrder fromValue(Context context, String value) {
      return StringResEnum.fromValue(context, DisplayOrder.values(), value);
    }
  }

  /**
   * Order when sorting the name. In some conventions, names are displayed as given name first, but
   * sorted by family name.
   */
  enum SortOrder implements StringResEnum {
    /**
     * Sort by the default display order of a name. For western names it will be "Given Family". For
     * unstructured names like east asian this will be the only order.
     *
     * @see android.provider.ContactsContract.Contacts#DISPLAY_NAME_PRIMARY
     */
    BY_PRIMARY(R.string.display_options_sort_by_given_name_value),
    /**
     * Sort by the alternative display order of a name. For western names it will be "Family,
     * Given". For unstructured names like east asian this order will be ignored and treated as
     * primary.
     *
     * @see android.provider.ContactsContract.Contacts#DISPLAY_NAME_ALTERNATIVE
     */
    BY_ALTERNATIVE(R.string.display_options_sort_by_family_name_value);

    @StringRes private final int value;

    SortOrder(@StringRes int value) {
      this.value = value;
    }

    @Override
    @StringRes
    public int getStringRes() {
      return value;
    }

    static SortOrder fromValue(Context context, String value) {
      return StringResEnum.fromValue(context, SortOrder.values(), value);
    }
  }

  DisplayOrder getDisplayOrder();

  void setDisplayOrder(DisplayOrder displayOrder);

  SortOrder getSortOrder();

  void setSortOrder(SortOrder sortOrder);

  /** Selects display name based on {@link DisplayOrder} */
  default String getDisplayName(@Nullable String primaryName, @Nullable String alternativeName) {
    if (TextUtils.isEmpty(alternativeName)) {
      return primaryName;
    }
    switch (getDisplayOrder()) {
      case PRIMARY:
        return primaryName;
      case ALTERNATIVE:
        return alternativeName;
    }
    throw new AssertionError("exhaustive switch");
  }

  /** Selects sort name based on {@link SortOrder} */
  default String getSortName(@Nullable String primaryName, @Nullable String alternativeName) {
    if (TextUtils.isEmpty(alternativeName)) {
      return primaryName;
    }
    switch (getSortOrder()) {
      case BY_PRIMARY:
        return primaryName;
      case BY_ALTERNATIVE:
        return alternativeName;
    }
    throw new AssertionError("exhaustive switch");
  }
}