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