From dd1341ec62c6c4e595e0e8168c2401d99a741ab0 Mon Sep 17 00:00:00 2001 From: Dave Mankoff Date: Thu, 22 Oct 2020 10:49:13 -0400 Subject: [PATCH] Add initInternal to ViewController. ViewController's init method was not ideal to override. On the one hand, if you put a call super.init() first your onViewAttach method is called before you're subclass's init. Further, if you put your own code befer super.init(), you are still left with the problem that your init is not automatically idempotent, and multiple calls to #init() could result in multiple executions of your code unless you handle it yourself. With this change, #initInternal() is introduced, giving ViewControllers a place to put their run-once code such that it runs before any view-attachment callbacks are fired. Fixes: 171472009 Test: manual Change-Id: I2e284024c82e3f7c7b6f29f22a1ffa3c8aae9fcb --- .../KeyguardAbsKeyInputViewController.java | 3 +-- .../KeyguardClockSwitchController.java | 3 +-- .../keyguard/KeyguardHostViewController.java | 3 +-- .../KeyguardPatternViewController.java | 4 ++-- .../KeyguardSecurityContainerController.java | 3 +-- .../KeyguardStatusViewController.java | 3 +-- .../qs/QSContainerImplController.java | 3 +-- .../android/systemui/util/ViewController.java | 21 ++++++++++++++++--- 8 files changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java index 53f847434dccc..89911e01cde97 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java @@ -81,8 +81,7 @@ public abstract class KeyguardAbsKeyInputViewController } /** Initialize the Controller. */ - public void init() { - super.init(); + public void initInternal() { mKeyguardSecurityContainerController.init(); } diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java index 3db9db7be00c6..94913c80ac5c3 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java @@ -190,8 +190,8 @@ public class KeyguardPatternViewController } @Override - public void init() { - super.init(); + public void initInternal() { + super.initInternal(); mMessageAreaController.init(); } diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java index 1c23605a8516b..e9173a3ca3a0b 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java @@ -169,8 +169,7 @@ public class KeyguardSecurityContainerController extends ViewController { } @Override - public void init() { - super.init(); + public void initInternal() { mQuickStatusBarHeaderController.init(); } diff --git a/packages/SystemUI/src/com/android/systemui/util/ViewController.java b/packages/SystemUI/src/com/android/systemui/util/ViewController.java index 3dbc6f101a909..e8b837ee5bed1 100644 --- a/packages/SystemUI/src/com/android/systemui/util/ViewController.java +++ b/packages/SystemUI/src/com/android/systemui/util/ViewController.java @@ -26,8 +26,8 @@ import android.view.View.OnAttachStateChangeListener; * * Implementations should handle setup and teardown related activities inside of * {@link #onViewAttached()} and {@link #onViewDetached()}. Be sure to call {@link #init()} on - * any child controllers that this uses. This can be done in {@link init()} if the controllers - * are injected, or right after creation time of the child controller. + * any child controllers that this uses. This can be done in {@link #initInternal()} if the + * controllers are injected, or right after creation time of the child controller. * * Tip: View "attachment" happens top down - parents are notified that they are attached before * any children. That means that if you call a method on a child controller in @@ -62,11 +62,18 @@ public abstract class ViewController { mView = view; } - /** Call immediately after constructing Controller in order to handle view lifecycle events. */ + /** + * Call immediately after constructing Controller in order to handle view lifecycle events. + * + * Generally speaking, you don't want to override this method. Instead, override + * {@link #initInternal()} as a way to have an run-once idempotent method that you can use for + * setup of your ViewController. + */ public void init() { if (mInited) { return; } + initInternal(); mInited = true; if (mView != null) { @@ -77,6 +84,14 @@ public abstract class ViewController { } } + /** + * Run once when {@link #init()} is called. + * + * Override this to perform idempotent, one-time setup that your controller needs. It will + * be called before {@link #onViewAttached()}. + */ + protected void initInternal() {} + protected Context getContext() { return mView.getContext(); }