• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.common.base.Preconditions.checkNotNull;
20 
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageView;
26 import android.widget.TextView;
27 
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.Fragment;
30 import androidx.lifecycle.ViewModelProvider;
31 
32 import com.android.devicelockcontroller.R;
33 
34 /**
35  * A screen which always displays a progress bar.
36  */
37 public final class ProgressFragment extends Fragment {
38 
39     /** The Bundle key for the resource id of the icon. */
40     private static final String KEY_ICON_ID = "key_icon_id";
41 
42     /** The Bundle key for the resource id of the header text. */
43     private static final String KEY_HEADER_TEXT_ID = "key_header_text_id";
44 
45     private static final String KEY_SUBHEADER_TEXT_ID = "key_subheader_text_id";
46 
47     @Nullable
48     @Override
onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)49     public View onCreateView(
50             LayoutInflater inflater,
51             @Nullable ViewGroup container,
52             @Nullable Bundle savedInstanceState) {
53         View v = inflater.inflate(R.layout.fragment_progress, container, false);
54 
55         ImageView headerIconImageView = v.findViewById(R.id.header_icon);
56         checkNotNull(headerIconImageView);
57 
58         TextView headerTextView = v.findViewById(R.id.header_text);
59         checkNotNull(headerTextView);
60 
61         TextView subheaderTextView = v.findViewById(R.id.subheader_text);
62         checkNotNull(subheaderTextView);
63 
64         ProvisioningProgressViewModel provisioningProgressViewModel =
65                 new ViewModelProvider(requireActivity()).get(ProvisioningProgressViewModel.class);
66         provisioningProgressViewModel.getProvisioningProgressLiveData().observe(
67                 getViewLifecycleOwner(), provisioningProgress -> {
68                     if (provisioningProgress.mIconId != 0) {
69                         headerIconImageView.setImageResource(provisioningProgress.mIconId);
70                     }
71                     if (provisioningProgress.mHeaderId != 0) {
72                         headerTextView.setText(
73                                 requireContext().getString(provisioningProgress.mHeaderId,
74                                         provisioningProgressViewModel
75                                                 .mProviderNameLiveData.getValue()));
76                     }
77                     if (provisioningProgress.mSubheaderId != 0) {
78                         subheaderTextView.setText(
79                                 requireContext().getString(provisioningProgress.mSubheaderId));
80                     }
81                 });
82 
83         return v;
84     }
85 
create(int iconId, int headerTextId, int subheaderTextId)86     static ProgressFragment create(int iconId, int headerTextId, int subheaderTextId) {
87         ProgressFragment progressFragment = new ProgressFragment();
88         Bundle bundle = new Bundle();
89         if (iconId != 0) {
90             bundle.putInt(KEY_ICON_ID, iconId);
91         }
92         if (headerTextId != 0) {
93             bundle.putInt(KEY_HEADER_TEXT_ID, headerTextId);
94         }
95         if (subheaderTextId != 0) {
96             bundle.putInt(KEY_SUBHEADER_TEXT_ID, subheaderTextId);
97         }
98         progressFragment.setArguments(bundle);
99         return progressFragment;
100     }
101 
102 }
103