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.managedprovisioning;
18 
19 import android.app.Activity;
20 import android.app.Application;
21 import android.content.ComponentName;
22 import android.view.WindowManager;
23 
24 import com.android.managedprovisioning.preprovisioning.EncryptionController;
25 
26 import dagger.hilt.android.HiltAndroidApp;
27 
28 import javax.inject.Inject;
29 
30 /**
31  * A base {@link Application} that is meant to be extended.
32  *
33  * <p>{@code ManagedProvisioning} inheritors are required to extend this class. They
34  * can map their own {@link Activity} classes to existing {@code ManagedProvisioning}
35  * screens by calling {@link #setOverrideActivity(ManagedProvisioningScreens, Class)}.
36  *
37  * <p>By default, the existing {@code ManagedProvisioning} {@link Activity} classes are used.
38  */
39 @HiltAndroidApp(Application.class)
40 public abstract class ManagedProvisioningBaseApplication extends
41         Hilt_ManagedProvisioningBaseApplication {
42     @Inject
43     protected ScreenManager mScreenManager;
44     private EncryptionController mEncryptionController;
45     private boolean mKeepScreenOn;
46 
47     @Override
onCreate()48     public void onCreate() {
49         super.onCreate();
50         mEncryptionController = EncryptionController.getInstance(
51                 this,
52                 new ComponentName(
53                         /* pkg= */ this,
54                         getActivityClassForScreen(ManagedProvisioningScreens.POST_ENCRYPT)));
55     }
56 
getEncryptionController()57     public final EncryptionController getEncryptionController() {
58         return mEncryptionController;
59     }
60 
61     /**
62      * Maps the provided {@code screen} to the provided {@code activityClass}.
63      *
64      * <p>When ManagedProvisioning wants to launch any of the screens in {@link
65      * ManagedProvisioningScreens}, instead of its base {@link Activity} implementation, it will
66      * launch the class provided here.
67      */
setOverrideActivity( ManagedProvisioningScreens screen, Class<? extends Activity> activityClass)68     public final void setOverrideActivity(
69             ManagedProvisioningScreens screen, Class<? extends Activity> activityClass) {
70         mScreenManager.setOverrideActivity(screen, activityClass);
71     }
72 
73     /**
74      * Retrieves the {@link Activity} class associated with the provided {@code screen}.
75      *
76      * <p>If no screens were set via {@link #setOverrideActivity(ManagedProvisioningScreens,
77      * Class)}, the base ManagedProvisioning {@link Activity} implementation will be returned.
78      */
getActivityClassForScreen( ManagedProvisioningScreens screen)79     public final Class<? extends Activity> getActivityClassForScreen(
80             ManagedProvisioningScreens screen) {
81         return mScreenManager.getActivityClassForScreen(screen);
82     }
83 
getScreenManager()84     public ScreenManager getScreenManager() {
85         return mScreenManager;
86     }
87 
88     /**
89      * Sets the screen On for whole provisioning flow
90      */
keepScreenOn(Activity activity)91     public void keepScreenOn(Activity activity) {
92         activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
93     }
94 }
95