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

Commit ce5f3cba authored by Craig Mautner's avatar Craig Mautner
Browse files

Refactoring Activity Manager.

Mid-course corrections.

- Simplify ActivityStackSupervisor and ActivityStack constructors to
get passed parameters from ActivityManagerService.
- Store WindowManagerService referecne locally in
ActivityStackSupervisor and ActivityStack.
- Rename getTopStack to getFocusedStack in ActivityStackSupervisor
and ActivityManagerService.
- Move mWaitingActivityLaunched/reportActivityLaunchedLocked and
mWaitingActivityVisible/reportActivityVisibleLocked from ActivityStack
to ActivityStackSupervisor.
- Moved reportResumedActivity to ActivityStackSupervisor.
- Added a Handler to ActivityStackSupervisor. Will populate it on next
CL.

Change-Id: I1bbe5eb737c5cac6b896bc9748f329891e94d00f
parent 3505a9c6
Loading
Loading
Loading
Loading
+12 −12
Original line number Diff line number Diff line
@@ -900,7 +900,7 @@ public final class ActivityManagerService extends ActivityManagerNative
    static ActivityManagerService mSelf;
    static ActivityThread mSystemThread;
    private int mCurrentUserId = 0;
    int mCurrentUserId = 0;
    private UserManagerService mUserManager;
    private final class AppDeathRecipient implements IBinder.DeathRecipient {
@@ -1488,6 +1488,7 @@ public final class ActivityManagerService extends ActivityManagerNative
    public void setWindowManager(WindowManagerService wm) {
        mWindowManager = wm;
        mStackSupervisor.setWindowManager(wm);
        wm.createStack(HOME_STACK_ID, -1, StackBox.TASK_STACK_GOES_OVER, 1.0f);
    }
@@ -1520,7 +1521,6 @@ public final class ActivityManagerService extends ActivityManagerNative
        m.mIntentFirewall = new IntentFirewall(m.new IntentFirewallInterface());
        m.mStackSupervisor = new ActivityStackSupervisor(m, context, thr.mLooper);
        m.mStackSupervisor.init(m.mCurrentUserId);
        m.mBatteryStatsService.publish(context);
        m.mUsageStatsService.publish(context);
@@ -2659,7 +2659,7 @@ public final class ActivityManagerService extends ActivityManagerNative
        synchronized (this) {
            // If this is coming from the currently resumed activity, it is
            // effectively saying that app switches are allowed at this point.
            final ActivityStack stack = getTopStack();
            final ActivityStack stack = getFocusedStack();
            if (stack.mResumedActivity != null &&
                    stack.mResumedActivity.info.applicationInfo.uid == Binder.getCallingUid()) {
                mAppSwitchesAllowedTime = 0;
@@ -7243,7 +7243,7 @@ public final class ActivityManagerService extends ActivityManagerNative
        synchronized(this) {
            final long origId = Binder.clearCallingIdentity();
            try {
                getTopStack().unhandledBackLocked();
                getFocusedStack().unhandledBackLocked();
            } finally {
                Binder.restoreCallingIdentity(origId);
            }
@@ -7649,7 +7649,7 @@ public final class ActivityManagerService extends ActivityManagerNative
        PendingActivityExtras pae;
        Bundle extras = new Bundle();
        synchronized (this) {
            ActivityRecord activity = getTopStack().mResumedActivity;
            ActivityRecord activity = getFocusedStack().mResumedActivity;
            if (activity == null) {
                Slog.w(TAG, "getTopActivityExtras failed: no resumed activity");
                return null;
@@ -7745,7 +7745,7 @@ public final class ActivityManagerService extends ActivityManagerNative
    public boolean isTopActivityImmersive() {
        enforceNotIsolatedCaller("startActivity");
        synchronized (this) {
            ActivityRecord r = getTopStack().topRunningActivityLocked(null);
            ActivityRecord r = getFocusedStack().topRunningActivityLocked(null);
            return (r != null) ? r.immersive : false;
        }
    }
@@ -9651,7 +9651,7 @@ public final class ActivityManagerService extends ActivityManagerNative
        }
        pw.println("  mConfiguration: " + mConfiguration);
        if (dumpAll) {
            pw.println("  mConfigWillChange: " + getTopStack().mConfigWillChange);
            pw.println("  mConfigWillChange: " + getFocusedStack().mConfigWillChange);
            if (mCompatModePackages.getPackages().size() > 0) {
                boolean printed = false;
                for (Map.Entry<String, Integer> entry
@@ -9711,8 +9711,8 @@ public final class ActivityManagerService extends ActivityManagerNative
            pw.print("  mLastPowerCheckUptime=");
                    TimeUtils.formatDuration(mLastPowerCheckUptime, pw);
                    pw.println("");
            pw.println("  mGoingToSleep=" + getTopStack().mGoingToSleep);
            pw.println("  mLaunchingActivity=" + getTopStack().mLaunchingActivity);
            pw.println("  mGoingToSleep=" + getFocusedStack().mGoingToSleep);
            pw.println("  mLaunchingActivity=" + getFocusedStack().mLaunchingActivity);
            pw.println("  mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
            pw.println("  mNumNonHiddenProcs=" + mNumNonHiddenProcs
                    + " mNumHiddenProcs=" + mNumHiddenProcs
@@ -12451,8 +12451,8 @@ public final class ActivityManagerService extends ActivityManagerNative
        return config;
    }
    ActivityStack getTopStack() {
        return mStackSupervisor.getTopStack();
    ActivityStack getFocusedStack() {
        return mStackSupervisor.getFocusedStack();
    }
    public Configuration getConfiguration() {
@@ -12596,7 +12596,7 @@ public final class ActivityManagerService extends ActivityManagerNative
        }
        boolean kept = true;
        final ActivityStack mainStack = mStackSupervisor.getTopStack();
        final ActivityStack mainStack = mStackSupervisor.getFocusedStack();
        if (changes != 0 && starting == null) {
            // If the configuration changed, and the caller is not already
            // in the process of starting an activity, then find the top
+2 −2
Original line number Diff line number Diff line
@@ -803,7 +803,7 @@ final class ActivityRecord {
                    }
                    Log.i(ActivityManagerService.TAG, sb.toString());
                }
                stack.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
                mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
                if (totalTime > 0) {
                    service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
                }
@@ -818,7 +818,7 @@ final class ActivityRecord {
    public void windowsVisible() {
        synchronized(service) {
            final ActivityStack stack = task.stack;
            stack.reportActivityVisibleLocked(this);
            mStackSupervisor.reportActivityVisibleLocked(this);
            if (ActivityManagerService.DEBUG_SWITCH) Log.v(
                    ActivityManagerService.TAG, "windowsVisible(): " + this);
            if (!nowVisible) {
+94 −156

File changed.

Preview size limit exceeded, changes collapsed.

+101 −24
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.AppGlobals;
import android.app.IActivityManager;
import android.app.IApplicationThread;
import android.app.IThumbnailReceiver;
import android.app.PendingIntent;
@@ -50,6 +51,7 @@ import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
@@ -64,6 +66,7 @@ import com.android.internal.app.HeavyWeightSwitcherActivity;
import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
import com.android.server.am.ActivityStack.ActivityState;
import com.android.server.wm.StackBox;
import com.android.server.wm.WindowManagerService;

import java.io.FileDescriptor;
import java.io.IOException;
@@ -86,6 +89,11 @@ public class ActivityStackSupervisor {
    final Context mContext;
    final Looper mLooper;

    final ActivityStackSupervisorHandler mHandler;

    /** Short cut */
    WindowManagerService mWindowManager;

    /** Dismiss the keyguard after the next activity is displayed? */
    private boolean mDismissKeyguardOnNextActivity = false;

@@ -119,6 +127,14 @@ public class ActivityStackSupervisor {
     * whatever operation they are supposed to do. */
    final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();

    /** List of processes waiting to find out about the next visible activity. */
    final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
            new ArrayList<IActivityManager.WaitResult>();

    /** List of processes waiting to find out about the next launched activity. */
    final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
            new ArrayList<IActivityManager.WaitResult>();

    /** List of activities that are ready to be stopped, but waiting for the next activity to
     * settle down before doing so. */
    final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
@@ -132,21 +148,23 @@ public class ActivityStackSupervisor {
        mService = service;
        mContext = context;
        mLooper = looper;
        mHandler = new ActivityStackSupervisorHandler(looper);
    }

    void init(int userId) {
        mHomeStack = new ActivityStack(mService, mContext, mLooper, HOME_STACK_ID, this, userId);
    void setWindowManager(WindowManagerService wm) {
        mWindowManager = wm;
        mHomeStack = new ActivityStack(mService, mContext, mLooper, HOME_STACK_ID);
        mStacks.add(mHomeStack);
    }

    void dismissKeyguard() {
        if (mDismissKeyguardOnNextActivity) {
            mDismissKeyguardOnNextActivity = false;
            mService.mWindowManager.dismissKeyguard();
            mWindowManager.dismissKeyguard();
        }
    }

    ActivityStack getTopStack() {
    ActivityStack getFocusedStack() {
        if (mFocusedStack == null) {
            return mHomeStack;
        }
@@ -173,8 +191,12 @@ public class ActivityStackSupervisor {
        }
    }

    boolean isFocusedStack(ActivityStack stack) {
        return getFocusedStack() == stack;
    }

    boolean isFrontStack(ActivityStack stack) {
        return !(stack.isHomeStack() ^ getTopStack().isHomeStack());
        return !(stack.isHomeStack() ^ getFocusedStack().isHomeStack());
    }

    boolean homeIsInFront() {
@@ -256,7 +278,7 @@ public class ActivityStackSupervisor {
            if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack " + stack);
            mStacks.remove(stack);
            final int stackId = stack.mStackId;
            final int nextStackId = mService.mWindowManager.removeStack(stackId);
            final int nextStackId = mWindowManager.removeStack(stackId);
            // TODO: Perhaps we need to let the ActivityManager determine the next focus...
            if (mFocusedStack.mStackId == stackId) {
                mFocusedStack = nextStackId == HOME_STACK_ID ? null : getStack(nextStackId);
@@ -265,7 +287,10 @@ public class ActivityStackSupervisor {
    }

    ActivityRecord resumedAppLocked() {
        ActivityStack stack = getTopStack();
        ActivityStack stack = getFocusedStack();
        if (stack == null) {
            return null;
        }
        ActivityRecord resumedActivity = stack.mResumedActivity;
        if (resumedActivity == null || resumedActivity.app == null) {
            resumedActivity = stack.mPausingActivity;
@@ -361,6 +386,34 @@ public class ActivityStackSupervisor {
        return true;
    }

    void reportActivityVisibleLocked(ActivityRecord r) {
        for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
            WaitResult w = mWaitingActivityVisible.get(i);
            w.timeout = false;
            if (r != null) {
                w.who = new ComponentName(r.info.packageName, r.info.name);
            }
            w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
            w.thisTime = w.totalTime;
        }
        mService.notifyAll();
        dismissKeyguard();
    }

    void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
            long thisTime, long totalTime) {
        for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
            WaitResult w = mWaitingActivityLaunched.get(i);
            w.timeout = timeout;
            if (r != null) {
                w.who = new ComponentName(r.info.packageName, r.info.name);
            }
            w.thisTime = thisTime;
            w.totalTime = totalTime;
        }
        mService.notifyAll();
    }

    ActivityRecord topRunningActivityLocked() {
        ActivityRecord r = null;
        if (mFocusedStack != null) {
@@ -478,7 +531,7 @@ public class ActivityStackSupervisor {
                callingPid = callingUid = -1;
            }

            final ActivityStack stack = getTopStack();
            final ActivityStack stack = getFocusedStack();
            stack.mConfigWillChange = config != null
                    && mService.mConfiguration.diff(config) != 0;
            if (DEBUG_CONFIGURATION) Slog.v(TAG,
@@ -578,7 +631,7 @@ public class ActivityStackSupervisor {
            if (outResult != null) {
                outResult.result = res;
                if (res == ActivityManager.START_SUCCESS) {
                    stack.mWaitingActivityLaunched.add(outResult);
                    mWaitingActivityLaunched.add(outResult);
                    do {
                        try {
                            mService.wait();
@@ -594,7 +647,7 @@ public class ActivityStackSupervisor {
                        outResult.thisTime = 0;
                    } else {
                        outResult.thisTime = SystemClock.uptimeMillis();
                        stack.mWaitingActivityVisible.add(outResult);
                        mWaitingActivityVisible.add(outResult);
                        do {
                            try {
                                mService.wait();
@@ -694,7 +747,7 @@ public class ActivityStackSupervisor {
            throws RemoteException {

        r.startFreezingScreenLocked(app, 0);
        mService.mWindowManager.setAppVisibility(r.appToken, true);
        mWindowManager.setAppVisibility(r.appToken, true);

        // schedule launch ticks to collect information about slow apps.
        r.startLaunchTickingLocked();
@@ -706,7 +759,7 @@ public class ActivityStackSupervisor {
        // because the activity is not currently running so we are
        // just restarting it anyway.
        if (checkConfig) {
            Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
            Configuration config = mWindowManager.updateOrientationFromAppTokens(
                    mService.mConfiguration,
                    r.mayFreezeScreenLocked(app) ? r.appToken : null);
            mService.updateConfigurationLocked(config, r, false, false);
@@ -1028,7 +1081,7 @@ public class ActivityStackSupervisor {
            outActivity[0] = r;
        }

        final ActivityStack stack = getTopStack();
        final ActivityStack stack = getFocusedStack();
        if (stack.mResumedActivity == null
                || stack.mResumedActivity.info.applicationInfo.uid != callingUid) {
            if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
@@ -1123,7 +1176,7 @@ public class ActivityStackSupervisor {
        if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
            ActivityRecord checkedCaller = sourceRecord;
            if (checkedCaller == null) {
                checkedCaller = getTopStack().topRunningNonDelayedActivityLocked(notTop);
                checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
            }
            if (!checkedCaller.realActivity.equals(r.realActivity)) {
                // Caller is not the same as launcher, so always needed.
@@ -1363,7 +1416,7 @@ public class ActivityStackSupervisor {
            // If the activity being launched is the same as the one currently
            // at the top, then we need to check if it should only be launched
            // once.
            ActivityStack topStack = getTopStack();
            ActivityStack topStack = getFocusedStack();
            ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
            if (top != null && r.resultTo == null) {
                if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
@@ -1605,8 +1658,7 @@ public class ActivityStackSupervisor {
                    break;
                }
            }
            mStacks.add(new ActivityStack(mService, mContext, mLooper, mLastStackId, this,
                    mCurrentUser));
            mStacks.add(new ActivityStack(mService, mContext, mLooper, mLastStackId));
            return mLastStackId;
        }
    }
@@ -1673,17 +1725,29 @@ public class ActivityStackSupervisor {
    }

    void comeOutOfSleepIfNeededLocked() {
        final boolean homeIsBack = !homeIsInFront();
        final int numStacks = mStacks.size();
        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
            final ActivityStack stack = mStacks.get(stackNdx);
            if (stack.isHomeStack() ^ homeIsBack) {
            stack.awakeFromSleepingLocked();
            if (isFrontStack(stack)) {
                stack.resumeTopActivityLocked(null);
            }
        }
    }

    boolean reportResumedActivityLocked(ActivityRecord r) {
        final ActivityStack stack = r.task.stack;
        if (isFrontStack(stack)) {
            mService.reportResumedActivityLocked(r);
            mService.setFocusedActivityLocked(r);
        }
        if (allResumedActivitiesComplete()) {
            ensureActivitiesVisibleLocked(null, 0);
            mWindowManager.executeAppTransition();
            return true;
        }
        return false;
    }

    void handleAppCrashLocked(ProcessRecord app) {
        final int numStacks = mStacks.size();
        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
@@ -1742,7 +1806,7 @@ public class ActivityStackSupervisor {
                    // normal flow and hide it once we determine that it is
                    // hidden by the activities in front of it.
                    if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
                    mService.mWindowManager.setAppVisibility(s.appToken, false);
                    mWindowManager.setAppVisibility(s.appToken, false);
                }
            }
            if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
@@ -1766,7 +1830,7 @@ public class ActivityStackSupervisor {
    }

    ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
        return getTopStack().getDumpActivitiesLocked(name);
        return getFocusedStack().getDumpActivitiesLocked(name);
    }

    boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
@@ -1893,4 +1957,17 @@ public class ActivityStackSupervisor {
            }
        }
    }

    private final class ActivityStackSupervisorHandler extends Handler {
        public ActivityStackSupervisorHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                
            }
        }
    }
}
+5 −5
Original line number Diff line number Diff line
@@ -165,7 +165,7 @@ public class CompatModePackages {
    }

    public boolean getFrontActivityAskCompatModeLocked() {
        ActivityRecord r = mService.getTopStack().topRunningActivityLocked(null);
        ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
        if (r == null) {
            return false;
        }
@@ -177,7 +177,7 @@ public class CompatModePackages {
    }

    public void setFrontActivityAskCompatModeLocked(boolean ask) {
        ActivityRecord r = mService.getTopStack().topRunningActivityLocked(null);
        ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
        if (r != null) {
            setPackageAskCompatModeLocked(r.packageName, ask);
        }
@@ -199,7 +199,7 @@ public class CompatModePackages {
    }

    public int getFrontActivityScreenCompatModeLocked() {
        ActivityRecord r = mService.getTopStack().topRunningActivityLocked(null);
        ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
        if (r == null) {
            return ActivityManager.COMPAT_MODE_UNKNOWN;
        }
@@ -207,7 +207,7 @@ public class CompatModePackages {
    }

    public void setFrontActivityScreenCompatModeLocked(int mode) {
        ActivityRecord r = mService.getTopStack().topRunningActivityLocked(null);
        ActivityRecord r = mService.getFocusedStack().topRunningActivityLocked(null);
        if (r == null) {
            Slog.w(TAG, "setFrontActivityScreenCompatMode failed: no top activity");
            return;
@@ -294,7 +294,7 @@ public class CompatModePackages {
            Message msg = mHandler.obtainMessage(MSG_WRITE);
            mHandler.sendMessageDelayed(msg, 10000);

            final ActivityStack stack = mService.getTopStack();
            final ActivityStack stack = mService.getFocusedStack();
            ActivityRecord starting = stack.restartPackage(packageName);

            // Tell all processes that loaded this package about the change.