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