1 /*
2  * Copyright (C) 2021 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 com.android.car;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 
22 import com.android.car.internal.NotificationHelperBase;
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 import java.lang.reflect.Constructor;
26 
27 /**
28  * Declared all dependencies into builtin package, mostly for Activity / class / method names.
29  *
30  * <p> This is for tracking all dependencies done through java reflection.
31  */
32 public final class BuiltinPackageDependency {
BuiltinPackageDependency()33     private BuiltinPackageDependency() {};
34 
35     /** Package name of builtin, Will be necessary to send Intent. */
36     public static final String BUILTIN_CAR_SERVICE_PACKAGE_NAME = "com.android.car";
37 
38     /** {@code com.android.car.am.ContinuousBlankActivity} */
39     public static final String BLANK_ACTIVITY_CLASS = "com.android.car.am.ContinuousBlankActivity";
40 
41     /** {@code com.android.car.pm.CarSafetyAccessibilityService} */
42     public static final String CAR_ACCESSIBILITY_SERVICE_CLASS =
43             "com.android.car.pm.CarSafetyAccessibilityService";
44 
45     /** {@code com.android.car.CarPerUserService} */
46     public static final String CAR_USER_PER_SERVICE_CLASS = "com.android.car.CarPerUserService";
47 
48     public static final String EVS_HAL_WRAPPER_CLASS = "com.android.car.evs.EvsHalWrapperImpl";
49 
50     /** {@code com.android.car.admin.NotificationHelper} class. */
51     @VisibleForTesting
52     public static final String NOTIFICATION_HELPER_CLASS =
53             "com.android.car.admin.NotificationHelper";
54 
55     /** Returns {@code ComponentName} string for builtin package component */
getComponentName(String className)56     public static String getComponentName(String className) {
57         return new StringBuilder()
58                 .append(BUILTIN_CAR_SERVICE_PACKAGE_NAME)
59                 .append('/')
60                 .append(className)
61                 .toString();
62     }
63 
64     /** Sets builtin package's class to the passed Intent and returns the Intent. */
addClassNameToIntent(Context context, Intent intent, String className)65     public static Intent addClassNameToIntent(Context context, Intent intent, String className) {
66         intent.setClassName(context.getPackageName(), className);
67         return intent;
68     }
69 
70     /**
71      * Creates {@link NotificationHelperBase} implemented from Builtin Car service.
72      *
73      * @param builtinContext {@code Context} of the builtin CarService where the
74      *                       NotificationHelper's code is loaded.
75      */
createNotificationHelper(Context builtinContext)76     public static NotificationHelperBase createNotificationHelper(Context builtinContext) {
77         try {
78             Class helperClass = builtinContext.getClassLoader().loadClass(
79                     NOTIFICATION_HELPER_CLASS);
80             // Use default constructor always
81             Constructor constructor = helperClass.getConstructor(new Class[]{Context.class});
82             return (NotificationHelperBase) constructor.newInstance(builtinContext);
83         } catch (Exception e) {
84             throw new IllegalStateException("Cannot load class:" + NOTIFICATION_HELPER_CLASS, e);
85         }
86     }
87 }
88