Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b478a716 authored by Daniel Sandler's avatar Daniel Sandler
Browse files

Update date format in panel on locale change.

The logic that updated the locale was only running at times
the DateView should have been updating its display, namely,
when the view was visible on-screen.

The new version trades a small amount of efficiency (the
broadcast receiver is now active at all times, and the date
is re-formatted each TIME_TICK) in exchange for simplicity
and correctness.

Bug: 10725912
Change-Id: I2f0988ed478374ed0eb0c76c3d8caed6c86eb8df
parent 564e8e6f
Loading
Loading
Loading
Loading
+20 −62
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.TextView;
@@ -39,10 +40,7 @@ public class DateView extends TextView {
    private final Date mCurrentTime = new Date();

    private SimpleDateFormat mDateFormat;
    private boolean mChangedLocale;
    private boolean mAttachedToWindow;
    private boolean mWindowVisible;
    private boolean mUpdating;
    private String mLastText;

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
@@ -53,7 +51,8 @@ public class DateView extends TextView {
                    || Intent.ACTION_TIMEZONE_CHANGED.equals(action)
                    || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
                if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
                    mChangedLocale = true;
                    // need to get a fresh date format
                    mDateFormat = null;
                }
                updateClock();
            }
@@ -67,80 +66,39 @@ public class DateView extends TextView {
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mAttachedToWindow = true;
        setUpdates();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mAttachedToWindow = false;
        setUpdates();
    }
        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);
        mContext.registerReceiver(mIntentReceiver, filter, null, null);

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        mWindowVisible = visibility == VISIBLE;
        setUpdates();
        updateClock();
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        setUpdates();
    }
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

    @Override
    protected int getSuggestedMinimumWidth() {
        // makes the large background bitmap not force us to full width
        return 0;
        mDateFormat = null; // reload the locale next time
        mContext.unregisterReceiver(mIntentReceiver);
    }

    protected void updateClock() {
        if (mDateFormat == null || mChangedLocale) {
        if (mDateFormat == null) {
            final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
            final Locale l = Locale.getDefault();
            final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
            mDateFormat = new SimpleDateFormat(fmt, l);
            mChangedLocale = false;
        }

        mCurrentTime.setTime(System.currentTimeMillis());
        setText(mDateFormat.format(mCurrentTime));
    }

    private boolean isVisible() {
        View v = this;
        while (true) {
            if (v.getVisibility() != VISIBLE) {
                return false;
            }
            final ViewParent parent = v.getParent();
            if (parent instanceof View) {
                v = (View)parent;
            } else {
                return true;
            }
        }
    }

    private void setUpdates() {
        boolean update = mAttachedToWindow && mWindowVisible && isVisible();
        if (update != mUpdating) {
            mUpdating = update;
            if (update) {
                // Register for Intent broadcasts for the clock and battery
                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);
                mContext.registerReceiver(mIntentReceiver, filter, null, null);
                updateClock();
            } else {
                mContext.unregisterReceiver(mIntentReceiver);
            }
        final String text = mDateFormat.format(mCurrentTime);
        if (!text.equals(mLastText)) {
            setText(text);
            mLastText = text;
        }
    }
}