Home | History | Annotate | Download | only in radio
      1 /*
      2  * Copyright (C) 2016 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.car.radio;
     18 
     19 import android.content.Context;
     20 import android.media.session.PlaybackState;
     21 import android.support.car.ui.FabDrawable;
     22 import android.util.AttributeSet;
     23 import android.util.Log;
     24 import android.widget.ImageView;
     25 
     26 /**
     27  * An {@link ImageView} that renders a play/pause button like a floating action button.
     28  */
     29 public class PlayPauseButton extends ImageView {
     30     private static final String TAG = "Em.PlayPauseButton";
     31 
     32     private final int[] STATE_PLAYING = {R.attr.state_playing};
     33     private final int[] STATE_PAUSED = {R.attr.state_paused};
     34 
     35     private int mPlaybackState = -1;
     36 
     37     public PlayPauseButton(Context context, AttributeSet attrs) {
     38         super(context, attrs);
     39 
     40         FabDrawable fabDrawable = new FabDrawable(context);
     41         fabDrawable.setFabAndStrokeColor(
     42                 context.getColor(R.color.car_radio_accent_color));
     43         setBackground(fabDrawable);
     44     }
     45 
     46     /**
     47      * Set the current play state of the button.
     48      *
     49      * @param playState One of the values from {@link PlaybackState}. Only
     50      *                  {@link PlaybackState#STATE_PAUSED} and {@link PlaybackState#STATE_PLAYING}
     51      *                  are valid.
     52      */
     53     public void setPlayState(int playState) {
     54         if (playState != PlaybackState.STATE_PAUSED && playState != PlaybackState.STATE_PLAYING) {
     55             throw new IllegalArgumentException("Playback state should be either "
     56                     + "PlaybackState.STATE_PAUSED or PlaybackState.STATE_PLAYING");
     57         }
     58 
     59         mPlaybackState = playState;
     60     }
     61 
     62     @Override
     63     public int[] onCreateDrawableState(int extraSpace) {
     64         // + 1 so we can potentially add our custom PlayState
     65         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
     66 
     67         switch(mPlaybackState) {
     68             case PlaybackState.STATE_PLAYING:
     69                 mergeDrawableStates(drawableState, STATE_PLAYING);
     70                 break;
     71             case PlaybackState.STATE_PAUSED:
     72                 mergeDrawableStates(drawableState, STATE_PAUSED);
     73                 break;
     74             default:
     75                 Log.e(TAG, "Unknown PlaybackState: " + mPlaybackState);
     76         }
     77         if (getBackground() != null) {
     78             getBackground().setState(drawableState);
     79         }
     80         return drawableState;
     81     }
     82 }
     83