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