1 /*
2  * Copyright (C) 2013 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.gallery3d.ingest.ui;
18 
19 import com.android.gallery3d.R;
20 import com.android.gallery3d.ingest.data.SimpleDate;
21 
22 import android.content.Context;
23 import android.util.AttributeSet;
24 import android.widget.FrameLayout;
25 import android.widget.TextView;
26 
27 
28 import java.text.DateFormatSymbols;
29 import java.util.Locale;
30 
31 /**
32  * Displays a date in a square tile.
33  */
34 public class DateTileView extends FrameLayout {
35   private static String[] sMonthNames = DateFormatSymbols.getInstance().getShortMonths();
36   private static Locale sLocale;
37 
38   static {
refreshLocale()39     refreshLocale();
40   }
41 
refreshLocale()42   public static boolean refreshLocale() {
43     Locale currentLocale = Locale.getDefault();
44     if (!currentLocale.equals(sLocale)) {
45       sLocale = currentLocale;
46       sMonthNames = DateFormatSymbols.getInstance(sLocale).getShortMonths();
47       return true;
48     } else {
49       return false;
50     }
51   }
52 
53   private TextView mDateTextView;
54   private TextView mMonthTextView;
55   private TextView mYearTextView;
56   private int mMonth = -1;
57   private int mYear = -1;
58   private int mDate = -1;
59   private String[] mMonthNames = sMonthNames;
60 
DateTileView(Context context)61   public DateTileView(Context context) {
62     super(context);
63   }
64 
DateTileView(Context context, AttributeSet attrs)65   public DateTileView(Context context, AttributeSet attrs) {
66     super(context, attrs);
67   }
68 
DateTileView(Context context, AttributeSet attrs, int defStyle)69   public DateTileView(Context context, AttributeSet attrs, int defStyle) {
70     super(context, attrs, defStyle);
71   }
72 
73   @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)74   public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
75     // Force this to be square
76     super.onMeasure(widthMeasureSpec, widthMeasureSpec);
77   }
78 
79   @Override
onFinishInflate()80   protected void onFinishInflate() {
81     super.onFinishInflate();
82     mDateTextView = (TextView) findViewById(R.id.date_tile_day);
83     mMonthTextView = (TextView) findViewById(R.id.date_tile_month);
84     mYearTextView = (TextView) findViewById(R.id.date_tile_year);
85   }
86 
setDate(SimpleDate date)87   public void setDate(SimpleDate date) {
88     setDate(date.getDay(), date.getMonth(), date.getYear());
89   }
90 
setDate(int date, int month, int year)91   public void setDate(int date, int month, int year) {
92     if (date != mDate) {
93       mDate = date;
94       mDateTextView.setText(mDate > 9 ? Integer.toString(mDate) : "0" + mDate);
95     }
96     if (mMonthNames != sMonthNames) {
97       mMonthNames = sMonthNames;
98       if (month == mMonth) {
99         mMonthTextView.setText(mMonthNames[mMonth]);
100       }
101     }
102     if (month != mMonth) {
103       mMonth = month;
104       mMonthTextView.setText(mMonthNames[mMonth]);
105     }
106     if (year != mYear) {
107       mYear = year;
108       mYearTextView.setText(Integer.toString(mYear));
109     }
110   }
111 }
112