• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.support.v7.app;
18 
19 import android.content.Context;
20 import android.graphics.ColorFilter;
21 import android.graphics.PorterDuff;
22 import android.graphics.PorterDuffColorFilter;
23 import android.graphics.drawable.AnimationDrawable;
24 import android.support.v4.content.ContextCompat;
25 import android.support.v7.mediarouter.R;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.widget.ImageButton;
29 
30 /**
31  * Chevron/Caret button to expand/collapse group volume list with animation.
32  */
33 class MediaRouteExpandCollapseButton extends ImageButton {
34     private final AnimationDrawable mExpandAnimationDrawable;
35     private final AnimationDrawable mCollapseAnimationDrawable;
36     private final String mExpandGroupDescription;
37     private final String mCollapseGroupDescription;
38     private boolean mIsGroupExpanded;
39     private OnClickListener mListener;
40 
MediaRouteExpandCollapseButton(Context context)41     public MediaRouteExpandCollapseButton(Context context) {
42         this(context, null);
43     }
44 
MediaRouteExpandCollapseButton(Context context, AttributeSet attrs)45     public MediaRouteExpandCollapseButton(Context context, AttributeSet attrs) {
46         this(context, attrs, 0);
47     }
48 
MediaRouteExpandCollapseButton(Context context, AttributeSet attrs, int defStyleAttr)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
setOnClickListener(OnClickListener listener)89     public void setOnClickListener(OnClickListener listener) {
90         mListener = listener;
91     }
92 }
93