Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 19 | "strconv" |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | RegisterSingletonType("api_levels", ApiLevelsSingleton) |
| 24 | } |
| 25 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 26 | func ApiLevelsSingleton() Singleton { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 27 | return &apiLevelsSingleton{} |
| 28 | } |
| 29 | |
| 30 | type apiLevelsSingleton struct{} |
| 31 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 32 | func createApiLevelsJson(ctx SingletonContext, file WritablePath, |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 33 | apiLevelsMap map[string]int) { |
| 34 | |
| 35 | jsonStr, err := json.Marshal(apiLevelsMap) |
| 36 | if err != nil { |
| 37 | ctx.Errorf(err.Error()) |
| 38 | } |
| 39 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 40 | ctx.Build(pctx, BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 41 | Rule: WriteFile, |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 42 | Description: "generate " + file.Base(), |
| 43 | Output: file, |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 44 | Args: map[string]string{ |
| 45 | "content": string(jsonStr[:]), |
| 46 | }, |
| 47 | }) |
| 48 | } |
| 49 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 50 | func GetApiLevelsJson(ctx PathContext) WritablePath { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 51 | return PathForOutput(ctx, "api_levels.json") |
| 52 | } |
| 53 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 54 | func getApiLevelsMap(config Config) map[string]int { |
| 55 | return config.Once("ApiLevelsMap", func() interface{} { |
| 56 | baseApiLevel := 9000 |
| 57 | apiLevelsMap := map[string]int{ |
| 58 | "G": 9, |
| 59 | "I": 14, |
| 60 | "J": 16, |
| 61 | "J-MR1": 17, |
| 62 | "J-MR2": 18, |
| 63 | "K": 19, |
| 64 | "L": 21, |
| 65 | "L-MR1": 22, |
| 66 | "M": 23, |
| 67 | "N": 24, |
| 68 | "N-MR1": 25, |
| 69 | "O": 26, |
| 70 | "O-MR1": 27, |
| 71 | "P": 28, |
| 72 | } |
| 73 | for i, codename := range config.PlatformVersionCombinedCodenames() { |
| 74 | apiLevelsMap[codename] = baseApiLevel + i |
| 75 | } |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 76 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 77 | return apiLevelsMap |
| 78 | }).(map[string]int) |
| 79 | } |
| 80 | |
| 81 | // Converts an API level string into its numeric form. |
| 82 | // * Codenames are decoded. |
| 83 | // * Numeric API levels are simply converted. |
| 84 | // * "minimum" and "current" are not currently handled since the former is |
| 85 | // NDK specific and the latter has inconsistent meaning. |
| 86 | func ApiStrToNum(ctx BaseContext, apiLevel string) (int, error) { |
| 87 | num, ok := getApiLevelsMap(ctx.Config())[apiLevel] |
| 88 | if ok { |
| 89 | return num, nil |
| 90 | } |
| 91 | return strconv.Atoi(apiLevel) |
| 92 | } |
| 93 | |
| 94 | func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 95 | apiLevelsMap := getApiLevelsMap(ctx.Config()) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 96 | apiLevelsJson := GetApiLevelsJson(ctx) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 97 | createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 98 | } |