Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 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.contacts.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.widget.ImageView; |
| 24 | import android.widget.LinearLayout; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | import com.android.contacts.R; |
| 28 | |
| 29 | public class EmptyContentView extends LinearLayout implements View.OnClickListener { |
| 30 | |
| 31 | public static final int NO_LABEL = 0; |
| 32 | public static final int NO_IMAGE = 0; |
| 33 | |
| 34 | private ImageView mImageView; |
| 35 | private TextView mDescriptionView; |
| 36 | private TextView mActionView; |
| 37 | private OnEmptyViewActionButtonClickedListener mOnActionButtonClickedListener; |
| 38 | |
| 39 | public interface OnEmptyViewActionButtonClickedListener { |
| 40 | public void onEmptyViewActionButtonClicked(); |
| 41 | } |
| 42 | |
| 43 | public EmptyContentView(Context context) { |
| 44 | this(context, null); |
| 45 | } |
| 46 | |
| 47 | public EmptyContentView(Context context, AttributeSet attrs) { |
| 48 | this(context, attrs, 0); |
| 49 | } |
| 50 | |
| 51 | public EmptyContentView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 52 | this(context, attrs, defStyleAttr, 0); |
| 53 | } |
| 54 | |
| 55 | public EmptyContentView(Context context, AttributeSet attrs, int defStyleAttr, |
| 56 | int defStyleRes) { |
| 57 | super(context, attrs, defStyleAttr, defStyleRes); |
| 58 | setOrientation(LinearLayout.VERTICAL); |
| 59 | |
| 60 | final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( |
| 61 | Context.LAYOUT_INFLATER_SERVICE); |
| 62 | inflater.inflate(R.layout.empty_content_view, this); |
| 63 | // Don't let touches fall through the empty view. |
| 64 | setClickable(true); |
| 65 | mImageView = (ImageView) findViewById(R.id.emptyListViewImage); |
| 66 | mDescriptionView = (TextView) findViewById(R.id.emptyListViewMessage); |
| 67 | mActionView = (TextView) findViewById(R.id.emptyListViewAction); |
| 68 | mActionView.setOnClickListener(this); |
| 69 | } |
| 70 | |
| 71 | public void setDescription(int resourceId) { |
| 72 | if (resourceId == NO_LABEL) { |
| 73 | mDescriptionView.setText(null); |
| 74 | mDescriptionView.setVisibility(View.GONE); |
| 75 | } else { |
| 76 | mDescriptionView.setText(resourceId); |
| 77 | mDescriptionView.setVisibility(View.VISIBLE); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public void setImage(int resourceId) { |
| 82 | if (resourceId == NO_LABEL) { |
| 83 | mImageView.setImageDrawable(null); |
| 84 | mImageView.setVisibility(View.GONE); |
| 85 | } else { |
| 86 | mImageView.setImageResource(resourceId); |
| 87 | mImageView.setVisibility(View.VISIBLE); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public void setActionLabel(int resourceId) { |
| 92 | if (resourceId == NO_LABEL) { |
| 93 | mActionView.setText(null); |
| 94 | mActionView.setVisibility(View.GONE); |
| 95 | } else { |
| 96 | mActionView.setText(resourceId); |
| 97 | mActionView.setVisibility(View.VISIBLE); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public boolean isShowingContent() { |
| 102 | return mImageView.getVisibility() == View.VISIBLE |
| 103 | || mDescriptionView.getVisibility() == View.VISIBLE |
| 104 | || mActionView.getVisibility() == View.VISIBLE; |
| 105 | } |
| 106 | |
| 107 | public void setActionClickedListener(OnEmptyViewActionButtonClickedListener listener) { |
| 108 | mOnActionButtonClickedListener = listener; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onClick(View v) { |
| 113 | if (mOnActionButtonClickedListener != null) { |
| 114 | mOnActionButtonClickedListener.onEmptyViewActionButtonClicked(); |
| 115 | } |
| 116 | } |
| 117 | } |