• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bluetooth.audio_util;
18 
19 import android.media.session.PlaybackState;
20 
21 /** Carries the playback status information in a custom object. */
22 // TODO(apanicke): Send the current active song ID along with this object so that all information
23 // is carried by our custom types.
24 public class PlayStatus {
25     static final byte STOPPED = 0;
26     static final byte PLAYING = 1;
27     static final byte PAUSED = 2;
28     static final byte FWD_SEEK = 3;
29     static final byte REV_SEEK = 4;
30     static final byte ERROR = -1;
31 
32     public long position = 0;
33     public long duration = 0x00L;
34     public byte state = STOPPED;
35 
36     // Duration info isn't contained in the PlaybackState so the service must supply it.
fromPlaybackState(PlaybackState state, long duration)37     public static PlayStatus fromPlaybackState(PlaybackState state, long duration) {
38         PlayStatus ret = new PlayStatus();
39         if (state == null) return ret;
40 
41         ret.state = playbackStateToAvrcpState(state.getState());
42         ret.position = (state.getPosition() > 0) ? state.getPosition() : 0;
43         ret.duration = duration;
44         return ret;
45     }
46 
playbackStateToAvrcpState(int playbackState)47     static byte playbackStateToAvrcpState(int playbackState) {
48         switch (playbackState) {
49             case PlaybackState.STATE_STOPPED:
50             case PlaybackState.STATE_NONE:
51             case PlaybackState.STATE_CONNECTING:
52                 return PlayStatus.STOPPED;
53 
54             case PlaybackState.STATE_BUFFERING:
55             case PlaybackState.STATE_PLAYING:
56                 return PlayStatus.PLAYING;
57 
58             case PlaybackState.STATE_PAUSED:
59                 return PlayStatus.PAUSED;
60 
61             case PlaybackState.STATE_FAST_FORWARDING:
62             case PlaybackState.STATE_SKIPPING_TO_NEXT:
63             case PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM:
64                 return PlayStatus.FWD_SEEK;
65 
66             case PlaybackState.STATE_REWINDING:
67             case PlaybackState.STATE_SKIPPING_TO_PREVIOUS:
68                 return PlayStatus.REV_SEEK;
69 
70             case PlaybackState.STATE_ERROR:
71             default:
72                 return PlayStatus.ERROR;
73         }
74     }
75 
76     @Override
toString()77     public String toString() {
78         return "{ state=" + state + " position=" + position + " duration=" + duration + " }";
79     }
80 }
81