1/*
2 * Copyright (C) 2015 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
17package com.android.tv.tuner.util;
18
19import java.util.Locale;
20
21/**
22 * Utility class for tuner status messages.
23 */
24public class StatusTextUtils {
25    private static final int PACKETS_PER_SEC_YELLOW = 1500;
26    private static final int PACKETS_PER_SEC_RED = 1000;
27    private static final int AUDIO_POSITION_MS_RATE_DIFF_YELLOW = 100;
28    private static final int AUDIO_POSITION_MS_RATE_DIFF_RED = 200;
29    private static final String COLOR_RED = "red";
30    private static final String COLOR_YELLOW = "yellow";
31    private static final String COLOR_GREEN = "green";
32    private static final String COLOR_GRAY = "gray";
33
34    private StatusTextUtils() { }
35
36    /**
37     * Returns tuner status warning message in HTML.
38     *
39     * <p>This is only called for debuging and always shown in english.</p>
40     */
41    public static String getStatusWarningInHTML(long packetsPerSec,
42            int videoFrameDrop, int bytesInQueue,
43            long audioPositionUs, long audioPositionUsRate,
44            long audioPtsUs, long audioPtsUsRate,
45            long videoPtsUs, long videoPtsUsRate) {
46        StringBuffer buffer = new StringBuffer();
47
48        // audioPosition should go in rate of 1000ms.
49        long audioPositionMsRate = audioPositionUsRate / 1000;
50        String audioPositionColor;
51        if (Math.abs(audioPositionMsRate - 1000) > AUDIO_POSITION_MS_RATE_DIFF_RED) {
52            audioPositionColor = COLOR_RED;
53        } else if (Math.abs(audioPositionMsRate - 1000) > AUDIO_POSITION_MS_RATE_DIFF_YELLOW) {
54            audioPositionColor = COLOR_YELLOW;
55        } else {
56            audioPositionColor = COLOR_GRAY;
57        }
58        buffer.append(String.format(Locale.US, "<font color=%s>", audioPositionColor));
59        buffer.append(
60                String.format(Locale.US, "audioPositionMs: %d (%d)<br>", audioPositionUs / 1000,
61                        audioPositionMsRate));
62        buffer.append("</font>\n");
63        buffer.append("<font color=" + COLOR_GRAY + ">");
64        buffer.append(String.format(Locale.US, "audioPtsMs: %d (%d, %d)<br>", audioPtsUs / 1000,
65                        audioPtsUsRate / 1000, (audioPtsUs - audioPositionUs) / 1000));
66        buffer.append(String.format(Locale.US, "videoPtsMs: %d (%d, %d)<br>", videoPtsUs / 1000,
67                        videoPtsUsRate / 1000, (videoPtsUs - audioPositionUs) / 1000));
68        buffer.append("</font>\n");
69
70        appendStatusLine(buffer, "KbytesInQueue", bytesInQueue / 1000, 1, 10);
71        buffer.append("<br/>");
72        appendErrorStatusLine(buffer, "videoFrameDrop", videoFrameDrop, 0, 2);
73        buffer.append("<br/>");
74        appendStatusLine(buffer, "packetsPerSec", packetsPerSec, PACKETS_PER_SEC_RED,
75                PACKETS_PER_SEC_YELLOW);
76        return buffer.toString();
77    }
78
79    /**
80     * Returns audio unavailable warning message in HTML.
81     */
82    public static String getAudioWarningInHTML(String msg) {
83        return String.format("<font color=%s>%s</font>\n", COLOR_YELLOW, msg);
84    }
85
86    private static void appendStatusLine(StringBuffer buffer, String factorName, long value,
87            int minRed, int minYellow) {
88        buffer.append("<font color=");
89        if (value <= minRed) {
90            buffer.append(COLOR_RED);
91        } else if (value <= minYellow) {
92            buffer.append(COLOR_YELLOW);
93        } else {
94            buffer.append(COLOR_GREEN);
95        }
96        buffer.append(">");
97        buffer.append(factorName);
98        buffer.append(" : ");
99        buffer.append(value);
100        buffer.append("</font>");
101    }
102
103    private static void appendErrorStatusLine(StringBuffer buffer, String factorName, int value,
104            int minGreen, int minYellow) {
105        buffer.append("<font color=");
106        if (value <= minGreen) {
107            buffer.append(COLOR_GREEN);
108        } else if (value <= minYellow) {
109            buffer.append(COLOR_YELLOW);
110        } else {
111            buffer.append(COLOR_RED);
112        }
113        buffer.append(">");
114        buffer.append(factorName);
115        buffer.append(" : ");
116        buffer.append(value);
117        buffer.append("</font>");
118    }
119}
120