summaryrefslogtreecommitdiff
path: root/java/com/android/incallui/incall/impl/CheckableLabeledButton.java
blob: ec932b9dc44d1d67b68c9375312a699f567e9282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.incallui.incall.impl;

import android.animation.AnimatorInflater;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.SoundEffectConstants;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/** A button to show on the incall screen */
public class CheckableLabeledButton extends LinearLayout implements Checkable {

  private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
  private static final float DISABLED_STATE_OPACITY = .3f;
  private boolean broadcasting;
  private boolean isChecked;
  private OnCheckedChangeListener onCheckedChangeListener;
  private ImageView iconView;
  @DrawableRes private int iconResource = 0;
  private TextView labelView;
  private Drawable background;
  private Drawable backgroundMore;

  public CheckableLabeledButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
  }

  public CheckableLabeledButton(Context context) {
    this(context, null);
  }

  private void init(Context context, AttributeSet attrs) {
    setOrientation(VERTICAL);
    setGravity(Gravity.CENTER_HORIZONTAL);
    Drawable icon;
    CharSequence labelText;
    boolean enabled;

    backgroundMore =
        getResources().getDrawable(R.drawable.incall_button_background_more, context.getTheme());
    background =
        getResources().getDrawable(R.drawable.incall_button_background, context.getTheme());

    TypedArray typedArray =
        context.obtainStyledAttributes(attrs, R.styleable.CheckableLabeledButton);
    icon = typedArray.getDrawable(R.styleable.CheckableLabeledButton_incall_icon);
    labelText = typedArray.getString(R.styleable.CheckableLabeledButton_incall_labelText);
    enabled = typedArray.getBoolean(R.styleable.CheckableLabeledButton_android_enabled, true);
    typedArray.recycle();

    int paddingSize = getResources().getDimensionPixelOffset(R.dimen.incall_button_padding);
    setPadding(paddingSize, paddingSize, paddingSize, paddingSize);

    int iconSize = getResources().getDimensionPixelSize(R.dimen.incall_labeled_button_size);
    int imageSize = getResources().getDimensionPixelSize(R.dimen.incall_labeled_button_icon_size);
    int iconPadding = (iconSize - imageSize) / 2;

    iconView = new ImageView(context, null, android.R.style.Widget_Material_Button_Colored);
    LayoutParams iconParams = generateDefaultLayoutParams();
    iconParams.width = iconSize;
    iconParams.height = iconSize;
    iconView.setLayoutParams(iconParams);
    iconView.setPadding(iconPadding, iconPadding, iconPadding, iconPadding);
    iconView.setImageDrawable(icon);
    iconView.setImageTintMode(Mode.SRC_IN);
    iconView.setImageTintList(
        getResources().getColorStateList(R.color.incall_button_icon, context.getTheme()));

    iconView.setBackground(
        getResources().getDrawable(R.drawable.incall_button_background, context.getTheme()));
    iconView.setDuplicateParentStateEnabled(true);
    iconView.setElevation(getResources().getDimension(R.dimen.incall_button_elevation));
    iconView.setStateListAnimator(
        AnimatorInflater.loadStateListAnimator(context, R.animator.incall_button_elevation));
    addView(iconView);

    labelView = new TextView(context);
    LayoutParams labelParams = generateDefaultLayoutParams();
    labelParams.width = LayoutParams.WRAP_CONTENT;
    labelParams.height = LayoutParams.WRAP_CONTENT;
    labelParams.topMargin =
        context.getResources().getDimensionPixelOffset(R.dimen.incall_button_label_margin);
    labelView.setLayoutParams(labelParams);
    labelView.setTextAppearance(R.style.Dialer_Incall_TextAppearance_Label);
    labelView.setText(labelText);
    labelView.setSingleLine();
    labelView.setMaxEms(9);
    labelView.setEllipsize(TruncateAt.END);
    labelView.setGravity(Gravity.CENTER);
    labelView.setDuplicateParentStateEnabled(true);
    addView(labelView);

    setFocusable(true);
    setClickable(true);
    setEnabled(enabled);
    setOutlineProvider(null);
  }

  @Override
  public void refreshDrawableState() {
    super.refreshDrawableState();
    iconView.setAlpha(isEnabled() ? 1f : DISABLED_STATE_OPACITY);
    labelView.setAlpha(isEnabled() ? 1f : DISABLED_STATE_OPACITY);
  }

  public void setCheckedColor(@ColorInt int color) {
    iconView.setImageTintList(
        new ColorStateList(
            new int[][] {new int[] {android.R.attr.state_checked}, new int[] {}},
            new int[] {color, Color.WHITE}));
  }

  public Drawable getIconDrawable() {
    return iconView.getDrawable();
  }

  public void setIconDrawable(@DrawableRes int drawableRes) {
    if (iconResource != drawableRes) {
      iconView.setImageResource(drawableRes);
      iconResource = drawableRes;
    }
  }

  public void setLabelText(@StringRes int stringRes) {
    labelView.setText(stringRes);
  }

  public void setLabelText(CharSequence label) {
    labelView.setText(label);
  }

  /** Shows or hides a little down arrow to indicate that the button will pop up a menu. */
  public void setShouldShowMoreIndicator(boolean shouldShow) {
    iconView.setBackground(shouldShow ? backgroundMore : background);
  }

  @Override
  public boolean isChecked() {
    return isChecked;
  }

  @Override
  public void setChecked(boolean checked) {
    performSetChecked(checked);
  }

  @Override
  public void toggle() {
    userRequestedSetChecked(!isChecked());
  }

  @Override
  public int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    if (isChecked()) {
      mergeDrawableStates(drawableState, CHECKED_STATE_SET);
    }
    return drawableState;
  }

  @Override
  protected void drawableStateChanged() {
    super.drawableStateChanged();
    invalidate();
  }

  public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
    this.onCheckedChangeListener = listener;
  }

  @Override
  public boolean performClick() {
    if (!isCheckable()) {
      return super.performClick();
    }

    toggle();
    final boolean handled = super.performClick();
    if (!handled) {
      // View only makes a sound effect if the onClickListener was
      // called, so we'll need to make one here instead.
      playSoundEffect(SoundEffectConstants.CLICK);
    }
    return handled;
  }

  private boolean isCheckable() {
    return onCheckedChangeListener != null;
  }

  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    SavedState savedState = (SavedState) state;
    super.onRestoreInstanceState(savedState.getSuperState());
    performSetChecked(savedState.isChecked);
    requestLayout();
  }

  @Override
  protected Parcelable onSaveInstanceState() {
    return new SavedState(isChecked(), super.onSaveInstanceState());
  }

  /**
   * Called when the state of the button should be updated, this should not be the result of user
   * interaction.
   *
   * @param checked {@code true} if the button should be in the checked state, {@code false}
   *     otherwise.
   */
  private void performSetChecked(boolean checked) {
    if (isChecked() == checked) {
      return;
    }
    isChecked = checked;
    refreshDrawableState();
  }

  /**
   * Called when the user interacts with a button. This should not result in the button updating
   * state, rather the request should be propagated to the associated listener.
   *
   * @param checked {@code true} if the button should be in the checked state, {@code false}
   *     otherwise.
   */
  private void userRequestedSetChecked(boolean checked) {
    if (isChecked() == checked) {
      return;
    }
    if (broadcasting) {
      return;
    }
    broadcasting = true;
    if (onCheckedChangeListener != null) {
      onCheckedChangeListener.onCheckedChanged(this, checked);
    }
    broadcasting = false;
  }

  /** Callback interface to notify when the button's checked state has changed */
  public interface OnCheckedChangeListener {

    void onCheckedChanged(CheckableLabeledButton checkableLabeledButton, boolean isChecked);
  }

  private static class SavedState extends BaseSavedState {

    public static final Creator<SavedState> CREATOR =
        new Creator<SavedState>() {
          @Override
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }

          @Override
          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
        };
    public final boolean isChecked;

    private SavedState(boolean isChecked, Parcelable superState) {
      super(superState);
      this.isChecked = isChecked;
    }

    protected SavedState(Parcel in) {
      super(in);
      isChecked = in.readByte() != 0;
    }

    @Override
    public int describeContents() {
      return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
      super.writeToParcel(dest, flags);
      dest.writeByte((byte) (isChecked ? 1 : 0));
    }
  }
}