xref: /aosp_15_r20/build/soong/fsgen/util.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright (C) 2024 The Android Open Source Project
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 fsgen
16
17import (
18	"android/soong/android"
19	"fmt"
20	"strconv"
21	"strings"
22)
23
24// Returns the appropriate dpi for recovery common resources selection. Replicates the logic in
25// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2536;drc=a6af369e71ded123734523ea640b97b70a557cb9
26func getDpi(ctx android.LoadHookContext) string {
27	recoveryDensity := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.TargetScreenDensity
28	if len(recoveryDensity) == 0 {
29		aaptPreferredConfig := ctx.Config().ProductAAPTPreferredConfig()
30		if len(aaptPreferredConfig) > 0 {
31			recoveryDensity = aaptPreferredConfig
32		} else {
33			recoveryDensity = "mdpi"
34		}
35	}
36	if !android.InList(recoveryDensity, []string{"xxxhdpi", "xxhdpi", "xhdpi", "hdpi", "mdpi"}) {
37		recoveryDensity = strings.TrimSuffix(recoveryDensity, "dpi")
38		dpiInt, err := strconv.ParseInt(recoveryDensity, 10, 64)
39		if err != nil {
40			panic(fmt.Sprintf("Error in parsing recoveryDensity: %s", err.Error()))
41		}
42		if dpiInt >= 560 {
43			recoveryDensity = "xxxhdpi"
44		} else if dpiInt >= 400 {
45			recoveryDensity = "xxhdpi"
46		} else if dpiInt >= 280 {
47			recoveryDensity = "xhdpi"
48		} else if dpiInt >= 200 {
49			recoveryDensity = "hdpi"
50		} else {
51			recoveryDensity = "mdpi"
52		}
53	}
54
55	if p := android.ExistentPathForSource(ctx, fmt.Sprintf("bootable/recovery/res-%s", recoveryDensity)); !p.Valid() {
56		recoveryDensity = "xhdpi"
57	}
58
59	return recoveryDensity
60}
61