Home | History | Annotate | Download | only in service
      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.service;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 import android.support.annotation.Nullable;
     22 
     23 import java.util.Objects;
     24 
     25 /**
     26  * A representation of a radio station.
     27  */
     28 public class RadioStation implements Parcelable {
     29     private int mChannelNumber;
     30     private int mSubChannelNumber;
     31     private int mBand;
     32     private RadioRds mRds;
     33 
     34     /**
     35      * @param channelNumber Channel number in Hz.
     36      * @param subChannelNumber The subchannel number.
     37      * @param band One of {@link android.hardware.radio.RadioManager#BAND_AM},
     38      *             {@link android.hardware.radio.RadioManager#BAND_FM},
     39      *             {@link android.hardware.radio.RadioManager#BAND_AM_HD} or
     40      *             {@link android.hardware.radio.RadioManager#BAND_FM_HD}.
     41      * @param rds The Radio Data System for a particular channel. This represents the radio
     42      *            metadata.
     43      */
     44     public RadioStation(int channelNumber, int subChannelNumber, int band,
     45             @Nullable RadioRds rds) {
     46         mChannelNumber = channelNumber;
     47         mSubChannelNumber = subChannelNumber;
     48         mBand = band;
     49         mRds = rds;
     50     }
     51 
     52     private RadioStation(Parcel in) {
     53         mChannelNumber = in.readInt();
     54         mSubChannelNumber = in.readInt();
     55         mBand = in.readInt();
     56         mRds = in.readParcelable(RadioRds.class.getClassLoader());
     57     }
     58 
     59     @Override
     60     public void writeToParcel(Parcel out, int flags) {
     61         out.writeInt(mChannelNumber);
     62         out.writeInt(mSubChannelNumber);
     63         out.writeInt(mBand);
     64         out.writeParcelable(mRds, 0);
     65     }
     66 
     67     public int getChannelNumber() {
     68         return mChannelNumber;
     69     }
     70 
     71     public int getSubChannelNumber() {
     72         return mSubChannelNumber;
     73     }
     74 
     75     public int getRadioBand() {
     76         return mBand;
     77     }
     78 
     79     @Nullable
     80     public RadioRds getRds() {
     81         return mRds;
     82     }
     83 
     84     @Override
     85     public String toString() {
     86         return String.format("RadioStation [channel: %s, subchannel: %s, band: %s, rds: %s]",
     87                 mChannelNumber, mSubChannelNumber, mBand, mRds);
     88     }
     89 
     90     /**
     91      * Returns {@code true} if two {@link RadioStation}s are equal. RadioStations are considered
     92      * equal if they have the same channel and subchannel numbers.
     93      */
     94     @Override
     95     public boolean equals(Object object) {
     96         if (object == this) {
     97             return true;
     98         }
     99 
    100         if (!(object instanceof RadioStation)) {
    101             return false;
    102         }
    103 
    104         RadioStation station = (RadioStation) object;
    105         return station.getChannelNumber() == mChannelNumber
    106                 && station.getSubChannelNumber() == mSubChannelNumber;
    107     }
    108 
    109     @Override
    110     public int hashCode() {
    111         return Objects.hash(mChannelNumber, mSubChannelNumber);
    112     }
    113 
    114     @Override
    115     public int describeContents() {
    116         return 0;
    117     }
    118 
    119     public static final Parcelable.Creator<RadioStation> CREATOR = new
    120             Parcelable.Creator<RadioStation>() {
    121                 public RadioStation createFromParcel(Parcel in) {
    122                     return new RadioStation(in);
    123                 }
    124 
    125                 public RadioStation[] newArray(int size) {
    126                     return new RadioStation[size];
    127                 }
    128             };
    129 }
    130