Home | History | Annotate | Download | only in radio
      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 android.support.annotation.ColorInt;
     22 import android.support.annotation.NonNull;
     23 import com.android.car.radio.service.RadioStation;
     24 
     25 /**
     26  * A class that will take a {@link RadioStation} and return its corresponding color. The colors
     27  * for different channels can be found on go/aae-ncar-por in the radio section.
     28  */
     29 public class RadioChannelColorMapper {
     30     // AM values range from 530 - 1700. The following two values represent where this range is cut
     31     // into thirds.
     32     private static final int AM_LOW_THIRD_RANGE = 920;
     33     private static final int AM_HIGH_THIRD_RANGE = 1210;
     34 
     35     // FM values range from 87.9 - 108.1 kHz. The following two values represent where this range
     36     // is cut into thirds.
     37     private static final int FM_LOW_THIRD_RANGE = 94600;
     38     private static final int FM_HIGH_THIRD_RANGE = 101300;
     39 
     40     @ColorInt private final int mDefaultColor;
     41     @ColorInt private final int mAmRange1Color;
     42     @ColorInt private final int mAmRange2Color;
     43     @ColorInt private final int mAmRange3Color;
     44     @ColorInt private final int mFmRange1Color;
     45     @ColorInt private final int mFmRange2Color;
     46     @ColorInt private final int mFmRange3Color;
     47 
     48     public static RadioChannelColorMapper getInstance(Context context) {
     49         return new RadioChannelColorMapper(context);
     50     }
     51 
     52     private RadioChannelColorMapper(Context context) {
     53         mDefaultColor = context.getColor(R.color.car_radio_bg_color);
     54         mAmRange1Color = context.getColor(R.color.am_range_1_bg_color);
     55         mAmRange2Color = context.getColor(R.color.am_range_2_bg_color);
     56         mAmRange3Color = context.getColor(R.color.am_range_3_bg_color);
     57         mFmRange1Color = context.getColor(R.color.fm_range_1_bg_color);
     58         mFmRange2Color = context.getColor(R.color.fm_range_2_bg_color);
     59         mFmRange3Color = context.getColor(R.color.fm_range_3_bg_color);
     60     }
     61 
     62     /**
     63      * Returns the default color for the radio.
     64      */
     65     @ColorInt
     66     public int getDefaultColor() {
     67         return mDefaultColor;
     68     }
     69 
     70     /**
     71      * Convenience method for returning a color based on a {@link RadioStation}.
     72      *
     73      * @see {@link #getColorForStation(int, int)}
     74      */
     75     @ColorInt
     76     public int getColorForStation(@NonNull RadioStation radioStation) {
     77         return getColorForStation(radioStation.getRadioBand(), radioStation.getChannelNumber());
     78     }
     79 
     80     /**
     81      * Returns the color that should be used for the given radio band and channel. If a match cannot
     82      * be made, then {@link #mDefaultColor} is returned.
     83      *
     84      * @param band One of {@link RadioManager}'s band values. (e.g. {@link RadioManager#BAND_AM}.
     85      * @param channel The channel frequency in Hertz.
     86      */
     87     @ColorInt
     88     public int getColorForStation(int band, int channel) {
     89         switch (band) {
     90             case RadioManager.BAND_AM:
     91                 if (channel < AM_LOW_THIRD_RANGE) {
     92                     return mAmRange1Color;
     93                 } else if (channel > AM_HIGH_THIRD_RANGE) {
     94                     return mAmRange3Color;
     95                 }
     96 
     97                 return mAmRange2Color;
     98 
     99             case RadioManager.BAND_FM:
    100                 if (channel < FM_LOW_THIRD_RANGE) {
    101                     return mFmRange1Color;
    102                 } else if (channel > FM_HIGH_THIRD_RANGE) {
    103                     return mFmRange3Color;
    104                 }
    105 
    106                 return mFmRange2Color;
    107 
    108             default:
    109                 return mDefaultColor;
    110         }
    111     }
    112 }
    113