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

Commit 48edb07a authored by Manu Cornet's avatar Manu Cornet
Browse files

2-D recents: fix toggling behavior and orientation changes.

Test: Tested behavior locally on Ryu device.
Bug: 32101881
Change-Id: I6cf1638452b96332d9a8bc376e882e9888f92bc3
parent 34b94b3f
Loading
Loading
Loading
Loading
+84 −11
Original line number Original line Diff line number Diff line
@@ -15,12 +15,13 @@
 */
 */
package com.android.systemui.recents.grid;
package com.android.systemui.recents.grid;


import static android.app.ActivityManager.StackId.INVALID_STACK_ID;

import android.app.Activity;
import android.app.Activity;
import android.content.Intent;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View;
@@ -70,11 +71,12 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On


    private TaskStack mTaskStack;
    private TaskStack mTaskStack;
    private List<Task> mTasks = new ArrayList<>();
    private List<Task> mTasks = new ArrayList<>();
    private List<View> mTaskViews = new ArrayList<>();
    private List<TaskView> mTaskViews = new ArrayList<>();
    private FrameLayout mRecentsView;
    private FrameLayout mRecentsView;
    private TextView mEmptyView;
    private TextView mEmptyView;
    private View mClearAllButton;
    private View mClearAllButton;
    private int mDisplayOrientation = Configuration.ORIENTATION_UNDEFINED;
    private int mLastDisplayOrientation = Configuration.ORIENTATION_UNDEFINED;
    private int mLastDisplayDensity;
    private Rect mDisplayRect = new Rect();
    private Rect mDisplayRect = new Rect();
    private LayoutInflater mInflater;
    private LayoutInflater mInflater;
    private boolean mTouchExplorationEnabled;
    private boolean mTouchExplorationEnabled;
@@ -86,8 +88,10 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On
        SystemServicesProxy ssp = Recents.getSystemServices();
        SystemServicesProxy ssp = Recents.getSystemServices();


        mInflater = LayoutInflater.from(this);
        mInflater = LayoutInflater.from(this);
        mDisplayOrientation = Utilities.getAppConfiguration(this).orientation;
        Configuration appConfiguration = Utilities.getAppConfiguration(this);
        mDisplayRect = ssp.getDisplayRect();
        mDisplayRect = ssp.getDisplayRect();
        mLastDisplayOrientation = appConfiguration.orientation;
        mLastDisplayDensity = appConfiguration.densityDpi;
        mTouchExplorationEnabled = ssp.isTouchExplorationEnabled();
        mTouchExplorationEnabled = ssp.isTouchExplorationEnabled();


        mRecentsView = (FrameLayout) findViewById(R.id.recents_view);
        mRecentsView = (FrameLayout) findViewById(R.id.recents_view);
