diff --git a/build.gradle b/build.gradle index 1ea4bd05..395f30b3 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ buildscript { allprojects { repositories { jcenter() + maven { url 'https://maven.google.com' } } } diff --git a/camerakit/build.gradle b/camerakit/build.gradle index 82ec2fca..6503553e 100644 --- a/camerakit/build.gradle +++ b/camerakit/build.gradle @@ -33,6 +33,11 @@ android { dependencies { compile 'com.android.support:appcompat-v7:25.2.0' + + + compile "android.arch.lifecycle:runtime:1.0.0-alpha1" + compile "android.arch.lifecycle:extensions:1.0.0-alpha1" + annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1" } apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index a20adc03..5c73df1a 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -2,6 +2,10 @@ package com.flurgle.camerakit; import android.Manifest; 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.ContextWrapper; import android.content.pm.PackageManager; @@ -37,7 +41,21 @@ import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_LAZY; import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_PICTURE; import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_STRICT; -public class CameraView extends FrameLayout { +/** + * The CameraView implements the LifecycleObserver interface for ease of use. To take advantage of + * this, simply call the following from any LifecycleOwner: + *
+ * {@code
+ * protected void onCreate(@Nullable Bundle savedInstanceState) {
+ *     super.onCreate(savedInstanceState);
+ *     setContentView(R.layout.my_view);
+ *     ...
+ *     getLifecycle().addObserver(mCameraView);
+ * }
+ * }
+ * 
+ */ +public class CameraView extends FrameLayout implements LifecycleObserver { private static Handler sWorkerHandler; @@ -81,6 +99,7 @@ public class CameraView extends FrameLayout { private PreviewImpl mPreviewImpl; + private Lifecycle mLifecycle; private boolean mIsStarted; public CameraView(@NonNull Context context) { @@ -154,6 +173,7 @@ public class CameraView extends FrameLayout { return true; } }); + mLifecycle = null; } @Override @@ -209,8 +229,34 @@ public class CameraView extends FrameLayout { return mIsStarted; } + @Override + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + if (mLifecycle != null && mLifecycle.getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) { + // Potentially update the UI + if (enabled) { + start(); + } else { + stop(); + } + } + } + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + public void onResume(LifecycleOwner owner) { + mLifecycle = owner.getLifecycle(); + start(); + } + + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + public void onPause(LifecycleOwner owner) { + mLifecycle = owner.getLifecycle(); + stop(); + } + public void start() { - if (mIsStarted) { + if (mIsStarted || !isEnabled()) { // Already started, do nothing. return; }