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