MediaRouteExpandCollapseButton.java revision 328f1006c17ddeb3eb563580bcd6638e1cfb4320
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 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 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.ic_expand); 53 mCollapseAnimationDrawable = (AnimationDrawable) ContextCompat.getDrawable( 54 context, R.drawable.ic_collapse); 55 56 ColorFilter filter = new PorterDuffColorFilter( 57 MediaRouterThemeHelper.getControllerColor(context), PorterDuff.Mode.SRC_IN); 58 mExpandAnimationDrawable.setColorFilter(filter); 59 mCollapseAnimationDrawable.setColorFilter(filter); 60 61 mExpandGroupDescription = context.getString(R.string.mr_controller_expand_group); 62 mCollapseGroupDescription = context.getString(R.string.mr_controller_collapse_group); 63 64 setImageDrawable(mExpandAnimationDrawable.getFrame(0)); 65 setContentDescription(mExpandGroupDescription); 66 67 super.setOnClickListener(new OnClickListener() { 68 @Override 69 public void onClick(View view) { 70 mIsGroupExpanded = !mIsGroupExpanded; 71 if (mIsGroupExpanded) { 72 setImageDrawable(mExpandAnimationDrawable); 73 mExpandAnimationDrawable.start(); 74 setContentDescription(mCollapseGroupDescription); 75 } else { 76 setImageDrawable(mCollapseAnimationDrawable); 77 mCollapseAnimationDrawable.start(); 78 setContentDescription(mExpandGroupDescription); 79 } 80 if (mListener != null) { 81 mListener.onClick(view); 82 } 83 } 84 }); 85 } 86 87 @Override 88 public void setOnClickListener(OnClickListener listener) { 89 mListener = listener; 90 } 91} 92