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.net.Uri 24 import android.tools.common.traces.component.ComponentNameMatcher 25 26 /** Helper to launch the Calendar app. */ 27 class CalendarAppHelper 28 @JvmOverloads 29 constructor( 30 instrumentation: Instrumentation, 31 pkgManager: PackageManager = instrumentation.context.packageManager 32 ) : 33 StandardAppHelper( 34 instrumentation, 35 CalendarAppHelper.Companion.getCalendarLauncherName(pkgManager), 36 CalendarAppHelper.Companion.getCalendarComponent(pkgManager) 37 ) { 38 companion object { getCalendarIntentnull39 private fun getCalendarIntent(): Intent { 40 val epochEventStartTime = 0 41 val uri = Uri.parse("content://com.android.calendar/time/" + epochEventStartTime) 42 val intent = Intent(Intent.ACTION_VIEW) 43 intent.data = uri 44 intent.putExtra("VIEW", "DAY") 45 intent.flags = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 46 return intent 47 } 48 getResolveInfonull49 private fun getResolveInfo(pkgManager: PackageManager): ResolveInfo { 50 val intent = CalendarAppHelper.Companion.getCalendarIntent() 51 return pkgManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) 52 ?: error("unable to resolve calendar activity") 53 } 54 getCalendarComponentnull55 private fun getCalendarComponent(pkgManager: PackageManager): ComponentNameMatcher { 56 val resolveInfo = CalendarAppHelper.Companion.getResolveInfo(pkgManager) 57 return ComponentNameMatcher( 58 resolveInfo.activityInfo.packageName, 59 className = resolveInfo.activityInfo.name 60 ) 61 } 62 getCalendarLauncherNamenull63 private fun getCalendarLauncherName(pkgManager: PackageManager): String { 64 val resolveInfo = CalendarAppHelper.Companion.getResolveInfo(pkgManager) 65 return resolveInfo.loadLabel(pkgManager).toString() 66 } 67 } 68 } 69