• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.preprovisioning;
18 
19 import static com.android.managedprovisioning.model.ProvisioningParams.EXTRA_PROVISIONING_PARAMS;
20 
21 import android.os.Bundle;
22 
23 import androidx.lifecycle.ViewModelProvider;
24 
25 import com.android.managedprovisioning.ManagedProvisioningBaseApplication;
26 import com.android.managedprovisioning.R;
27 import com.android.managedprovisioning.common.ErrorDialogUtils;
28 import com.android.managedprovisioning.common.ErrorWrapper;
29 import com.android.managedprovisioning.common.RoleHolderProvider;
30 import com.android.managedprovisioning.common.SetupGlifLayoutActivity;
31 import com.android.managedprovisioning.model.ProvisioningParams;
32 import com.android.managedprovisioning.preprovisioning.DownloadRoleHolderViewModel.DownloadRoleHolderViewModelFactory;
33 
34 /**
35  * Spinner which takes care of network connectivity if needed, and downloading of the role holder.
36  *
37  * <p>If successfully connected to network and downloaded the role holder, {@link #RESULT_OK} is
38  * returned. Otherwise the result is {@link #RESULT_CANCELED}.
39  *
40  * <p>If the result is {@link #RESULT_CANCELED}, it may be accompanied by
41  * {@link ErrorDialogUtils#EXTRA_DIALOG_TITLE_ID}, {@link
42  * ErrorDialogUtils#EXTRA_ERROR_MESSAGE_RES_ID} and {@link
43  * ErrorDialogUtils#EXTRA_FACTORY_RESET_REQUIRED} which can be used to display in a user-visible
44  * dialog.
45  */
46 public class DownloadRoleHolderActivity extends SetupGlifLayoutActivity {
47     private DownloadRoleHolderViewModel mViewModel;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52 
53         ProvisioningParams params = getIntent().getParcelableExtra(EXTRA_PROVISIONING_PARAMS);
54         if (params.roleHolderDownloadInfo == null) {
55             setResult(RESULT_CANCELED);
56             getTransitionHelper().finishActivity(this);
57             return;
58         }
59 
60         mViewModel = new ViewModelProvider(
61                 this,
62                 new DownloadRoleHolderViewModelFactory(
63                         (ManagedProvisioningBaseApplication) getApplication(),
64                         params,
65                         mUtils,
66                         mSettingsFacade,
67                         RoleHolderProvider.DEFAULT.getPackageName(this)))
68                 .get(DownloadRoleHolderViewModel.class);
69         mViewModel.observeState().observe(this, this::onStateChanged);
70         mViewModel.connectToNetworkAndDownloadRoleHolder(getApplicationContext());
71         initializeUi();
72     }
73 
onStateChanged(Integer state)74     private void onStateChanged(Integer state) {
75         switch(state) {
76             case DownloadRoleHolderViewModel.STATE_IDLE:
77                 break;
78             case DownloadRoleHolderViewModel.STATE_DOWNLOADING:
79                 break;
80             case DownloadRoleHolderViewModel.STATE_DOWNLOADED:
81                 setResult(RESULT_OK);
82                 getTransitionHelper().finishActivity(this);
83                 break;
84             case DownloadRoleHolderViewModel.STATE_ERROR:
85                 ErrorWrapper error = mViewModel.getError();
86                 setResult(RESULT_CANCELED, ErrorDialogUtils.createResultIntent(error));
87                 getTransitionHelper().finishActivity(this);
88                 break;
89         }
90     }
91 
initializeUi()92     private void initializeUi() {
93         final int headerResId = R.string.setting_up;
94         final int titleResId = R.string.setting_up;
95         initializeLayoutParams(R.layout.empty_loading_layout, headerResId);
96         setTitle(titleResId);
97     }
98 
99     @Override
isWaitingScreen()100     protected boolean isWaitingScreen() {
101         return true;
102     }
103 }
104