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 android.support.v7.app;
18
19import android.content.Context;
20import android.graphics.ColorFilter;
21import android.graphics.PorterDuff;
22import android.graphics.PorterDuffColorFilter;
23import android.graphics.drawable.AnimationDrawable;
24import android.support.v4.content.ContextCompat;
25import android.support.v7.mediarouter.R;
26import android.util.AttributeSet;
27import android.view.View;
28import android.widget.ImageButton;
29
30/**
31 * Chevron/Caret button to expand/collapse group volume list with animation.
32 */
33class MediaRouteExpandCollapseButton extends ImageButton {
34    final AnimationDrawable mExpandAnimationDrawable;
35    final AnimationDrawable mCollapseAnimationDrawable;
36    final String mExpandGroupDescription;
37    final String mCollapseGroupDescription;
38    boolean mIsGroupExpanded;
39    OnClickListener mListener;
40
41    public MediaRouteExpandCollapseButton(Context context) {
42        this(context, null);
43    }
44
45    public MediaRouteExpandCollapseButton(Context context, AttributeSet attrs) {
46        this(context, attrs, 0);
47    }
48
49    public MediaRouteExpandCollapseButton(Context context, AttributeSet attrs, int defStyleAttr) {
50        super(context, attrs, defStyleAttr);
51        mExpandAnimationDrawable = (AnimationDrawable) ContextCompat.getDrawable(
52                context, R.drawable.mr_group_expand);
53        mCollapseAnimationDrawable = (AnimationDrawable) ContextCompat.getDrawable(
54                context, R.drawable.mr_group_collapse);
55
56        ColorFilter filter = new PorterDuffColorFilter(
57                MediaRouterThemeHelper.getControllerColor(context, defStyleAttr),
58                PorterDuff.Mode.SRC_IN);
59        mExpandAnimationDrawable.setColorFilter(filter);
60        mCollapseAnimationDrawable.setColorFilter(filter);
61
62        mExpandGroupDescription = context.getString(R.string.mr_controller_expand_group);
63        mCollapseGroupDescription = context.getString(R.string.mr_controller_collapse_group);
64
65        setImageDrawable(mExpandAnimationDrawable.getFrame(0));
66        setContentDescription(mExpandGroupDescription);
67
68        super.setOnClickListener(new OnClickListener() {
69            @Override
70            public void onClick(View view) {
71                mIsGroupExpanded = !mIsGroupExpanded;
72                if (mIsGroupExpanded) {
73                    setImageDrawable(mExpandAnimationDrawable);
74                    mExpandAnimationDrawable.start();
75                    setContentDescription(mCollapseGroupDescription);
76                } else {
77                    setImageDrawable(mCollapseAnimationDrawable);
78                    mCollapseAnimationDrawable.start();
79                    setContentDescription(mExpandGroupDescription);
80                }
81                if (mListener != null) {
82                    mListener.onClick(view);
83                }
84            }
85        });
86    }
87
88    @Override
89    public void setOnClickListener(OnClickListener listener) {
90        mListener = listener;
91    }
92}
93