1/*
2 * Copyright (C) 2015 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.common.compat;
18
19import android.content.res.Resources;
20import android.provider.ContactsContract.CommonDataKinds.BaseTypes;
21import android.provider.ContactsContract.CommonDataKinds.Event;
22import android.text.TextUtils;
23
24/**
25 * Compatibility class for {@link Event}
26 */
27public class EventCompat {
28    /**
29     * Not instantiable.
30     */
31    private EventCompat() {
32    }
33
34    /**
35     * Return a {@link CharSequence} that best describes the given type, possibly substituting
36     * the given label value for TYPE_CUSTOM.
37     */
38    public static CharSequence getTypeLabel(Resources res, int type, CharSequence label) {
39        if (CompatUtils.isLollipopCompatible()) {
40            return Event.getTypeLabel(res, type, label);
41        } else {
42            return getTypeLabelInternal(res, type, label);
43        }
44    }
45
46    /**
47     * The method was added in API level 21, and below is the implementation copied from
48     * {@link Event#getTypeLabel(Resources, int, CharSequence)}
49     */
50    private static CharSequence getTypeLabelInternal(Resources res, int type, CharSequence label) {
51        if (type == BaseTypes.TYPE_CUSTOM && !TextUtils.isEmpty(label)) {
52            return label;
53        } else {
54            return res.getText(Event.getTypeResource(type));
55        }
56    }
57
58}
59