1 /*
2  * Copyright 2019 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.media.common.playback;
18 
19 import android.annotation.SuppressLint;
20 import android.media.session.PlaybackState;
21 
22 import java.util.concurrent.TimeUnit;
23 
24 /**
25  * This class represents the current playback progress, and provides methods to display the
26  * progress in a human-readable way.
27  */
28 public class PlaybackProgress {
29 
30     private final long mProgress;
31     private final long mMaxProgress;
32 
PlaybackProgress(long progress, long maxProgress)33     public PlaybackProgress(long progress, long maxProgress) {
34         mProgress = progress;
35         mMaxProgress = maxProgress;
36     }
37 
38     /**
39      * Returns the current track's progress
40      */
getProgress()41     public long getProgress() {
42         return mProgress;
43     }
44 
45     /**
46      * Returns the current track's maximum progress
47      */
getMaxProgress()48     public long getMaxProgress() {
49         return mMaxProgress;
50     }
51 
52     /**
53      * Returns the current track's progress in text form
54      */
getCurrentTimeText()55     public CharSequence getCurrentTimeText() {
56         boolean showHours = TimeUnit.MILLISECONDS.toHours(mMaxProgress) > 0;
57         return formatTime(mProgress, showHours);
58     }
59 
60     /**
61      * Returns the current track's maximum progress in text form
62      */
getMaxTimeText()63     public CharSequence getMaxTimeText() {
64         boolean showHours = TimeUnit.MILLISECONDS.toHours(mMaxProgress) > 0;
65         return formatTime(mMaxProgress, showHours);
66     }
67 
68     /**
69      * Returns whether the current track's progress is available
70      */
hasTime()71     public boolean hasTime() {
72         return mMaxProgress > 0 && mProgress != PlaybackState.PLAYBACK_POSITION_UNKNOWN;
73     }
74 
75     @SuppressLint("DefaultLocale")
formatTime(long millis, boolean showHours)76     private static String formatTime(long millis, boolean showHours) {
77         long hours = TimeUnit.MILLISECONDS.toHours(millis);
78         long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1);
79         long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1);
80         if (showHours) {
81             return String.format("%d:%02d:%02d", hours, minutes, seconds);
82         } else {
83             return String.format("%d:%02d", minutes, seconds);
84         }
85     }
86 }
87