1 /* 2 * Copyright (C) 2016 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.radio; 18 19 import android.content.Context; 20 import android.hardware.radio.RadioManager; 21 import com.android.car.radio.service.RadioStation; 22 23 import java.text.DecimalFormat; 24 import java.util.Locale; 25 26 /** 27 * Common formatters for displaying channel numbers for various radio bands. 28 */ 29 public final class RadioChannelFormatter { 30 private static final String FM_CHANNEL_FORMAT = "###.#"; 31 private static final String AM_CHANNEL_FORMAT = "####"; 32 RadioChannelFormatter()33 private RadioChannelFormatter() {} 34 35 /** 36 * The formatter for AM radio stations. 37 */ 38 public static final DecimalFormat FM_FORMATTER = new DecimalFormat(FM_CHANNEL_FORMAT); 39 40 /** 41 * The formatter for FM radio stations. 42 */ 43 public static final DecimalFormat AM_FORMATTER = new DecimalFormat(AM_CHANNEL_FORMAT); 44 45 /** 46 * Convenience method to format a given {@link RadioStation} based on the value in 47 * {@link RadioStation#getRadioBand()}. If the band is invalid or support for its formatting is 48 * not available, then an empty String is returned. 49 * 50 * @param band One of the band values specified in {@link RadioManager}. For example, 51 * {@link RadioManager#BAND_FM}. 52 * @param channelNumber The channel number to format. This value should be in KHz. 53 * @return A correctly formatted channel number or an empty string if one cannot be formed. 54 */ formatRadioChannel(int band, int channelNumber)55 public static String formatRadioChannel(int band, int channelNumber) { 56 switch (band) { 57 case RadioManager.BAND_AM: 58 return AM_FORMATTER.format(channelNumber); 59 60 case RadioManager.BAND_FM: 61 // FM channels are displayed in KHz, so divide by 1000. 62 return FM_FORMATTER.format((float) channelNumber / 1000); 63 64 // TODO: Handle formats for AM and FM HD stations. 65 66 default: 67 return ""; 68 } 69 } 70 71 /** 72 * Formats the given band value into a readable String. 73 * 74 * @param band One of the band values specified in {@link RadioManager}. For example, 75 * {@link RadioManager#BAND_FM}. 76 * @return The formatted string or an empty string if the band is invalid. 77 */ formatRadioBand(Context context, int band)78 public static String formatRadioBand(Context context, int band) { 79 String radioBandText; 80 81 switch (band) { 82 case RadioManager.BAND_AM: 83 radioBandText = context.getString(R.string.radio_am_text); 84 break; 85 86 case RadioManager.BAND_FM: 87 radioBandText = context.getString(R.string.radio_fm_text); 88 break; 89 90 // TODO: Handle formats for AM and FM HD stations. 91 92 default: 93 radioBandText = ""; 94 } 95 96 return radioBandText.toUpperCase(Locale.getDefault()); 97 } 98 } 99