/*
* Copyright (C) 2014 AChep@xda <artemchep@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package com.achep.acdisplay.ui.widgets;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.widget.TextView;
import com.achep.acdisplay.R;
import com.achep.base.Device;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateView extends TextView {
private static final String TAG = "DateView";
private final Date mCurrentTime = new Date();
private SimpleDateFormat mDateFormat;
private String mLastText;
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_TIME_TICK.equals(action)
|| Intent.ACTION_TIME_CHANGED.equals(action)
|| Intent.ACTION_TIMEZONE_CHANGED.equals(action)
|| Intent.ACTION_LOCALE_CHANGED.equals(action)) {
if (Intent.ACTION_LOCALE_CHANGED.equals(action)
|| Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
// need to get a fresh date format
mDateFormat = null;
}
updateClock();
}
}
};
public DateView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
getContext().registerReceiver(mIntentReceiver, filter, null, null);
updateClock();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mDateFormat = null; // reload the locale next time
getContext().unregisterReceiver(mIntentReceiver);
}
protected void updateClock() {
if (mDateFormat == null) {
final String dateFormat = getContext().getString(R.string.status_date_format);
mDateFormat = getBestDateTimePattern(dateFormat);
}
mCurrentTime.setTime(System.currentTimeMillis());
final String text = mDateFormat.format(mCurrentTime);
if (!text.equals(mLastText)) {
setText(text);
mLastText = text;
}
}
@SuppressLint("NewApi")
private SimpleDateFormat getBestDateTimePattern(String dateFormat) {
if (Device.hasJellyBeanMR2Api()) {
final Locale l = Locale.getDefault();
final String fmt = DateFormat.getBestDateTimePattern(l, dateFormat);
return new SimpleDateFormat(fmt, l);
} else {
return new SimpleDateFormat(dateFormat);
}
}
}