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