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 static com.android.documentsui.base.State.MODE_GRID; 20 21 import android.content.Context; 22 import android.database.Cursor; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.Button; 26 import android.widget.ImageView; 27 import android.widget.TextView; 28 29 import com.android.documentsui.R; 30 import com.android.documentsui.base.State.ViewMode; 31 32 /** 33 * RecyclerView.ViewHolder class that displays at the top of the directory list when there 34 * are more information from the Provider. 35 * Used by {@link DirectoryAddonsAdapter}. 36 */ 37 final class HeaderMessageDocumentHolder extends MessageHolder { 38 private final View mRoot; 39 private final ImageView mIcon; 40 private final TextView mTitle; 41 private final TextView mSubtitle; 42 private final TextView mTextView; 43 private final View mActionView; 44 private final Button mDismissButton; 45 private final Button mActionButton; 46 private Message mMessage; 47 HeaderMessageDocumentHolder(Context context, ViewGroup parent)48 public HeaderMessageDocumentHolder(Context context, ViewGroup parent) { 49 super(context, parent, R.layout.item_doc_header_message); 50 51 mRoot = itemView.findViewById(R.id.item_root); 52 mIcon = (ImageView) itemView.findViewById(R.id.message_icon); 53 mTitle = itemView.findViewById(R.id.message_title); 54 mSubtitle = itemView.findViewById(R.id.message_subtitle); 55 mTextView = (TextView) itemView.findViewById(R.id.message_textview); 56 mActionView = (View) itemView.findViewById(R.id.action_view); 57 mActionButton = (Button) itemView.findViewById(R.id.action_button); 58 mDismissButton = (Button) itemView.findViewById(R.id.dismiss_button); 59 } 60 bind(Message message)61 public void bind(Message message) { 62 mMessage = message; 63 mDismissButton.setOnClickListener(this::onButtonClick); 64 mActionButton.setOnClickListener(this::onButtonClick); 65 bind(null, null); 66 } 67 68 /** 69 * We set different padding on directory parent in different mode by 70 * {@link DirectoryFragment#onViewModeChanged()}. To avoid the layout shifting when 71 * display mode changed, we set the opposite padding on the item. 72 */ setPadding(@iewMode int mode)73 public void setPadding(@ViewMode int mode) { 74 int padding = itemView.getResources().getDimensionPixelSize(mode == MODE_GRID 75 ? R.dimen.list_container_padding : R.dimen.grid_container_padding); 76 mRoot.setPadding(padding, 0, padding, 0); 77 } 78 onButtonClick(View button)79 private void onButtonClick(View button) { 80 mMessage.runCallback(); 81 } 82 83 @Override bind(Cursor cursor, String modelId)84 public void bind(Cursor cursor, String modelId) { 85 if (mMessage.getTitleString() != null) { 86 mTitle.setVisibility(View.VISIBLE); 87 mSubtitle.setVisibility(View.VISIBLE); 88 mTextView.setVisibility(View.GONE); 89 mTitle.setText(mMessage.getTitleString()); 90 mSubtitle.setText(mMessage.getMessageString()); 91 } else { 92 mTitle.setVisibility(View.GONE); 93 mSubtitle.setVisibility(View.GONE); 94 mTextView.setVisibility(View.VISIBLE); 95 mTextView.setText(mMessage.getMessageString()); 96 } 97 98 mIcon.setImageDrawable(mMessage.getIcon()); 99 100 if (mMessage.shouldKeep()) { 101 mActionView.setVisibility(View.VISIBLE); 102 mDismissButton.setVisibility(View.GONE); 103 if (mMessage.getButtonString() != null) { 104 mActionButton.setText(mMessage.getButtonString()); 105 } 106 } else { 107 mActionView.setVisibility(View.GONE); 108 mDismissButton.setVisibility(View.VISIBLE); 109 if (mMessage.getButtonString() != null) { 110 mDismissButton.setText(mMessage.getButtonString()); 111 } 112 } 113 } 114 }