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 android.widget;
18
19import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
20
21import android.annotation.NonNull;
22import android.annotation.Nullable;
23import android.icu.text.SimpleDateFormat;
24import android.text.format.DateFormat;
25
26import java.util.Calendar;
27import java.util.Locale;
28
29/**
30 * Delegate that provides implementation for some methods in {@link SimpleMonthView}.
31 * <p/>
32 * Through the layoutlib_create tool, selected methods of SimpleMonthView have been replaced by
33 * calls to methods of the same name in this delegate class.
34 * <p/>
35 * The main purpose of this class is to use {@link android.icu.text.SimpleDateFormat} instead of
36 * {@link java.text.SimpleDateFormat}.
37 */
38public class SimpleMonthView_Delegate {
39
40    private static final String DEFAULT_TITLE_FORMAT = "MMMMy";
41    private static final String DAY_OF_WEEK_FORMAT = "EEEEE";
42
43    // Maintain a cache of the last view used, so that the formatters can be reused.
44    @Nullable private static SimpleMonthView sLastView;
45    @Nullable private static SimpleMonthView_Delegate sLastDelegate;
46
47    private SimpleDateFormat mTitleFormatter;
48    private SimpleDateFormat mDayOfWeekFormatter;
49
50    private Locale locale;
51
52    @LayoutlibDelegate
53    /*package*/ static CharSequence getTitle(SimpleMonthView view) {
54        if (view.mTitle == null) {
55            SimpleMonthView_Delegate delegate = getDelegate(view);
56            if (delegate.mTitleFormatter == null) {
57                delegate.mTitleFormatter = new SimpleDateFormat(DateFormat.getBestDateTimePattern(
58                        getLocale(delegate, view), DEFAULT_TITLE_FORMAT));
59            }
60            view.mTitle = delegate.mTitleFormatter.format(view.mCalendar.getTime());
61        }
62        return view.mTitle;
63    }
64
65    @LayoutlibDelegate
66    /*package*/ static String getDayOfWeekLabel(SimpleMonthView view, int dayOfWeek) {
67        view.mDayOfWeekLabelCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
68        SimpleMonthView_Delegate delegate = getDelegate(view);
69        if (delegate.mDayOfWeekFormatter == null) {
70            delegate.mDayOfWeekFormatter =
71                    new SimpleDateFormat(DAY_OF_WEEK_FORMAT, getLocale(delegate, view));
72        }
73        return delegate.mDayOfWeekFormatter.format(view.mDayOfWeekLabelCalendar.getTime());
74    }
75
76    private static Locale getLocale(SimpleMonthView_Delegate delegate, SimpleMonthView view) {
77        if (delegate.locale == null) {
78            delegate.locale = view.getContext().getResources().getConfiguration().locale;
79        }
80        return delegate.locale;
81    }
82
83    @NonNull
84    private static SimpleMonthView_Delegate getDelegate(SimpleMonthView view) {
85        if (view == sLastView) {
86            assert sLastDelegate != null;
87            return sLastDelegate;
88        } else {
89            sLastView = view;
90            sLastDelegate = new SimpleMonthView_Delegate();
91            return sLastDelegate;
92        }
93    }
94
95    public static void clearCache() {
96        sLastView = null;
97        sLastDelegate = null;
98    }
99}
100