Skip to content

Commit

Permalink
Add app:facing
Browse files Browse the repository at this point in the history
Change-Id: I6446e9bfe231a9ba5857b996a204263112f1a86e
  • Loading branch information
yaraki committed May 31, 2016
1 parent 052ebe6 commit 9609fac
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 87 deletions.

This file was deleted.

3 changes: 2 additions & 1 deletion demo/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
app:focusMode="continuousPicture"/>
app:focusMode="continuousPicture"
app:facing="back"/>

</FrameLayout>
3 changes: 2 additions & 1 deletion demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:focusMode="continuousPicture"/>
app:focusMode="continuousPicture"
app:facing="back"/>

</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,9 @@ public void testSetup() {
@Test
@FlakyTest
public void preview_isShowing() throws Exception {
onView(withId(R.id.texture_view))
.perform(new WaitAction(1000))
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
TextureView textureView = (TextureView) view;
Bitmap bitmap = textureView.getBitmap();
int topLeft = bitmap.getPixel(0, 0);
int center = bitmap.getPixel(bitmap.getWidth() / 2, bitmap.getHeight() / 2);
int bottomRight = bitmap.getPixel(
bitmap.getWidth() - 1, bitmap.getHeight() - 1);
assertFalse("Preview possibly blank: " + Integer.toHexString(topLeft),
topLeft == center && center == bottomRight);
}
});
onView(withId(R.id.camera))
.perform(waitFor(1000))
.check(showingPreview());
}

@Test
Expand Down Expand Up @@ -219,6 +207,48 @@ public void check(View view, NoMatchingViewException noViewFoundException) {
});
}

@Test
public void testFacing() {
onView(withId(R.id.camera))
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
assertThat(cameraView.getFacing(), is(CameraView.FACING_BACK));
cameraView.setFacing(CameraView.FACING_FRONT);
assertThat(cameraView.getFacing(), is(CameraView.FACING_FRONT));
}
})
.perform(waitFor(1000))
.check(showingPreview());
}

private static ViewAction waitFor(final long ms) {
return new AnythingAction("wait") {
@Override
public void perform(UiController uiController, View view) {
SystemClock.sleep(ms);
}
};
}

private static ViewAssertion showingPreview() {
return new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
TextureView textureView = (TextureView) cameraView.findViewById(R.id.texture_view);
Bitmap bitmap = textureView.getBitmap();
int topLeft = bitmap.getPixel(0, 0);
int center = bitmap.getPixel(bitmap.getWidth() / 2, bitmap.getHeight() / 2);
int bottomRight = bitmap.getPixel(
bitmap.getWidth() - 1, bitmap.getHeight() - 1);
assertFalse("Preview possibly blank: " + Integer.toHexString(topLeft),
topLeft == center && center == bottomRight);
}
};
}

/**
* Wait for a camera to open.
*/
Expand Down Expand Up @@ -277,22 +307,6 @@ public void registerIdleTransitionCallback(ResourceCallback callback) {

}

private static class WaitAction extends AnythingAction {

private final long mMs;

public WaitAction(long ms) {
super("wait");
mMs = ms;
}

@Override
public void perform(UiController uiController, View view) {
SystemClock.sleep(mMs);
}

}

