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

Commit 7256c379 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Stop timer when notification isn't visible" into qt-dev

parents 10a9fde8 3c4a8e48
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RemoteViews;

import java.util.ArrayList;

/**
 * A TextView that can float around an image on the end.
 *
@@ -42,6 +44,7 @@ public class MediaNotificationView extends FrameLayout {
    private View mMainColumn;
    private View mMediaContent;
    private int mImagePushIn;
    private ArrayList<VisibilityChangeListener> mListeners;

    public MediaNotificationView(Context context) {
        this(context, null);
@@ -168,4 +171,48 @@ public class MediaNotificationView extends FrameLayout {
        mMainColumn = findViewById(com.android.internal.R.id.notification_main_column);
        mMediaContent = findViewById(com.android.internal.R.id.notification_media_content);
    }

    @Override
    public void onVisibilityAggregated(boolean isVisible) {
        super.onVisibilityAggregated(isVisible);
        for (int i = 0; i < mListeners.size(); i++) {
            mListeners.get(i).onAggregatedVisibilityChanged(isVisible);
        }
    }

    /**
     * Add a listener to receive updates on the visibility of this view
     *
     * @param listener The listener to add.
     */
    public void addVisibilityListener(VisibilityChangeListener listener) {
        if (mListeners == null) {
            mListeners = new ArrayList<>();
        }
        if (!mListeners.contains(listener)) {
            mListeners.add(listener);
        }
    }

    /**
     * Remove the specified listener
     *
     * @param listener The listener to remove.
     */
    public void removeVisibilityListener(VisibilityChangeListener listener) {
        if (mListeners != null) {
            mListeners.remove(listener);
        }
    }

    /**
     * Interface for receiving updates when the view's visibility changes
     */
    public interface VisibilityChangeListener {
        /**
         * Method called when the visibility of this view has changed
         * @param isVisible true if the view is now visible
         */
        void onAggregatedVisibilityChanged(boolean isVisible);
    }
}
+39 −8
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.widget.MediaNotificationView;
import com.android.systemui.Dependency;
import com.android.systemui.statusbar.NotificationMediaManager;
import com.android.systemui.statusbar.TransformableView;
@@ -67,6 +68,7 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi
    private View mSeekBarView;
    private Context mContext;
    private MetricsLogger mMetricsLogger;
    private boolean mIsViewVisible;

    @VisibleForTesting
    protected SeekBar.OnSeekBarChangeListener mSeekListener =
@@ -88,11 +90,33 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi
        }
    };

    MediaNotificationView.VisibilityChangeListener mVisibilityListener =
            new MediaNotificationView.VisibilityChangeListener() {
        @Override
        public void onAggregatedVisibilityChanged(boolean isVisible) {
            mIsViewVisible = isVisible;
            if (isVisible) {
                // Restart timer if we're currently playing and didn't already have one going
                PlaybackState state = mMediaController.getPlaybackState();
                if (state != null && state.getState() == PlaybackState.STATE_PLAYING
                        && mSeekBarTimer == null && mSeekBarView != null
                        && mSeekBarView.getVisibility() != View.GONE) {
                    startTimer();
                }
            } else {
                clearTimer();
            }
        }
    };

    private MediaController.Callback mMediaCallback = new MediaController.Callback() {
        @Override
        public void onSessionDestroyed() {
            clearTimer();
            mMediaController.unregisterCallback(this);
            if (mView instanceof MediaNotificationView) {
                ((MediaNotificationView) mView).removeVisibilityListener(mVisibilityListener);
            }
        }

        @Override
@@ -126,10 +150,16 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi
        mContext = ctx;
        mMediaManager = Dependency.get(NotificationMediaManager.class);
        mMetricsLogger = Dependency.get(MetricsLogger.class);

        if (mView instanceof MediaNotificationView) {
            MediaNotificationView mediaView = (MediaNotificationView) mView;
            mediaView.addVisibilityListener(mVisibilityListener);
        }
    }

    private void resolveViews() {
        mActions = mView.findViewById(com.android.internal.R.id.media_actions);
        mIsViewVisible = mView.isShown();

        final MediaSession.Token token = mRow.getEntry().notification.getNotification().extras
                .getParcelable(Notification.EXTRA_MEDIA_SESSION);
@@ -208,6 +238,7 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi

    private void startTimer() {
        clearTimer();
        if (mIsViewVisible) {
            mSeekBarTimer = new Timer(true /* isDaemon */);
            mSeekBarTimer.schedule(new TimerTask() {
                @Override
@@ -216,10 +247,10 @@ public class NotificationMediaTemplateViewWrapper extends NotificationTemplateVi
                }
            }, 0, PROGRESS_UPDATE_INTERVAL);
        }
    }

    private void clearTimer() {
        if (mSeekBarTimer != null) {
            // TODO: also trigger this when the notification panel is collapsed
            mSeekBarTimer.cancel();
            mSeekBarTimer.purge();
            mSeekBarTimer = null;