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
17package com.android.dialer.widget;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.content.res.Resources.Theme;
22import android.support.annotation.ColorRes;
23import android.support.annotation.Nullable;
24import android.support.annotation.StringRes;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.widget.ImageView;
29import android.widget.LinearLayout;
30import android.widget.TextView;
31
32public class EmptyContentView extends LinearLayout implements View.OnClickListener {
33
34  /** Listener to call when action button is clicked. */
35  public interface OnEmptyViewActionButtonClickedListener {
36    void onEmptyViewActionButtonClicked();
37  }
38
39  public static final int NO_LABEL = 0;
40  public static final int NO_IMAGE = 0;
41
42  private ImageView imageView;
43  private TextView descriptionView;
44  private TextView actionView;
45  private OnEmptyViewActionButtonClickedListener onActionButtonClickedListener;
46
47  private @StringRes int actionLabel;
48
49  public EmptyContentView(Context context) {
50    this(context, null);
51  }
52
53  public EmptyContentView(Context context, AttributeSet attrs) {
54    this(context, attrs, 0);
55  }
56
57  public EmptyContentView(Context context, AttributeSet attrs, int defStyleAttr) {
58    this(context, attrs, defStyleAttr, 0);
59  }
60
61  public EmptyContentView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
62    super(context, attrs, defStyleAttr, defStyleRes);
63    inflateLayout();
64
65    // Don't let touches fall through the empty view.
66    setClickable(true);
67    imageView = (ImageView) findViewById(R.id.empty_list_view_image);
68    descriptionView = (TextView) findViewById(R.id.empty_list_view_message);
69    actionView = (TextView) findViewById(R.id.empty_list_view_action);
70    actionView.setOnClickListener(this);
71  }
72
73  public void setDescription(int resourceId) {
74    if (resourceId == NO_LABEL) {
75      descriptionView.setText(null);
76      descriptionView.setVisibility(View.GONE);
77    } else {
78      descriptionView.setText(resourceId);
79      descriptionView.setVisibility(View.VISIBLE);
80    }
81  }
82
83  public void setImage(int resourceId) {
84    if (resourceId == NO_LABEL) {
85      imageView.setImageDrawable(null);
86      imageView.setVisibility(View.GONE);
87    } else {
88      imageView.setImageResource(resourceId);
89      imageView.setVisibility(View.VISIBLE);
90    }
91  }
92
93  public void setImageTint(@ColorRes int color, @Nullable Theme theme) {
94    imageView.setImageTintList(
95        (ColorStateList.valueOf(getContext().getResources().getColor(color, theme))));
96  }
97
98  public void setActionLabel(@StringRes int resourceId) {
99    actionLabel = resourceId;
100    if (resourceId == NO_LABEL) {
101      actionView.setText(null);
102      actionView.setVisibility(View.GONE);
103    } else {
104      actionView.setText(resourceId);
105      actionView.setVisibility(View.VISIBLE);
106    }
107  }
108
109  public @StringRes int getActionLabel() {
110    return actionLabel;
111  }
112
113  public boolean isShowingContent() {
114    return imageView.getVisibility() == View.VISIBLE
115        || descriptionView.getVisibility() == View.VISIBLE
116        || actionView.getVisibility() == View.VISIBLE;
117  }
118
119  public void setActionClickedListener(OnEmptyViewActionButtonClickedListener listener) {
120    onActionButtonClickedListener = listener;
121  }
122
123  @Override
124  public void onClick(View v) {
125    if (onActionButtonClickedListener != null) {
126      onActionButtonClickedListener.onEmptyViewActionButtonClicked();
127    }
128  }
129
130  protected void inflateLayout() {
131    setOrientation(LinearLayout.VERTICAL);
132    final LayoutInflater inflater =
133        (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
134    inflater.inflate(R.layout.empty_content_view, this);
135  }
136}
137