• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.google.android.car.bugreport;
17 
18 import android.view.LayoutInflater;
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.widget.Button;
22 import android.widget.TextView;
23 
24 import androidx.recyclerview.widget.RecyclerView;
25 
26 import java.util.List;
27 
28 public class BugInfoAdapter extends RecyclerView.Adapter<BugInfoAdapter.BugInfoViewHolder> {
29 
30     static final int BUTTON_TYPE_UPLOAD = 0;
31     static final int BUTTON_TYPE_MOVE = 1;
32 
33     /** Provides a handler for click events*/
34     interface ItemClickedListener {
35         /** onItemClicked handles click events differently depending on provided buttonType and
36          * uses additional information provided in metaBugReport. */
onItemClicked(int buttonType, MetaBugReport metaBugReport)37         void onItemClicked(int buttonType, MetaBugReport metaBugReport);
38     }
39 
40     /**
41      * Reference to each bug report info views.
42      */
43     public static class BugInfoViewHolder extends RecyclerView.ViewHolder {
44         /** Title view */
45         public TextView titleView;
46 
47         /** User view */
48         public TextView userView;
49 
50         /** TimeStamp View */
51         public TextView timestampView;
52 
53         /** Status View */
54         public TextView statusView;
55 
56         /** Message View */
57         public TextView messageView;
58 
59         /** Move Button */
60         public Button moveButton;
61 
62         /** Upload Button */
63         public Button uploadButton;
64 
BugInfoViewHolder(View v)65         BugInfoViewHolder(View v) {
66             super(v);
67             titleView = itemView.findViewById(R.id.bug_info_row_title);
68             userView = itemView.findViewById(R.id.bug_info_row_user);
69             timestampView = itemView.findViewById(R.id.bug_info_row_timestamp);
70             statusView = itemView.findViewById(R.id.bug_info_row_status);
71             messageView = itemView.findViewById(R.id.bug_info_row_message);
72             moveButton = itemView.findViewById(R.id.bug_info_move_button);
73             uploadButton = itemView.findViewById(R.id.bug_info_upload_button);
74         }
75     }
76 
77     private final List<MetaBugReport> mDataset;
78     private final ItemClickedListener mItemClickedListener;
79 
BugInfoAdapter(List<MetaBugReport> dataSet, ItemClickedListener itemClickedListener)80     BugInfoAdapter(List<MetaBugReport> dataSet, ItemClickedListener itemClickedListener) {
81         mDataset = dataSet;
82         mItemClickedListener = itemClickedListener;
83     }
84 
85     @Override
onCreateViewHolder(ViewGroup parent, int viewType)86     public BugInfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
87         // create a new view
88         View v = LayoutInflater.from(parent.getContext())
89                 .inflate(R.layout.bug_info_view, parent, false);
90         return new BugInfoViewHolder(v);
91     }
92 
93     @Override
onBindViewHolder(BugInfoViewHolder holder, int position)94     public void onBindViewHolder(BugInfoViewHolder holder, int position) {
95         MetaBugReport bugreport = mDataset.get(position);
96         holder.titleView.setText(mDataset.get(position).getTitle());
97         holder.userView.setText(mDataset.get(position).getUsername());
98         holder.timestampView.setText(mDataset.get(position).getTimestamp());
99         holder.statusView.setText(Status.toString(mDataset.get(position).getStatus()));
100         holder.messageView.setText(mDataset.get(position).getStatusMessage());
101         if (bugreport.getStatus() == Status.STATUS_PENDING_USER_ACTION.getValue()
102                 || bugreport.getStatus() == Status.STATUS_MOVE_FAILED.getValue()
103                 || bugreport.getStatus() == Status.STATUS_UPLOAD_FAILED.getValue()) {
104             holder.moveButton.setOnClickListener(
105                     view -> mItemClickedListener.onItemClicked(BUTTON_TYPE_MOVE, bugreport));
106             holder.uploadButton.setOnClickListener(
107                     view -> mItemClickedListener.onItemClicked(BUTTON_TYPE_UPLOAD, bugreport));
108         } else {
109             holder.moveButton.setEnabled(false);
110             holder.uploadButton.setEnabled(false);
111         }
112     }
113 
114     @Override
getItemCount()115     public int getItemCount() {
116         return mDataset.size();
117     }
118 }
119