From 8b3e6c3a69ecf37aa7eab2d5b026ea1633e9ad3d Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 21 Sep 2018 16:04:20 -0300 Subject: [PATCH] Fix demo app null pointer, add null annotations --- .../otaliastudios/cameraview/CameraView.java | 51 +++++++++++-------- .../com/otaliastudios/cameraview/Gesture.java | 4 +- .../cameraview/demo/CameraActivity.java | 6 +-- .../cameraview/demo/Control.java | 4 +- .../cameraview/demo/ControlView.java | 4 +- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index ff300152..10a243ff 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -445,7 +445,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * @param action which action should be assigned * @return true if this action could be assigned to this gesture */ - public boolean mapGesture(@NonNull Gesture gesture, GestureAction action) { + public boolean mapGesture(@NonNull Gesture gesture, @NonNull GestureAction action) { GestureAction none = GestureAction.NONE; if (gesture.isAssignableTo(action)) { mGestureMap.put(gesture, action); @@ -490,6 +490,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * @param gesture which gesture to inspect * @return mapped action */ + @NonNull public GestureAction getGestureAction(@NonNull Gesture gesture) { return mGestureMap.get(gesture); } @@ -583,7 +584,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param owner the owner activity or fragment */ - public void setLifecycleOwner(LifecycleOwner owner) { + public void setLifecycleOwner(@NonNull LifecycleOwner owner) { if (mLifecycle != null) mLifecycle.removeObserver(this); mLifecycle = owner.getLifecycle(); mLifecycle.addObserver(this); @@ -616,7 +617,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * @return true if we can go on, false otherwise. */ @SuppressLint("NewApi") - protected boolean checkPermissions(Mode mode, Audio audio) { + protected boolean checkPermissions(@NonNull Mode mode, @NonNull Audio audio) { checkPermissionsManifestOrThrow(mode, audio); // Manifest is OK at this point. Let's check runtime permissions. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true; @@ -641,7 +642,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * If the developer did not add this to its manifest, throw and fire warnings. * (Hoping this is not caught elsewhere... we should test). */ - private void checkPermissionsManifestOrThrow(Mode mode, Audio audio) { + private void checkPermissionsManifestOrThrow(@NonNull Mode mode, @NonNull Audio audio) { if (mode == Mode.VIDEO && audio == Audio.ON) { try { PackageManager manager = getContext().getPackageManager(); @@ -695,7 +696,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param control desired value */ - public void set(Control control) { + public void set(@NonNull Control control) { if (control instanceof Audio) { setAudio((Audio) control); } else if (control instanceof Facing) { @@ -802,7 +803,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param gridMode desired grid mode */ - public void setGrid(Grid gridMode) { + public void setGrid(@NonNull Grid gridMode) { mGridLinesLayout.setGridMode(gridMode); } @@ -811,6 +812,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the current grid mode. * @return the current grid mode */ + @NonNull public Grid getGrid() { return mGridLinesLayout.getGridMode(); } @@ -842,7 +844,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param hdr desired hdr value */ - public void setHdr(Hdr hdr) { + public void setHdr(@NonNull Hdr hdr) { mCameraController.setHdr(hdr); } @@ -859,7 +861,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param preview desired preview engine */ - public void setPreview(Preview preview) { + public void setPreview(@NonNull Preview preview) { mPreview = preview; } @@ -868,6 +870,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the current hdr value. * @return the current hdr value */ + @NonNull public Hdr getHdr() { return mCameraController.getHdr(); } @@ -894,7 +897,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param location current location */ - public void setLocation(Location location) { + public void setLocation(@Nullable Location location) { mCameraController.setLocation(location); } @@ -921,7 +924,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param whiteBalance desired white balance behavior. */ - public void setWhiteBalance(WhiteBalance whiteBalance) { + public void setWhiteBalance(@NonNull WhiteBalance whiteBalance) { mCameraController.setWhiteBalance(whiteBalance); } @@ -930,6 +933,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Returns the current white balance behavior. * @return white balance value. */ + @NonNull public WhiteBalance getWhiteBalance() { return mCameraController.getWhiteBalance(); } @@ -943,7 +947,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param facing a facing value. */ - public void setFacing(Facing facing) { + public void setFacing(@NonNull Facing facing) { mCameraController.setFacing(facing); } @@ -952,6 +956,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the facing camera currently being used. * @return a facing value. */ + @NonNull public Facing getFacing() { return mCameraController.getFacing(); } @@ -989,7 +994,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * @param flash desired flash mode. */ - public void setFlash(Flash flash) { + public void setFlash(@NonNull Flash flash) { mCameraController.setFlash(flash); } @@ -998,6 +1003,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the current flash mode. * @return a flash mode */ + @NonNull public Flash getFlash() { return mCameraController.getFlash(); } @@ -1011,7 +1017,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param audio desired audio value */ - public void setAudio(Audio audio) { + public void setAudio(@NonNull Audio audio) { if (audio == getAudio() || isStopped()) { // Check did took place, or will happen on start(). @@ -1035,6 +1041,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the current audio value. * @return the current audio value */ + @NonNull public Audio getAudio() { return mCameraController.getAudio(); } @@ -1062,7 +1069,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param mode desired session type. */ - public void setMode(Mode mode) { + public void setMode(@NonNull Mode mode) { if (mode == getMode() || isStopped()) { // Check did took place, or will happen on start(). @@ -1086,6 +1093,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the current mode. * @return the current mode */ + @NonNull public Mode getMode() { return mCameraController.getMode(); } @@ -1158,7 +1166,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param cameraListener a listener for events. */ - public void addCameraListener(CameraListener cameraListener) { + public void addCameraListener(@Nullable CameraListener cameraListener) { if (cameraListener != null) { mListeners.add(cameraListener); } @@ -1170,7 +1178,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param cameraListener a listener for events. */ - public void removeCameraListener(CameraListener cameraListener) { + public void removeCameraListener(@Nullable CameraListener cameraListener) { if (cameraListener != null) { mListeners.remove(cameraListener); } @@ -1192,7 +1200,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param processor a frame processor. */ - public void addFrameProcessor(FrameProcessor processor) { + public void addFrameProcessor(@Nullable FrameProcessor processor) { if (processor != null) { mFrameProcessors.add(processor); } @@ -1204,7 +1212,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param processor a frame processor */ - public void removeFrameProcessor(FrameProcessor processor) { + public void removeFrameProcessor(@Nullable FrameProcessor processor) { if (processor != null) { mFrameProcessors.remove(processor); } @@ -1296,7 +1304,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * @param durationMillis recording max duration * */ - public void takeVideo(File file, int durationMillis) { + public void takeVideo(@Nullable File file, int durationMillis) { final int old = getVideoMaxDuration(); addCameraListener(new CameraListener() { @Override @@ -1309,7 +1317,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { takeVideo(file); } - public void takeVideoSnapshot(File file, int durationMillis) { + public void takeVideoSnapshot(@Nullable File file, int durationMillis) { final int old = getVideoMaxDuration(); addCameraListener(new CameraListener() { @Override @@ -1462,7 +1470,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * * @param codec requested video codec */ - public void setVideoCodec(VideoCodec codec) { + public void setVideoCodec(@NonNull VideoCodec codec) { mCameraController.setVideoCodec(codec); } @@ -1471,6 +1479,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * Gets the current encoder for video recordings. * @return the current video codec */ + @NonNull public VideoCodec getVideoCodec() { return mCameraController.getVideoCodec(); } diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java index 536fc5fa..f9a17368 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java @@ -1,6 +1,8 @@ package com.otaliastudios.cameraview; +import android.support.annotation.NonNull; + import java.util.Arrays; import java.util.List; @@ -74,7 +76,7 @@ public enum Gesture { private List mControls; - boolean isAssignableTo(GestureAction control) { + boolean isAssignableTo(@NonNull GestureAction control) { return control == GestureAction.NONE || mControls.contains(control); } diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java index 05488795..c52c83d5 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java @@ -42,7 +42,7 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis camera = findViewById(R.id.camera); camera.setLifecycleOwner(this); camera.addCameraListener(new CameraListener() { - public void onCameraOpened(CameraOptions options) { onOpened(); } + public void onCameraOpened(CameraOptions options) { onOpened(options); } public void onPictureTaken(PictureResult result) { onPicture(result); } public void onVideoTaken(VideoResult result) { onVideo(result); } public void onCameraError(@NonNull CameraException exception) { @@ -81,11 +81,11 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis Toast.makeText(this, content, length).show(); } - private void onOpened() { + private void onOpened(CameraOptions options) { ViewGroup group = (ViewGroup) controlPanel.getChildAt(0); for (int i = 0; i < group.getChildCount(); i++) { ControlView view = (ControlView) group.getChildAt(i); - view.onCameraOpened(camera); + view.onCameraOpened(camera, options); } } diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java index 386e7a24..2c4b9b4d 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java @@ -1,6 +1,7 @@ package com.otaliastudios.cameraview.demo; import android.graphics.Color; +import android.support.annotation.NonNull; import android.view.View; import android.view.ViewGroup; @@ -64,8 +65,7 @@ public enum Control { return last; } - public Collection getValues(CameraView view) { - CameraOptions options = view.getCameraOptions(); + public Collection getValues(CameraView view, @NonNull CameraOptions options) { switch (this) { case WIDTH: case HEIGHT: diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/ControlView.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/ControlView.java index 22330416..28b5724d 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/ControlView.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/ControlView.java @@ -57,8 +57,8 @@ public class ControlView extends LinearLayout implements Spinner.OnItemSe } @SuppressWarnings("all") - public void onCameraOpened(CameraView view) { - values = new ArrayList(control.getValues(view)); + public void onCameraOpened(CameraView view, CameraOptions options) { + values = new ArrayList(control.getValues(view, options)); value = (Value) control.getCurrentValue(view); valuesStrings = new ArrayList<>(); for (Value value : values) {