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

Commit 0f5120f3 authored by Winson Chung's avatar Winson Chung
Browse files

Remove events bus code from task loading code.

- Remove extra class RecentsPackageMonitor, just have RecentsActivity drive
  the event itself.

Bug: 67510855
Test: Remove package while Recents is open, ensure activity infos are
      trimmed

Change-Id: Id963434ff868e607fe90ef770e4ba2070905cfba
parent 2db35570
Loading
Loading
Loading
Loading
+27 −7
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.systemui.recents;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.TaskStackBuilder;
import android.app.WallpaperManager;
@@ -29,10 +28,10 @@ import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
@@ -42,6 +41,7 @@ import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.android.internal.colorextraction.ColorExtractor;
import com.android.internal.content.PackageMonitor;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.keyguard.LatencyTracker;
@@ -63,6 +63,7 @@ import com.android.systemui.recents.events.activity.HideRecentsEvent;
import com.android.systemui.recents.events.activity.LaunchTaskFailedEvent;
import com.android.systemui.recents.events.activity.LaunchTaskSucceededEvent;
import com.android.systemui.recents.events.activity.MultiWindowStateChangedEvent;
import com.android.systemui.recents.events.activity.PackagesChangedEvent;
import com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
import com.android.systemui.recents.events.component.ActivityUnpinnedEvent;
@@ -85,7 +86,6 @@ import com.android.systemui.recents.events.ui.focus.NavigateTaskViewEvent;
import com.android.systemui.recents.events.ui.focus.NavigateTaskViewEvent.Direction;
import com.android.systemui.recents.misc.SystemServicesProxy;
import com.android.systemui.recents.misc.Utilities;
import com.android.systemui.recents.model.RecentsPackageMonitor;
import com.android.systemui.recents.model.RecentsTaskLoadPlan;
import com.android.systemui.recents.model.RecentsTaskLoader;
import com.android.systemui.recents.model.Task;
@@ -96,7 +96,6 @@ import com.android.systemui.statusbar.phone.StatusBar;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;

/**
 * The main Recents activity that is started from RecentsComponent.
@@ -110,7 +109,23 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD
    public final static int EVENT_BUS_PRIORITY = Recents.EVENT_BUS_PRIORITY + 1;
    public final static int INCOMPATIBLE_APP_ALPHA_DURATION = 150;

    private RecentsPackageMonitor mPackageMonitor;
    private PackageMonitor mPackageMonitor = new PackageMonitor() {
            @Override
            public void onPackageRemoved(String packageName, int uid) {
                RecentsActivity.this.onPackageChanged(packageName, getChangingUserId());
            }

            @Override
            public boolean onPackageChanged(String packageName, int uid, String[] components) {
                RecentsActivity.this.onPackageChanged(packageName, getChangingUserId());
                return true;
            }

            @Override
            public void onPackageModified(String packageName) {
                RecentsActivity.this.onPackageChanged(packageName, getChangingUserId());
            }
        };
    private Handler mHandler = new Handler();
    private long mLastTabKeyEventTime;
    private boolean mFinishedOnStartup;
@@ -300,8 +315,8 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD
        EventBus.getDefault().register(this, EVENT_BUS_PRIORITY);

        // Initialize the package monitor
        mPackageMonitor = new RecentsPackageMonitor();
        mPackageMonitor.register(this);
        mPackageMonitor.register(this, Looper.getMainLooper(), UserHandle.ALL,
                true /* externalStorage */);

        // Select theme based on wallpaper colors
        mColorExtractor = Dependency.get(SysuiColorExtractor.class);
@@ -835,6 +850,11 @@ public class RecentsActivity extends Activity implements ViewTreeObserver.OnPreD
        return true;
    }

    public void onPackageChanged(String packageName, int userId) {
        Recents.getTaskLoader().onPackageChanged(packageName);
        EventBus.getDefault().send(new PackagesChangedEvent(packageName, userId));
    }

    @Override
    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
        super.dump(prefix, fd, writer, args);
+3 −5
Original line number Diff line number Diff line
@@ -17,22 +17,20 @@
package com.android.systemui.recents.events.activity;

import com.android.systemui.recents.events.EventBus;
import com.android.systemui.recents.model.RecentsPackageMonitor;
import com.android.systemui.recents.views.TaskStackView;
import com.android.systemui.recents.RecentsActivity;

