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 static java.math.RoundingMode.HALF_UP; 20 21 import android.content.Context; 22 import android.icu.number.LocalizedNumberFormatter; 23 import android.icu.number.NumberFormatter; 24 import android.view.Display; 25 26 import com.android.tv.settings.R; 27 28 import java.util.Locale; 29 30 31 /** This utility class for Resolution Setting **/ 32 public class ResolutionSelectionUtils { 33 34 /** 35 * Returns the refresh rate converted to a string in the local language. If the refresh rate has 36 * only 0s after the floating point, they are removed. 37 * The unit "Hz" is added to end of refresh rate. 38 */ getRefreshRateString(float refreshRate)39 public static String getRefreshRateString(float refreshRate) { 40 LocalizedNumberFormatter localizedNumberFormatter = NumberFormatter.with().roundingMode( 41 HALF_UP).locale(Locale.getDefault()); 42 double roundedRefreshRate = Math.round(refreshRate * 100.0f) / 100.0f; 43 if (roundedRefreshRate % 1 == 0) { 44 return localizedNumberFormatter.format(roundedRefreshRate).toString(); 45 } else { 46 return String.format(Locale.getDefault(), "%.2f", 47 localizedNumberFormatter.format(roundedRefreshRate).toBigDecimal()); 48 } 49 } 50 51 /** 52 * Returns the resolution converted to a string. The unit "p" is added to end of refresh rate. 53 * If the resolution in 2160p, the string returned is "4k". 54 */ getResolutionString(int width, int height)55 public static String getResolutionString(int width, int height) { 56 int resolution = Math.min(width, height); 57 if (resolution == 2160) { 58 return "4k"; 59 } 60 return resolution + "p"; 61 } 62 63 /** 64 * Returns the {@link Display.Mode} converted to a string. 65 * Format: Resolution + "p" + RefreshRate + "Hz" 66 */ modeToString(Display.Mode mode, Context context)67 public static String modeToString(Display.Mode mode, Context context) { 68 if (mode == null) { 69 return context.getString(R.string.resolution_selection_auto_title); 70 } 71 final String modeString = context.getString(R.string.resolution_display_mode, 72 ResolutionSelectionUtils.getResolutionString( 73 mode.getPhysicalWidth(), mode.getPhysicalHeight()), 74 ResolutionSelectionUtils.getRefreshRateString(mode.getRefreshRate())); 75 return modeString; 76 } 77 78 /** 79 * Returns the resolution mode converted to a string in the local language. 80 * Format: width + " x " + height 81 */ getResolutionSummary(int physicalWidth, int physicalHeight)82 public static String getResolutionSummary(int physicalWidth, int physicalHeight) { 83 LocalizedNumberFormatter localizedNumberFormatter = NumberFormatter.with().locale( 84 Locale.getDefault()); 85 return localizedNumberFormatter.format(physicalWidth).toString() + " x " 86 + localizedNumberFormatter.format(physicalHeight).toString(); 87 } 88 } 89