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.tv.tuner.hdhomerun;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.text.TextUtils;
22 
23 /**
24  * An HDHomeRun device detected on the network. This abstraction only contains network data
25  * necessary to establish a connection with the device and does not represent a communication
26  * channel with the device itself. Currently, we only support devices with HTTP streaming
27  * functionality.
28  */
29 public class HdHomeRunDevice implements Parcelable {
30     private int mIpAddress;
31     private int mDeviceType;
32     private int mDeviceId;
33     private int mTunerIndex;
34     private String mDeviceModel;
35 
36     /**
37      * Creates {@code HdHomeRunDevice} object from a parcel.
38      *
39      * @param parcel The parcel to create {@code HdHomeRunDevice} object from.
40      */
HdHomeRunDevice(Parcel parcel)41     public HdHomeRunDevice(Parcel parcel) {
42         mIpAddress = parcel.readInt();
43         mDeviceType = parcel.readInt();
44         mDeviceId = parcel.readInt();
45         mTunerIndex = parcel.readInt();
46         mDeviceModel = parcel.readString();
47     }
48 
49     /**
50      * Creates {@code HdHomeRunDevice} object from IP address, device type, device ID and tuner
51      * index.
52      *
53      * @param ipAddress The IP address to create {@code HdHomeRunDevice} object from.
54      * @param deviceType The device type to create {@code HdHomeRunDevice} object from.
55      * @param deviceId The device ID to create {@code HdHomeRunDevice} object from.
56      * @param tunerIndex The tuner index to {@code HdHomeRunDevice} object from.
57      */
HdHomeRunDevice( int ipAddress, int deviceType, int deviceId, int tunerIndex, String deviceModel)58     public HdHomeRunDevice(
59             int ipAddress, int deviceType, int deviceId, int tunerIndex, String deviceModel) {
60         mIpAddress = ipAddress;
61         mDeviceType = deviceType;
62         mDeviceId = deviceId;
63         mTunerIndex = tunerIndex;
64         mDeviceModel = deviceModel;
65     }
66 
67     /**
68      * Returns the IP address.
69      *
70      * @return the IP address of this homerun device.
71      */
getIpAddress()72     public int getIpAddress() {
73         return mIpAddress;
74     }
75 
76     /**
77      * Returns the device type.
78      *
79      * @return the type of device for this homerun device.
80      */
getDeviceType()81     public int getDeviceType() {
82         return mDeviceType;
83     }
84 
85     /**
86      * Returns the device ID.
87      *
88      * @return the device ID of this homerun device.
89      */
getDeviceId()90     public int getDeviceId() {
91         return mDeviceId;
92     }
93 
94     /**
95      * Returns the tuner index.
96      *
97      * @return the tuner index of this homerun device.
98      */
getTunerIndex()99     public int getTunerIndex() {
100         return mTunerIndex;
101     }
102 
103     /**
104      * Returns the device model.
105      *
106      * @return the device model of this homerun device.
107      */
getDeviceModel()108     public String getDeviceModel() {
109         return mDeviceModel;
110     }
111 
112     @Override
toString()113     public String toString() {
114         String ipAddress =
115                 ""
116                         + ((mIpAddress >>> 24) & 0xff)
117                         + "."
118                         + ((mIpAddress >>> 16) & 0xff)
119                         + "."
120                         + ((mIpAddress >>> 8) & 0xff)
121                         + "."
122                         + (mIpAddress & 0xff);
123         return String.format("[%x-%d:%s]", mDeviceId, mTunerIndex, ipAddress);
124     }
125 
126     @Override
describeContents()127     public int describeContents() {
128         return 0;
129     }
130 
131     @Override
writeToParcel(Parcel out, int flags)132     public void writeToParcel(Parcel out, int flags) {
133         out.writeInt(mIpAddress);
134         out.writeInt(mDeviceType);
135         out.writeInt(mDeviceId);
136         out.writeInt(mTunerIndex);
137         out.writeString(mDeviceModel);
138     }
139 
140     @Override
hashCode()141     public int hashCode() {
142         int hash = 17;
143         hash = hash * 31 + getIpAddress();
144         hash = hash * 31 + getDeviceType();
145         hash = hash * 31 + getDeviceId();
146         hash = hash * 31 + getTunerIndex();
147         return hash;
148     }
149 
150     @Override
equals(Object o)151     public boolean equals(Object o) {
152         if (!(o instanceof HdHomeRunDevice)) {
153             return false;
154         }
155         HdHomeRunDevice rhs = (HdHomeRunDevice) o;
156         return rhs != null
157                 && getIpAddress() == rhs.getIpAddress()
158                 && getDeviceType() == rhs.getDeviceType()
159                 && getDeviceId() == rhs.getDeviceId()
160                 && getTunerIndex() == rhs.getTunerIndex()
161                 && TextUtils.equals(getDeviceModel(), rhs.getDeviceModel());
162     }
163 
164     public static final Parcelable.Creator<HdHomeRunDevice> CREATOR =
165             new Parcelable.Creator<HdHomeRunDevice>() {
166 
167                 @Override
168                 public HdHomeRunDevice createFromParcel(Parcel in) {
169                     return new HdHomeRunDevice(in);
170                 }
171 
172                 @Override
173                 public HdHomeRunDevice[] newArray(int size) {
174                     return new HdHomeRunDevice[size];
175                 }
176             };
177 }
178