1d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu/*
2d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * Copyright (C) 2013 The Android Open Source Project
3d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *
4d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * Licensed under the Apache License, Version 2.0 (the "License");
5d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * you may not use this file except in compliance with the License.
6d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * You may obtain a copy of the License at
7d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *
8d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *      http://www.apache.org/licenses/LICENSE-2.0
9d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *
10d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * Unless required by applicable law or agreed to in writing, software
11d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * distributed under the License is distributed on an "AS IS" BASIS,
12d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * See the License for the specific language governing permissions and
14d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * limitations under the License.
15d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu */
16d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
17d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescupackage com.android.gallery3d.ingest.ui;
18d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
19f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport com.android.gallery3d.R;
20f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport com.android.gallery3d.ingest.data.SimpleDate;
21f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
22d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.content.Context;
23d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.util.AttributeSet;
24d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.widget.FrameLayout;
25d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.widget.TextView;
26d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
27d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
28d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport java.text.DateFormatSymbols;
29f63e1f7ab9a4f4a97909564cfc5975cbac66872fBobby Georgescuimport java.util.Locale;
30d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
31f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu/**
32f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * Displays a date in a square tile.
33f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu */
34d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescupublic class DateTileView extends FrameLayout {
35f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private static String[] sMonthNames = DateFormatSymbols.getInstance().getShortMonths();
36f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private static Locale sLocale;
37f63e1f7ab9a4f4a97909564cfc5975cbac66872fBobby Georgescu
38f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  static {
39f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    refreshLocale();
40f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
41f63e1f7ab9a4f4a97909564cfc5975cbac66872fBobby Georgescu
42f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public static boolean refreshLocale() {
43f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    Locale currentLocale = Locale.getDefault();
44f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (!currentLocale.equals(sLocale)) {
45f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      sLocale = currentLocale;
46f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      sMonthNames = DateFormatSymbols.getInstance(sLocale).getShortMonths();
47f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return true;
48f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    } else {
49f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return false;
50f63e1f7ab9a4f4a97909564cfc5975cbac66872fBobby Georgescu    }
51f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
52d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
53f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private TextView mDateTextView;
54f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private TextView mMonthTextView;
55f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private TextView mYearTextView;
56f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private int mMonth = -1;
57f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private int mYear = -1;
58f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private int mDate = -1;
59f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private String[] mMonthNames = sMonthNames;
60d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
61f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public DateTileView(Context context) {
62f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super(context);
63f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
64d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
65f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public DateTileView(Context context, AttributeSet attrs) {
66f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super(context, attrs);
67f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
68d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
69f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public DateTileView(Context context, AttributeSet attrs, int defStyle) {
70f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super(context, attrs, defStyle);
71f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
72d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
73f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
74f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
75f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    // Force this to be square
76f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
77f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
78d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
79f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
80f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  protected void onFinishInflate() {
81f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super.onFinishInflate();
82f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    mDateTextView = (TextView) findViewById(R.id.date_tile_day);
83f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    mMonthTextView = (TextView) findViewById(R.id.date_tile_month);
84f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    mYearTextView = (TextView) findViewById(R.id.date_tile_year);
85f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
86d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
87f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void setDate(SimpleDate date) {
88f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    setDate(date.getDay(), date.getMonth(), date.getYear());
89f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
90d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
91f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void setDate(int date, int month, int year) {
92f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (date != mDate) {
93f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mDate = date;
94f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mDateTextView.setText(mDate > 9 ? Integer.toString(mDate) : "0" + mDate);
95f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
96f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (mMonthNames != sMonthNames) {
97f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mMonthNames = sMonthNames;
98f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      if (month == mMonth) {
99f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu        mMonthTextView.setText(mMonthNames[mMonth]);
100f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      }
101f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
102f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (month != mMonth) {
103f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mMonth = month;
104f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mMonthTextView.setText(mMonthNames[mMonth]);
105f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
106f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (year != mYear) {
107f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mYear = year;
108f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mYearTextView.setText(Integer.toString(mYear));
109d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu    }
110f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
111d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu}
112