blob: 222564c7f9e517f9b52390b61ad4d16322bcabdc [file] [log] [blame]
bigbiff6e0ca7d2021-02-06 19:15:16 -05001package twrp
2
3import (
4 "android/soong/android"
5 "android/soong/cc"
6 "fmt"
Mohd Farazf202c032022-06-05 13:31:47 +02007 "io/ioutil"
bigbiff6e0ca7d2021-02-06 19:15:16 -05008 "os"
9 "path"
10 "strconv"
11 "strings"
12)
13
14func printThemeWarning(theme string) {
15 if theme == "" {
16 theme = "not set"
17 }
18 themeWarning := "***************************************************************************\n"
19 themeWarning += "Could not find ui.xml for TW_THEME: "
20 themeWarning += theme
21 themeWarning += "\nSet TARGET_SCREEN_WIDTH and TARGET_SCREEN_HEIGHT to automatically select\n"
22 themeWarning += "an appropriate theme, or set TW_THEME to one of the following:\n"
23 themeWarning += "landscape_hdpi landscape_mdpi portrait_hdpi portrait_mdpi watch_mdpi\n"
24 themeWarning += "****************************************************************************\n"
25 themeWarning += "(theme selection failed; exiting)\n"
26
27 fmt.Printf(themeWarning)
28}
29
30func printCustomThemeWarning(theme string, location string) {
31 customThemeWarning := "****************************************************************************\n"
32 customThemeWarning += "Could not find ui.xml for TW_CUSTOM_THEME: "
33 customThemeWarning += theme + "\n"
34 customThemeWarning += "Expected to find custom theme's ui.xml at: "
35 customThemeWarning += location
36 customThemeWarning += "Please fix this or set TW_THEME to one of the following:\n"
37 customThemeWarning += "landscape_hdpi landscape_mdpi portrait_hdpi portrait_mdpi watch_mdpi\n"
38 customThemeWarning += "****************************************************************************\n"
39 customThemeWarning += "(theme selection failed; exiting)\n"
40 fmt.Printf(customThemeWarning)
41}
42
43func copyThemeResources(ctx android.BaseContext, dirs []string, files []string) {
44 outDir := ctx.Config().Getenv("OUT")
45 twRes := outDir + "/recovery/root/twres/"
Captain Throwbackb7a3c6f2022-02-18 12:55:42 -050046 os.MkdirAll(twRes, os.ModePerm)
bigbiff6e0ca7d2021-02-06 19:15:16 -050047 recoveryDir := getRecoveryAbsDir(ctx)
48 theme := determineTheme(ctx)
49 for idx, dir := range dirs {
50 _ = idx
51 dirToCopy := ""
52 destDir := twRes + path.Base(dir)
53 baseDir := path.Base(dir)
54 if baseDir == theme {
55 destDir = twRes
56 dirToCopy = recoveryDir + dir
57 } else {
58 dirToCopy = recoveryDir + dir
59 }
60 copyDir(dirToCopy, destDir)
61 }
62 for idx, file := range files {
63 _ = idx
64 fileToCopy := recoveryDir + file
65 fileDest := twRes + path.Base(file)
66 copyFile(fileToCopy, fileDest)
67 }
Mohd Farazf202c032022-06-05 13:31:47 +020068 data, err := ioutil.ReadFile(recoveryDir + "variables.h")
69 if err != nil {
70 fmt.Println(err)
71 return
72 }
73 version := "0"
74 for _, line := range strings.Split(string(data), "\n") {
75 if strings.Contains(line, "TW_THEME_VERSION") {
76 version = strings.Split(line, " ")[2]
77 }
78 }
79 _files := [2]string{"splash.xml", "ui.xml"}
80 for _, i := range _files {
81 data, err = ioutil.ReadFile(twRes + i)
82 if err != nil {
83 fmt.Println(err)
84 return
85 }
86 newFile := strings.Replace(string(data), "{themeversion}", version, -1)
87 err = ioutil.WriteFile(twRes + i, []byte(newFile), 0)
88 if err != nil {
89 fmt.Println(err)
90 return
91 }
92 }
bigbiff6e0ca7d2021-02-06 19:15:16 -050093}
94
95func copyCustomTheme(ctx android.BaseContext, customTheme string) {
96 outDir := ctx.Config().Getenv("OUT")
97 twRes := outDir + "/recovery/root/twres/"
Captain Throwbackb7a3c6f2022-02-18 12:55:42 -050098 os.MkdirAll(twRes, os.ModePerm)
bigbiff6e0ca7d2021-02-06 19:15:16 -050099 fileDest := twRes + path.Base(customTheme)
100 fileToCopy := fmt.Sprintf("%s%s", getBuildAbsDir(ctx), customTheme)
101 copyFile(fileToCopy, fileDest)
102}
103
104func determineTheme(ctx android.BaseContext) string {
105 guiWidth := 0
106 guiHeight := 0
107 if getMakeVars(ctx, "TW_CUSTOM_THEME") == "" {
108 if getMakeVars(ctx, "TW_THEME") == "" {
109 if getMakeVars(ctx, "DEVICE_RESOLUTION") == "" {
110 width, err := strconv.Atoi(getMakeVars(ctx, "TARGET_SCREEN_WIDTH"))
111 if err == nil {
112 guiWidth = width
113 }
114 height, err := strconv.Atoi(getMakeVars(ctx, "TARGET_SCREEN_HEIGHT"))
115 if err == nil {
116 guiHeight = height
117 }
118 } else {
119 deviceRes := getMakeVars(ctx, "DEVICE_RESOLUTION")
120 width, err := strconv.Atoi(strings.Split(deviceRes, "x")[0])
121 if err == nil {
122 guiWidth = width
123 }
124 height, err := strconv.Atoi(strings.Split(deviceRes, "x")[1])
125 if err == nil {
126 guiHeight = height
127 }
128 }
129 }
130 if guiWidth > 100 {
131 if guiHeight > 100 {
132 if guiWidth > guiHeight {
133 if guiWidth > 1280 {
134 return "landscape_hdpi"
135 } else {
136 return "landscape_mdpi"
137 }
138 } else if guiWidth < guiHeight {
139 if guiWidth > 720 {
140 return "portrait_hdpi"
141 } else {
142 return "portrait_mdpi"
143 }
144 } else if guiWidth == guiHeight {
145 return "watch_mdpi"
146 }
147 }
148 }
149 }
150
151 return getMakeVars(ctx, "TW_THEME")
152}
153
154func copyTheme(ctx android.BaseContext) bool {
155 var directories []string
156 var files []string
157 var customThemeLoc string
158 localPath := ctx.ModuleDir()
159 directories = append(directories, "gui/theme/common/fonts/")
160 directories = append(directories, "gui/theme/common/languages/")
161 if getMakeVars(ctx, "TW_EXTRA_LANGUAGES") == "true" {
162 directories = append(directories, "gui/theme/extra-languages/fonts/")
163 directories = append(directories, "gui/theme/extra-languages/languages/")
164 }
165 var theme = determineTheme(ctx)
166 directories = append(directories, "gui/theme/"+theme)
167 themeXML := fmt.Sprintf("gui/theme/common/%s.xml", strings.Split(theme, "_")[0])
168 files = append(files, themeXML)
169 if getMakeVars(ctx, "TW_CUSTOM_THEME") == "" {
170 defaultTheme := fmt.Sprintf("%s/theme/%s/ui.xml", localPath, theme)
171 if android.ExistentPathForSource(ctx, defaultTheme).Valid() {
172 fullDefaultThemePath := fmt.Sprintf("gui/theme/%s/ui.xml", theme)
173 files = append(files, fullDefaultThemePath)
174 } else {
175 printThemeWarning(theme)
176 return false
177 }
178 } else {
179 customThemeLoc = getMakeVars(ctx, "TW_CUSTOM_THEME")
180 if android.ExistentPathForSource(ctx, customThemeLoc).Valid() {
181 } else {
182 printCustomThemeWarning(customThemeLoc, getMakeVars(ctx, "TW_CUSTOM_THEME"))
183 return false
184 }
185 }
186 copyThemeResources(ctx, directories, files)
187 if customThemeLoc != "" {
188 copyCustomTheme(ctx, customThemeLoc)
189 }
190 return true
191}
192
193func globalFlags(ctx android.BaseContext) []string {
194 var cflags []string
195
196 if getMakeVars(ctx, "TW_DELAY_TOUCH_INIT_MS") != "" {
197 cflags = append(cflags, "-DTW_DELAY_TOUCH_INIT_MS="+getMakeVars(ctx, "TW_DELAY_TOUCH_INIT_MS"))
198 }
199
200 if getMakeVars(ctx, "TW_EVENT_LOGGING") == "true" {
201 cflags = append(cflags, "-D_EVENT_LOGGING")
202 }
203
204 if getMakeVars(ctx, "TW_USE_KEY_CODE_TOUCH_SYNC") != "" {
205 cflags = append(cflags, "DTW_USE_KEY_CODE_TOUCH_SYNC="+getMakeVars(ctx, "TW_USE_KEY_CODE_TOUCH_SYNC"))
206 }
207
HemanthJabalpuric6512442021-07-05 11:26:44 +0000208 if getMakeVars(ctx, "TW_SCREEN_BLANK_ON_BOOT") != "" {
bigbiff6e0ca7d2021-02-06 19:15:16 -0500209 cflags = append(cflags, "-DTW_SCREEN_BLANK_ON_BOOT")
210 }
211
212 if getMakeVars(ctx, "TW_OZIP_DECRYPT_KEY") != "" {
213 cflags = append(cflags, "-DTW_OZIP_DECRYPT_KEY=\""+getMakeVars(ctx, "TW_OZIP_DECRYPT_KEY")+"\"")
214 } else {
215 cflags = append(cflags, "-DTW_OZIP_DECRYPT_KEY=0")
216 }
217
218 if getMakeVars(ctx, "TW_NO_SCREEN_BLANK") != "" {
219 cflags = append(cflags, "-DTW_NO_SCREEN_BLANK")
220 }
221
222 if getMakeVars(ctx, "TW_NO_SCREEN_TIMEOUT") != "" {
223 cflags = append(cflags, "-DTW_NO_SCREEN_TIMEOUT")
224 }
225
226 if getMakeVars(ctx, "TW_OEM_BUILD") != "" {
227 cflags = append(cflags, "-DTW_OEM_BUILD")
228 }
229
230 if getMakeVars(ctx, "TW_X_OFFSET") != "" {
231 cflags = append(cflags, "-DTW_X_OFFSET="+getMakeVars(ctx, "TW_X_OFFSET"))
232 }
233
234 if getMakeVars(ctx, "TW_Y_OFFSET") != "" {
235 cflags = append(cflags, "-DTW_Y_OFFSET="+getMakeVars(ctx, "TW_Y_OFFSET"))
236 }
237
238 if getMakeVars(ctx, "TW_W_OFFSET") != "" {
239 cflags = append(cflags, "-DTW_W_OFFSET="+getMakeVars(ctx, "TW_W_OFFSET"))
240 }
241
242 if getMakeVars(ctx, "TW_H_OFFSET") != "" {
243 cflags = append(cflags, "-DTW_H_OFFSET="+getMakeVars(ctx, "TW_H_OFFSET"))
244 }
245
246 if getMakeVars(ctx, "TW_ROUND_SCREEN") == "true" {
247 cflags = append(cflags, "-DTW_ROUND_SCREEN")
248 }
249
250 if getMakeVars(ctx, "TW_EXCLUDE_NANO") == "true" {
251 cflags = append(cflags, "-DTW_EXCLUDE_NANO")
252 }
253
254 if getMakeVars(ctx, "AB_OTA_UPDATER") == "true" {
255 cflags = append(cflags, "-DAB_OTA_UPDATER=1")
256 }
257
bigbiff6e0ca7d2021-02-06 19:15:16 -0500258 return cflags
259}
260
261func globalSrcs(ctx android.BaseContext) []string {
262 var srcs []string
263
264 if getMakeVars(ctx, "TWRP_CUSTOM_KEYBOARD") != "" {
265 srcs = append(srcs, getMakeVars(ctx, "TWRP_CUSTOM_KEYBOARD"))
266 } else {
267 srcs = append(srcs, "hardwarekeyboard.cpp")
268 }
269 return srcs
270}
271
272func globalIncludes(ctx android.BaseContext) []string {
273 var includes []string
274
275 if getMakeVars(ctx, "TW_INCLUDE_CRYPTO") != "" {
276 includes = append(includes, "bootable/recovery/crypto/fscrypt")
277 }
278 return includes
279}
280
281func libGuiDefaults(ctx android.LoadHookContext) {
282 type props struct {
283 Target struct {
284 Android struct {
285 Cflags []string
286 Enabled *bool
287 }
288 }
289 Cflags []string
290 Srcs []string
291 Include_dirs []string
292 }
293
294 p := &props{}
295 p.Cflags = globalFlags(ctx)
296 s := globalSrcs(ctx)
297 p.Srcs = s
298 i := globalIncludes(ctx)
299 p.Include_dirs = i
300 ctx.AppendProperties(p)
301 if copyTheme(ctx) == false {
302 os.Exit(-1)
303 }
304}
305
306func init() {
307 android.RegisterModuleType("libguitwrp_defaults", libGuiDefaultsFactory)
308}
309
310func libGuiDefaultsFactory() android.Module {
311 module := cc.DefaultsFactory()
312 android.AddLoadHook(module, libGuiDefaults)
313
314 return module
315}