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.broadcastradio.support; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.graphics.Bitmap; 22 import android.hardware.radio.ProgramSelector; 23 import android.hardware.radio.RadioManager.ProgramInfo; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import com.android.car.broadcastradio.support.platform.ProgramInfoExt; 28 29 import java.util.Objects; 30 31 /** 32 * Holds storable information about a Program. 33 * 34 * Contrary to {@link android.hardware.radio.RadioManager.ProgramInfo}, it doesn't hold runtime 35 * information, like artist or signal quality. 36 */ 37 public final class Program implements Parcelable { 38 private final @NonNull ProgramSelector mSelector; 39 private final @NonNull String mName; 40 41 public Program(@NonNull ProgramSelector selector, @NonNull String name) { 42 mSelector = Objects.requireNonNull(selector); 43 mName = Objects.requireNonNull(name); 44 } 45 46 public @NonNull ProgramSelector getSelector() { 47 return mSelector; 48 } 49 50 public @NonNull String getName() { 51 return mName; 52 } 53 54 /** @hide */ 55 public @Nullable Bitmap getIcon() { 56 // TODO(b/73950974): implement saving icons 57 return null; 58 } 59 60 @Override 61 public String toString() { 62 return "Program(\"" + mName + "\", " + mSelector + ")"; 63 } 64 65 @Override 66 public int hashCode() { 67 return mSelector.hashCode(); 68 } 69 70 /** 71 * Two programs are considered equal if their selectors are equal. 72 */ 73 @Override 74 public boolean equals(Object obj) { 75 if (this == obj) return true; 76 if (!(obj instanceof Program)) return false; 77 Program other = (Program) obj; 78 return mSelector.equals(other.mSelector); 79 } 80 81 /** 82 * Builds a new {@link Program} object from {@link ProgramInfo}. 83 */ 84 public static @NonNull Program fromProgramInfo(@NonNull ProgramInfo info) { 85 return new Program(info.getSelector(), ProgramInfoExt.getProgramName(info, 0)); 86 } 87 88 private Program(Parcel in) { 89 mSelector = Objects.requireNonNull(in.readTypedObject(ProgramSelector.CREATOR)); 90 mName = Objects.requireNonNull(in.readString()); 91 } 92 93 @Override 94 public void writeToParcel(Parcel dest, int flags) { 95 dest.writeTypedObject(mSelector, 0); 96 dest.writeString(mName); 97 } 98 99 @Override 100 public int describeContents() { 101 return 0; 102 } 103 104 public static final Parcelable.Creator<Program> CREATOR = new Parcelable.Creator<Program>() { 105 public Program createFromParcel(Parcel in) { 106 return new Program(in); 107 } 108 109 public Program[] newArray(int size) { 110 return new Program[size]; 111 } 112 }; 113 } 114