1 /*
2  * Copyright (C) 2016 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.documentsui.dirlist;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.text.TextUtils;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ImageView;
25 import android.widget.ProgressBar;
26 import android.widget.TextView;
27 
28 import com.android.documentsui.R;
29 
30 /**
31  * RecyclerView.ViewHolder class that displays a message when there are no contents
32  * in the directory, whether due to no items, no search results or an error.
33  * Used by {@link DirectoryAddonsAdapter}.
34  */
35 final class InflateMessageDocumentHolder extends MessageHolder {
36     public static final int LAYOUT_CROSS_PROFILE_ERROR = 1;
37 
38     private Message mMessage;
39 
40     private TextView mContentMessage;
41     private ImageView mContentImage;
42 
43     private TextView mCrossProfileTitle;
44     private TextView mCrossProfileMessage;
45     private ImageView mCrossProfileImage;
46     private TextView mCrossProfileButton;
47 
48 
49     private View mContentView;
50     private View mCrossProfileView;
51     private View mCrossProfileContent;
52     private ProgressBar mCrossProfileProgress;
53 
InflateMessageDocumentHolder(Context context, ViewGroup parent)54     public InflateMessageDocumentHolder(Context context, ViewGroup parent) {
55         super(context, parent, R.layout.item_doc_inflated_message);
56         mContentView = itemView.findViewById(R.id.content);
57         mCrossProfileView = itemView.findViewById(R.id.cross_profile);
58         mCrossProfileContent = mCrossProfileView.findViewById(R.id.cross_profile_content);
59         mCrossProfileProgress = mCrossProfileView.findViewById(R.id.cross_profile_progress);
60 
61         mContentMessage = mContentView.findViewById(R.id.message);
62         mContentImage = mContentView.findViewById(R.id.artwork);
63 
64         mCrossProfileTitle = mCrossProfileView.findViewById(R.id.title);
65         mCrossProfileMessage = mCrossProfileView.findViewById(R.id.message);
66         mCrossProfileImage = mCrossProfileView.findViewById(R.id.artwork);
67         mCrossProfileButton = mCrossProfileView.findViewById(R.id.button);
68     }
69 
bind(Message message)70     public void bind(Message message) {
71         mMessage = message;
72         bind(null, null);
73     }
74 
75     @Override
bind(Cursor cursor, String modelId)76     public void bind(Cursor cursor, String modelId) {
77         if (mMessage.getLayout() == LAYOUT_CROSS_PROFILE_ERROR) {
78             bindCrossProfileMessageView();
79         } else {
80             bindContentMessageView();
81         }
82     }
83 
onCrossProfileButtonClick(View button)84     private void onCrossProfileButtonClick(View button) {
85         mCrossProfileContent.setVisibility(View.GONE);
86         mCrossProfileProgress.setVisibility(View.VISIBLE);
87         mMessage.runCallback();
88     }
89 
bindContentMessageView()90     private void bindContentMessageView() {
91         mContentView.setVisibility(View.VISIBLE);
92         mCrossProfileView.setVisibility(View.GONE);
93 
94         mContentMessage.setText(mMessage.getMessageString());
95         mContentImage.setImageDrawable(mMessage.getIcon());
96     }
97 
bindCrossProfileMessageView()98     private void bindCrossProfileMessageView() {
99         mContentView.setVisibility(View.GONE);
100         mCrossProfileView.setVisibility(View.VISIBLE);
101         mCrossProfileContent.setVisibility(View.VISIBLE);
102         mCrossProfileProgress.setVisibility(View.GONE);
103 
104         mCrossProfileTitle.setText(mMessage.getTitleString());
105         if (!TextUtils.isEmpty(mMessage.getMessageString())) {
106             mCrossProfileMessage.setVisibility(View.VISIBLE);
107             mCrossProfileMessage.setText(mMessage.getMessageString());
108         } else {
109             mCrossProfileMessage.setVisibility(View.GONE);
110         }
111         mCrossProfileImage.setImageDrawable(mMessage.getIcon());
112         if (!TextUtils.isEmpty(mMessage.getButtonString())) {
113             mCrossProfileButton.setVisibility(View.VISIBLE);
114             mCrossProfileButton.setText(mMessage.getButtonString());
115             mCrossProfileButton.setOnClickListener(this::onCrossProfileButtonClick);
116         } else {
117             mCrossProfileButton.setVisibility(View.GONE);
118         }
119     }
120 }
121