1 /* 2 * Copyright (C) 2021 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.tv.settings.util; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.view.Display; 22 23 import com.android.tv.settings.R; 24 25 /** This utility class for Resolution Setting **/ 26 public class ResolutionSelectionUtils { 27 28 /** 29 * Returns the refresh rate converted to a string. If the refresh rate has only 0s after the 30 * floating point, they are removed. The unit "Hz" is added to end of refresh rate. 31 */ getRefreshRateString(Resources resources, float refreshRate)32 public static String getRefreshRateString(Resources resources, float refreshRate) { 33 float roundedRefreshRate = Math.round(refreshRate * 100.0f) / 100.0f; 34 if (roundedRefreshRate % 1 == 0) { 35 return ((int) roundedRefreshRate) + " " 36 + resources.getString(R.string.resolution_selection_hz); 37 } else { 38 return roundedRefreshRate + " " + resources.getString(R.string.resolution_selection_hz); 39 } 40 } 41 42 /** 43 * Returns the resolution converted to a string. The unit "p" is added to end of refresh rate. 44 * If the resolution in 2160p, the string returned is "4k". 45 */ getResolutionString(int width, int height)46 public static String getResolutionString(int width, int height) { 47 int resolution = Math.min(width, height); 48 if (resolution == 2160) { 49 return "4k"; 50 } 51 return resolution + "p"; 52 } 53 54 /** 55 * Returns the {@link Display.Mode} converted to a string. 56 * Format: Resolution + "p" + RefreshRate + "Hz" 57 */ modeToString(Display.Mode mode, Context context)58 public static String modeToString(Display.Mode mode, Context context) { 59 if (mode == null) { 60 return context.getString(R.string.resolution_selection_auto_title); 61 } 62 String modeString = getResolutionString(mode.getPhysicalWidth(), mode.getPhysicalHeight()); 63 modeString += " " + getRefreshRateString(context.getResources(), mode.getRefreshRate()); 64 return modeString; 65 } 66 } 67