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