@@ -122,16 +126,29 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On
        return (TaskView) mInflater.inflate(R.layout.recents_task_view, mRecentsView, false);
        return (TaskView) mInflater.inflate(R.layout.recents_task_view, mRecentsView, false);
    }
    }


    private void clearTaskViews() {
    private void removeTaskViews() {
        for (View taskView : mTaskViews) {
        for (View taskView : mTaskViews) {
            ViewGroup parent = (ViewGroup) taskView.getParent();
            ViewGroup parent = (ViewGroup) taskView.getParent();
            if (parent != null) {
            if (parent != null) {
                parent.removeView(taskView);
                parent.removeView(taskView);
            }
            }
        }
        }
    }

    private void clearTaskViews() {
        removeTaskViews();
        mTaskViews.clear();
        mTaskViews.clear();
    }
    }


    private TaskView getChildViewForTask(Task task) {
        for (TaskView tv : mTaskViews) {
            if (tv.getTask() == task) {
                return tv;
            }
        }
        return null;
    }

    private void updateControlVisibility() {
    private void updateControlVisibility() {
        boolean empty = (mTasks.size() == 0);
        boolean empty = (mTasks.size() == 0);
        mClearAllButton.setVisibility(empty ? View.INVISIBLE : View.VISIBLE);
        mClearAllButton.setVisibility(empty ? View.INVISIBLE : View.VISIBLE);
@@ -170,7 +187,8 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On
        for (int i = 0; i < mTasks.size(); i++) {
        for (int i = 0; i < mTasks.size(); i++) {
            Task task = mTasks.get(i);
            Task task = mTasks.get(i);
            TaskView taskView = createView();
            TaskView taskView = createView();
            taskView.onTaskBound(task, mTouchExplorationEnabled, mDisplayOrientation, mDisplayRect);
            taskView.onTaskBound(task, mTouchExplorationEnabled, mLastDisplayOrientation,
                    mDisplayRect);
            Recents.getTaskLoader().loadTaskData(task);
            Recents.getTaskLoader().loadTaskData(task);
            taskView.setTouchEnabled(true);
            taskView.setTouchEnabled(true);
            // Show dismiss button right away.
            // Show dismiss button right away.
@@ -210,6 +228,22 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On
        EventBus.getDefault().send(new ToggleRecentsEvent());
        EventBus.getDefault().send(new ToggleRecentsEvent());
    }
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Notify of the config change.
        Configuration newDeviceConfiguration = Utilities.getAppConfiguration(this);
        mDisplayRect = Recents.getSystemServices().getDisplayRect();
        mRecentsView.getViewTreeObserver().addOnPreDrawListener(this);
        mRecentsView.requestLayout();
        int numStackTasks = mTaskStack.getStackTaskCount();
        EventBus.getDefault().send(new ConfigurationChangedEvent(false /* fromMultiWindow */,
                mLastDisplayOrientation != newDeviceConfiguration.orientation,
                mLastDisplayDensity != newDeviceConfiguration.densityDpi, numStackTasks > 0));
        mLastDisplayOrientation = newDeviceConfiguration.orientation;
        mLastDisplayDensity = newDeviceConfiguration.densityDpi;
    }

    @Override
    @Override
    public boolean onPreDraw() {
    public boolean onPreDraw() {
        mRecentsView.getViewTreeObserver().removeOnPreDrawListener(this);
        mRecentsView.getViewTreeObserver().removeOnPreDrawListener(this);
@@ -218,6 +252,7 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On


        List<Rect> rects = TaskGridLayoutAlgorithm.getRectsForTaskCount(
        List<Rect> rects = TaskGridLayoutAlgorithm.getRectsForTaskCount(
            mTasks.size(), width, height, false /* allowLineOfThree */, 30 /* padding */);
            mTasks.size(), width, height, false /* allowLineOfThree */, 30 /* padding */);
        removeTaskViews();
        for (int i = 0; i < rects.size(); i++) {
        for (int i = 0; i < rects.size(); i++) {
            Rect rect = rects.get(i);
            Rect rect = rects.get(i);
            View taskView = mTaskViews.get(i);
            View taskView = mTaskViews.get(i);
@@ -235,20 +270,43 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On
        startActivity(startMain);
        startActivity(startMain);
    }
    }


    /** Launches the task that recents was launched from if possible. */
    boolean launchPreviousTask() {
        if (mRecentsView != null) {
            Task task = mTaskStack.getLaunchTarget();
            if (task != null) {
                TaskView taskView = getChildViewForTask(task);
                EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
                        INVALID_STACK_ID, false));
                return true;
            }
        }
        return false;
    }

    /** Dismisses recents back to the launch target task. */
    boolean dismissRecentsToLaunchTargetTaskOrHome() {
        SystemServicesProxy ssp = Recents.getSystemServices();
        if (ssp.isRecentsActivityVisible()) {
            // If we can launch the task that Recents was launched from, do that, otherwise go home.
            if (launchPreviousTask()) return true;
            dismissRecentsToHome();
        }
        return false;
    }

    /**** EventBus events ****/
    /**** EventBus events ****/


    public final void onBusEvent(HideRecentsEvent event) {
    public final void onBusEvent(HideRecentsEvent event) {
        if (event.triggeredFromAltTab) {
        if (event.triggeredFromAltTab) {
            // Do nothing for now.
            dismissRecentsToLaunchTargetTaskOrHome();
        } else if (event.triggeredFromHomeKey) {
        } else if (event.triggeredFromHomeKey) {
            dismissRecentsToHome();
            dismissRecentsToHome();
        }
        }
    }
    }


    public final void onBusEvent(ToggleRecentsEvent event) {
    public final void onBusEvent(ToggleRecentsEvent event) {
        // Always go back home for simplicity for now. If recents is entered from another app, this
        dismissRecentsToLaunchTargetTaskOrHome();
        // code will eventually need to go back to the original app.
        dismissRecentsToHome();
    }
    }


    public final void onBusEvent(DismissTaskViewEvent event) {
    public final void onBusEvent(DismissTaskViewEvent event) {
@@ -306,7 +364,22 @@ public class RecentsGridActivity extends Activity implements ViewTreeObserver.On
    }
    }


    public final void onBusEvent(LaunchNextTaskRequestEvent event) {
    public final void onBusEvent(LaunchNextTaskRequestEvent event) {
        // Always go back home for simplicity for now. Quick switch will be supported soon.
        if (mTaskStack.getTaskCount() > 0) {
            // The task to launch is the second most recent, which is at index 1 given our ordering.
            // If there is only one task, launch that one instead.
            int launchTaskIndex = (mTaskStack.getStackTaskCount() > 1) ? 1 : 0;
            Task launchTask = mTaskStack.getStackTasks().get(launchTaskIndex);
            TaskView launchTaskView = getChildViewForTask(launchTask);
            if (launchTaskView != null) {
                EventBus.getDefault().send(new LaunchTaskEvent(launchTaskView,
                        launchTask, null, INVALID_STACK_ID, false /* screenPinningRequested */));
                MetricsLogger.action(this, MetricsEvent.OVERVIEW_LAUNCH_PREVIOUS_TASK,
                        launchTask.key.getComponent().toString());
                return;
            }
        }
        // We couldn't find a matching task view, or there are no tasks. Just hide recents back
        // to home.
        EventBus.getDefault().send(new HideRecentsEvent(false, true));
        EventBus.getDefault().send(new HideRecentsEvent(false, true));
    }
    }