• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014, 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.common;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.VIEW_UNKNOWN;
22 import static com.android.managedprovisioning.provisioning.Constants.LOCK_TO_PORTRAIT_MODE;
23 
24 import android.app.Activity;
25 import android.app.DialogFragment;
26 import android.app.Fragment;
27 import android.app.FragmentManager;
28 import android.app.FragmentTransaction;
29 import android.content.pm.ActivityInfo;
30 import android.content.res.Configuration;
31 import android.os.Bundle;
32 
33 import androidx.annotation.NonNull;
34 import androidx.annotation.VisibleForTesting;
35 import androidx.appcompat.app.AppCompatActivity;
36 import androidx.appcompat.app.AppCompatDelegate;
37 
38 import com.android.managedprovisioning.ManagedProvisioningBaseApplication;
39 import com.android.managedprovisioning.ManagedProvisioningScreens;
40 import com.android.managedprovisioning.R;
41 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
42 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
43 import com.android.managedprovisioning.analytics.TimeLogger;
44 import com.android.managedprovisioning.common.ThemeHelper.DefaultNightModeChecker;
45 import com.android.managedprovisioning.common.ThemeHelper.DefaultSetupWizardBridge;
46 import com.android.managedprovisioning.preprovisioning.EncryptionController;
47 
48 import dagger.hilt.android.AndroidEntryPoint;
49 
50 /**
51  * Base class for setting up the layout.
52  */
53 @AndroidEntryPoint(AppCompatActivity.class)
54 public abstract class SetupLayoutActivity extends Hilt_SetupLayoutActivity {
55     protected final Utils mUtils;
56     protected final SettingsFacade mSettingsFacade;
57     private final ThemeHelper mThemeHelper;
58     private final TransitionHelper mTransitionHelper;
59     private TimeLogger mTimeLogger;
60 
SetupLayoutActivity()61     public SetupLayoutActivity() {
62         this(new Utils(), new SettingsFacade(),
63                 new ThemeHelper(new DefaultNightModeChecker(), new DefaultSetupWizardBridge()));
64     }
65 
66     @VisibleForTesting
SetupLayoutActivity( Utils utils, SettingsFacade settingsFacade, ThemeHelper themeHelper)67     protected SetupLayoutActivity(
68             Utils utils, SettingsFacade settingsFacade, ThemeHelper themeHelper) {
69         mUtils = utils;
70         mSettingsFacade = settingsFacade;
71         mThemeHelper = themeHelper;
72         // TODO(b/183036855): Add dependency injection in ManagedProvisioning
73         mTransitionHelper = new TransitionHelper();
74     }
75 
76     @Override
onCreate(Bundle savedInstanceState)77     protected void onCreate(Bundle savedInstanceState) {
78         if (!isWaitingScreen()) {
79             mTransitionHelper.applyContentScreenTransitions(this);
80         }
81         boolean themeSet = mThemeHelper.setSuwTheme(this);
82         ProvisionLogger.logd("applyStyles themeSet:" + themeSet);
83         if (!themeSet) {
84             updateDefaultNightMode();
85             setTheme(mThemeHelper.inferThemeResId(this, getIntent()));
86             if (shouldSetupDynamicColors()) {
87                 mThemeHelper.setupDynamicColors(this);
88             }
89         }
90 
91         super.onCreate(savedInstanceState);
92 
93         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
94 
95         mTimeLogger = new TimeLogger(this, getMetricsCategory());
96         mTimeLogger.start();
97 
98         // lock orientation to portrait on phones if necessary
99         if (LOCK_TO_PORTRAIT_MODE && getResources().getBoolean(R.bool.lock_to_portrait)) {
100             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
101         }
102 
103         getBaseApplication().keepScreenOn(this);
104         logMetrics();
105     }
106 
shouldSetupDynamicColors()107     protected boolean shouldSetupDynamicColors() {
108         return true;
109     }
110 
111     @Override
onConfigurationChanged(@onNull Configuration newConfig)112     public void onConfigurationChanged(@NonNull Configuration newConfig) {
113         super.onConfigurationChanged(newConfig);
114         if (!mThemeHelper.setSuwTheme(this)) {
115             updateDefaultNightMode();
116         }
117     }
118 
updateDefaultNightMode()119     private void updateDefaultNightMode() {
120         int nightMode = mThemeHelper.getDefaultNightMode(this, getIntent());
121         AppCompatDelegate delegate = AppCompatDelegate.create(this, /* callback= */ null);
122         delegate.setLocalNightMode(nightMode);
123     }
124 
125     @Override
onResume()126     protected void onResume() {
127         super.onResume();
128         if (isWaitingScreen()) {
129             mTransitionHelper.applyWaitingScreenTransitions(this);
130         }
131     }
132 
logMetrics()133     private void logMetrics() {
134         // TODO(b/183036855): Add dependency injection in ManagedProvisioning
135         ProvisioningAnalyticsTracker analyticsTracker = new ProvisioningAnalyticsTracker(
136                 MetricsWriterFactory.getMetricsWriter(this, new SettingsFacade()),
137                 new ManagedProvisioningSharedPreferences(this));
138         final int orientation = getResources().getConfiguration().orientation;
139         analyticsTracker.logIsLandscape(
140                 orientation == Configuration.ORIENTATION_LANDSCAPE,
141                 getLocalClassName());
142     }
143 
144     @Override
onDestroy()145     public void onDestroy() {
146         mTimeLogger.stop();
147         super.onDestroy();
148     }
149 
getMetricsCategory()150     protected int getMetricsCategory() {
151         return VIEW_UNKNOWN;
152     }
153 
getUtils()154     protected Utils getUtils() {
155         return mUtils;
156     }
157 
getThemeHelper()158     protected ThemeHelper getThemeHelper() {
159         return mThemeHelper;
160     }
161 
getTransitionHelper()162     protected TransitionHelper getTransitionHelper() {
163         return mTransitionHelper;
164     }
165 
getBaseApplication()166     private ManagedProvisioningBaseApplication getBaseApplication() {
167         return ((ManagedProvisioningBaseApplication) getApplication());
168     }
169 
getActivityForScreen(ManagedProvisioningScreens screen)170     protected Class<? extends Activity> getActivityForScreen(ManagedProvisioningScreens screen) {
171         return getBaseApplication().getActivityClassForScreen(screen);
172     }
173 
getEncryptionController()174     protected EncryptionController getEncryptionController() {
175         return getBaseApplication().getEncryptionController();
176     }
177 
178     /**
179      * Whether the current screen is a waiting screen.
180      *
181      * <p>A waiting screen is a screen that shows a spinner and not content.
182      */
isWaitingScreen()183     protected boolean isWaitingScreen() {
184         return false;
185     }
186 
187     /**
188      * Constructs and shows a {@link DialogFragment} unless it is already displayed.
189      *
190      * @param dialogBuilder Lightweight builder, that it is inexpensive to discard it if dialog
191      *                      already shown.
192      * @param tag           The tag for this dialog, as per
193      *                      {@link FragmentTransaction#add(Fragment, String)}.
194      */
showDialog(DialogBuilder dialogBuilder, String tag)195     protected void showDialog(DialogBuilder dialogBuilder, String tag) {
196         FragmentManager fragmentManager = getFragmentManager();
197         if (!isDialogAdded(tag)) {
198             dialogBuilder.build().show(fragmentManager, tag);
199         }
200     }
201 
202     /**
203      * Checks whether the {@link DialogFragment} associated with the given tag is currently showing.
204      *
205      * @param tag The tag for this dialog.
206      */
isDialogAdded(String tag)207     protected boolean isDialogAdded(String tag) {
208         Fragment fragment = getFragmentManager().findFragmentByTag(tag);
209         return (fragment != null) && (fragment.isAdded());
210     }
211 }