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