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

Commit a67a0dd0 authored by Darrell Shi's avatar Darrell Shi Committed by Android (Google) Code Review
Browse files

Merge "Clean up DreamService."

parents 2b450612 b344d76f
Loading
Loading
Loading
Loading
+54 −58
Original line number Diff line number Diff line
/**
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.service.dreams;

import android.annotation.IdRes;
@@ -57,7 +58,6 @@ import android.view.WindowManager.LayoutParams;
import android.view.accessibility.AccessibilityEvent;

import com.android.internal.util.DumpUtils;
import com.android.internal.util.DumpUtils.Dump;

import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -111,7 +111,7 @@ import java.util.function.Consumer;
 * <p>If specified with the {@code <meta-data>} element,
 * additional information for the dream is defined using the
 * {@link android.R.styleable#Dream &lt;dream&gt;} element in a separate XML file.
 * Currently, the only addtional
 * Currently, the only additional
 * information you can provide is for a settings activity that allows the user to configure
 * the dream behavior. For example:</p>
 * <p class="code-caption">res/xml/my_dream.xml</p>
@@ -159,7 +159,8 @@ import java.util.function.Consumer;
 * </pre>
 */
public class DreamService extends Service implements Window.Callback {
    private final String TAG = DreamService.class.getSimpleName() + "[" + getClass().getSimpleName() + "]";
    private final String mTag =
            DreamService.class.getSimpleName() + "[" + getClass().getSimpleName() + "]";

    /**
     * The name of the dream manager service.
@@ -224,13 +225,13 @@ public class DreamService extends Service implements Window.Callback {
    private DreamServiceWrapper mDreamServiceWrapper;
    private Runnable mDispatchAfterOnAttachedToWindow;

    private OverlayConnection mOverlayConnection;
    private final OverlayConnection mOverlayConnection;

    private static class OverlayConnection implements ServiceConnection {
        // Overlay set during onBind.
        private IDreamOverlay mOverlay;
        // A Queue of pending requests to execute on the overlay.
        private ArrayDeque<Consumer<IDreamOverlay>> mRequests;
        private final ArrayDeque<Consumer<IDreamOverlay>> mRequests;

        private boolean mBound;

@@ -292,7 +293,7 @@ public class DreamService extends Service implements Window.Callback {
        }
    }

    private IDreamOverlayCallback mOverlayCallback = new IDreamOverlayCallback.Stub() {
    private final IDreamOverlayCallback mOverlayCallback = new IDreamOverlayCallback.Stub() {
        @Override
        public void onExitRequested() {
            // Simply finish dream when exit is requested.
@@ -319,11 +320,11 @@ public class DreamService extends Service implements Window.Callback {
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO: create more flexible version of mInteractive that allows use of KEYCODE_BACK
        if (!mInteractive) {
            if (mDebug) Slog.v(TAG, "Waking up on keyEvent");
            if (mDebug) Slog.v(mTag, "Waking up on keyEvent");
            wakeUp();
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mDebug) Slog.v(TAG, "Waking up on back key");
            if (mDebug) Slog.v(mTag, "Waking up on back key");
            wakeUp();
            return true;
        }
@@ -334,7 +335,7 @@ public class DreamService extends Service implements Window.Callback {
    @Override
    public boolean dispatchKeyShortcutEvent(KeyEvent event) {
        if (!mInteractive) {
            if (mDebug) Slog.v(TAG, "Waking up on keyShortcutEvent");
            if (mDebug) Slog.v(mTag, "Waking up on keyShortcutEvent");
            wakeUp();
            return true;
        }
@@ -347,7 +348,7 @@ public class DreamService extends Service implements Window.Callback {
        // TODO: create more flexible version of mInteractive that allows clicks
        // but finish()es on any other kind of activity
        if (!mInteractive && event.getActionMasked() == MotionEvent.ACTION_UP) {
            if (mDebug) Slog.v(TAG, "Waking up on touchEvent");
            if (mDebug) Slog.v(mTag, "Waking up on touchEvent");
            wakeUp();
            return true;
        }
@@ -358,7 +359,7 @@ public class DreamService extends Service implements Window.Callback {
    @Override
    public boolean dispatchTrackballEvent(MotionEvent event) {
        if (!mInteractive) {
            if (mDebug) Slog.v(TAG, "Waking up on trackballEvent");
            if (mDebug) Slog.v(mTag, "Waking up on trackballEvent");
            wakeUp();
            return true;
        }
@@ -369,7 +370,7 @@ public class DreamService extends Service implements Window.Callback {
    @Override
    public boolean dispatchGenericMotionEvent(MotionEvent event) {
        if (!mInteractive) {
            if (mDebug) Slog.v(TAG, "Waking up on genericMotionEvent");
            if (mDebug) Slog.v(mTag, "Waking up on genericMotionEvent");
            wakeUp();
            return true;
        }
@@ -624,7 +625,7 @@ public class DreamService extends Service implements Window.Callback {
    }

    /**
     * Returns whether or not this dream is interactive.  Defaults to false.
     * Returns whether this dream is interactive. Defaults to false.
     *
     * @see #setInteractive(boolean)
     */
@@ -648,7 +649,7 @@ public class DreamService extends Service implements Window.Callback {
    }

