From a94c3c87f92d6b725ec71d491867d2b55be81298 Mon Sep 17 00:00:00 2001 From: Arka Prava Basu Date: Wed, 31 Jul 2019 18:04:45 +0530 Subject: [PATCH] Api to set LifecycleOwner for BlurLayout - with the Lifecycle provided by the LifecycleOwner we can remove boilerplate calls to startBlur() and pauseBlur(). BlurLayout will implement Lifecycleobserver for this. - added dependency on android.arch.lifecycle:runtime:1.1.1 for the same - better not to use this API while using Java 8 Signed-off-by: Arka Prava Basu --- blurkit/build.gradle | 2 + .../java/io/alterac/blurkit/BlurLayout.java | 44 ++++++++++++++++++- .../io/alterac/blurkit/BlurLayoutTest.java | 35 +++++++++++++++ .../io/alterac/blurkit/demo/MainActivity.java | 10 +---- 4 files changed, 82 insertions(+), 9 deletions(-) diff --git a/blurkit/build.gradle b/blurkit/build.gradle index bacd08f..8767d92 100644 --- a/blurkit/build.gradle +++ b/blurkit/build.gradle @@ -41,6 +41,8 @@ dependencies { androidTestImplementation 'com.android.support.test:rules:1.0.2' androidTestImplementation 'org.hamcrest:hamcrest-library:1.3' + // lifecycle + implementation "android.arch.lifecycle:runtime:1.1.1" } install { diff --git a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java index 7d03c9e..340c250 100644 --- a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java +++ b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java @@ -1,6 +1,10 @@ package io.alterac.blurkit; import android.app.Activity; +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.OnLifecycleEvent; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; @@ -9,6 +13,8 @@ import android.graphics.Point; import android.graphics.PointF; import android.graphics.Rect; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.Choreographer; import android.view.View; @@ -23,7 +29,7 @@ * and finds its relative position to the top parent to draw properly regardless of where the layout is * placed. */ -public class BlurLayout extends FrameLayout { +public class BlurLayout extends FrameLayout implements LifecycleObserver { public static final float DEFAULT_DOWNSCALE_FACTOR = 0.12f; public static final int DEFAULT_BLUR_RADIUS = 12; @@ -74,6 +80,10 @@ public class BlurLayout extends FrameLayout { /** A saved bitmap for the view to re-use when {@link #lockView()} called. */ private Bitmap mLockedBitmap; + /** A {@link Lifecycle} for this layout. **/ + @Nullable + private Lifecycle mLifecycle; + public BlurLayout(Context context) { super(context, null); } @@ -107,6 +117,36 @@ public BlurLayout(Context context, AttributeSet attrs) { setCornerRadius(mCornerRadius); } + /** + * Sets the {@link LifecycleOwner} for this layout. No need to call {@link #startBlur()} + * or {@link #pauseBlur()}. + * + * Since this works due to {@link LifecycleObserver} which relies on {@link OnLifecycleEvent} + * annotated methods, it is not advisable to use this while using Java 8 + * + * @param lifecycleOwner the LifecycleOwner for this layout + */ + public void setLifecycleOwner(@NonNull LifecycleOwner lifecycleOwner) { + if (mLifecycle != null) { + mLifecycle.removeObserver(this); + } + mLifecycle = lifecycleOwner.getLifecycle(); + mLifecycle.addObserver(this); + } + + /** + * Get the {@link Lifecycle} associated with this layout. This is only applicable if + * we have set the {@link LifecycleOwner}. + * + * @see #setLifecycleOwner(LifecycleOwner) + * + * @return the Lifecycle associated with this layout, {@code null} if no Lifecyle is associated + */ + @Nullable + public Lifecycle getLifecycle() { + return mLifecycle; + } + /** Choreographer callback that re-draws the blur and schedules another callback. */ private Choreographer.FrameCallback invalidationLoop = new Choreographer.FrameCallback() { @Override @@ -117,6 +157,7 @@ public void doFrame(long frameTimeNanos) { }; /** Start BlurLayout continuous invalidation. **/ + @OnLifecycleEvent(Lifecycle.Event.ON_START) public void startBlur() { if (mRunning) { return; @@ -129,6 +170,7 @@ public void startBlur() { } /** Pause BlurLayout continuous invalidation. **/ + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void pauseBlur() { if (!mRunning) { return; diff --git a/blurkit/src/test/java/io/alterac/blurkit/BlurLayoutTest.java b/blurkit/src/test/java/io/alterac/blurkit/BlurLayoutTest.java index fd8c9e3..ed4a41d 100644 --- a/blurkit/src/test/java/io/alterac/blurkit/BlurLayoutTest.java +++ b/blurkit/src/test/java/io/alterac/blurkit/BlurLayoutTest.java @@ -1,6 +1,10 @@ package io.alterac.blurkit; +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; import android.content.Context; +import android.support.annotation.NonNull; import org.junit.Before; import org.junit.Test; @@ -16,6 +20,32 @@ public class BlurLayoutTest { public static final int TEST_INT = 1; private static final float TEST_FLOAT = 1.1f; + private static final LifecycleOwner LIFECYCLE_OWNER = new LifecycleOwner() { + + private final Lifecycle lifecycle = new Lifecycle() { + @Override + public void addObserver(@NonNull LifecycleObserver observer) { + + } + + @Override + public void removeObserver(@NonNull LifecycleObserver observer) { + + } + + @NonNull + @Override + public Lifecycle.State getCurrentState() { + return State.DESTROYED; + } + }; + + @NonNull + @Override + public Lifecycle getLifecycle() { + return lifecycle; + } + }; private BlurLayout blurLayout; @@ -60,4 +90,9 @@ public void lockViewTest() { blurLayout.lockView(); } + @Test + public void setLifecycleOwnerTest() { + blurLayout.setLifecycleOwner(LIFECYCLE_OWNER); + assertEquals(blurLayout.getLifecycle(), LIFECYCLE_OWNER.getLifecycle()); + } } diff --git a/demo/src/main/java/io/alterac/blurkit/demo/MainActivity.java b/demo/src/main/java/io/alterac/blurkit/demo/MainActivity.java index 6d6ba29..b21c8a1 100644 --- a/demo/src/main/java/io/alterac/blurkit/demo/MainActivity.java +++ b/demo/src/main/java/io/alterac/blurkit/demo/MainActivity.java @@ -16,8 +16,9 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - blurLayout = (BlurLayout) findViewById(R.id.blurLayout); + blurLayout = findViewById(R.id.blurLayout); + blurLayout.setLifecycleOwner(this); blurLayout.animate().translationY(movement).setDuration(1500).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { @@ -31,13 +32,6 @@ public void onAnimationEnd(Animator animation) { @Override protected void onStart() { super.onStart(); - blurLayout.startBlur(); blurLayout.lockView(); } - - @Override - protected void onStop() { - super.onStop(); - blurLayout.pauseBlur(); - } }