1 /*
2  * Copyright (C) 2022 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 package android.car;
17 
18 import android.annotation.NonNull;
19 import android.os.Build;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 /**
24  * Represents the API version of the {@code Car} SDK.
25  */
26 public final class CarVersion extends ApiVersion<CarVersion> implements Parcelable {
27 
28     /**
29      * Contains pre-defined versions matching Car releases.
30      */
31     public static class VERSION_CODES {
32 
33         /**
34          * Helper object for main version of Android 13.
35          */
36         @NonNull
37         public static final CarVersion TIRAMISU_0 =
38                 new CarVersion("TIRAMISU_0", Build.VERSION_CODES.TIRAMISU, 0);
39 
40         /**
41          * Helper object for first minor upgrade of Android 13.
42          */
43         @NonNull
44         public static final CarVersion TIRAMISU_1 =
45                 new CarVersion("TIRAMISU_1", Build.VERSION_CODES.TIRAMISU, 1);
46 
47         /**
48          * Helper object for second minor upgrade of Android 13.
49          */
50         @NonNull
51         public static final CarVersion TIRAMISU_2 =
52                 new CarVersion("TIRAMISU_2", Build.VERSION_CODES.TIRAMISU, 2);
53 
54         /**
55          * Helper object for third minor upgrade of Android 13.
56          */
57         @NonNull
58         public static final CarVersion TIRAMISU_3 =
59                 new CarVersion("TIRAMISU_3", Build.VERSION_CODES.TIRAMISU, 3);
60 
61         /**
62          * Helper object for main version of Android 14.
63          */
64         @NonNull
65         public static final CarVersion UPSIDE_DOWN_CAKE_0 =
66                 new CarVersion("UPSIDE_DOWN_CAKE_0", Build.VERSION_CODES.UPSIDE_DOWN_CAKE, 0);
67 
68        /**
69          * Helper object for first minor upgrade of Android 14.
70          */
71         @NonNull
72         public static final CarVersion UPSIDE_DOWN_CAKE_1 =
73                 new CarVersion("UPSIDE_DOWN_CAKE_1", Build.VERSION_CODES.UPSIDE_DOWN_CAKE, 1);
74 
75         /**
76          * Helper object for main version of Android 15.
77          */
78         @NonNull
79         public static final CarVersion VANILLA_ICE_CREAM_0 =
80                 new CarVersion("VANILLA_ICE_CREAM_0", Build.VERSION_CODES.VANILLA_ICE_CREAM, 0);
81 
VERSION_CODES()82         private VERSION_CODES() {
83             throw new UnsupportedOperationException("Only provide constants");
84         }
85     }
86 
87     /**
88      * Creates a named instance with the given major and minor versions.
89      */
newInstance(String versionName, int majorVersion, int minorVersion)90     static CarVersion newInstance(String versionName, int majorVersion, int minorVersion) {
91         return new CarVersion(versionName, majorVersion, minorVersion);
92     }
93 
94     /**
95      * Creates a new instance with the given major and minor versions.
96      */
97     @NonNull
forMajorAndMinorVersions(int majorVersion, int minorVersion)98     public static CarVersion forMajorAndMinorVersions(int majorVersion, int minorVersion) {
99         return new CarVersion(majorVersion, minorVersion);
100     }
101 
102     /**
103      * Creates a new instance for a major version (i.e., the minor version will be {@code 0}.
104      */
105     @NonNull
forMajorVersion(int majorVersion)106     public static CarVersion forMajorVersion(int majorVersion) {
107         return new CarVersion(majorVersion, /* minorVersion= */ 0);
108     }
109 
CarVersion(String name, int majorVersion, int minorVersion)110     private CarVersion(String name, int majorVersion, int minorVersion) {
111         super(name, majorVersion, minorVersion);
112     }
113 
CarVersion(int majorVersion, int minorVersion)114     private CarVersion(int majorVersion, int minorVersion) {
115         super(majorVersion, minorVersion);
116     }
117 
118     @Override
describeContents()119     public int describeContents() {
120         return 0;
121     }
122 
123     @Override
writeToParcel(@onNull Parcel dest, int flags)124     public void writeToParcel(@NonNull Parcel dest, int flags) {
125         writeToParcel(dest);
126     }
127 
128     @NonNull
129     public static final Parcelable.Creator<CarVersion> CREATOR =
130             new Parcelable.Creator<CarVersion>() {
131 
132         @Override
133         public CarVersion createFromParcel(Parcel source) {
134             return ApiVersion.readFromParcel(source,
135                     (name, major, minor) -> new CarVersion(name, major, minor));
136         }
137 
138         @Override
139         public CarVersion[] newArray(int size) {
140             return new CarVersion[size];
141         }
142     };
143 }
144