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 standard Android SDK.
25  */
26 public final class PlatformVersion extends ApiVersion<PlatformVersion> implements Parcelable {
27 
28     private static final String CODENAME_REL = "REL";
29 
30     /**
31      * Contains pre-defined versions matching Car releases.
32      */
33     public static class VERSION_CODES {
34 
35         /**
36          * Helper object for main version of Android 13.
37          */
38         @NonNull
39         public static final PlatformVersion TIRAMISU_0 =
40                 new PlatformVersion("TIRAMISU_0", Build.VERSION_CODES.TIRAMISU, 0);
41 
42         /**
43          * Helper object for first minor upgrade of Android 13.
44          */
45         @NonNull
46         public static final PlatformVersion TIRAMISU_1 =
47                 new PlatformVersion("TIRAMISU_1", Build.VERSION_CODES.TIRAMISU, 1);
48 
49         /**
50          * Helper object for second minor upgrade of Android 13.
51          */
52         @NonNull
53         public static final PlatformVersion TIRAMISU_2 =
54                 new PlatformVersion("TIRAMISU_2", Build.VERSION_CODES.TIRAMISU, 2);
55 
56         /**
57          * Helper object for third minor upgrade of Android 13.
58          */
59         @NonNull
60         public static final PlatformVersion TIRAMISU_3 =
61                 new PlatformVersion("TIRAMISU_3", Build.VERSION_CODES.TIRAMISU, 3);
62 
63         /**
64          * Helper object for main version of Android 14.
65          */
66         @NonNull
67         public static final PlatformVersion UPSIDE_DOWN_CAKE_0 =
68                 new PlatformVersion("UPSIDE_DOWN_CAKE_0", Build.VERSION_CODES.UPSIDE_DOWN_CAKE, 0);
69 
70         /**
71          * Helper object for first minor upgrade of Android 14.
72          */
73         @NonNull
74         public static final PlatformVersion UPSIDE_DOWN_CAKE_1 =
75                 new PlatformVersion("UPSIDE_DOWN_CAKE_1", Build.VERSION_CODES.UPSIDE_DOWN_CAKE, 1);
76 
77         // DO NOT ADD minor UPSIDE_DOWN_CAKE version until lint tool is working. (b/275125924)
78 
79         /**
80          * Helper object for main version of Android 15.
81          */
82         @NonNull
83         public static final PlatformVersion VANILLA_ICE_CREAM_0 =
84                 new PlatformVersion("VANILLA_ICE_CREAM_0", Build.VERSION_CODES.VANILLA_ICE_CREAM,
85                         0);
86 
VERSION_CODES()87         private VERSION_CODES() {
88             throw new UnsupportedOperationException("Only provide constants");
89         }
90     }
91 
92     /**
93      * Creates a named instance with the given major and minor versions.
94      */
95     // TODO(b/243429779): should not need @ApiRequirements as it's package-protected
newInstance(String versionName, int majorVersion, int minorVersion)96     static PlatformVersion newInstance(String versionName, int majorVersion, int minorVersion) {
97         return new PlatformVersion(versionName, majorVersion, minorVersion);
98     }
99 
100     /**
101      * Returns the current platform version with given {@code minorVersion}.
102      */
getCurrentPlatformVersionForMinor(String versionName, int minorVersion)103     static PlatformVersion getCurrentPlatformVersionForMinor(String versionName, int minorVersion) {
104         // For un-released version, CUR_DEVELOPMENT should be used instead of SDK_INT.
105         // ex) VERSION_CODES.T is CUR_DEVELOPMENT first then becomes 33 (=SDK_INT) when SDK is
106         // finalized.
107         return new PlatformVersion(versionName,
108                 CODENAME_REL.equals(Build.VERSION.CODENAME) ? Build.VERSION.SDK_INT
109                         : Build.VERSION_CODES.CUR_DEVELOPMENT, minorVersion);
110     }
111     /**
112      * Creates a new instance with the given major and minor versions.
113      */
114     @NonNull
forMajorAndMinorVersions(int majorVersion, int minorVersion)115     public static PlatformVersion forMajorAndMinorVersions(int majorVersion, int minorVersion) {
116         return new PlatformVersion(majorVersion, minorVersion);
117     }
118 
119     /**
120      * Creates a new instance for a major version (i.e., the minor version will be {@code 0}.
121      */
122     @NonNull
forMajorVersion(int majorVersion)123     public static PlatformVersion forMajorVersion(int majorVersion) {
124         return new PlatformVersion(majorVersion, /* minorVersion= */ 0);
125     }
126 
PlatformVersion(String name, int majorVersion, int minorVersion)127     private PlatformVersion(String name, int majorVersion, int minorVersion) {
128         super(name, majorVersion, minorVersion);
129     }
130 
PlatformVersion(int majorVersion, int minorVersion)131     private PlatformVersion(int majorVersion, int minorVersion) {
132         super(majorVersion, minorVersion);
133     }
134 
135     @Override
describeContents()136     public int describeContents() {
137         return 0;
138     }
139 
140     @Override
writeToParcel(@onNull Parcel dest, int flags)141     public void writeToParcel(@NonNull Parcel dest, int flags) {
142         writeToParcel(dest);
143     }
144 
145     @NonNull
146     public static final Parcelable.Creator<PlatformVersion> CREATOR =
147             new Parcelable.Creator<PlatformVersion>() {
148 
149         @Override
150         public PlatformVersion createFromParcel(Parcel source) {
151             return ApiVersion.readFromParcel(source,
152                     (name, major, minor) -> new PlatformVersion(name, major, minor));
153         }
154 
155         @Override
156         public PlatformVersion[] newArray(int size) {
157             return new PlatformVersion[size];
158         }
159     };
160 }
161