• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AlarmClock
24 import android.tools.common.traces.component.ComponentNameMatcher
25 
26 /** Helper to launch the Camera app. */
27 class ClockAppHelper
28 @JvmOverloads
29 constructor(
30     instrumentation: Instrumentation,
31     pkgManager: PackageManager = instrumentation.context.packageManager
32 ) :
33     StandardAppHelper(
34         instrumentation,
35         getClockLauncherName(pkgManager),
36         getClockComponent(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 getClockIntent(): Intent = Intent(AlarmClock.ACTION_SET_ALARM)
45 
46         private fun getResolveInfo(pkgManager: PackageManager): ResolveInfo =
47             pkgManager.resolveActivity(getClockIntent(), PackageManager.MATCH_DEFAULT_ONLY)
48                 ?: error("unable to resolve camera activity")
49 
50         private fun getClockComponent(pkgManager: PackageManager): ComponentNameMatcher =
51             ComponentNameMatcher(
52                 getResolveInfo(pkgManager).activityInfo.packageName,
53                 className = ""
54             )
55 
56         private fun getClockLauncherName(pkgManager: PackageManager): String =
57             getResolveInfo(pkgManager).loadLabel(pkgManager).toString()
58     }
59 }
60