/**
 * This event is sent by {@link RecentsPackageMonitor} when a package on the the system changes.
 * This event is sent by {@link RecentsActivity} when a package on the the system changes.
 * {@link TaskStackView}s listen for this event, and remove the tasks associated with the removed
 * packages.
 */
public class PackagesChangedEvent extends EventBus.Event {

    public final RecentsPackageMonitor monitor;
    public final String packageName;
    public final int userId;

    public PackagesChangedEvent(RecentsPackageMonitor monitor, String packageName, int userId) {
        this.monitor = monitor;
    public PackagesChangedEvent(String packageName, int userId) {
        this.packageName = packageName;
        this.userId = userId;
    }
+0 −74
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 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.model;

import android.content.Context;
import android.os.UserHandle;

import com.android.internal.content.PackageMonitor;
import com.android.systemui.recents.events.EventBus;
import com.android.systemui.recents.events.activity.PackagesChangedEvent;
import com.android.systemui.recents.misc.ForegroundThread;

/**
 * The package monitor listens for changes from PackageManager to update the contents of the
 * Recents list.
 */
public class RecentsPackageMonitor extends PackageMonitor {

    /** Registers the broadcast receivers with the specified callbacks. */
    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.  Ensure that events are
            // handled in a background thread.
            register(context, ForegroundThread.get().getLooper(), UserHandle.ALL, true);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /** Unregisters the broadcast receivers. */
    @Override
    public void unregister() {
        try {
            super.unregister();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPackageRemoved(String packageName, int uid) {
        // Notify callbacks on the main thread that a package has changed
        final int eventUserId = getChangingUserId();
        EventBus.getDefault().post(new PackagesChangedEvent(this, packageName, eventUserId));
    }

    @Override
    public boolean onPackageChanged(String packageName, int uid, String[] components) {
        onPackageModified(packageName);
        return true;
    }

    @Override
    public void onPackageModified(String packageName) {
        // Notify callbacks on the main thread that a package has changed
        final int eventUserId = getChangingUserId();
        EventBus.getDefault().post(new PackagesChangedEvent(this, packageName, eventUserId));
    }
}
+16 −18
Original line number Diff line number Diff line
@@ -256,7 +256,7 @@ public class RecentsTaskLoader {
    // This activity info LruCache is useful because it can be expensive to retrieve ActivityInfos
    // for many tasks, which we use to get the activity labels and icons.  Unlike the other caches
    // below, this is per-package so we can't invalidate the items in the cache based on the last
    // active time.  Instead, we rely on the RecentsPackageMonitor to keep us informed whenever a
    // active time.  Instead, we rely on the PackageMonitor to keep us informed whenever a
    // package in the cache has been updated, so that we may remove it.
    private final LruCache<ComponentName, ActivityInfo> mActivityInfoCache;
    private final TaskKeyLruCache<Drawable> mIconCache;
@@ -435,6 +435,21 @@ public class RecentsTaskLoader {
        }
    }

    public void onPackageChanged(String packageName) {
        // Remove all the cached activity infos for this package.  The other caches do not need to
        // be pruned at this time, as the TaskKey expiration checks will flush them next time their
        // cached contents are requested
        Map<ComponentName, ActivityInfo> activityInfoCache = mActivityInfoCache.snapshot();
        for (ComponentName cn : activityInfoCache.keySet()) {
            if (cn.getPackageName().equals(packageName)) {
                if (DEBUG) {
                    Log.d(TAG, "Removing activity info from cache: " + cn);
                }
                mActivityInfoCache.remove(cn);
            }
        }
    }

    /**
     * Returns the cached task label if the task key is not expired, updating the cache if it is.
     */
@@ -623,23 +638,6 @@ public class RecentsTaskLoader {
        mLoadQueue.clearTasks();
    }

    /**** Event Bus Events ****/

    public final void onBusEvent(PackagesChangedEvent event) {
        // Remove all the cached activity infos for this package.  The other caches do not need to
        // be pruned at this time, as the TaskKey expiration checks will flush them next time their
        // cached contents are requested
        Map<ComponentName, ActivityInfo> activityInfoCache = mActivityInfoCache.snapshot();
        for (ComponentName cn : activityInfoCache.keySet()) {
            if (cn.getPackageName().equals(event.packageName)) {
                if (DEBUG) {
                    Log.d(TAG, "Removing activity info from cache: " + cn);
                }
                mActivityInfoCache.remove(cn);
            }
        }
    }

    public synchronized void dump(String prefix, PrintWriter writer) {
        String innerPrefix = prefix + "  ";