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