Migrated away from view detaching as a signal

Previously the seekbar update stopped pretty much
immediately since the Lifecycle was set to destroyed
whenever detatched.
We're now listening whenever a view exists.
Similarly, were we making the view inactive whenever
reattaching, which happens constantly now

Test: add any player, observe progress bar updating
Bug: 154137987
Change-Id: I5bac0822ceb282999db35c797d7355127b1daf31
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
index 6621ca1..5a5cd6d 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
@@ -16,8 +16,6 @@
 
 package com.android.systemui.media;
 
-import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle;
-
 import android.app.PendingIntent;
 import android.content.ComponentName;
 import android.content.Context;
@@ -41,7 +39,6 @@
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.View.OnAttachStateChangeListener;
 import android.view.ViewGroup;
 import android.widget.ImageButton;
 import android.widget.ImageView;
@@ -139,17 +136,6 @@
         }
     };
 
-    private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() {
-        @Override
-        public void onViewAttachedToWindow(View unused) {
-            makeActive();
-        }
-        @Override
-        public void onViewDetachedFromWindow(View unused) {
-            makeInactive();
-        }
-    };
-
     private final LocalMediaManager.DeviceCallback mDeviceCallback =
             new LocalMediaManager.DeviceCallback() {
         @Override
@@ -191,29 +177,25 @@
         mMediaNotifView = (MotionLayout) inflater.inflate(R.layout.qs_media_panel, parent, false);
         mBackground = mMediaNotifView.findViewById(R.id.media_background);
         mKeyFrames = mMediaNotifView.getDefinedTransitions().get(0).getKeyFrameList();
-        // TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views
-        // mStateListener shouldn't need to be unregistered since this object shares the same
-        // lifecycle with the inflated view. It would be better, however, if this controller used an
-        // attach/detach of views instead of inflating them in the constructor, which would allow
-        // mStateListener to be unregistered in detach.
-        mMediaNotifView.addOnAttachStateChangeListener(mStateListener);
         mLocalMediaManager = routeManager;
         mForegroundExecutor = foregroundExecutor;
         mBackgroundExecutor = backgroundExecutor;
         mActivityStarter = activityStarter;
         mSeekBarViewModel = new SeekBarViewModel(backgroundExecutor);
         mSeekBarObserver = new SeekBarObserver(getView());
-        // Can't use the viewAttachLifecycle of media player because remove/add is used to adjust
-        // priority of players. As soon as it is removed, the lifecycle will end and the seek bar
-        // will stop updating. So, use the lifecycle of the parent instead.
-        // TODO: this parent is also detached, need to fix that
-        mSeekBarViewModel.getProgress().observe(viewAttachLifecycle(parent), mSeekBarObserver);
+        // TODO: we should pause this whenever the screen is off / panel is collapsed etc.
+        mSeekBarViewModel.getProgress().observeForever(mSeekBarObserver);
         SeekBar bar = getView().findViewById(R.id.media_progress_bar);
         bar.setOnSeekBarChangeListener(mSeekBarViewModel.getSeekBarListener());
         bar.setOnTouchListener(mSeekBarViewModel.getSeekBarTouchListener());
         loadDimens();
     }
 
+    public void onDestroy() {
+        mSeekBarViewModel.getProgress().removeObserver(mSeekBarObserver);
+        makeInactive();
+    }
+
     private void loadDimens() {
         mAlbumArtRadius = mContext.getResources().getDimensionPixelSize(
                 Utils.getThemeAttr(mContext, android.R.attr.dialogCornerRadius));