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