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

Commit 36ad06c2 authored by Winson Chung's avatar Winson Chung Committed by android-build-merger
Browse files

Merge "Ensure that we timeout waiting when starting the PiP menu activity"...

Merge "Ensure that we timeout waiting when starting the PiP menu activity" into oc-dr1-dev am: e3184764
am: 9d35a1ee

Change-Id: I87f0758cccc701d9da5c9eb753105f407bb2e461
parents 76f63b7b 9d35a1ee
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -92,7 +92,7 @@ public class PipManager implements BasePipManager {


            ComponentName topPipActivity = PipUtils.getTopPinnedActivity(mContext,
            ComponentName topPipActivity = PipUtils.getTopPinnedActivity(mContext,
                    mActivityManager);
                    mActivityManager);
            mMenuController.hideMenu();
            mMenuController.onActivityUnpinned(topPipActivity);
            mTouchHandler.onActivityUnpinned(topPipActivity);
            mTouchHandler.onActivityUnpinned(topPipActivity);
            mNotificationController.onActivityUnpinned(topPipActivity);
            mNotificationController.onActivityUnpinned(topPipActivity);


+55 −8
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import android.app.ActivityManager.StackInfo;
import android.app.ActivityOptions;
import android.app.ActivityOptions;
import android.app.IActivityManager;
import android.app.IActivityManager;
import android.app.RemoteAction;
import android.app.RemoteAction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.pm.ParceledListSlice;
import android.content.pm.ParceledListSlice;
@@ -32,6 +33,7 @@ import android.os.Handler;
import android.os.Message;
import android.os.Message;
import android.os.Messenger;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserHandle;
import android.util.Log;
import android.util.Log;
import android.view.IWindowManager;
import android.view.IWindowManager;
@@ -77,6 +79,9 @@ public class PipMenuActivityController {
    public static final int MENU_STATE_CLOSE = 1;
    public static final int MENU_STATE_CLOSE = 1;
    public static final int MENU_STATE_FULL = 2;
    public static final int MENU_STATE_FULL = 2;


    // The duration to wait before we consider the start activity as having timed out
    private static final long START_ACTIVITY_REQUEST_TIMEOUT_MS = 300;

    /**
    /**
     * A listener interface to receive notification on changes in PIP.
     * A listener interface to receive notification on changes in PIP.
     */
     */
@@ -125,8 +130,9 @@ public class PipMenuActivityController {


    private ReferenceCountedTrigger mOnAttachDecrementTrigger;
    private ReferenceCountedTrigger mOnAttachDecrementTrigger;
    private boolean mStartActivityRequested;
    private boolean mStartActivityRequested;
    private long mStartActivityRequestedTime;
    private Messenger mToActivityMessenger;
    private Messenger mToActivityMessenger;
    private Messenger mMessenger = new Messenger(new Handler() {
    private Handler mHandler = new Handler() {
        @Override
        @Override
        public void handleMessage(Message msg) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            switch (msg.what) {
@@ -161,7 +167,7 @@ public class PipMenuActivityController {
                }
                }
                case MESSAGE_UPDATE_ACTIVITY_CALLBACK: {
                case MESSAGE_UPDATE_ACTIVITY_CALLBACK: {
                    mToActivityMessenger = msg.replyTo;
                    mToActivityMessenger = msg.replyTo;
                    mStartActivityRequested = false;
                    setStartActivityRequested(false);
                    if (mOnAttachDecrementTrigger != null) {
                    if (mOnAttachDecrementTrigger != null) {
                        mOnAttachDecrementTrigger.decrement();
                        mOnAttachDecrementTrigger.decrement();
                        mOnAttachDecrementTrigger = null;
                        mOnAttachDecrementTrigger = null;
@@ -174,7 +180,17 @@ public class PipMenuActivityController {
                }
                }
            }
            }
        }
        }
    });
    };
    private Messenger mMessenger = new Messenger(mHandler);

    private Runnable mStartActivityRequestedTimeoutRunnable = () -> {
        setStartActivityRequested(false);
        if (mOnAttachDecrementTrigger != null) {
            mOnAttachDecrementTrigger.decrement();
            mOnAttachDecrementTrigger = null;
        }
        Log.e(TAG, "Expected start menu activity request timed out");
    };


    private ActionListener mMediaActionListener = new ActionListener() {
    private ActionListener mMediaActionListener = new ActionListener() {
        @Override
        @Override
@@ -202,6 +218,11 @@ public class PipMenuActivityController {
        }
        }
    }
    }


    public void onActivityUnpinned(ComponentName topPipActivity) {
        hideMenu();
        setStartActivityRequested(false);
    }

    public void onPinnedStackAnimationEnded() {
    public void onPinnedStackAnimationEnded() {
        // Note: Only active menu activities care about this event
        // Note: Only active menu activities care about this event
        if (mToActivityMessenger != null) {
        if (mToActivityMessenger != null) {
@@ -243,7 +264,9 @@ public class PipMenuActivityController {
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                Log.e(TAG, "Could not notify menu to update dismiss fraction", e);
                Log.e(TAG, "Could not notify menu to update dismiss fraction", e);
            }
            }
        } else if (!mStartActivityRequested) {
        } else if (!mStartActivityRequested || isStartActivityRequestedElapsed()) {
            // If we haven't requested the start activity, or if it previously took too long to
            // start, then start it
            startMenuActivity(MENU_STATE_NONE, null /* stackBounds */,
            startMenuActivity(MENU_STATE_NONE, null /* stackBounds */,
                    null /* movementBounds */, false /* allowMenuTimeout */);
                    null /* movementBounds */, false /* allowMenuTimeout */);
        }
        }
@@ -273,7 +296,9 @@ public class PipMenuActivityController {
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                Log.e(TAG, "Could not notify menu to show", e);
                Log.e(TAG, "Could not notify menu to show", e);
            }
            }
        } else if (!mStartActivityRequested) {
        } else if (!mStartActivityRequested || isStartActivityRequestedElapsed()) {
            // If we haven't requested the start activity, or if it previously took too long to
            // start, then start it
            startMenuActivity(menuState, stackBounds, movementBounds, allowMenuTimeout);
            startMenuActivity(menuState, stackBounds, movementBounds, allowMenuTimeout);
        }
        }
    }
    }
