项目场景:
要求相机只使用Camera2
测试原理:
通过命令启动相机,拍照,查询图片数量
@GmsTest(requirement = "GMS-7.5-002")
@Test
public void testAssistantOpenRearCameraWithoutVoiceInteraction() throws DeviceNotAvailableException, InterruptedException {
Assume.assumeTrue("Device does not support rear camera.",
FeatureUtil.hasSystemFeature(getDevice(), "android.hardware.camera"));
StringBuilder cmdBuilder = new StringBuilder();
cmdBuilder
.append(String.format("am start -a %s ", new Object[]{"android.media.action.STILL_IMAGE_CAMERA"})).append(String.format("--ez %s %s ", new Object[]{"com.google.assistant.extra.CAMERA_OPEN_ONLY", "true"})).append(String.format("--ez %s %s ", new Object[]{"android.intent.extra.CAMERA_OPEN_ONLY", "true"})).append(String.format("--ez %s %s ", new Object[]{"isVoiceQuery", "true"})).append(String.format("--ez %s %s ", new Object[]{"NoUiQuery", "true"})).append(String.format("--es %s %s ", new Object[]{"android.intent.extra.REFERRER_NAME", "android-app://com.google.android.googlequicksearchbox/https/www.google.com"})).append("--activity-clear-task");
getDevice().executeShellCommand(cmdBuilder.toString());
sleep();
MetricsReportLog reportLog = new MetricsReportLog(getBuild(), getAbi().toString(), String.format("%s#testAssistantOpenRearCameraWithoutVoiceInteraction", new Object[]{getClass().getCanonicalName()}), "GtsAssistantTestCases", "report_for_assistant_test");
reportLog.addValue("test_name", "testAssistantOpenRearCameraWithoutVoiceInteraction", ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.addValue("status", (
getActiveCameraFace().equals("back") &&
getCameraDirectoryJpegAmount() == this.mImgCountBeforeTesting), ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.submit();
if (ApiLevelUtil.isAfter(getDevice(), 28)) {
Assert.assertTrue(getActiveCameraFace().equals("back"));
Assert.assertTrue((getCameraDirectoryJpegAmount() == this.mImgCountBeforeTesting));
}
}
private int getCameraDirectoryJpegAmount() throws DeviceNotAvailableException {
try {
List<String> results = Arrays.asList(getDevice().executeShellCommand("find $EXTERNAL_STORAGE/ -size +100k | grep -i '\\.jpg' | wc -l")
.trim().split("\n"));
return Integer.parseInt(results.get(results.size() - 1));
} catch (NumberFormatException e) {
return 0;
}
}
解决方案:
- 修改AndroidManifest
<category android:name="android.intent.category.VOICE" />
- 修改CaptureModule.java
//在init()调用
private void initModeByIntent() {
String action = mActivity.getIntent().getAction();
Log.d(TAG, "initModeByIntent: " + action);
Bundle bundle = mActivity.getIntent().getExtras();
if (bundle != null) {
Log.v(TAG, "initModeByIntent bundle :" + bundle.toString());
}
if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)) {
mIntentMode = INTENT_MODE_STILL_IMAGE_CAMERA;
Set<String> categories = mActivity.getIntent().getCategories();
if (categories != null) {
for(String categorie: categories) {
if(categorie.equals("android.intent.category.VOICE")) {
mIsVoiceTakePhoto = true;
}
}
}
boolean isOpenOnly = mActivity.getIntent().getBooleanExtra(
"com.google.assistant.extra.CAMERA_OPEN_ONLY", false);
Log.v(TAG, "initModeByIntent isOpenOnly = " + isOpenOnly);
if (isOpenOnly) {
mIsVoiceTakePhoto = false;
}
}
}
private class MainHandler extends Handler {
public MainHandler() {
super(Looper.getMainLooper());
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case VOICE_INTERACTION_CAPTURE: {
takePictureNow();
mIsVoiceTakePhoto = false;
break;
}
}
}
}
在OpenCallback的onReadyForCapture()添加拍照处理:
if (mIntentMode == INTENT_MODE_STILL_IMAGE_CAMERA && mIsVoiceTakePhoto) {
mHandler.sendEmptyMessageDelayed(VOICE_INTERACTION_CAPTURE, 500);
}