blob: 51d4703813fd9ca473eb9aca398db59beb53b7ae [file] [log] [blame]
Dan Albert30c9d6e2017-03-28 14:54:55 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18 "encoding/json"
Dan Albert6bc5b832018-05-03 15:42:34 -070019 "strconv"
Dan Albert30c9d6e2017-03-28 14:54:55 -070020)
21
22func init() {
23 RegisterSingletonType("api_levels", ApiLevelsSingleton)
24}
25
Colin Cross0875c522017-11-28 17:34:01 -080026func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -070027 return &apiLevelsSingleton{}
28}
29
30type apiLevelsSingleton struct{}
31
Colin Cross0875c522017-11-28 17:34:01 -080032func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -070033 apiLevelsMap map[string]int) {
34
35 jsonStr, err := json.Marshal(apiLevelsMap)
36 if err != nil {
37 ctx.Errorf(err.Error())
38 }
39
Colin Cross0875c522017-11-28 17:34:01 -080040 ctx.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070041 Rule: WriteFile,
Colin Cross0875c522017-11-28 17:34:01 -080042 Description: "generate " + file.Base(),
43 Output: file,
Dan Albert30c9d6e2017-03-28 14:54:55 -070044 Args: map[string]string{
45 "content": string(jsonStr[:]),
46 },
47 })
48}
49
Colin Cross0875c522017-11-28 17:34:01 -080050func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -070051 return PathForOutput(ctx, "api_levels.json")
52}
53
Colin Cross571cccf2019-02-04 11:22:08 -080054var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
55
Dan Albert6bc5b832018-05-03 15:42:34 -070056func getApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -080057 return config.Once(apiLevelsMapKey, func() interface{} {
Dan Albert6bc5b832018-05-03 15:42:34 -070058 baseApiLevel := 9000
59 apiLevelsMap := map[string]int{
60 "G": 9,
61 "I": 14,
62 "J": 16,
63 "J-MR1": 17,
64 "J-MR2": 18,
65 "K": 19,
66 "L": 21,
67 "L-MR1": 22,
68 "M": 23,
69 "N": 24,
70 "N-MR1": 25,
71 "O": 26,
72 "O-MR1": 27,
73 "P": 28,
74 }
75 for i, codename := range config.PlatformVersionCombinedCodenames() {
76 apiLevelsMap[codename] = baseApiLevel + i
77 }
Dan Albert30c9d6e2017-03-28 14:54:55 -070078
Dan Albert6bc5b832018-05-03 15:42:34 -070079 return apiLevelsMap
80 }).(map[string]int)
81}
82
83// Converts an API level string into its numeric form.
84// * Codenames are decoded.
85// * Numeric API levels are simply converted.
86// * "minimum" and "current" are not currently handled since the former is
87// NDK specific and the latter has inconsistent meaning.
88func ApiStrToNum(ctx BaseContext, apiLevel string) (int, error) {
89 num, ok := getApiLevelsMap(ctx.Config())[apiLevel]
90 if ok {
91 return num, nil
92 }
93 return strconv.Atoi(apiLevel)
94}
95
96func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
97 apiLevelsMap := getApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -070098 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -080099 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700100}