From: Jason Monk Date: Thu, 6 Jul 2017 19:06:36 +0000 (-0400) Subject: Fix leak in ScrimView X-Git-Tag: android-x86-9.0-r1~813^2~203^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=31bac9ca877f4bf3146712f683775d8a1496ad09;p=android-x86%2Fframeworks-base.git Fix leak in ScrimView Test: runtest systemui Change-Id: I8964181a60b67eea0eea32d42bb8e92862cc8d0f Fixes: 38461559 --- diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java b/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java index ba34ac1c60ab..4052211e1657 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java @@ -94,6 +94,11 @@ public class ScrimView extends View implements ConfigurationController.Configura mColors = new ColorExtractor.GradientColors(); updateScreenSize(); updateColorWithTint(false); + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); // We need to know about configuration changes to update the gradient size // since it's independent from view bounds. @@ -102,6 +107,14 @@ public class ScrimView extends View implements ConfigurationController.Configura } @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + + ConfigurationController config = Dependency.get(ConfigurationController.class); + config.removeCallback(this); + } + + @Override protected void onDraw(Canvas canvas) { if (mDrawAsSrc || mDrawable.getAlpha() > 0) { if (!mHasExcludedArea) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/ScrimViewTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/ScrimViewTest.java index 08867806f933..ae89ebab8f40 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/ScrimViewTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/ScrimViewTest.java @@ -28,11 +28,15 @@ import android.graphics.Color; import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; -import android.testing.AndroidTestingRunner; import android.support.test.filters.SmallTest; +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper; +import android.testing.TestableLooper.RunWithLooper; +import android.testing.ViewUtils; import android.view.View; -import com.android.systemui.SysuiTestCase; +import com.android.systemui.statusbar.policy.ConfigurationController; +import com.android.systemui.utils.leaks.LeakCheckedTest; import com.google.android.colorextraction.ColorExtractor; import com.google.android.colorextraction.drawable.GradientDrawable; @@ -43,18 +47,28 @@ import org.junit.runner.RunWith; @RunWith(AndroidTestingRunner.class) @SmallTest -public class ScrimViewTest extends SysuiTestCase { +public class ScrimViewTest extends LeakCheckedTest { ScrimView mView; @Before public void setUp() { + injectLeakCheckedDependency(ConfigurationController.class); mView = new ScrimView(getContext()); mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); mView.layout(0, 0, 1920, 1080); } @Test + @RunWithLooper + public void testAttachDetach() { + ViewUtils.attachView(mView); + TestableLooper.get(this).processAllMessages(); + ViewUtils.detachView(mView); + TestableLooper.get(this).processAllMessages(); + } + + @Test public void testSetDrawable_UpdateDrawable() { Drawable drawable = new ColorDrawable(Color.GREEN); mView.setDrawable(drawable); diff --git a/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/FakeConfigurationController.java b/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/FakeConfigurationController.java new file mode 100644 index 000000000000..9ef30c3d1859 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/FakeConfigurationController.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.android.systemui.utils.leaks; + +import com.android.systemui.statusbar.policy.ConfigurationController; + +public class FakeConfigurationController + extends BaseLeakChecker + implements ConfigurationController { + + public FakeConfigurationController(LeakCheckedTest.SysuiLeakCheck sysuiLeakCheck) { + super(sysuiLeakCheck, "config"); + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java b/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java index 94af77332f46..ecda9620f7fe 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java @@ -26,6 +26,7 @@ import com.android.systemui.statusbar.phone.StatusBarIconController; import com.android.systemui.statusbar.policy.BatteryController; import com.android.systemui.statusbar.policy.BluetoothController; import com.android.systemui.statusbar.policy.CastController; +import com.android.systemui.statusbar.policy.ConfigurationController; import com.android.systemui.statusbar.policy.FlashlightController; import com.android.systemui.statusbar.policy.HotspotController; import com.android.systemui.statusbar.policy.KeyguardMonitor; @@ -68,6 +69,7 @@ public abstract class LeakCheckedTest extends SysuiTestCase { PluginManager.class, TunerService.class, StatusBarIconController.class, + ConfigurationController.class, }; @Rule @@ -134,6 +136,8 @@ public abstract class LeakCheckedTest extends SysuiTestCase { obj = new FakeTunerService(this); } else if (cls == StatusBarIconController.class) { obj = new FakeStatusBarIconController(this); + } else if (cls == ConfigurationController.class) { + obj = new FakeConfigurationController(this); } else { Assert.fail(cls.getName() + " is not supported by LeakCheckedTest yet"); }