1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.tools.device.apphelpers 18 19 import android.app.Instrumentation 20 import android.content.Intent 21 import android.content.pm.PackageManager 22 import android.content.pm.ResolveInfo 23 import android.provider.MediaStore 24 import android.tools.common.traces.component.ComponentNameMatcher 25 26 /** Helper to launch the Camera app. */ 27 class CameraAppHelper 28 @JvmOverloads 29 constructor( 30 instrumentation: Instrumentation, 31 pkgManager: PackageManager = instrumentation.context.packageManager 32 ) : 33 StandardAppHelper( 34 instrumentation, 35 CameraAppHelper.Companion.getCameraLauncherName(pkgManager), 36 CameraAppHelper.Companion.getCameraComponent(pkgManager) 37 ) { 38 getOpenAppIntentnull39 override fun getOpenAppIntent(): Intent = 40 pkgManager.getLaunchIntentForPackage(packageName) 41 ?: error("Unable to find intent for camera") 42 43 companion object { 44 private fun getCameraIntent(): Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) 45 46 private fun getResolveInfo(pkgManager: PackageManager): ResolveInfo = 47 pkgManager.resolveActivity( 48 CameraAppHelper.Companion.getCameraIntent(), 49 PackageManager.MATCH_DEFAULT_ONLY 50 ) 51 ?: error("unable to resolve camera activity") 52 53 private fun getCameraComponent(pkgManager: PackageManager): ComponentNameMatcher = 54 ComponentNameMatcher( 55 getResolveInfo(pkgManager).activityInfo.packageName, 56 className = "" 57 ) 58 59 private fun getCameraLauncherName(pkgManager: PackageManager): String = 60 CameraAppHelper.Companion.getResolveInfo(pkgManager).loadLabel(pkgManager).toString() 61 } 62 } 63