private static abstract class AnythingAction implements ViewAction {

private final String mDescription;
Expand All @@ -310,6 +324,7 @@ public Matcher<View> getConstraints() {
public String getDescription() {
return mDescription;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public CameraViewImpl(Callback callback) {

abstract int getFocusMode();

abstract void setFacing(int facing);

abstract int getFacing();

interface Callback {

void onCameraOpened();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ interface Constants {
int FOCUS_MODE_CONTINUOUS_VIDEO = 4;
int FOCUS_MODE_EDOF = 5;

int FACING_BACK = 0;
int FACING_FRONT = 1;
int FACING_EXTERNAL = 2;

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Camera1 extends CameraViewImpl {

private static final AspectRatio DEFAULT_ASPECT_RATIO = AspectRatio.of(4, 3);

private static final String TAG = "Camera1";

private final Context mContext;

private int mCameraId;
Expand All @@ -56,6 +58,8 @@ class Camera1 extends CameraViewImpl {

private int mFocusMode;

private int mFacing;

private static class PreviewInfo {
SurfaceTexture surface;
int width;
Expand Down Expand Up @@ -190,14 +194,29 @@ int getFocusMode() {
return mFocusMode;
}

@Override
void setFacing(int facing) {
if (mFacing != facing) {
mFacing = facing;
if (mCamera != null) {
stop();
start();
}
}
}

@Override
int getFacing() {
return mFacing;
}

/**
* This rewrites {@link #mCameraId} and {@link #mCameraInfo}.
*/
private void chooseCamera() {
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
Camera.getCameraInfo(i, mCameraInfo);
// TODO: Just choose the back-facing camera for now
if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
if (mCameraInfo.facing == mFacing) {
mCameraId = i;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Camera1Constants {
static String convertFocusMode(int focusMode) {
if (sFocusModesMap == null) {
sFocusModesMap = new SparseArrayCompat<>();
sFocusModesMap.put(Constants.FOCUS_MODE_OFF, Camera.Parameters.FOCUS_MODE_FIXED);
sFocusModesMap.put(Constants.FOCUS_MODE_OFF, Camera.Parameters.FOCUS_MODE_INFINITY);
sFocusModesMap.put(Constants.FOCUS_MODE_AUTO, Camera.Parameters.FOCUS_MODE_AUTO);
sFocusModesMap.put(Constants.FOCUS_MODE_MACRO, Camera.Parameters.FOCUS_MODE_MACRO);
sFocusModesMap.put(Constants.FOCUS_MODE_CONTINUOUS_PICTURE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public class CameraView extends FrameLayout {
public @interface FocusMode {
}

public static final int FACING_BACK = Constants.FACING_BACK;
public static final int FACING_FRONT = Constants.FACING_FRONT;
public static final int FACING_EXTERNAL = Constants.FACING_EXTERNAL;

@IntDef({FACING_BACK, FACING_FRONT, FACING_EXTERNAL})
@Retention(RetentionPolicy.SOURCE)
public @interface Facing {
}

private final CameraViewImpl mImpl;

private final CallbackBridge mCallbacks;
Expand All @@ -62,6 +71,7 @@ public CameraView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

@SuppressWarnings("WrongConstant")
public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// Internal setup
Expand All @@ -79,8 +89,8 @@ public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView, defStyleAttr,
R.style.Widget_CameraView);
mAdjustViewBounds = a.getBoolean(R.styleable.CameraView_android_adjustViewBounds, false);
//noinspection WrongConstant
setFocusMode(a.getInt(R.styleable.CameraView_focusMode, FOCUS_MODE_OFF));
setFacing(a.getInt(R.styleable.CameraView_facing, FACING_BACK));
a.recycle();
}

Expand Down Expand Up @@ -251,6 +261,27 @@ public int getFocusMode() {
return mImpl.getFocusMode();
}

/**
* Chooses camera by the direction it faces.
*
* @param facing The camera facing. Must be one of {@link #FACING_BACK},
* {@link #FACING_FRONT}, and {@link #FACING_EXTERNAL}
*/
public void setFacing(@Facing int facing) {
mImpl.setFacing(facing);
}

/**
* Gets the direction that the current camera faces.
*
* @return The camera facing.
*/
@Facing
public int getFacing() {
//noinspection WrongConstant
return mImpl.getFacing();
}

private class CallbackBridge implements CameraViewImpl.Callback {

private final ArrayList<Callback> mCallbacks = new ArrayList<>();
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@
<enum name="continuousVideo" value="4"/>
<enum name="edof" value="5"/>
</attr>
<attr name="facing" format="enum">
<enum name="back" value="0"/>
<enum name="front" value="1"/>
<enum name="external" value="2"/>
</attr>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions library/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<style name="Widget.CameraView" parent="android:Widget">
<item name="android:adjustViewBounds">false</item>
<item name="focusMode">off</item>
<item name="facing">back</item>
</style>

</resources>

0 comments on commit 9609fac

Please sign in to comment.