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 android.content.Context;
20 import android.text.TextUtils;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.recyclerview.widget.DiffUtil;
28 import androidx.recyclerview.widget.ListAdapter;
29 import androidx.recyclerview.widget.RecyclerView;
30 
31 import com.android.devicelockcontroller.R;
32 import com.android.devicelockcontroller.activities.ProvisionInfo.ProvisionInfoType;
33 import com.android.devicelockcontroller.activities.util.UrlUtils;
34 import com.android.devicelockcontroller.util.LogUtil;
35 
36 /**
37  * An Adapter which provides the binding between {@link ProvisionInfo} and the
38  * {@link androidx.recyclerview.widget.RecyclerView}.
39  */
40 public final class ProvisionInfoListAdapter extends
41         ListAdapter<ProvisionInfo, ProvisionInfoListAdapter.ProvisionInfoViewHolder> {
42 
ProvisionInfoListAdapter()43     ProvisionInfoListAdapter() {
44         super(new DiffUtil.ItemCallback<>() {
45             @Override
46             public boolean areItemsTheSame(@NonNull ProvisionInfo oldItem,
47                     @NonNull ProvisionInfo newItem) {
48                 return oldItem.equals(newItem);
49             }
50 
51             @Override
52             public boolean areContentsTheSame(@NonNull ProvisionInfo oldItem,
53                     @NonNull ProvisionInfo newItem) {
54                 return oldItem.equals(newItem);
55             }
56         });
57     }
58 
59     @NonNull
60     @Override
onCreateViewHolder(@onNull ViewGroup parent, int viewType)61     public ProvisionInfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
62         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_provision_info,
63                 parent, false);
64         return new ProvisionInfoViewHolder(view);
65     }
66 
67     @Override
onBindViewHolder(@onNull ProvisionInfoViewHolder provisionInfoViewHolder, int position)68     public void onBindViewHolder(@NonNull ProvisionInfoViewHolder provisionInfoViewHolder,
69             int position) {
70         provisionInfoViewHolder.bindProvisionInfo(getItem(position));
71     }
72 
73     /**
74      * A {@link androidx.recyclerview.widget.RecyclerView.ViewHolder} class which describes the item
75      * views used in the {@link RecyclerView}
76      */
77     static final class ProvisionInfoViewHolder extends RecyclerView.ViewHolder {
78 
79         private static final String TAG = ProvisionInfoViewHolder.class.getSimpleName();
80 
81         private final TextView mTextView;
82 
ProvisionInfoViewHolder(@onNull View itemView)83         ProvisionInfoViewHolder(@NonNull View itemView) {
84             super(itemView);
85             mTextView = itemView.findViewById(R.id.text_view_item_provision_info);
86         }
87 
88         /**
89          * Bind the item view with url.
90          *
91          * @param provisionInfo The {@link ProvisionInfo} data to bind to the held view.
92          */
bindProvisionInfo(ProvisionInfo provisionInfo)93         void bindProvisionInfo(ProvisionInfo provisionInfo) {
94             Context context = mTextView.getContext();
95             String providerName = provisionInfo.getProviderName();
96             if (TextUtils.isEmpty(providerName)) {
97                 LogUtil.w(TAG, "Provider name is not provided!");
98                 mTextView.setText("");
99             } else if (provisionInfo.getType() == ProvisionInfoType.REGULAR) {
100                 mTextView.setText(context.getString(provisionInfo.getTextId(), providerName));
101             } else {
102                 UrlUtils.setUrlText(mTextView,
103                         String.format(context.getString(provisionInfo.getTextId()), providerName,
104                                 provisionInfo.getUrl()));
105             }
106             mTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(provisionInfo.getDrawableId(),
107                     /* top=*/ 0,
108                     /* end=*/ 0,
109                     /* bottom=*/ 0);
110         }
111     }
112 }
113