1 /*
2  * Copyright (C) 2024 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.tv.feedbackconsent;
18 
19 import android.annotation.LayoutRes;
20 import android.annotation.StyleRes;
21 import android.app.Dialog;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.util.Log;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 
30 import androidx.annotation.NonNull;
31 import androidx.recyclerview.widget.RecyclerView;
32 import androidx.recyclerview.widget.LinearLayoutManager;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 public class TvFeedbackConsentInformationDialog extends Dialog {
38 
39     private static final String TAG = TvFeedbackConsentInformationDialog.class.getSimpleName();
40     private final int mLayoutId;
41     private final TvFeedbackConsentRecyclerViewAdapter mAdapter;
42 
TvFeedbackConsentInformationDialog(@onNull Context context, @StyleRes int themeResId, @LayoutRes int layoutId, List<String> recyclerViewContent)43     public TvFeedbackConsentInformationDialog(@NonNull Context context,
44                                               @StyleRes int themeResId,
45                                               @LayoutRes int layoutId,
46                                               List<String> recyclerViewContent) {
47         super(context, themeResId);
48         mLayoutId = layoutId;
49         mAdapter = new TvFeedbackConsentRecyclerViewAdapter(recyclerViewContent);
50     }
51 
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55 
56         setContentView(LayoutInflater.from(getContext()).inflate(mLayoutId, null));
57         setCancelable(true);
58 
59         setUpRecyclerView();
60     }
61 
setUpRecyclerView()62     private void setUpRecyclerView() {
63         RecyclerView mViewLogsContentRecycler = requireViewById(R.id.feedbackViewLogsDialogContent);
64         mViewLogsContentRecycler.setAdapter(mAdapter);
65         mViewLogsContentRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
66     }
67 
updateRecyclerView(List<String> viewData)68     protected final void updateRecyclerView(List<String> viewData) {
69         if (mAdapter == null) {
70             Log.w(TAG, "Cannot update recycler view before setting an adapter ");
71             return;
72         }
73         mAdapter.updateRecyclerView(viewData);
74         mAdapter.notifyDataSetChanged();
75     }
76 
77     private static final class TvFeedbackConsentRecyclerViewAdapter extends
78             RecyclerView.Adapter<TvFeedbackConsentRecyclerViewAdapter.ViewHolder> {
79 
80         private List<String> mViewData = new ArrayList<>(0);;
81 
TvFeedbackConsentRecyclerViewAdapter(List<String> viewData)82         TvFeedbackConsentRecyclerViewAdapter(List<String> viewData) {
83             mViewData.addAll(viewData);
84         }
85 
updateRecyclerView(List<String> viewData)86         void updateRecyclerView(List<String> viewData) {
87             mViewData.clear();
88             mViewData.addAll(viewData);
89         }
90 
91         public static class ViewHolder extends RecyclerView.ViewHolder {
92             private final TextView textView;
93 
ViewHolder(View v)94             public ViewHolder(View v) {
95                 super(v);
96                 textView = (TextView) v.findViewById(R.id.textView);
97             }
98 
getTextView()99             public TextView getTextView() {
100                 return textView;
101             }
102         }
103 
104         @NonNull
105         @Override
onCreateViewHolder(ViewGroup viewGroup, int viewType)106         public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
107             View v = LayoutInflater.from(viewGroup.getContext())
108                     .inflate(R.layout.text_line_item, viewGroup, /* attachToRoot= */ false);
109 
110             return new ViewHolder(v);
111         }
112 
113         @Override
onBindViewHolder(ViewHolder viewHolder, int position)114         public void onBindViewHolder(ViewHolder viewHolder, int position) {
115             viewHolder.getTextView().setText(mViewData.get(position));
116         }
117 
118         @Override
getItemCount()119         public int getItemCount() {
120             return mViewData.size();
121         }
122     }
123 }
124