1 /* 2 * Copyright (C) 2021 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.google.android.tv.btservices.remote; 18 19 public class Version implements Comparable<Version> { 20 21 public static class OverrideVersion extends Version { OverrideVersion(Version ver)22 public OverrideVersion(Version ver) { 23 super(ver.major(), ver.minor(), ver.vid(), ver.pid()); 24 } 25 } 26 27 public static final Version BAD_VERSION = new Version(-1, -1, (byte) 0, (byte) 0); 28 29 protected int mMajorVersion; 30 protected int mMinorVersion; 31 protected int mVendorId; 32 protected int mProductId; 33 Version(int majorVersion, int minorVersion, int vendorId, int productId)34 public Version(int majorVersion, int minorVersion, int vendorId, int productId) { 35 mMajorVersion = majorVersion; 36 mMinorVersion = minorVersion; 37 mVendorId = vendorId; 38 mProductId = productId; 39 } 40 major()41 public int major() { 42 return mMajorVersion; 43 } 44 minor()45 public int minor() { 46 return mMinorVersion; 47 } 48 vid()49 public int vid() { 50 return mVendorId; 51 } 52 pid()53 public int pid() { 54 return mProductId; 55 } 56 57 @Override equals(Object o)58 public boolean equals(Object o) { 59 if (!(o instanceof Version)) { 60 return false; 61 } 62 Version v = (Version) o; 63 if (mVendorId != v.mVendorId || mProductId != v.mProductId) { 64 return false; 65 } 66 return v.mMajorVersion == mMajorVersion && v.mMinorVersion == mMinorVersion; 67 } 68 69 @Override compareTo(Version version)70 public int compareTo(Version version) { 71 if (this instanceof OverrideVersion && !(version instanceof OverrideVersion)) { 72 return 1; 73 } 74 if (!(this instanceof OverrideVersion) && version instanceof OverrideVersion) { 75 return -1; 76 } 77 if (mVendorId != version.mVendorId) { 78 return mVendorId - version.mVendorId; 79 } 80 if (mProductId != version.mProductId) { 81 return mProductId - version.mProductId; 82 } 83 final int major = mMajorVersion - version.mMajorVersion; 84 if (major != 0) { 85 return major; 86 } 87 return mMinorVersion - version.mMinorVersion; 88 } 89 toVersionString()90 public String toVersionString() { 91 String major = String.format("%01d", mMajorVersion); 92 String minor = String.format("%02d", mMinorVersion); 93 return major + "." + minor; 94 } 95 96 @Override toString()97 public String toString() { 98 return String.format( 99 "%s (%02X:%02X)", toVersionString(), mVendorId, mProductId); 100 } 101 } 102