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.Flags; 30 import com.android.managedprovisioning.common.RoleHolderProvider; 31 import com.android.managedprovisioning.common.SetupGlifLayoutActivity; 32 import com.android.managedprovisioning.contracts.DownloadRoleHolderContract; 33 import com.android.managedprovisioning.model.ProvisioningParams; 34 import com.android.managedprovisioning.preprovisioning.DownloadRoleHolderViewModel.DownloadRoleHolderViewModelFactory; 35 36 import dagger.hilt.android.AndroidEntryPoint; 37 38 import javax.inject.Inject; 39 40 /** 41 * Spinner which takes care of network connectivity if needed, and downloading of the role holder. 42 * 43 * <p>If successfully connected to network and downloaded the role holder, {@link #RESULT_OK} is 44 * returned. Otherwise the result is {@link #RESULT_CANCELED}. 45 * 46 * <p>If the result is {@link #RESULT_CANCELED}, it may be accompanied by 47 * {@link ErrorDialogUtils#EXTRA_DIALOG_TITLE_ID}, {@link 48 * ErrorDialogUtils#EXTRA_ERROR_MESSAGE_RES} and {@link 49 * ErrorDialogUtils#EXTRA_FACTORY_RESET_REQUIRED} which can be used to display in a user-visible 50 * dialog. 51 */ 52 @AndroidEntryPoint(SetupGlifLayoutActivity.class) 53 public class DownloadRoleHolderActivity extends Hilt_DownloadRoleHolderActivity { 54 private DownloadRoleHolderViewModel mViewModel; 55 56 @Inject 57 protected Flags mFlags; 58 59 @Inject 60 protected DownloadRoleHolderContract mContract; 61 62 @Override onCreate(Bundle savedInstanceState)63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 if (mFlags.isCosmicRayEnabled()) { 66 mContract.attach(this, getIntent()); 67 } 68 69 ProvisioningParams params = getIntent().getParcelableExtra(EXTRA_PROVISIONING_PARAMS); 70 if (params.roleHolderDownloadInfo == null) { 71 setResult(RESULT_CANCELED); 72 getTransitionHelper().finishActivity(this); 73 return; 74 } 75 76 mViewModel = new ViewModelProvider( 77 this, 78 new DownloadRoleHolderViewModelFactory( 79 (ManagedProvisioningBaseApplication) getApplication(), 80 params, 81 mUtils, 82 mSettingsFacade, 83 RoleHolderProvider.DEFAULT.getPackageName(this))) 84 .get(DownloadRoleHolderViewModel.class); 85 mViewModel.observeState().observe(this, this::onStateChanged); 86 mViewModel.connectToNetworkAndDownloadRoleHolder(getApplicationContext()); 87 initializeUi(); 88 } 89 onStateChanged(Integer state)90 private void onStateChanged(Integer state) { 91 switch (state) { 92 case DownloadRoleHolderViewModel.STATE_IDLE: 93 break; 94 case DownloadRoleHolderViewModel.STATE_DOWNLOADING: 95 break; 96 case DownloadRoleHolderViewModel.STATE_DOWNLOADED: 97 setResult(RESULT_OK); 98 getTransitionHelper().finishActivity(this); 99 break; 100 case DownloadRoleHolderViewModel.STATE_ERROR: 101 ErrorWrapper error = mViewModel.getError(); 102 setResult(RESULT_CANCELED, 103 ErrorDialogUtils.createResultIntent(error, getApplicationContext())); 104 getTransitionHelper().finishActivity(this); 105 break; 106 } 107 } 108 initializeUi()109 private void initializeUi() { 110 final int headerResId = R.string.setting_up; 111 final int titleResId = R.string.setting_up; 112 initializeLayoutParams(R.layout.empty_loading_layout, headerResId); 113 setTitle(titleResId); 114 } 115 116 @Override isWaitingScreen()117 protected boolean isWaitingScreen() { 118 return true; 119 } 120 } 121