Integrate Lifecycle events with CameraView for easier use.

pull/1/head
Yuval Peress 8 years ago
parent 3282a95c61
commit 4a6ef7659b
  1. 1
      build.gradle
  2. 5
      camerakit/build.gradle
  3. 50
      camerakit/src/main/java/com/flurgle/camerakit/CameraView.java

@ -15,6 +15,7 @@ buildscript {
allprojects { allprojects {
repositories { repositories {
jcenter() jcenter()
maven { url 'https://maven.google.com' }
} }
} }

@ -33,6 +33,11 @@ android {
dependencies { dependencies {
compile 'com.android.support:appcompat-v7:25.2.0' 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' apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'

@ -2,6 +2,10 @@ package com.flurgle.camerakit;
import android.Manifest; import android.Manifest;
import android.app.Activity; 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.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.content.pm.PackageManager; 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_PICTURE;
import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_STRICT; 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:
* <pre>
* {@code
* protected void onCreate(@Nullable Bundle savedInstanceState) {
* super.onCreate(savedInstanceState);
* setContentView(R.layout.my_view);
* ...
* getLifecycle().addObserver(mCameraView);
* }
* }
* </pre>
*/
public class CameraView extends FrameLayout implements LifecycleObserver {
private static Handler sWorkerHandler; private static Handler sWorkerHandler;
@ -81,6 +99,7 @@ public class CameraView extends FrameLayout {
private PreviewImpl mPreviewImpl; private PreviewImpl mPreviewImpl;
private Lifecycle mLifecycle;
private boolean mIsStarted; private boolean mIsStarted;
public CameraView(@NonNull Context context) { public CameraView(@NonNull Context context) {
@ -154,6 +173,7 @@ public class CameraView extends FrameLayout {
return true; return true;
} }
}); });
mLifecycle = null;
} }
@Override @Override
@ -209,8 +229,34 @@ public class CameraView extends FrameLayout {
return mIsStarted; 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() { public void start() {
if (mIsStarted) { if (mIsStarted || !isEnabled()) {
// Already started, do nothing. // Already started, do nothing.
return; return;
} }

Loading…
Cancel
Save