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.activities.DevicePoliciesViewModel.HEADER_DRAWABLE_ID;
20 import static com.android.devicelockcontroller.activities.DevicePoliciesViewModel.HEADER_TEXT_ID;
21 
22 import static com.google.common.base.Preconditions.checkNotNull;
23 
24 import android.content.Context;
25 import android.os.Bundle;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.Button;
30 import android.widget.ImageView;
31 import android.widget.TextView;
32 
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 import androidx.fragment.app.Fragment;
36 import androidx.lifecycle.ViewModelProvider;
37 import androidx.recyclerview.widget.RecyclerView;
38 
39 import com.android.devicelockcontroller.R;
40 import com.android.devicelockcontroller.policy.PolicyObjectsProvider;
41 import com.android.devicelockcontroller.policy.ProvisionHelper;
42 import com.android.devicelockcontroller.policy.ProvisionHelperImpl;
43 
44 /**
45  * A screen which lists the polies enforced on the device by the device provider.
46  */
47 public final class DevicePoliciesFragment extends Fragment {
48 
49     private static final String TAG = "DevicePoliciesFragment";
50 
51     @Nullable
52     @Override
onCreateView( @onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)53     public View onCreateView(
54             @NonNull LayoutInflater inflater,
55             @Nullable ViewGroup container,
56             @Nullable Bundle savedInstanceState) {
57         return inflater.inflate(R.layout.fragment_device_policies, container, false);
58     }
59 
60     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)61     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
62         super.onViewCreated(view, savedInstanceState);
63 
64         DevicePoliciesViewModel viewModel = new ViewModelProvider(this).get(
65                 DevicePoliciesViewModel.class);
66 
67         RecyclerView recyclerView = view.findViewById(R.id.recyclerview_device_policy_group);
68         DevicePolicyGroupListAdapter adapter = new DevicePolicyGroupListAdapter();
69         viewModel.mDevicePolicyGroupListLiveData.observe(getViewLifecycleOwner(),
70                 devicePolicyGroups -> {
71                     adapter.setProviderName(viewModel.mProviderNameLiveData.getValue());
72                     adapter.submitList(devicePolicyGroups);
73                 });
74         checkNotNull(recyclerView);
75         recyclerView.setAdapter(adapter);
76 
77         ImageView imageView = view.findViewById(R.id.header_icon);
78         checkNotNull(imageView);
79         imageView.setImageResource(HEADER_DRAWABLE_ID);
80 
81         TextView headerTextView = view.findViewById(R.id.header_text);
82         checkNotNull(headerTextView);
83         viewModel.mProviderNameLiveData.observe(getViewLifecycleOwner(),
84                 providerName -> headerTextView.setText(
85                         getString(HEADER_TEXT_ID, providerName)));
86 
87         Context context = requireContext().getApplicationContext();
88         ProvisionHelper provisionHelper = new ProvisionHelperImpl(context,
89                 ((PolicyObjectsProvider) context).getProvisionStateController());
90 
91         ProvisioningProgressViewModel provisioningProgressViewModel =
92                 new ViewModelProvider(requireActivity()).get(ProvisioningProgressViewModel.class);
93         Button button = view.findViewById(R.id.button_next);
94         checkNotNull(button);
95         viewModel.getIsMandatoryLiveData().observe(this,
96                 isMandatory -> {
97                     button.setOnClickListener(
98                             v -> provisionHelper.scheduleKioskAppInstallation(requireActivity(),
99                                     provisioningProgressViewModel,
100                                     isMandatory));
101                     button.setVisibility(View.VISIBLE);
102                 });
103     }
104 }
105