Home | History | Annotate | Download | only in android
      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"
     19 	"strconv"
     20 )
     21 
     22 func init() {
     23 	RegisterSingletonType("api_levels", ApiLevelsSingleton)
     24 }
     25 
     26 func ApiLevelsSingleton() Singleton {
     27 	return &apiLevelsSingleton{}
     28 }
     29 
     30 type apiLevelsSingleton struct{}
     31 
     32 func createApiLevelsJson(ctx SingletonContext, file WritablePath,
     33 	apiLevelsMap map[string]int) {
     34 
     35 	jsonStr, err := json.Marshal(apiLevelsMap)
     36 	if err != nil {
     37 		ctx.Errorf(err.Error())
     38 	}
     39 
     40 	ctx.Build(pctx, BuildParams{
     41 		Rule:        WriteFile,
     42 		Description: "generate " + file.Base(),
     43 		Output:      file,
     44 		Args: map[string]string{
     45 			"content": string(jsonStr[:]),
     46 		},
     47 	})
     48 }
     49 
     50 func GetApiLevelsJson(ctx PathContext) WritablePath {
     51 	return PathForOutput(ctx, "api_levels.json")
     52 }
     53 
     54 var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
     55 
     56 func getApiLevelsMap(config Config) map[string]int {
     57 	return config.Once(apiLevelsMapKey, func() interface{} {
     58 		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 			"Q":     29,
     75 		}
     76 		for i, codename := range config.PlatformVersionCombinedCodenames() {
     77 			apiLevelsMap[codename] = baseApiLevel + i
     78 		}
     79 
     80 		return apiLevelsMap
     81 	}).(map[string]int)
     82 }
     83 
     84 // Converts an API level string into its numeric form.
     85 // * Codenames are decoded.
     86 // * Numeric API levels are simply converted.
     87 // * "minimum" and "current" are not currently handled since the former is
     88 //   NDK specific and the latter has inconsistent meaning.
     89 func ApiStrToNum(ctx BaseContext, apiLevel string) (int, error) {
     90 	num, ok := getApiLevelsMap(ctx.Config())[apiLevel]
     91 	if ok {
     92 		return num, nil
     93 	}
     94 	return strconv.Atoi(apiLevel)
     95 }
     96 
     97 func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
     98 	apiLevelsMap := getApiLevelsMap(ctx.Config())
     99 	apiLevelsJson := GetApiLevelsJson(ctx)
    100 	createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
    101 }
    102