1 /*
2  * Copyright (C) 2023 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.devicelockcontroller.activities;
18 
19 import static com.android.devicelockcontroller.common.DeviceLockConstants.ProvisionFailureReason.POLICY_ENFORCEMENT_FAILED;
20 import static com.android.devicelockcontroller.common.DeviceLockConstants.ProvisionFailureReason.UNKNOWN_REASON;
21 
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.view.WindowInsets;
25 import android.view.WindowInsetsController;
26 
27 import androidx.annotation.Nullable;
28 import androidx.appcompat.app.AppCompatActivity;
29 import androidx.lifecycle.ViewModelProvider;
30 
31 import com.android.devicelockcontroller.R;
32 import com.android.devicelockcontroller.util.LogUtil;
33 
34 /**
35  * The activity displayed when provisioning is in progress.
36  */
37 public final class ProvisioningActivity extends AppCompatActivity {
38 
39     private static final String TAG = "ProvisioningActivity";
40 
41     static final String EXTRA_SHOW_PROVISION_FAILED_UI_ON_START =
42             "com.android.devicelockcontroller.activities.extra.SHOW_PROVISION_FAILED_UI_ON_START";
43 
44     /**
45      * An extra boolean set on the provisioning activity intent to signal that it should
46      * show the provisioning failed screen on start.
47      */
48     public static final String EXTRA_SHOW_CRITICAL_PROVISION_FAILED_UI_ON_START =
49             "com.android.devicelockcontroller.activities.extra.SHOW_CRITICAL_PROVISION"
50                     + "_FAILED_UI_ON_START";
51 
52     @Override
onCreate(@ullable Bundle savedInstanceState)53     protected void onCreate(@Nullable Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         setContentView(R.layout.provisioning_activity);
56 
57         WindowInsetsController controller = getWindow().getInsetsController();
58         if (controller != null) {
59             controller.hide(WindowInsets.Type.systemBars());
60         }
61         ProvisioningProgressViewModel viewModel = new ViewModelProvider(this).get(
62                 ProvisioningProgressViewModel.class);
63         viewModel.getProvisioningProgressLiveData().observe(this, progress -> {
64             ProgressFragment progressFragment = new ProgressFragment();
65             getSupportFragmentManager()
66                     .beginTransaction()
67                     .replace(R.id.fragment_container, progressFragment)
68                     .commit();
69         });
70         final Intent intent = getIntent();
71         if (intent.getBooleanExtra(EXTRA_SHOW_CRITICAL_PROVISION_FAILED_UI_ON_START, false)) {
72             LogUtil.d(TAG, "showing critical provision failed ui");
73             viewModel.setProvisioningProgress(
74                     ProvisioningProgress.getMandatoryProvisioningFailedProgress(
75                             POLICY_ENFORCEMENT_FAILED));
76         } else if (intent.getBooleanExtra(EXTRA_SHOW_PROVISION_FAILED_UI_ON_START, false)) {
77             LogUtil.d(TAG, "showing provision failed ui");
78             // Using an unknown reason should not be allowed but is okay here because the UI will
79             // not report to the server
80             // TODO(b/321110148): Refactor reporting logic so that we don't need to do this
81             viewModel.setProvisioningProgress(
82                     ProvisioningProgress.getNonMandatoryProvisioningFailedProgress(UNKNOWN_REASON));
83         }
84         if (savedInstanceState == null) {
85             getSupportFragmentManager()
86                     .beginTransaction()
87                     .add(R.id.fragment_container, new DevicePoliciesFragment())
88                     .commit();
89         }
90     }
91 }
92