OSDN Git Service

Add onDestroyView to allow clock plugins to clean up resources.
authorRobert Snoeberger <snoeberger@google.com>
Wed, 3 Apr 2019 17:09:55 +0000 (13:09 -0400)
committerRobert Snoeberger <snoeberger@google.com>
Wed, 3 Apr 2019 17:29:16 +0000 (17:29 +0000)
Fixes: 129859743
Test: New test point added to KeyguardClockSwitchTest
Change-Id: Ia0eb7cb329820a64a4783937f65135fda742a2d1

packages/SystemUI/plugin/src/com/android/systemui/plugins/ClockPlugin.java
packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitch.java
packages/SystemUI/src/com/android/keyguard/clock/BubbleClockController.java
packages/SystemUI/src/com/android/keyguard/clock/DefaultClockController.java
packages/SystemUI/src/com/android/keyguard/clock/StretchAnalogClockController.java
packages/SystemUI/src/com/android/keyguard/clock/TypeClockController.java
packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchTest.java

index 105be46..58d50ea 100644 (file)
@@ -28,7 +28,7 @@ import java.util.TimeZone;
 public interface ClockPlugin extends Plugin {
 
     String ACTION = "com.android.systemui.action.PLUGIN_CLOCK";
-    int VERSION = 3;
+    int VERSION = 4;
 
     /**
      * Get the name of the clock face.
@@ -72,6 +72,14 @@ public interface ClockPlugin extends Plugin {
     }
 
     /**
+     * Allows the plugin to clean up resources when no longer needed.
+     *
+     * Called when the view previously created by {@link ClockPlugin#getView()} has been detached
+     * from the view hierarchy.
+     */
+    void onDestroyView();
+
+    /**
      * Set clock paint style.
      * @param style The new style to set in the paint.
      */
index fd92e9e..b738b57 100644 (file)
@@ -187,6 +187,7 @@ public class KeyguardClockSwitch extends RelativeLayout {
                 mBigClockContainer.removeAllViews();
                 updateBigClockVisibility();
             }
+            mClockPlugin.onDestroyView();
             mClockPlugin = null;
         }
         if (plugin == null) {
index 147def3..d30f45f 100644 (file)
@@ -102,6 +102,16 @@ public class BubbleClockController implements ClockPlugin {
     }
 
     @Override
+    public void onDestroyView() {
+        mView = null;
+        mDigitalClock = null;
+        mAnalogClock = null;
+        mLockClockContainer = null;
+        mLockClock = null;
+        mDarkController = null;
+    }
+
+    @Override
     public String getName() {
         return "bubble";
     }
index 73414b3..488cb27 100644 (file)
@@ -93,6 +93,13 @@ public class DefaultClockController implements ClockPlugin {
     }
 
     @Override
+    public void onDestroyView() {
+        mView = null;
+        mTextTime = null;
+        mTextDate = null;
+    }
+
+    @Override
     public String getName() {
         return "default";
     }
index ea9f0cd..81b6a60 100644 (file)
@@ -101,6 +101,17 @@ public class StretchAnalogClockController implements ClockPlugin {
         mDarkController = new CrossFadeDarkController(mDigitalClock, mLockClock);
     }
 
+
+    @Override
+    public void onDestroyView() {
+        mBigClockView = null;
+        mDigitalClock = null;
+        mAnalogClock = null;
+        mView = null;
+        mLockClock = null;
+        mDarkController = null;
+    }
+
     @Override
     public String getName() {
         return "stretch";
index 67c0989..1c6b38b 100644 (file)
@@ -99,6 +99,14 @@ public class TypeClockController implements ClockPlugin {
     }
 
     @Override
+    public void onDestroyView() {
+        mView = null;
+        mTypeClock = null;
+        mLockClock = null;
+        mDarkController = null;
+    }
+
+    @Override
     public String getName() {
         return "type";
     }
index 632b0c0..f01c0b4 100644 (file)
@@ -211,6 +211,19 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {
     }
 
     @Test
+    public void onPluginDisconnected_onDestroyView() {
+        // GIVEN a plugin is connected
+        ClockPlugin clockPlugin = mock(ClockPlugin.class);
+        when(clockPlugin.getView()).thenReturn(new TextClock(getContext()));
+        ClockManager.ClockChangedListener listener = mKeyguardClockSwitch.getClockChangedListener();
+        listener.onClockChanged(clockPlugin);
+        // WHEN the plugin is disconnected
+        listener.onClockChanged(null);
+        // THEN onDestroyView is called on the plugin
+        verify(clockPlugin).onDestroyView();
+    }
+
+    @Test
     public void setTextColor_defaultClockSetTextColor() {
         mKeyguardClockSwitch.setTextColor(Color.YELLOW);