1 /*
2  * Copyright (C) 2018 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.server.wm.component;
18 
19 import android.content.ComponentName;
20 
21 /**
22  * Base class for Components constants holding class.
23  *
24  * Every testing APK should have Components class in Java package that equals to package of APK.
25  * Those Components class should extends {@link ComponentsBase}.
26  */
27 public class ComponentsBase {
28 
29     /**
30      * Build {@link ComponentName} constant which belongs to {@code componentsClass}'s package.
31      * @param componentsClass Components class object which has the same package of APK.
32      * @param className simple class name (has no '.') or fully qualified class name.
33      * @return {@link ComponentName} object.
34      */
component( Class<? extends ComponentsBase> componentsClass, String className)35     protected static ComponentName component(
36             Class<? extends ComponentsBase> componentsClass, String className) {
37         if (className.startsWith(".")) {
38             throw new AssertionError("Class name should not start with '.'");
39         }
40         final String packageName = getPackageName(componentsClass);
41         final boolean isSimpleClassName = className.indexOf('.') < 0;
42         final String fullClassName = isSimpleClassName ? packageName + "." + className : className;
43         return new ComponentName(packageName, fullClassName);
44     }
45 
46     /**
47      * Get package name of {@code componentsClass}.
48      * @param componentsClass Components class object which has the same package of APK.
49      * @return package name of APK.
50      */
51     protected static String getPackageName(Class<? extends ComponentsBase> componentsClass) {
52         if (!"Components".equals(componentsClass.getSimpleName())) {
53             throw new AssertionError("The class name must be 'Components'");
54         }
55         return componentsClass.getPackage().getName();
56     }
57 }
58