summaryrefslogtreecommitdiffstats
path: root/tools/layoutlib/bridge/src/android/widget/SimpleMonthView_Delegate.java
blob: 8e41e51625135ed661913a4e84d9a1883846594c (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
/*
 * Copyright (C) 2015 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 android.widget;

import com.android.tools.layoutlib.annotations.LayoutlibDelegate;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.icu.text.SimpleDateFormat;
import android.text.format.DateFormat;

import java.util.Calendar;
import java.util.Locale;

/**
 * Delegate that provides implementation for some methods in {@link SimpleMonthView}.
 * <p/>
 * Through the layoutlib_create tool, selected methods of SimpleMonthView have been replaced by
 * calls to methods of the same name in this delegate class.
 * <p/>
 * The main purpose of this class is to use {@link android.icu.text.SimpleDateFormat} instead of
 * {@link java.text.SimpleDateFormat}.
 */
public class SimpleMonthView_Delegate {

    private static final String DEFAULT_TITLE_FORMAT = "MMMMy";
    private static final String DAY_OF_WEEK_FORMAT = "EEEEE";

    // Maintain a cache of the last view used, so that the formatters can be reused.
    @Nullable private static SimpleMonthView sLastView;
    @Nullable private static SimpleMonthView_Delegate sLastDelegate;

    private SimpleDateFormat mTitleFormatter;
    private SimpleDateFormat mDayOfWeekFormatter;

    private Locale locale;

    @LayoutlibDelegate
    /*package*/ static CharSequence getTitle(SimpleMonthView view) {
        if (view.mTitle == null) {
            SimpleMonthView_Delegate delegate = getDelegate(view);
            if (delegate.mTitleFormatter == null) {
                delegate.mTitleFormatter = new SimpleDateFormat(DateFormat.getBestDateTimePattern(
                        getLocale(delegate, view), DEFAULT_TITLE_FORMAT));
            }
            view.mTitle = delegate.mTitleFormatter.format(view.mCalendar.getTime());
        }
        return view.mTitle;
    }

    @LayoutlibDelegate
    /*package*/ static String getDayOfWeekLabel(SimpleMonthView view, int dayOfWeek) {
        view.mDayOfWeekLabelCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        SimpleMonthView_Delegate delegate = getDelegate(view);
        if (delegate.mDayOfWeekFormatter == null) {
            delegate.mDayOfWeekFormatter =
                    new SimpleDateFormat(DAY_OF_WEEK_FORMAT, getLocale(delegate, view));
        }
        return delegate.mDayOfWeekFormatter.format(view.mDayOfWeekLabelCalendar.getTime());
    }

    private static Locale getLocale(SimpleMonthView_Delegate delegate, SimpleMonthView view) {
        if (delegate.locale == null) {
            delegate.locale = view.getContext().getResources().getConfiguration().locale;
        }
        return delegate.locale;
    }

    @NonNull
    private static SimpleMonthView_Delegate getDelegate(SimpleMonthView view) {
        if (view == sLastView) {
            assert sLastDelegate != null;
            return sLastDelegate;
        } else {
            sLastView = view;
            sLastDelegate = new SimpleMonthView_Delegate();
            return sLastDelegate;
        }
    }

    public static void clearCache() {
        sLastView = null;
        sLastDelegate = null;
    }
}