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

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

Fix ActivityView lifecycle

Major changes to maintain the VirtualDisplay across repeated
attach/detach cycles of an ActivityView. This keeps the activities
and VirtualDisplays in the ActivityView from getting into bad states.

Fixes bug 14107002.

Change-Id: Idc2aaf85ac496eab0eeb436736cb10a2020040e8
parent 72eec7f6
Loading
Loading
Loading
Loading
+95 −75
Original line number Diff line number Diff line
@@ -33,15 +33,18 @@ import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import dalvik.system.CloseGuard;

import java.lang.ref.WeakReference;

/** @hide */
public class ActivityView extends ViewGroup {
    private final String TAG = "ActivityView";
    private final boolean DEBUG = false;
    private static final String TAG = "ActivityView";
    private static final boolean DEBUG = false;

    DisplayMetrics mMetrics;
    private final TextureView mTextureView;
    private IActivityContainer mActivityContainer;
    private Activity mActivity;
@@ -53,6 +56,8 @@ public class ActivityView extends ViewGroup {
    IIntentSender mQueuedPendingIntent;
    Intent mQueuedIntent;

    private final CloseGuard mGuard = CloseGuard.get();

    public ActivityView(Context context) {
        this(context, null);
    }
@@ -75,60 +80,30 @@ public class ActivityView extends ViewGroup {
            throw new IllegalStateException("The ActivityView's Context is not an Activity.");
        }

        mTextureView = new TextureView(context);
        mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener());
        addView(mTextureView);
        if (DEBUG) Log.v(TAG, "ctor()");
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mTextureView.layout(0, 0, r - l, b - t);
    }

    @Override
    protected void onAttachedToWindow() {
        if (DEBUG) Log.v(TAG, "onAttachedToWindow()");
        super.onAttachedToWindow();
        try {
            final IBinder token = mActivity.getActivityToken();
            mActivityContainer = ActivityManagerNative.getDefault().createActivityContainer(token,
                      new ActivityContainerCallback());
            mActivityContainer = ActivityManagerNative.getDefault().createActivityContainer(
                    mActivity.getActivityToken(), new ActivityContainerCallback(this));
        } catch (RemoteException e) {
            throw new IllegalStateException("ActivityView: Unable to create ActivityContainer. "
                    + e);
        }

        attachToSurfaceWhenReady();
    }
        mTextureView = new TextureView(context);
        mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener());
        addView(mTextureView);

    @Override
    protected void onDetachedFromWindow() {
        if (DEBUG) Log.v(TAG, "onDetachedFromWindow(): mActivityContainer=" + mActivityContainer);
        super.onDetachedFromWindow();
        if (mActivityContainer != null) {
            detach();
            try {
                ActivityManagerNative.getDefault().deleteActivityContainer(mActivityContainer);
            } catch (RemoteException e) {
            }
            mActivityContainer = null;
        }
        WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
        mMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(mMetrics);

        mGuard.open("release");

        if (DEBUG) Log.v(TAG, "ctor()");
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        if (DEBUG) Log.v(TAG, "onWindowVisibilityChanged(): visibility=" + visibility);
        super.onWindowVisibilityChanged(visibility);
        switch (visibility) {
            case  View.VISIBLE:
                attachToSurfaceWhenReady();
                break;
            case  View.INVISIBLE:
                break;
            case View.GONE:
                break;
        }
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mTextureView.layout(0, 0, r - l, b - t);
    }

    private boolean injectInputEvent(InputEvent event) {
@@ -159,6 +134,9 @@ public class ActivityView extends ViewGroup {
    }

    public void startActivity(Intent intent) {
        if (mActivityContainer == null) {
            throw new IllegalStateException("Attempt to call startActivity after release");
        }
        if (DEBUG) Log.v(TAG, "startActivity(): intent=" + intent + " " +
                (isAttachedToDisplay() ? "" : "not") + " attached");
        if (mSurface != null) {
@@ -183,6 +161,9 @@ public class ActivityView extends ViewGroup {
    }

    public void startActivity(IntentSender intentSender) {
        if (mActivityContainer == null) {
            throw new IllegalStateException("Attempt to call startActivity after release");
        }
        if (DEBUG) Log.v(TAG, "startActivityIntentSender(): intentSender=" + intentSender + " " +
                (isAttachedToDisplay() ? "" : "not") + " attached");
        final IIntentSender iIntentSender = intentSender.getTarget();
@@ -195,6 +176,9 @@ public class ActivityView extends ViewGroup {
    }

    public void startActivity(PendingIntent pendingIntent) {
        if (mActivityContainer == null) {
            throw new IllegalStateException("Attempt to call startActivity after release");
        }
        if (DEBUG) Log.v(TAG, "startActivityPendingIntent(): PendingIntent=" + pendingIntent + " "
                + (isAttachedToDisplay() ? "" : "not") + " attached");
        final IIntentSender iIntentSender = pendingIntent.getTarget();
@@ -206,24 +190,54 @@ public class ActivityView extends ViewGroup {
        }
    }

    public void release() {
        if (DEBUG) Log.v(TAG, "release()");
        if (mActivityContainer == null) {
            Log.e(TAG, "Duplicate call to release");
            return;
        }
        try {
            mActivityContainer.release();
        } catch (RemoteException e) {
        }
        mActivityContainer = null;

        if (mSurface != null) {
            mSurface.release();
            mSurface = null;
        }

        mTextureView.setSurfaceTextureListener(null);

        mGuard.close();
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            if (mGuard != null) {
                mGuard.warnIfOpen();
                release();
            }
        } finally {
            super.finalize();
        }
    }

    private void attachToSurfaceWhenReady() {
        final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        if (mActivityContainer == null || surfaceTexture == null || mSurface != null) {
        if (surfaceTexture == null || mSurface != null) {
            // Either not ready to attach, or already attached.
            return;
        }

        WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

        mSurface = new Surface(surfaceTexture);
        try {
            mActivityContainer.attachToSurface(mSurface, mWidth, mHeight, metrics.densityDpi);
            mActivityContainer.setSurface(mSurface, mWidth, mHeight, mMetrics.densityDpi);
        } catch (RemoteException e) {
            mSurface.release();
            mSurface = null;
            throw new IllegalStateException(
            throw new RuntimeException(
                    "ActivityView: Unable to create ActivityContainer. " + e);
        }

@@ -238,41 +252,43 @@ public class ActivityView extends ViewGroup {
        }
    }

    private void detach() {
        if (DEBUG) Log.d(TAG, "detach: attached=" + isAttachedToDisplay());
        if (mSurface != null) {
            try {
                mActivityContainer.detachFromDisplay();
            } catch (RemoteException e) {
            }
            mSurface.release();
            mSurface = null;
        }
    }

    private class ActivityViewSurfaceTextureListener implements SurfaceTextureListener {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
                int height) {
            if (mActivityContainer == null) {
                return;
            }
            if (DEBUG) Log.d(TAG, "onSurfaceTextureAvailable: width=" + width + " height="
                    + height);
            mWidth = width;
            mHeight = height;
            if (mActivityContainer != null) {
            attachToSurfaceWhenReady();
        }
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width,
                int height) {
            if (mActivityContainer == null) {
                return;
            }
            if (DEBUG) Log.d(TAG, "onSurfaceTextureSizeChanged: w=" + width + " h=" + height);
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
            if (mActivityContainer == null) {
                return true;
            }
            if (DEBUG) Log.d(TAG, "onSurfaceTextureDestroyed");
            detach();
            mSurface.release();
            mSurface = null;
            try {
                mActivityContainer.setSurface(null, mWidth, mHeight, mMetrics.densityDpi);
            } catch (RemoteException e) {
                throw new RuntimeException(
                        "ActivityView: Unable to set surface of ActivityContainer. " + e);
            }
            return true;
        }

@@ -283,13 +299,17 @@ public class ActivityView extends ViewGroup {

    }

    private class ActivityContainerCallback extends IActivityContainerCallback.Stub {
    private static class ActivityContainerCallback extends IActivityContainerCallback.Stub {
        private final WeakReference<ActivityView> mActivityViewWeakReference;

        ActivityContainerCallback(ActivityView activityView) {
            mActivityViewWeakReference = new WeakReference<ActivityView>(activityView);
        }

        @Override
        public void setVisible(IBinder container, boolean visible) {
            if (DEBUG) Log.v(TAG, "setVisible(): container=" + container + " visible=" + visible);
            if (visible) {
            } else {
            }
            if (DEBUG) Log.v(TAG, "setVisible(): container=" + container + " visible=" + visible +
                    " ActivityView=" + mActivityViewWeakReference.get());
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -26,10 +26,10 @@ import android.view.Surface;
/** @hide */
interface IActivityContainer {
    void attachToDisplay(int displayId);
    void attachToSurface(in Surface surface, int width, int height, int density);
    void detachFromDisplay();
    void setSurface(in Surface surface, int width, int height, int density);
    int startActivity(in Intent intent);
    int startActivityIntentSender(in IIntentSender intentSender);
    int getDisplayId();
    boolean injectEvent(in InputEvent event);
    void release();
}
+3 −0
Original line number Diff line number Diff line
@@ -7078,6 +7078,9 @@ public final class ActivityManagerService extends ActivityManagerNative
            if (r == null) {
                return null;
            }
            if (callback == null) {
                throw new IllegalArgumentException("callback must not be null");
            }
            return mStackSupervisor.createActivityContainer(r, callback);
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -139,7 +139,7 @@ final class ActivityRecord {
    boolean forceNewConfig; // force re-create with new config next time
    int launchCount;        // count of launches since last state
    long lastLaunchTime;    // time of last lauch of this activity
    ArrayList<ActivityStack> mChildContainers = new ArrayList<ActivityStack>();
    ArrayList<ActivityContainer> mChildContainers = new ArrayList<ActivityContainer>();

    String stringName;      // for caching of toString().

+20 −4
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;

import static com.android.server.am.ActivityStackSupervisor.ActivityContainer.CONTAINER_STATE_HAS_SURFACE;

import com.android.internal.os.BatteryStatsImpl;
import com.android.server.Watchdog;
import com.android.server.am.ActivityManagerService.ItemMatcher;
@@ -1028,9 +1030,9 @@ final class ActivityStack {
    private void setVisibile(ActivityRecord r, boolean visible) {
        r.visible = visible;
        mWindowManager.setAppVisibility(r.appToken, visible);
        final ArrayList<ActivityStack> containers = r.mChildContainers;
        final ArrayList<ActivityContainer> containers = r.mChildContainers;
        for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
            ActivityContainer container = containers.get(containerNdx).mActivityContainer;
            ActivityContainer container = containers.get(containerNdx);
            container.setVisible(visible);
        }
    }
@@ -1271,7 +1273,8 @@ final class ActivityStack {
        if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");

        ActivityRecord parent = mActivityContainer.mParentActivity;
        if (parent != null && parent.state != ActivityState.RESUMED) {
        if ((parent != null && parent.state != ActivityState.RESUMED) ||
                mActivityContainer.mContainerState != CONTAINER_STATE_HAS_SURFACE) {
            // Do not resume this stack if its parent is not resumed.
            // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
            return false;
@@ -2552,6 +2555,20 @@ final class ActivityStack {
        return r;
    }

    void finishAllActivitiesLocked() {
        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
                final ActivityRecord r = activities.get(activityNdx);
                if (r.finishing) {
                    continue;
                }
                Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
                finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
            }
        }
    }

    final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
            Intent resultData) {
        final ActivityRecord srec = ActivityRecord.forToken(token);
@@ -2787,7 +2804,6 @@ final class ActivityStack {
        }
        if (activityRemoved) {
            mStackSupervisor.resumeTopActivitiesLocked();

        }
    }

Loading