1 /*
2  * Copyright (C) 2019 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.backgroundactivity.appa;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.server.wm.component.ComponentsBase;
22 
23 import java.util.HashMap;
24 
25 public class Components extends ComponentsBase {
26     public static final String JAVA_PACKAGE_NAME = getPackageName(Components.class);
27     private static final HashMap<String, Components> sPackageNameToComponents = new HashMap<>();
28 
get(Context context)29     public static Components get(Context context) {
30         return get(context.getPackageName());
31     }
32 
get(String packageName)33     public static Components get(String packageName) {
34         synchronized (sPackageNameToComponents) {
35             return sPackageNameToComponents.computeIfAbsent(packageName, Components::new);
36         }
37     }
38 
39     /** Action constants for {@link #FOREGROUND_ACTIVITY}. */
40     public static class ForegroundActivityAction {
41         public final String LAUNCH_BACKGROUND_ACTIVITIES;
42         public final String FINISH_ACTIVITY;
43 
ForegroundActivityAction(String packageName)44         public ForegroundActivityAction(String packageName) {
45             LAUNCH_BACKGROUND_ACTIVITIES = packageName + ".ACTION_LAUNCH_BACKGROUND_ACTIVITIES";
46             FINISH_ACTIVITY = packageName + ".ACTION_FINISH_ACTIVITY";
47         }
48     }
49 
50     /** Action constants for {@link #FOREGROUND_EMBEDDING_ACTIVITY}. */
51     public static class ForegroundEmbeddedActivityAction {
52         public final String LAUNCH_EMBEDDED_ACTIVITY;
53         public final String FINISH_ACTIVITY;
54 
ForegroundEmbeddedActivityAction(String packageName)55         public ForegroundEmbeddedActivityAction(String packageName) {
56             LAUNCH_EMBEDDED_ACTIVITY = packageName + ".ACTION_LAUNCH_EMBEDDED_ACTIVITY";
57             FINISH_ACTIVITY = packageName + ".ACTION_FINISH_ACTIVITY";
58         }
59     }
60 
61     /** Action constants for {@link #LAUNCH_INTO_PIP_ACTIVITY}. */
62     public static class LaunchIntoPipActivityAction {
63         public final String LAUNCH_INTO_PIP;
64 
LaunchIntoPipActivityAction(String packageName)65         public LaunchIntoPipActivityAction(String packageName) {
66             LAUNCH_INTO_PIP = packageName + ".ACTION_LAUNCH_INTO_PIP";
67         }
68     }
69 
70     /** Extra key constants for {@link #FOREGROUND_ACTIVITY}. */
71     public static class ForegroundActivityExtra {
72         public final String RELAUNCH_FOREGROUND_ACTIVITY_EXTRA =
73                 "RELAUNCH_FOREGROUND_ACTIVITY_EXTRA";
74 
75         // Keep in sync with  android.server.wm.backgroundactivity.common.CommonComponents
76         // .CommonForegroundActivityExtras
77         public final String ACTIVITY_ID = "ACTIVITY_ID_EXTRA";
78         public final String ALLOW_CROSS_UID = "ALLOW_CROSS_UID_EXTRA";
79 
80         public final String LAUNCH_INTENTS = "LAUNCH_INTENTS_EXTRA";
81         public final String LAUNCH_PENDING_INTENTS = "LAUNCH_PENDING_INTENTS_EXTRA";
82         public final String LAUNCH_FOR_RESULT_AND_FINISH = "LAUNCH_FOR_RESULT_AND_FINISH";
83         public final String FINISH_FIRST = "FINISH_FIRST_EXTRA";
84     }
85 
86     /** Extra key constants for {@link #START_PENDING_INTENT_ACTIVITY} */
87     public static class StartPendingIntentActivityExtra {
88         /**
89          * The bundle to use to start the pending intent/
90          */
91         public final String START_BUNDLE = "START_BUNDLE";
92 
93         public final String PENDING_INTENT = "PENDING_INTENT_EXTRA";
94     }
95 
96     /** Extra key constants for {@link #VIRTUAL_DISPLAY_ACTIVITY} */
97     public static class VirtualDisplayActivityExtra {
98         public final String USE_PUBLIC_PRESENTATION =
99                 "USE_PUBLIC_PRESENTATION_EXTRA";
100     }
101 
102     // TODO(b/263368846) rename to camelCase
103     public final String APP_PACKAGE_NAME;
104     public final ComponentName BACKGROUND_ACTIVITY;
105     public final ComponentName SECOND_BACKGROUND_ACTIVITY;
106     public final ComponentName FOREGROUND_ACTIVITY;
107     public final ComponentName FOREGROUND_EMBEDDING_ACTIVITY;
108     public final ComponentName START_PENDING_INTENT_ACTIVITY;
109     public final ComponentName SIMPLE_BROADCAST_RECEIVER;
110     public final ComponentName SIMPLE_ADMIN_RECEIVER;
111     public final ComponentName ACTIVITY_START_SERVICE;
112     public final ComponentName PIP_ACTIVITY;
113     public final ComponentName LAUNCH_INTO_PIP_ACTIVITY;
114     public final ComponentName RELAUNCHING_ACTIVITY;
115     public final ComponentName VIRTUAL_DISPLAY_ACTIVITY;
116     public final ComponentName WIDGET_CONFIG_TEST_ACTIVITY;
117     public final ComponentName WIDGET_PROVIDER;
118     public final ComponentName START_NEXT_MATCHING_ACTIVITY;
119     public final ComponentName BIND_SERVICE_ACTIVITY;
120 
121 
122     public final ForegroundActivityAction FOREGROUND_ACTIVITY_ACTIONS;
123     public final ForegroundActivityExtra FOREGROUND_ACTIVITY_EXTRA = new ForegroundActivityExtra();
124 
125     public final ForegroundEmbeddedActivityAction FOREGROUND_EMBEDDING_ACTIVITY_ACTIONS;
126 
127     public final LaunchIntoPipActivityAction LAUNCH_INTO_PIP_ACTIONS;
128 
129     public final VirtualDisplayActivityExtra VIRTUAL_DISPLAY_ACTIVITY_EXTRA =
130             new VirtualDisplayActivityExtra();
131 
132     public final StartPendingIntentActivityExtra START_PENDING_INTENT_ACTIVITY_EXTRA =
133             new StartPendingIntentActivityExtra();
134 
Components(String appPackageName)135     public Components(String appPackageName) {
136         APP_PACKAGE_NAME = appPackageName;
137 
138         BACKGROUND_ACTIVITY =
139                 component(APP_PACKAGE_NAME, "BackgroundActivity");
140         SECOND_BACKGROUND_ACTIVITY =
141                 component(APP_PACKAGE_NAME, "SecondBackgroundActivity");
142         FOREGROUND_ACTIVITY =
143                 component(APP_PACKAGE_NAME, "ForegroundActivity");
144         FOREGROUND_EMBEDDING_ACTIVITY =
145                 component(APP_PACKAGE_NAME, "ForegroundEmbeddingActivity");
146         START_PENDING_INTENT_ACTIVITY =
147                 component(APP_PACKAGE_NAME, "StartPendingIntentActivity");
148         SIMPLE_BROADCAST_RECEIVER =
149                 component(APP_PACKAGE_NAME, "SimpleBroadcastReceiver");
150         SIMPLE_ADMIN_RECEIVER =
151                 component(APP_PACKAGE_NAME, "SimpleAdminReceiver");
152         PIP_ACTIVITY =
153                 component(APP_PACKAGE_NAME, "PipActivity");
154         LAUNCH_INTO_PIP_ACTIVITY =
155                 component(APP_PACKAGE_NAME, "LaunchIntoPipActivity");
156         RELAUNCHING_ACTIVITY =
157                 component(APP_PACKAGE_NAME, "RelaunchingActivity");
158         VIRTUAL_DISPLAY_ACTIVITY =
159                 component(APP_PACKAGE_NAME, "VirtualDisplayActivity");
160         WIDGET_CONFIG_TEST_ACTIVITY =
161                 component(APP_PACKAGE_NAME, "WidgetConfigTestActivity");
162         WIDGET_PROVIDER =
163                 component(APP_PACKAGE_NAME, "WidgetProvider");
164         ACTIVITY_START_SERVICE =
165                 component(APP_PACKAGE_NAME, "ActivityStarterService");
166         START_NEXT_MATCHING_ACTIVITY =
167                 component(APP_PACKAGE_NAME, "StartNextMatchingActivity");
168         BIND_SERVICE_ACTIVITY = component(APP_PACKAGE_NAME, "BindServiceActivity");
169 
170         FOREGROUND_ACTIVITY_ACTIONS = new ForegroundActivityAction(APP_PACKAGE_NAME);
171         FOREGROUND_EMBEDDING_ACTIVITY_ACTIONS =
172                 new ForegroundEmbeddedActivityAction(APP_PACKAGE_NAME);
173         LAUNCH_INTO_PIP_ACTIONS = new LaunchIntoPipActivityAction(APP_PACKAGE_NAME);
174     }
175 
component(String packageName, String className)176     private ComponentName component(String packageName, String className) {
177         String fullClassName = JAVA_PACKAGE_NAME + "." + className;
178         return new ComponentName(packageName, fullClassName);
179     }
180 }
181