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

Commit 851957cb authored by Ian Lake's avatar Ian Lake
Browse files

Move expensive TextClock operations to onAttach

Registering and unregistering are expensive operations
that should not be done when the visibility of the
TextClock changes. However, we want to ensure that a
non-visible TextClock does not consume extra resources
or cause layout/redraw passes. By keeping the TextClock
registered but avoiding the side effects of onTimeChanged,
we can get the best of both worlds.

Test: manual testing

BUG: 33960344
Change-Id: I80771524a09ca57488834bafd97b570f2a8235db
parent 4b0d2bf7
Loading
Loading
Loading
Loading
+36 −25
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ public class TextClock extends TextView {
    private CharSequence mDescFormat;

    private boolean mRegistered;
    private boolean mShouldRunTicker;

    private Calendar mTime;
    private String mTimeZone;
@@ -252,8 +253,7 @@ public class TextClock extends TextView {
        }

        createTime(mTimeZone);
        // Wait until registering for events to handle the ticker
        chooseFormat(false);
        chooseFormat();
    }

    private void createTime(String timeZone) {
@@ -460,16 +460,6 @@ public class TextClock extends TextView {
        onTimeChanged();
    }

    /**
     * Selects either one of {@link #getFormat12Hour()} or {@link #getFormat24Hour()}
     * depending on whether the user has selected 24-hour format.
     *
     * Calling this method does not schedule or unschedule the time ticker.
     */
    private void chooseFormat() {
        chooseFormat(true);
    }

    /**
     * Returns the current format string. Always valid after constructor has
     * finished, and will never be {@code null}.
@@ -483,11 +473,8 @@ public class TextClock extends TextView {
    /**
     * Selects either one of {@link #getFormat12Hour()} or {@link #getFormat24Hour()}
     * depending on whether the user has selected 24-hour format.
     *
     * @param handleTicker true if calling this method should schedule/unschedule the
     *                     time ticker, false otherwise
     */
    private void chooseFormat(boolean handleTicker) {
    private void chooseFormat() {
        final boolean format24Requested = is24HourModeEnabled();

        LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale);
@@ -503,7 +490,7 @@ public class TextClock extends TextView {
        boolean hadSeconds = mHasSeconds;
        mHasSeconds = DateFormat.hasSeconds(mFormat);

        if (handleTicker && mRegistered && hadSeconds != mHasSeconds) {
        if (mShouldRunTicker && hadSeconds != mHasSeconds) {
            if (hadSeconds) getHandler().removeCallbacks(mTicker);
            else mTicker.run();
        }
@@ -517,26 +504,44 @@ public class TextClock extends TextView {
    }

    @Override
    public void onVisibilityAggregated(boolean isVisible) {
        if (!mRegistered && isVisible) {
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (!mRegistered) {
            mRegistered = true;

            registerReceiver();
            registerObserver();

            createTime(mTimeZone);
        }
    }

    @Override
    public void onVisibilityAggregated(boolean isVisible) {
        super.onVisibilityAggregated(isVisible);

        if (!mShouldRunTicker && isVisible) {
            mShouldRunTicker = true;
            if (mHasSeconds) {
                mTicker.run();
            } else {
                onTimeChanged();
            }
        } else if (mRegistered && !isVisible) {
        } else if (mShouldRunTicker && !isVisible) {
            mShouldRunTicker = false;
            getHandler().removeCallbacks(mTicker);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        if (mRegistered) {
            unregisterReceiver();
            unregisterObserver();

            getHandler().removeCallbacks(mTicker);

            mRegistered = false;
        }
    }
@@ -586,11 +591,17 @@ public class TextClock extends TextView {
        }
    }

    /**
     * Update the displayed time if this view and its ancestors and window is visible
     */
    private void onTimeChanged() {
        // mShouldRunTicker always equals the last value passed into onVisibilityAggregated
        if (mShouldRunTicker) {
            mTime.setTimeInMillis(System.currentTimeMillis());
            setText(DateFormat.format(mFormat, mTime));
            setContentDescription(DateFormat.format(mDescFormat, mTime));
        }
    }

    /** @hide */
    @Override