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 /** 27 * A base {@link Application} that is meant to be extended. 28 * 29 * <p>{@code ManagedProvisioning} inheritors are required to extend this class. They 30 * can map their own {@link Activity} classes to existing {@code ManagedProvisioning} 31 * screens by calling {@link #setOverrideActivity(ManagedProvisioningScreens, Class)}. 32 * 33 * <p>By default, the existing {@code ManagedProvisioning} {@link Activity} classes are used. 34 */ 35 public abstract class ManagedProvisioningBaseApplication extends Application { 36 private final ScreenManager mScreenManager = 37 new ScreenManager(ScreenManager.DEFAULT_SCREEN_TO_ACTIVITY_MAP); 38 private EncryptionController mEncryptionController; 39 private boolean mKeepScreenOn; 40 41 @Override onCreate()42 public void onCreate() { 43 super.onCreate(); 44 mEncryptionController = EncryptionController.getInstance( 45 this, 46 new ComponentName( 47 /* pkg= */ this, 48 getActivityClassForScreen(ManagedProvisioningScreens.POST_ENCRYPT))); 49 } 50 getEncryptionController()51 public final EncryptionController getEncryptionController() { 52 return mEncryptionController; 53 } 54 55 /** 56 * Maps the provided {@code screen} to the provided {@code activityClass}. 57 * 58 * <p>When ManagedProvisioning wants to launch any of the screens in {@link 59 * ManagedProvisioningScreens}, instead of its base {@link Activity} implementation, it will 60 * launch the class provided here. 61 */ setOverrideActivity( ManagedProvisioningScreens screen, Class<? extends Activity> activityClass)62 public final void setOverrideActivity( 63 ManagedProvisioningScreens screen, Class<? extends Activity> activityClass) { 64 mScreenManager.setOverrideActivity(screen, activityClass); 65 } 66 67 /** 68 * Retrieves the {@link Activity} class associated with the provided {@code screen}. 69 * 70 * <p>If no screens were set via {@link #setOverrideActivity(ManagedProvisioningScreens, 71 * Class)}, the base ManagedProvisioning {@link Activity} implementation will be returned. 72 */ 73 public final Class<? extends Activity> getActivityClassForScreen(ManagedProvisioningScreens screen)74 getActivityClassForScreen(ManagedProvisioningScreens screen) { 75 return mScreenManager.getActivityClassForScreen(screen); 76 } 77 getScreenManager()78 public ScreenManager getScreenManager() { 79 return mScreenManager; 80 } 81 82 /** 83 * If {@link #markKeepScreenOn()} was previously called, makes the 84 * provided {@link Activity} keep its screen on while it's visible to the end-user. 85 */ maybeKeepScreenOn(Activity activity)86 public void maybeKeepScreenOn(Activity activity) { 87 if (mKeepScreenOn) { 88 markKeepScreenOn(); 89 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 90 } 91 } 92 93 /** 94 * Signals that all {@link Activity} instances should keep their screen on. 95 */ markKeepScreenOn()96 public void markKeepScreenOn() { 97 mKeepScreenOn = true; 98 } 99 } 100