1 /*
2  * Copyright (C) 2024 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.avrcpcontroller;
18 
19 import android.support.v4.media.session.PlaybackStateCompat;
20 
21 /** A package global set of utilities for the AVRCP Controller implementation to leverage */
22 public final class AvrcpControllerUtils {
23 
24     /** Convert an AVRCP Passthrough command id to a human readable version of the key */
passThruIdToString(int id)25     public static String passThruIdToString(int id) {
26         StringBuilder sb = new StringBuilder();
27         switch (id) {
28             case AvrcpControllerService.PASS_THRU_CMD_ID_PLAY:
29                 sb.append("PLAY");
30                 break;
31             case AvrcpControllerService.PASS_THRU_CMD_ID_PAUSE:
32                 sb.append("PAUSE");
33                 break;
34             case AvrcpControllerService.PASS_THRU_CMD_ID_VOL_UP:
35                 sb.append("VOL_UP");
36                 break;
37             case AvrcpControllerService.PASS_THRU_CMD_ID_VOL_DOWN:
38                 sb.append("VOL_DOWN");
39                 break;
40             case AvrcpControllerService.PASS_THRU_CMD_ID_STOP:
41                 sb.append("STOP");
42                 break;
43             case AvrcpControllerService.PASS_THRU_CMD_ID_FF:
44                 sb.append("FF");
45                 break;
46             case AvrcpControllerService.PASS_THRU_CMD_ID_REWIND:
47                 sb.append("REWIND");
48                 break;
49             case AvrcpControllerService.PASS_THRU_CMD_ID_FORWARD:
50                 sb.append("FORWARD");
51                 break;
52             case AvrcpControllerService.PASS_THRU_CMD_ID_BACKWARD:
53                 sb.append("BACKWARD");
54                 break;
55             default:
56                 sb.append("UNKNOWN_CMD_" + id);
57                 break;
58         }
59         sb.append(" (" + id + ")");
60         return sb.toString();
61     }
62 
63     /** Convert an entire PlaybackStateCompat to a string that contains human readable states */
playbackStateCompatToString(PlaybackStateCompat playbackState)64     public static String playbackStateCompatToString(PlaybackStateCompat playbackState) {
65         if (playbackState == null) {
66             return null;
67         }
68 
69         StringBuilder sb = new StringBuilder("PlaybackState {");
70         sb.append("state=").append(playbackStateToString(playbackState.getState()));
71         sb.append(", position=").append(playbackState.getPosition());
72         sb.append(", buffered position=").append(playbackState.getBufferedPosition());
73         sb.append(", speed=").append(playbackState.getPlaybackSpeed());
74         sb.append(", updated=").append(playbackState.getLastPositionUpdateTime());
75         sb.append(", actions=").append(playbackState.getActions());
76         sb.append(", error code=").append(playbackState.getErrorCode());
77         sb.append(", error message=").append(playbackState.getErrorMessage());
78         sb.append(", custom actions=").append(playbackState.getCustomActions());
79         sb.append(", active item id=").append(playbackState.getActiveQueueItemId());
80         sb.append("}");
81         return sb.toString();
82     }
83 
84     /** Convert a playback state constant to a human readable version of the state */
playbackStateToString(int playbackState)85     public static String playbackStateToString(int playbackState) {
86         StringBuilder sb = new StringBuilder();
87         switch (playbackState) {
88             case PlaybackStateCompat.STATE_NONE:
89                 sb.append("STATE_NONE");
90                 break;
91             case PlaybackStateCompat.STATE_STOPPED:
92                 sb.append("STATE_STOPPED");
93                 break;
94             case PlaybackStateCompat.STATE_PAUSED:
95                 sb.append("STATE_PAUSED");
96                 break;
97             case PlaybackStateCompat.STATE_PLAYING:
98                 sb.append("STATE_PLAYING");
99                 break;
100             case PlaybackStateCompat.STATE_FAST_FORWARDING:
101                 sb.append("STATE_FAST_FORWARDING");
102                 break;
103             case PlaybackStateCompat.STATE_REWINDING:
104                 sb.append("STATE_REWINDING");
105                 break;
106             case PlaybackStateCompat.STATE_BUFFERING:
107                 sb.append("STATE_BUFFERING");
108                 break;
109             case PlaybackStateCompat.STATE_ERROR:
110                 sb.append("STATE_ERROR");
111                 break;
112             case PlaybackStateCompat.STATE_CONNECTING:
113                 sb.append("STATE_CONNECTING");
114                 break;
115             case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
116                 sb.append("STATE_SKIPPING_TO_PREVIOUS");
117                 break;
118             case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
119                 sb.append("STATE_SKIPPING_TO_NEXT");
120                 break;
121             case PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM:
122                 sb.append("STATE_SKIPPING_TO_QUEUE_ITEM");
123                 break;
124             default:
125                 sb.append("UNKNOWN_PLAYBACK_STATE");
126                 break;
127         }
128         sb.append(" (" + playbackState + ")");
129         return sb.toString();
130     }
131 }
132