@@ -368,12 +393,12 @@ public class PipMenuActivityController {
                        pinnedStackInfo.taskIds[pinnedStackInfo.taskIds.length - 1]);
                        pinnedStackInfo.taskIds[pinnedStackInfo.taskIds.length - 1]);
                options.setTaskOverlay(true, true /* canResume */);
                options.setTaskOverlay(true, true /* canResume */);
                mContext.startActivityAsUser(intent, options.toBundle(), UserHandle.CURRENT);
                mContext.startActivityAsUser(intent, options.toBundle(), UserHandle.CURRENT);
                mStartActivityRequested = true;
                setStartActivityRequested(true);
            } else {
            } else {
                Log.e(TAG, "No PIP tasks found");
                Log.e(TAG, "No PIP tasks found");
            }
            }
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            mStartActivityRequested = false;
            setStartActivityRequested(false);
            Log.e(TAG, "Error showing PIP menu activity", e);
            Log.e(TAG, "Error showing PIP menu activity", e);
        }
        }
    }
    }
@@ -415,6 +440,14 @@ public class PipMenuActivityController {
        return actions != null && actions.getList().size() > 0;
        return actions != null && actions.getList().size() > 0;
    }
    }


    /**
     * @return whether the time of the activity request has exceeded the timeout.
     */
    private boolean isStartActivityRequestedElapsed() {
        return (SystemClock.uptimeMillis() - mStartActivityRequestedTime)
                >= START_ACTIVITY_REQUEST_TIMEOUT_MS;
    }

    /**
    /**
     * Handles changes in menu visibility.
     * Handles changes in menu visibility.
     */
     */
@@ -443,12 +476,24 @@ public class PipMenuActivityController {
        mMenuState = menuState;
        mMenuState = menuState;
    }
    }


    private void setStartActivityRequested(boolean requested) {
        mHandler.removeCallbacks(mStartActivityRequestedTimeoutRunnable);
        mStartActivityRequested = requested;
        mStartActivityRequestedTime = requested ? SystemClock.uptimeMillis() : 0;
    }

    public final void onBusEvent(HidePipMenuEvent event) {
    public final void onBusEvent(HidePipMenuEvent event) {
        if (mStartActivityRequested) {
        if (mStartActivityRequested) {
            // If the menu has been start-requested, but not actually started, then we defer the
            // If the menu has been start-requested, but not actually started, then we defer the
            // trigger callback until the menu has started and called back to the controller
            // trigger callback until the menu has started and called back to the controller.
            mOnAttachDecrementTrigger = event.getAnimationTrigger();
            mOnAttachDecrementTrigger = event.getAnimationTrigger();
            mOnAttachDecrementTrigger.increment();
            mOnAttachDecrementTrigger.increment();

            // Fallback for b/63752800, we have started the PipMenuActivity but it has not made any
            // callbacks. Don't continue to wait for the menu to show past some timeout.
            mHandler.removeCallbacks(mStartActivityRequestedTimeoutRunnable);
            mHandler.postDelayed(mStartActivityRequestedTimeoutRunnable,
                    START_ACTIVITY_REQUEST_TIMEOUT_MS);
        }
        }
    }
    }


@@ -458,5 +503,7 @@ public class PipMenuActivityController {
        pw.println(innerPrefix + "mMenuState=" + mMenuState);
        pw.println(innerPrefix + "mMenuState=" + mMenuState);
        pw.println(innerPrefix + "mToActivityMessenger=" + mToActivityMessenger);
        pw.println(innerPrefix + "mToActivityMessenger=" + mToActivityMessenger);
        pw.println(innerPrefix + "mListeners=" + mListeners.size());
        pw.println(innerPrefix + "mListeners=" + mListeners.size());
        pw.println(innerPrefix + "mStartActivityRequested=" + mStartActivityRequested);
        pw.println(innerPrefix + "mStartActivityRequestedTime=" + mStartActivityRequestedTime);
    }
    }
}
}