From b344d76fc7ca8ae898e18fd39a9646ad6fa4cc06 Mon Sep 17 00:00:00 2001 From: Darrell Shi Date: Tue, 11 Jan 2022 15:25:57 -0800 Subject: [PATCH] Clean up DreamService. - Rename TAG to mTag, for it is a private non-static variable - A few variables can be final - Unnecessary "final" for private method - Unnecessary return at the end of method - Double white spaces - Misspellings Test: manual Change-Id: Id8523a0857e2a8333a2756cdc778b7d468463f40 --- .../android/service/dreams/DreamService.java | 112 +++++++++--------- 1 file changed, 54 insertions(+), 58 deletions(-) diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java index 133e384dfa8fe..bb1f393b99bcd 100644 --- a/core/java/android/service/dreams/DreamService.java +++ b/core/java/android/service/dreams/DreamService.java @@ -1,4 +1,4 @@ -/** +/* * 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; *

If specified with the {@code } element, * additional information for the dream is defined using the * {@link android.R.styleable#Dream <dream>} 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:

*

res/xml/my_dream.xml

@@ -159,7 +159,8 @@ import java.util.function.Consumer; * */ 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> mRequests; + private final ArrayDeque> 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) @@ -680,7 +681,7 @@ public class DreamService extends Service implements Window.Callback { } /** - * Marks this dream as windowless. Only available to doze dreams. + * Marks this dream as windowless. Only available to doze dreams. * * @hide * @@ -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 */ @@ -717,12 +718,12 @@ public class DreamService extends Service implements Window.Callback { * Starts dozing, entering a deep dreamy sleep. *

* Dozing enables the system to conserve power while the user is not actively interacting - * with the device. While dozing, the display will remain on in a low-power state + * with the device. While dozing, the display will remain on in a low-power state * and will continue to show its previous contents but the application processor and * other system components will be allowed to suspend when possible. *

* While the application processor is suspended, the dream may stop executing code - * for long periods of time. Prior to being suspended, the dream may schedule periodic + * for long periods of time. Prior to being suspended, the dream may schedule periodic * wake-ups to render new content by scheduling an alarm with the {@link AlarmManager}. * The dream may also keep the CPU awake by acquiring a * {@link android.os.PowerManager#PARTIAL_WAKE_LOCK partial wake lock} when necessary. @@ -731,7 +732,7 @@ public class DreamService extends Service implements Window.Callback { * awake for very long. *

* It is a good idea to call this method some time after the dream's entry animation - * has completed and the dream is ready to doze. It is important to completely + * has completed and the dream is ready to doze. It is important to completely * finish all of the work needed before dozing since the application processor may * be suspended at any moment once this method is called unless other wake locks * are being held. @@ -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; } @@ -768,7 +769,7 @@ public class DreamService extends Service implements Window.Callback { /** * Stops dozing, returns to active dreaming. *

- * This method reverses the effect of {@link #startDozing}. From this moment onward, + * This method reverses the effect of {@link #startDozing}. From this moment onward, * the application processor will be kept awake as long as the dream is running * or until the dream starts dozing again. *

@@ -790,12 +791,11 @@ public class DreamService extends Service implements Window.Callback { /** * Returns true if the dream will allow the system to enter a low-power state while - * it is running without actually turning off the screen. Defaults to false, + * it is running without actually turning off the screen. Defaults to false, * keeping the application processor awake while the dream is running. * * @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) @@ -822,7 +822,7 @@ public class DreamService extends Service implements Window.Callback { * Sets the screen state to use while dozing. *

* The value of this property determines the power state of the primary display - * once {@link #startDozing} has been called. The default value is + * once {@link #startDozing} has been called. The default value is * {@link Display#STATE_UNKNOWN} which lets the system decide. * The dream may set a different state before starting to doze and may * perform transitions between states while dozing to conserve power and @@ -836,7 +836,7 @@ public class DreamService extends Service implements Window.Callback { * If not using Sidekick, it is recommended that the state be set to * {@link Display#STATE_DOZE_SUSPEND} once the dream has completely * finished drawing and before it releases its wakelock - * to allow the display hardware to be fully suspended. While suspended, + * to allow the display hardware to be fully suspended. While suspended, * the display will preserve its on-screen contents. *

* If the doze suspend state is used, the dream must make sure to set the mode back @@ -844,7 +844,7 @@ public class DreamService extends Service implements Window.Callback { * since the display updates may be ignored and not seen by the user otherwise. *

* The set of available display power states and their behavior while dozing is - * hardware dependent and may vary across devices. The dream may therefore + * hardware dependent and may vary across devices. The dream may therefore * need to be modified or configured to correctly support the hardware. *

* @@ -883,19 +883,19 @@ public class DreamService extends Service implements Window.Callback { * Sets the screen brightness to use while dozing. *

* The value of this property determines the power state of the primary display - * once {@link #startDozing} has been called. The default value is + * once {@link #startDozing} has been called. The default value is * {@link PowerManager#BRIGHTNESS_DEFAULT} which lets the system decide. * The dream may set a different brightness before starting to doze and may adjust * the brightness while dozing to conserve power and achieve various effects. *

* Note that dream may specify any brightness in the full 0-255 range, including * values that are less than the minimum value for manual screen brightness - * adjustments by the user. In particular, the value may be set to 0 which may + * adjustments by the user. In particular, the value may be set to 0 which may * turn off the backlight entirely while still leaving the screen on although * this behavior is device dependent and not guaranteed. *

* The available range of display brightness values and their behavior while dozing is - * hardware dependent and may vary across devices. The dream may therefore + * hardware dependent and may vary across devices. The dream may therefore * need to be modified or configured to correctly support the hardware. *

* @@ -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 } @@ -947,11 +947,11 @@ public class DreamService extends Service implements Window.Callback { * Called when the dream is being asked to stop itself and wake. *

* The default implementation simply calls {@link #finish} which ends the dream - * immediately. Subclasses may override this function to perform a smooth exit + * immediately. Subclasses may override this function to perform a smooth exit * transition then call {@link #finish} afterwards. *

* Note that the dream will only be given a short period of time (currently about - * five seconds) to wake up. If the dream does not finish itself in a timely manner + * five seconds) to wake up. If the dream does not finish itself in a timely manner * then the system will forcibly finish it once the time allowance is up. *

*/ @@ -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 { *

*/ 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 { @@ -1191,7 +1192,7 @@ public class DreamService extends Service implements Window.Callback { // along well. Dreams usually don't need such bars anyways, so disable them by default. mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); - // Hide all insets when the dream is showing + // Hide all insets when the dream is showing mWindow.getDecorView().getWindowInsetsController().hide(WindowInsets.Type.systemBars()); mWindow.setDecorFitsSystemWindows(false); @@ -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 {