1 /**
2  * Copyright (C) 2018 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 androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 import com.android.car.radio.bands.ProgramType;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Objects;
27 
28 /**
29  * Keeps track of the current band and list of supported program types/bands (AM/FM/DAB etc).
30  */
31 public class BandController {
32     private final Object mLock = new Object();
33 
34     @NonNull private List<ProgramType> mSupportedBands = new ArrayList<>();
35     @Nullable private ProgramType mCurrentBand;
36 
37     /**
38      * Sets supported program types.
39      */
setSupportedProgramTypes(@onNull List<ProgramType> supported)40     public void setSupportedProgramTypes(@NonNull List<ProgramType> supported) {
41         synchronized (mLock) {
42             mSupportedBands = Objects.requireNonNull(supported);
43         }
44     }
45 
46     /**
47      * Switches to the next supported band.
48      *
49      * @return The current band after the switch
50      */
switchToNext()51     public ProgramType switchToNext() {
52         synchronized (mLock) {
53             if (mSupportedBands.isEmpty()) {
54                 return null;
55             }
56             mCurrentBand = mSupportedBands.get(
57                     (mSupportedBands.indexOf(mCurrentBand) + 1) % mSupportedBands.size());
58             return mCurrentBand;
59         }
60     }
61 
62     /**
63      * Sets the current band.
64      *
65      * @param programType Program type to set.
66      */
setType(@onNull ProgramType programType)67     public void setType(@NonNull ProgramType programType) {
68         synchronized (mLock) {
69             mCurrentBand = programType;
70         }
71     }
72 
73     /**
74      * Retrieves the current band.
75      */
getCurrentBand()76     public ProgramType getCurrentBand() {
77         return mCurrentBand;
78     }
79 }
80 
81