Skip to content

Commit

Permalink
Change the aspect ratio in the demo app
Browse files Browse the repository at this point in the history
This also enhances the corresponding test.

Change-Id: I5265b499823763845c1c0744d5fd04a44ae583b3
  • Loading branch information
yaraki committed Dec 13, 2016
1 parent d2ceaa7 commit 061e4a3
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* 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.demo;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.android.cameraview.AspectRatio;

import java.util.Set;


/**
* A simple dialog that allows user to pick an aspect ratio.
*/
public class AspectRatioFragment extends DialogFragment {

private static final String ARG_ASPECT_RATIOS = "aspect_ratios";
private static final String ARG_CURRENT_ASPECT_RATIO = "current_aspect_ratio";

private Listener mListener;

public static AspectRatioFragment newInstance(Set<AspectRatio> ratios,
AspectRatio currentRatio) {
final AspectRatioFragment fragment = new AspectRatioFragment();
final Bundle args = new Bundle();
args.putParcelableArray(ARG_ASPECT_RATIOS,
ratios.toArray(new AspectRatio[ratios.size()]));
args.putParcelable(ARG_CURRENT_ASPECT_RATIO, currentRatio);
fragment.setArguments(args);
return fragment;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
mListener = (Listener) context;
}

@Override
public void onDetach() {
mListener = null;
super.onDetach();
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle args = getArguments();
final AspectRatio[] ratios = (AspectRatio[]) args.getParcelableArray(ARG_ASPECT_RATIOS);
if (ratios == null) {
throw new RuntimeException("No ratios");
}
final AspectRatio current = args.getParcelable(ARG_CURRENT_ASPECT_RATIO);
final AspectRatioAdapter adapter = new AspectRatioAdapter(ratios, current);
return new AlertDialog.Builder(getActivity())
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
mListener.onAspectRatioSelected(ratios[position]);
}
})
.create();
}

private static class AspectRatioAdapter extends BaseAdapter {

private final AspectRatio[] mRatios;
private final AspectRatio mCurrentRatio;

AspectRatioAdapter(AspectRatio[] ratios, AspectRatio current) {
mRatios = ratios;
mCurrentRatio = current;
}

@Override
public int getCount() {
return mRatios.length;
}

@Override
public AspectRatio getItem(int position) {
return mRatios[position];
}

@Override
public long getItemId(int position) {
return getItem(position).hashCode();
}

@Override
public View getView(int position, View view, ViewGroup parent) {
AspectRatioAdapter.ViewHolder holder;
if (view == null) {
view = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
holder = new AspectRatioAdapter.ViewHolder();
holder.text = (TextView) view.findViewById(android.R.id.text1);
view.setTag(holder);
} else {
holder = (AspectRatioAdapter.ViewHolder) view.getTag();
}
AspectRatio ratio = getItem(position);
StringBuilder sb = new StringBuilder(ratio.toString());
if (ratio.equals(mCurrentRatio)) {
sb.append(" *");
}
holder.text.setText(sb);
return view;
}

private static class ViewHolder {
TextView text;
}

}

