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

Commit e51ba45c authored by Winson Chung's avatar Winson Chung Committed by Android (Google) Code Review
Browse files

Merge "Reducing number of threads used in Recents."

parents 1485acc2 ab84fc56
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -26,10 +26,10 @@ import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;
import android.util.MutableBoolean;
import android.view.LayoutInflater;
import android.view.View;
@@ -44,6 +44,7 @@ import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
import com.android.systemui.recents.misc.Console;
import com.android.systemui.recents.misc.ForegroundThread;
import com.android.systemui.recents.misc.SystemServicesProxy;
import com.android.systemui.recents.model.RecentsTaskLoadPlan;
import com.android.systemui.recents.model.RecentsTaskLoader;
@@ -66,6 +67,7 @@ public class RecentsImpl extends IRecentsNonSystemUserCallbacks.Stub
        implements ActivityOptions.OnAnimationStartedListener {

    private final static String TAG = "RecentsImpl";
    private final static boolean DEBUG = false;

    private final static int sMinToggleDelay = 350;

@@ -162,6 +164,9 @@ public class RecentsImpl extends IRecentsNonSystemUserCallbacks.Stub
        Resources res = mContext.getResources();
        LayoutInflater inflater = LayoutInflater.from(mContext);

        // Initialize the static foreground thread
        ForegroundThread.get();

        // Register the task stack listener
        mTaskStackListener = new TaskStackListenerImpl(mHandler);
        SystemServicesProxy ssp = Recents.getSystemServices();
@@ -300,6 +305,8 @@ public class RecentsImpl extends IRecentsNonSystemUserCallbacks.Stub
            loader.preloadTasks(sInstanceLoadPlan, topTaskHome.value);
            TaskStack stack = sInstanceLoadPlan.getTaskStack();
            if (stack.getTaskCount() > 0) {
                // We try and draw the thumbnail transition bitmap in parallel before
                // toggle/show recents is called
                preCacheThumbnailTransitionBitmapAsync(topTask, stack, mDummyStackView);
            }
        }
@@ -458,7 +465,6 @@ public class RecentsImpl extends IRecentsNonSystemUserCallbacks.Stub
     * Preloads the icon of a task.
     */
    private void preloadIcon(ActivityManager.RunningTaskInfo task) {

        // Ensure that we load the running task's icon
        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
        launchOpts.runningTaskId = task.id;
@@ -483,18 +489,19 @@ public class RecentsImpl extends IRecentsNonSystemUserCallbacks.Stub
        final Task toTask = new Task();
        final TaskViewTransform toTransform = getThumbnailTransitionTransform(stack, stackView,
                topTask.id, toTask);
        new AsyncTask<Void, Void, Bitmap>() {
        ForegroundThread.getHandler().post(new Runnable() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return drawThumbnailTransitionBitmap(toTask, toTransform);
            }

            public void run() {
                final Bitmap transitionBitmap = drawThumbnailTransitionBitmap(toTask, toTransform);
                mHandler.post(new Runnable() {
                    @Override
            protected void onPostExecute(Bitmap bitmap) {
                mThumbnailTransitionBitmapCache = bitmap;
                    public void run() {
                        mThumbnailTransitionBitmapCache = transitionBitmap;
                        mThumbnailTransitionBitmapCacheKey = toTask;
                    }
        }.execute();
                });
            }
        });
    }

    /**
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.recents.misc;

import android.os.Handler;
import android.os.HandlerThread;

/**
 * Similar to {@link com.android.internal.os.BackgroundThread}, this is a shared singleton
 * foreground thread for each process.
 */
public final class ForegroundThread extends HandlerThread {
    private static ForegroundThread sInstance;
    private static Handler sHandler;

    private ForegroundThread() {
        super("recents.fg", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    }

    private static void ensureThreadLocked() {
        if (sInstance == null) {
            sInstance = new ForegroundThread();
            sInstance.start();
            sHandler = new Handler(sInstance.getLooper());
        }
    }

    public static ForegroundThread get() {
        synchronized (ForegroundThread.class) {
            ensureThreadLocked();
            return sInstance;
        }
    }

    public static Handler getHandler() {
        synchronized (ForegroundThread.class) {
            ensureThreadLocked();
            return sHandler;
        }
    }
}
+2 −10
Original line number Diff line number Diff line
@@ -46,8 +46,6 @@ import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.SystemProperties;
@@ -61,6 +59,7 @@ import android.view.Display;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityManager;
import com.android.internal.app.AssistUtils;
import com.android.internal.os.BackgroundThread;
import com.android.systemui.Prefs;
import com.android.systemui.R;
import com.android.systemui.recents.Constants;
@@ -83,12 +82,8 @@ public class SystemServicesProxy {
    final static String TAG = "SystemServicesProxy";

    final static BitmapFactory.Options sBitmapOptions;
    final static HandlerThread sBgThread;

    static {
        sBgThread = new HandlerThread("Recents-SystemServicesProxy",
                android.os.Process.THREAD_PRIORITY_BACKGROUND);
        sBgThread.start();
        sBitmapOptions = new BitmapFactory.Options();
        sBitmapOptions.inMutable = true;
    }
@@ -106,8 +101,6 @@ public class SystemServicesProxy {
    String mRecentsPackage;
    ComponentName mAssistComponent;

    Handler mBgThreadHandler;

    Bitmap mDummyIcon;
    int mDummyThumbnailWidth;
    int mDummyThumbnailHeight;
@@ -127,7 +120,6 @@ public class SystemServicesProxy {
        mUm = UserManager.get(context);
        mDisplay = mWm.getDefaultDisplay();
        mRecentsPackage = context.getPackageName();
        mBgThreadHandler = new Handler(sBgThread.getLooper());

        // Get the dummy thumbnail width/heights
        Resources res = context.getResources();
@@ -418,7 +410,7 @@ public class SystemServicesProxy {
        if (Constants.DebugFlags.App.EnableSystemServicesProxy) return;

        // Remove the task.
        mBgThreadHandler.post(new Runnable() {
        BackgroundThread.getHandler().post(new Runnable() {
            @Override
            public void run() {
                mAm.removeTask(taskId);
+4 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.recents.model;
import android.content.Context;
import android.os.UserHandle;
import com.android.internal.content.PackageMonitor;
import com.android.internal.os.BackgroundThread;
import com.android.systemui.recents.events.EventBus;
import com.android.systemui.recents.events.activity.PackagesChangedEvent;

@@ -32,9 +33,9 @@ public class RecentsPackageMonitor extends PackageMonitor {
    public void register(Context context) {
        try {
            // We register for events from all users, but will cross-reference them with
            // packages for the current user and any profiles they have.  Passing null into the
            // call also ensures that events are handled in a background thread.
            register(context, null, UserHandle.ALL, true);
            // packages for the current user and any profiles they have.  Ensure that events are
            // handled in a background thread.
            register(context, BackgroundThread.get().getLooper(), UserHandle.ALL, true);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }