• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.managedprovisioning;
18 
19 import android.app.Activity;
20 import android.app.Application;
21 import android.app.Application.ActivityLifecycleCallbacks;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.util.ArrayMap;
26 import android.view.WindowManager;
27 
28 import androidx.test.runner.AndroidJUnitRunner;
29 
30 import java.util.Map;
31 
32 public class TestInstrumentationRunner extends AndroidJUnitRunner {
33     // hard-coded package name as context.getPackageName() provides ManagedProvisioning app name
34     // instead of test package name
35     public static final String TEST_PACKAGE_NAME = "com.android.managedprovisioning.tests";
36 
37     private static final String TAG = "TestInstrumentationRunner";
38     private static final Map<String, OnActivityCreatedCallback> sReplacedActivityMap =
39             new ArrayMap();
40 
registerReplacedActivity(Class<?> oldActivity, OnActivityCreatedCallback onActivityCreatedCallback)41     public static void registerReplacedActivity(Class<?> oldActivity,
42             OnActivityCreatedCallback onActivityCreatedCallback) {
43         sReplacedActivityMap.put(oldActivity.getCanonicalName(), onActivityCreatedCallback);
44     }
45 
registerReplacedActivity(Class<?> oldActivity, Class<? extends Activity> newActivity)46     public static void registerReplacedActivity(Class<?> oldActivity,
47             Class<? extends Activity> newActivity) {
48         registerReplacedActivity(oldActivity,
49                 (classLoader, className, intent) -> newActivity.newInstance());
50     }
51 
unregisterReplacedActivity(Class<?> oldActivity)52     public static void unregisterReplacedActivity(Class<?> oldActivity) {
53         sReplacedActivityMap.remove(oldActivity.getCanonicalName());
54     }
55 
56     @Override
newActivity(ClassLoader cl, String className, Intent intent)57     public Activity newActivity(ClassLoader cl, String className, Intent intent)
58             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
59         OnActivityCreatedCallback callback = sReplacedActivityMap.get(className);
60         if (callback != null) {
61             return callback.createActivity(cl, className, intent);
62         } else {
63             return super.newActivity(cl, className, intent);
64         }
65     }
66 
67     @Override
newApplication(ClassLoader cl, String className, Context context)68     public Application newApplication(ClassLoader cl, String className, Context context)
69             throws InstantiationException, IllegalAccessException, ClassNotFoundException {
70         Application app = super.newApplication(cl, className, context);
71         app.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
72             @Override
73             public void onActivityCreated(Activity activity, Bundle bundle) {
74                 // Show activity on top of keyguard
75                 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
76                 // Turn on screen to prevent activity being paused by system. See b/31262906
77                 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
78                 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
79             }
80 
81             @Override
82             public void onActivityStarted(Activity activity) {}
83 
84             @Override
85             public void onActivityResumed(Activity activity) {}
86 
87             @Override
88             public void onActivityPaused(Activity activity) {}
89 
90             @Override
91             public void onActivityStopped(Activity activity) {}
92 
93             @Override
94             public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
95 
96             @Override
97             public void onActivityDestroyed(Activity activity) {}
98         });
99         return app;
100     }
101 
102     public interface OnActivityCreatedCallback {
createActivity(ClassLoader cl, String className, Intent intent)103         Activity createActivity(ClassLoader cl, String className, Intent intent)
104                 throws IllegalAccessException, InstantiationException;
105     }
106 }
107