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