public interface Listener {
void onAspectRatioSelected(@NonNull AspectRatio ratio);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,23 @@
import android.view.View;
import android.widget.Toast;

import com.google.android.cameraview.AspectRatio;
import com.google.android.cameraview.CameraView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;


/**
* This demo app saves the taken picture to a constant file.
* $ adb pull /sdcard/Android/data/com.google.android.cameraview.demo/files/Pictures/picture.jpg
*/
public class MainActivity extends AppCompatActivity implements
ActivityCompat.OnRequestPermissionsResultCallback {
ActivityCompat.OnRequestPermissionsResultCallback,
AspectRatioFragment.Listener {

private static final String TAG = "MainActivity";

Expand Down Expand Up @@ -184,6 +187,14 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.aspect_ratio:
if (mCameraView != null) {
final Set<AspectRatio> ratios = mCameraView.getSupportedAspectRatios();
final AspectRatio currentRatio = mCameraView.getAspectRatio();
AspectRatioFragment.newInstance(ratios, currentRatio)
.show(getSupportFragmentManager(), FRAGMENT_DIALOG);
}
break;
case R.id.switch_flash:
if (mCameraView != null) {
mCurrentFlash = (mCurrentFlash + 1) % FLASH_OPTIONS.length;
Expand All @@ -203,6 +214,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
return false;
}

@Override
public void onAspectRatioSelected(@NonNull AspectRatio ratio) {
if (mCameraView != null) {
Toast.makeText(this, ratio.toString(), Toast.LENGTH_SHORT).show();
mCameraView.setAspectRatio(ratio);
}
}

private Handler getBackgroundHandler() {
if (mBackgroundHandler == null) {
HandlerThread thread = new HandlerThread("background");
Expand Down
25 changes: 25 additions & 0 deletions demo/src/main/res/drawable/ic_aspect_ratio.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
~ 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.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M18,4L6,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,6c0,-1.1 -0.9,-2 -2,-2zM18,18L6,18L6,6h12v12z"/>
</vector>
6 changes: 6 additions & 0 deletions demo/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/aspect_ratio"
android:icon="@drawable/ic_aspect_ratio"
android:title="@string/aspect_ratio"
app:showAsAction="ifRoom"/>

<item
android:id="@+id/switch_flash"
android:icon="@drawable/ic_flash_auto"
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="camera_permission_confirmation">This app demonstrates the usage of CameraView. In order to do that, it needs permission to access camera.</string>
<string name="camera_permission_not_granted">Camera app cannot do anything without camera permission.</string>
<string name="picture_taken">Picture taken</string>
<string name="aspect_ratio">Aspect ratio</string>
<string name="switch_flash">Switch flash</string>
<string name="switch_camera">Switch camera</string>
<string name="flash_auto">Flash auto</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;

import android.support.annotation.NonNull;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;

import org.hamcrest.Matcher;

class CameraViewActions {

static ViewAction setAspectRatio(@NonNull final AspectRatio ratio) {
return new ViewAction() {

@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(CameraView.class);
}

@Override
public String getDescription() {
return "Set aspect ratio to " + ratio;
}

@Override
public void perform(UiController controller, View view) {
((CameraView) view).setAspectRatio(ratio);
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.support.annotation.NonNull;
import android.view.View;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

class CameraViewMatchers {

static Matcher<View> hasAspectRatio(@NonNull final AspectRatio ratio) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("has aspect ratio of " + ratio);
}

@Override
protected boolean matchesSafely(View view) {
return ratio.equals(((CameraView) view).getAspectRatio());
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static android.support.test.espresso.matcher.ViewMatchers.withId;

import static com.google.android.cameraview.AspectRatioIsCloseTo.closeToOrInverse;
import static com.google.android.cameraview.CameraViewActions.setAspectRatio;
import static com.google.android.cameraview.CameraViewMatchers.hasAspectRatio;

import static junit.framework.Assert.assertFalse;

Expand Down Expand Up @@ -110,31 +112,13 @@ public void preview_isShowing() throws Exception {

@Test
public void testAspectRatio() {
onView(withId(R.id.camera))
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
AspectRatio ratio = cameraView.getAspectRatio();
assertThat(ratio, is(notNullValue()));
Set<AspectRatio> ratios = cameraView.getSupportedAspectRatios();
assertThat(ratios.size(), is(greaterThanOrEqualTo(1)));
assertThat(ratios, hasItem(ratio));
if (ratios.size() == 1) {
return;
}
// Pick one ratio to change to
for (AspectRatio r : ratios) {
if (!r.equals(ratio)) {
ratio = r;
break;
}
}
assert ratio != null;
cameraView.setAspectRatio(ratio);
assertThat(cameraView.getAspectRatio(), is(equalTo(ratio)));
}
});
final CameraView cameraView = (CameraView) rule.getActivity().findViewById(R.id.camera);
final Set<AspectRatio> ratios = cameraView.getSupportedAspectRatios();
for (AspectRatio ratio : ratios) {
onView(withId(R.id.camera))
.perform(setAspectRatio(ratio))
.check(matches(hasAspectRatio(ratio)));
}
}

@Test
Expand Down

0 comments on commit 061e4a3

Please sign in to comment.