    /**
     * Returns whether or not this dream is in fullscreen mode. Defaults to false.
     * Returns whether this dream is in fullscreen mode. Defaults to false.
     *
     * @see #setFullscreen(boolean)
     */
@@ -670,7 +671,7 @@ public class DreamService extends Service implements Window.Callback {
    }

    /**
     * Returns whether or not this dream keeps the screen bright while dreaming.
     * Returns whether this dream keeps the screen bright while dreaming.
     * Defaults to false, allowing the screen to dim if necessary.
     *
     * @see #setScreenBright(boolean)
@@ -690,7 +691,7 @@ public class DreamService extends Service implements Window.Callback {
    }

    /**
     * Returns whether or not this dream is windowless.  Only available to doze dreams.
     * Returns whether this dream is windowless. Only available to doze dreams.
     *
     * @hide
     */
@@ -752,7 +753,7 @@ public class DreamService extends Service implements Window.Callback {

    private void updateDoze() {
        if (mDreamToken == null) {
            Slog.w(TAG, "Updating doze without a dream token.");
            Slog.w(mTag, "Updating doze without a dream token.");
            return;
        }

@@ -795,7 +796,6 @@ public class DreamService extends Service implements Window.Callback {
     *
     * @return True if the dream is dozing.
     *
     * @see #setDozing(boolean)
     * @hide For use by system UI components only.
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
@@ -922,7 +922,7 @@ public class DreamService extends Service implements Window.Callback {
     */
    @Override
    public void onCreate() {
        if (mDebug) Slog.v(TAG, "onCreate()");
        if (mDebug) Slog.v(mTag, "onCreate()");
        super.onCreate();
    }

@@ -930,7 +930,7 @@ public class DreamService extends Service implements Window.Callback {
     * Called when the dream's window has been created and is visible and animation may now begin.
     */
    public void onDreamingStarted() {
        if (mDebug) Slog.v(TAG, "onDreamingStarted()");
        if (mDebug) Slog.v(mTag, "onDreamingStarted()");
        // hook for subclasses
    }

@@ -939,7 +939,7 @@ public class DreamService extends Service implements Window.Callback {
     * before the window has been removed.
     */
    public void onDreamingStopped() {
        if (mDebug) Slog.v(TAG, "onDreamingStopped()");
        if (mDebug) Slog.v(mTag, "onDreamingStopped()");
        // hook for subclasses
    }

@@ -962,7 +962,7 @@ public class DreamService extends Service implements Window.Callback {
    /** {@inheritDoc} */
    @Override
    public final IBinder onBind(Intent intent) {
        if (mDebug) Slog.v(TAG, "onBind() intent = " + intent);
        if (mDebug) Slog.v(mTag, "onBind() intent = " + intent);
        mDreamServiceWrapper = new DreamServiceWrapper();

        // Connect to the overlay service if present.
@@ -981,7 +981,7 @@ public class DreamService extends Service implements Window.Callback {
     * </p>
     */
    public final void finish() {
        if (mDebug) Slog.v(TAG, "finish(): mFinished=" + mFinished);
        if (mDebug) Slog.v(mTag, "finish(): mFinished=" + mFinished);

        Activity activity = mActivity;
        if (activity != null) {
@@ -998,7 +998,7 @@ public class DreamService extends Service implements Window.Callback {
        mFinished = true;

        if (mDreamToken == null) {
            Slog.w(TAG, "Finish was called before the dream was attached.");
            Slog.w(mTag, "Finish was called before the dream was attached.");
            stopSelf();
            return;
        }
@@ -1026,8 +1026,10 @@ public class DreamService extends Service implements Window.Callback {
    }

    private void wakeUp(boolean fromSystem) {
        if (mDebug) Slog.v(TAG, "wakeUp(): fromSystem=" + fromSystem
                + ", mWaking=" + mWaking + ", mFinished=" + mFinished);
        if (mDebug) {
            Slog.v(mTag, "wakeUp(): fromSystem=" + fromSystem + ", mWaking=" + mWaking
                    + ", mFinished=" + mFinished);
        }

        if (!mWaking && !mFinished) {
            mWaking = true;
@@ -1052,7 +1054,7 @@ public class DreamService extends Service implements Window.Callback {
            // it we were finishing immediately.
            if (!fromSystem && !mFinished) {
                if (mActivity == null) {
                    Slog.w(TAG, "WakeUp was called before the dream was attached.");
                    Slog.w(mTag, "WakeUp was called before the dream was attached.");
                } else {
                    try {
                        mDreamManager.finishSelf(mDreamToken, false /*immediate*/);
@@ -1067,7 +1069,7 @@ public class DreamService extends Service implements Window.Callback {
    /** {@inheritDoc} */
    @Override
    public void onDestroy() {
        if (mDebug) Slog.v(TAG, "onDestroy()");
        if (mDebug) Slog.v(mTag, "onDestroy()");
        // hook for subclasses

        // Just in case destroy came in before detach, let's take care of that now
@@ -1083,9 +1085,9 @@ public class DreamService extends Service implements Window.Callback {
     *
     * Must run on mHandler.
     */
    private final void detach() {
    private void detach() {
        if (mStarted) {
            if (mDebug) Slog.v(TAG, "detach(): Calling onDreamingStopped()");
            if (mDebug) Slog.v(mTag, "detach(): Calling onDreamingStopped()");
            mStarted = false;
            onDreamingStopped();
        }
@@ -1110,12 +1112,12 @@ public class DreamService extends Service implements Window.Callback {
     */
    private void attach(IBinder dreamToken, boolean canDoze, IRemoteCallback started) {
        if (mDreamToken != null) {
            Slog.e(TAG, "attach() called when dream with token=" + mDreamToken
            Slog.e(mTag, "attach() called when dream with token=" + mDreamToken
                    + " already attached");
            return;
        }
        if (mFinished || mWaking) {
            Slog.w(TAG, "attach() called after dream already finished");
            Slog.w(mTag, "attach() called after dream already finished");
            try {
                mDreamManager.finishSelf(dreamToken, true /*immediate*/);
            } catch (RemoteException ex) {
@@ -1158,10 +1160,9 @@ public class DreamService extends Service implements Window.Callback {
            try {
                if (!ActivityTaskManager.getService().startDreamActivity(i)) {
                    detach();
                    return;
                }
            } catch (RemoteException e) {
                Log.w(TAG, "Could not connect to activity task manager to start dream activity");
                Log.w(mTag, "Could not connect to activity task manager to start dream activity");
                e.rethrowFromSystemServer();
            }
        } else {
@@ -1207,7 +1208,7 @@ public class DreamService extends Service implements Window.Callback {
                            try {
                                overlay.startDream(mWindow.getAttributes(), mOverlayCallback);
                            } catch (RemoteException e) {
                                Log.e(TAG, "could not send window attributes:" + e);
                                Log.e(mTag, "could not send window attributes:" + e);
                            }
                        });
                    }
@@ -1243,17 +1244,12 @@ public class DreamService extends Service implements Window.Callback {

    @Override
    protected void dump(final FileDescriptor fd, PrintWriter pw, final String[] args) {
        DumpUtils.dumpAsync(mHandler, new Dump() {
            @Override
            public void dump(PrintWriter pw, String prefix) {
                dumpOnHandler(fd, pw, args);
            }
        }, pw, "", 1000);
        DumpUtils.dumpAsync(mHandler, (pw1, prefix) -> dumpOnHandler(fd, pw1, args), pw, "", 1000);
    }

    /** @hide */
    protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
        pw.print(TAG + ": ");
        pw.print(mTag + ": ");
        if (mFinished) {
            pw.println("stopped");
        } else {