Skip to content

Commit

Permalink
Use numbers instead of Build.VERSION_CODES.*
Browse files Browse the repository at this point in the history
Sort some API Level dependent codes. This also fixes crash on Galaxy
Nexus running API Level 14.

Change-Id: I09f0c3bb218ec24bc71991a7be9f84811047341e
  • Loading branch information
yaraki committed Oct 14, 2016
1 parent ebc1772 commit f0dddd7
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/res/layout/include_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>

</FrameLayout>
</FrameLayout>
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ android {
main.java.srcDirs += 'src/main/api9'
main.java.srcDirs += 'src/main/api14'
main.java.srcDirs += 'src/main/api21'
main.java.srcDirs += 'src/main/api23'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private static ViewAssertion showingPreview() {
return new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (android.os.Build.VERSION.SDK_INT < 14) {
return;
}
CameraView cameraView = (CameraView) view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ void stop() {
private void setUpPreview() {
try {
if (mPreview.getOutputClass() == SurfaceHolder.class) {
final boolean needsToStopPreview = mShowingPreview &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH;
final boolean needsToStopPreview = mShowingPreview && Build.VERSION.SDK_INT < 14;
if (needsToStopPreview) {
mCamera.stopPreview();
}
Expand Down Expand Up @@ -233,16 +232,20 @@ public void onPictureTaken(byte[] data, Camera camera) {

@Override
void setDisplayOrientation(int displayOrientation) {
if (mDisplayOrientation == displayOrientation) {
return;
}
mDisplayOrientation = displayOrientation;
if (isCameraOpened()) {
int cameraRotation = calcCameraRotation(displayOrientation);
mCameraParameters.setRotation(cameraRotation);
mCamera.setParameters(mCameraParameters);
if (mShowingPreview) {
final boolean needsToStopPreview = mShowingPreview && Build.VERSION.SDK_INT < 14;
if (needsToStopPreview) {
mCamera.stopPreview();
}
mCamera.setDisplayOrientation(cameraRotation);
if (mShowingPreview) {
if (needsToStopPreview) {
mCamera.startPreview();
}
}
Expand Down Expand Up @@ -299,9 +302,10 @@ private AspectRatio chooseAspectRatio() {
}

private void adjustCameraParameters() {
final SortedSet<Size> sizes = mPreviewSizes.sizes(mAspectRatio);
SortedSet<Size> sizes = mPreviewSizes.sizes(mAspectRatio);
if (sizes == null) { // Not supported
mAspectRatio = chooseAspectRatio();
sizes = mPreviewSizes.sizes(mAspectRatio);
}
Size size = chooseOptimalSize(sizes);
final Camera.Size currentSize = mCameraParameters.getPictureSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import android.view.View;
import android.view.ViewGroup;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@TargetApi(14)
class TextureViewPreview extends PreviewImpl {

private final TextureView mTextureView;
Expand Down Expand Up @@ -64,10 +64,10 @@ public void onSurfaceTextureUpdated(SurfaceTexture surface) {
});
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
// This method is called only from Camera2.
@TargetApi(15)
@Override
void setBufferSize(int width, int height) {
// This method is called only from Camera2.
mTextureView.getSurfaceTexture().setDefaultBufferSize(width, height);
}

Expand Down
24 changes: 8 additions & 16 deletions library/src/main/api21/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import java.util.Set;
import java.util.SortedSet;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@TargetApi(21)
class Camera2 extends CameraViewImpl {

private static final String TAG = "Camera2";
Expand Down Expand Up @@ -390,27 +390,19 @@ private void collectCameraInfo() {
mPreviewSizes.add(new Size(size.getWidth(), size.getHeight()));
}
mPictureSizes.clear();
// try to get hi-res output sizes for Marshmallow and higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
android.util.Size[] outputSizes = map.getHighResolutionOutputSizes(ImageFormat.JPEG);
if (outputSizes != null) {
for (android.util.Size size : map.getHighResolutionOutputSizes(ImageFormat.JPEG)) {
mPictureSizes.add(new Size(size.getWidth(), size.getHeight()));
}
}
}
// fallback camera sizes and lower than Marshmallow
if (mPictureSizes.ratios().size() == 0) {
for (android.util.Size size : map.getOutputSizes(ImageFormat.JPEG)) {
mPictureSizes.add(new Size(size.getWidth(), size.getHeight()));
}
}
collectPictureSizes(mPictureSizes, map);

if (!mPreviewSizes.ratios().contains(mAspectRatio)) {
mAspectRatio = mPreviewSizes.ratios().iterator().next();
}
}

protected void collectPictureSizes(SizeMap sizes, StreamConfigurationMap map) {
for (android.util.Size size : map.getOutputSizes(ImageFormat.JPEG)) {
mPictureSizes.add(new Size(size.getWidth(), size.getHeight()));
}
}

private void prepareImageReader() {
Size largest = mPictureSizes.sizes(mAspectRatio).last();
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2016 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.google.android.cameraview;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.camera2.params.StreamConfigurationMap;


@TargetApi(23)
class Camera2Api23 extends Camera2 {

Camera2Api23(Callback callback, PreviewImpl preview, Context context) {
super(callback, preview, context);
}

@Override
protected void collectPictureSizes(SizeMap sizes, StreamConfigurationMap map) {
// Try to get hi-res output sizes
android.util.Size[] outputSizes = map.getHighResolutionOutputSizes(ImageFormat.JPEG);
if (outputSizes != null) {
for (android.util.Size size : map.getHighResolutionOutputSizes(ImageFormat.JPEG)) {
sizes.add(new Size(size.getWidth(), size.getHeight()));
}
}
if (sizes.isEmpty()) {
super.collectPictureSizes(sizes, map);
}
}

}
10 changes: 7 additions & 3 deletions library/src/main/base/com/google/android/cameraview/SizeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ public boolean add(Size size) {
return true;
}

public Set<AspectRatio> ratios() {
Set<AspectRatio> ratios() {
return mRatios.keySet();
}

public SortedSet<Size> sizes(AspectRatio ratio) {
SortedSet<Size> sizes(AspectRatio ratio) {
return mRatios.get(ratio);
}

public void clear() {
void clear() {
mRatios.clear();
}

boolean isEmpty() {
return mRatios.isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@ public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// Internal setup
final PreviewImpl preview;
if (Build.VERSION.SDK_INT >= 24 || Build.VERSION.SDK_INT < 14) {
if (Build.VERSION.SDK_INT < 14) {
preview = new SurfaceViewPreview(context, this);
} else {
preview = new TextureViewPreview(context, this);
}
mCallbacks = new CallbackBridge();
if (Build.VERSION.SDK_INT < 21) {
mImpl = new Camera1(mCallbacks, preview);
} else {
} else if (Build.VERSION.SDK_INT < 23) {
mImpl = new Camera2(mCallbacks, preview, context);
} else {
mImpl = new Camera2Api23(mCallbacks, preview, context);
}
// Attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView, defStyleAttr,
Expand Down

0 comments on commit f0dddd7

Please sign in to comment.