Skip to content

Commit ee6e565

Browse files
ADFA-1280 Externalize html to strings.xml (#903)
* Externalize html to strings.xml from PluginTooltipManager and ToolTipManager * Refactor color for R.string.tooltip_links_html_template to match authoirtative light/dark definition * Removed spurious usage of template variable * Move colors strings.xml to colors.xml
1 parent a8ae20b commit ee6e565

File tree

5 files changed

+143
-66
lines changed

5 files changed

+143
-66
lines changed

idetooltips/src/main/java/com/itsaky/androidide/idetooltips/ToolTipManager.kt

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import android.widget.PopupWindow
2424
import android.widget.TextView
2525
import androidx.appcompat.app.AlertDialog
2626
import androidx.core.content.ContextCompat.getColor
27-
import com.google.android.material.color.MaterialColors
2827
import com.itsaky.androidide.activities.editor.HelpActivity
2928
import com.itsaky.androidide.utils.Environment
29+
import com.itsaky.androidide.utils.isSystemInDarkMode
3030
import com.itsaky.androidide.utils.FeedbackManager
31+
import com.itsaky.androidide.resources.R as ResR
3132
import kotlinx.coroutines.CoroutineScope
3233
import kotlinx.coroutines.Dispatchers
3334
import kotlinx.coroutines.launch
@@ -267,15 +268,23 @@ object TooltipManager {
267268
val seeMore = popupView.findViewById<TextView>(R.id.see_more)
268269
val webView = popupView.findViewById<WebView>(R.id.webview)
269270

270-
val textColor = MaterialColors.getColor(
271-
context,
272-
com.google.android.material.R.attr.colorOnSurface,
273-
"Color attribute not found in theme"
274-
)
275-
276-
// TODO: The color string below should be externalized so our documentation team can control them, for example with CSS. --DS, 30-Jul-2025
277-
fun Int.toHexColor(): String = String.format("#%06X", 0xFFFFFF and this)
278-
val hexColor = textColor.toHexColor()
271+
val isDarkMode = context.isSystemInDarkMode()
272+
val bodyColorHex =
273+
"#%08X".format(
274+
getColor(
275+
context,
276+
if (isDarkMode) ResR.color.tooltip_text_color_dark
277+
else ResR.color.tooltip_text_color_light
278+
)
279+
)
280+
val linkColorHex =
281+
"#%08X".format(
282+
getColor(
283+
context,
284+
if (isDarkMode) ResR.color.tooltip_link_color_dark
285+
else ResR.color.tooltip_link_color_light
286+
)
287+
)
279288

280289
val tooltipHtmlContent = when (level) {
281290
0 -> {
@@ -284,11 +293,12 @@ object TooltipManager {
284293
1 -> {
285294
val detailContent = tooltipItem.detail.ifBlank { "" }
286295
if (tooltipItem.buttons.isNotEmpty()) {
287-
val linksHtml = tooltipItem.buttons.joinToString("<br>") { (label, url) ->
288-
context.getString(R.string.tooltip_links_html_template, url, label)
296+
val buttonsSeparator = context.getString(R.string.tooltip_buttons_separator)
297+
val linksHtml = tooltipItem.buttons.joinToString(buttonsSeparator) { (label, url) ->
298+
context.getString(R.string.tooltip_links_html_template, url, linkColorHex, label)
289299
}
290300
if (detailContent.isNotBlank()) {
291-
"$detailContent<br><br>$linksHtml"
301+
context.getString(R.string.tooltip_detail_links_template, detailContent, linksHtml)
292302
} else {
293303
linksHtml
294304
}
@@ -303,7 +313,7 @@ object TooltipManager {
303313
Log.d(TAG, "Level: $level, Content: ${tooltipHtmlContent.take(100)}...")
304314

305315
val styledHtml =
306-
context.getString(R.string.tooltip_html_template, hexColor, tooltipHtmlContent)
316+
context.getString(R.string.tooltip_html_template, bodyColorHex, tooltipHtmlContent, linkColorHex)
307317

308318
webView.webViewClient = object : WebViewClient() {
309319
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
@@ -376,19 +386,23 @@ object TooltipManager {
376386
) {
377387
popupWindow.dismiss()
378388

379-
val metadata = """
380-
<b>Version</b> <small>'${tooltip.lastChange}'</small><br/>
381-
<b>Row:</b> ${tooltip.rowId}<br/>
382-
<b>ID:</b> ${tooltip.id}<br/>
383-
<b>Category:</b> '${tooltip.category}'<br/>
384-
<b>Tag:</b> '${tooltip.tag}'<br/>
385-
<b>Raw Summary:</b> '${Html.escapeHtml(tooltip.summary)}'<br/>
386-
<b>Raw Detail:</b> '${Html.escapeHtml(tooltip.detail)}'<br/>
387-
<b>Buttons:</b> ${tooltip.buttons.joinToString { "'${it.first}${it.second}'" }}<br/>
388-
""".trimIndent()
389+
val buttonsFormatted = tooltip.buttons.joinToString {
390+
context.getString(R.string.tooltip_debug_button_item_template, it.first, it.second)
391+
}
392+
val metadata = context.getString(
393+
R.string.tooltip_debug_metadata_html,
394+
tooltip.lastChange,
395+
tooltip.rowId,
396+
tooltip.id,
397+
tooltip.category,
398+
tooltip.tag,
399+
Html.escapeHtml(tooltip.summary),
400+
Html.escapeHtml(tooltip.detail),
401+
buttonsFormatted
402+
)
389403

390404
val builder = AlertDialog.Builder(context)
391-
.setTitle("Tooltip Debug Info")
405+
.setTitle(context.getString(R.string.tooltip_debug_dialog_title))
392406
.setMessage(
393407
Html.fromHtml(
394408
metadata,

plugin-manager/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies {
3838
implementation(projects.logger)
3939
implementation(projects.lookup)
4040
implementation(projects.preferences)
41+
implementation(projects.resources)
4142
implementation(projects.idetooltips)
4243
implementation(projects.shared)
4344
implementation(projects.subprojects.projects)

plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/tooltip/PluginTooltipManager.kt

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import androidx.core.content.ContextCompat.getColor
2121
import androidx.core.graphics.drawable.toDrawable
2222
import com.google.android.material.color.MaterialColors
2323
import com.itsaky.androidide.idetooltips.IDETooltipItem
24+
import com.itsaky.androidide.utils.isSystemInDarkMode
2425
import com.itsaky.androidide.idetooltips.R
26+
import com.itsaky.androidide.resources.R as ResR
2527
import kotlinx.coroutines.CoroutineScope
2628
import kotlinx.coroutines.Dispatchers
2729
import kotlinx.coroutines.launch
@@ -59,7 +61,6 @@ object PluginTooltipManager {
5961
}
6062

6163

62-
private fun Int.toHexColor(): String = String.format("#%06X", 0xFFFFFF and this)
6364

6465
/**
6566
* Retrieve a tooltip from the plugin documentation database.
@@ -245,24 +246,35 @@ object PluginTooltipManager {
245246
val seeMore = popupView.findViewById<TextView>(R.id.see_more)
246247
val webView = popupView.findViewById<WebView>(R.id.webview)
247248

248-
val textColor = MaterialColors.getColor(
249-
context,
250-
com.google.android.material.R.attr.colorOnSurface,
251-
"Color attribute not found in theme"
252-
)
253-
254-
val hexColor = textColor.toHexColor()
249+
val isDarkMode = context.isSystemInDarkMode()
250+
val bodyColorHex =
251+
"#%08X".format(
252+
getColor(
253+
context,
254+
if (isDarkMode) ResR.color.tooltip_text_color_dark
255+
else ResR.color.tooltip_text_color_light
256+
)
257+
)
258+
val linkColorHex =
259+
"#%08X".format(
260+
getColor(
261+
context,
262+
if (isDarkMode) ResR.color.tooltip_link_color_dark
263+
else ResR.color.tooltip_link_color_light
264+
)
265+
)
255266

256267
val tooltipHtmlContent = when (level) {
257268
0 -> tooltipItem.summary
258269
1 -> {
259270
val detailContent = tooltipItem.detail.ifBlank { "" }
260271
if (tooltipItem.buttons.isNotEmpty()) {
261-
val linksHtml = tooltipItem.buttons.joinToString("<br>") { (label, url) ->
262-
context.getString(R.string.tooltip_links_html_template, url, label)
272+
val buttonsSeparator = context.getString(R.string.tooltip_buttons_separator)
273+
val linksHtml = tooltipItem.buttons.joinToString(buttonsSeparator) { (label, url) ->
274+
context.getString(R.string.tooltip_links_html_template, url, linkColorHex, label)
263275
}
264276
if (detailContent.isNotBlank()) {
265-
"$detailContent<br><br>$linksHtml"
277+
context.getString(R.string.tooltip_detail_links_template, detailContent, linksHtml)
266278
} else {
267279
linksHtml
268280
}
@@ -277,7 +289,7 @@ object PluginTooltipManager {
277289
Log.d(TAG, "Level: $level, Content: ${tooltipHtmlContent.take(100)}...")
278290

279291
val styledHtml =
280-
context.getString(R.string.tooltip_html_template, hexColor, tooltipHtmlContent)
292+
context.getString(R.string.tooltip_html_template, bodyColorHex, tooltipHtmlContent, linkColorHex)
281293

282294
webView.webViewClient = object : WebViewClient() {
283295
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
@@ -387,36 +399,46 @@ object PluginTooltipManager {
387399
// Get the WebView to display debug info
388400
val webView = popupView.findViewById<WebView>(R.id.webview)
389401

390-
val textColor = MaterialColors.getColor(
391-
context,
392-
com.google.android.material.R.attr.colorOnSurface,
393-
"Color attribute not found in theme"
394-
)
402+
val isDarkMode = context.isSystemInDarkMode()
403+
val bodyColorHex =
404+
"#%08X".format(
405+
getColor(
406+
context,
407+
if (isDarkMode) ResR.color.tooltip_text_color_dark
408+
else ResR.color.tooltip_text_color_light
409+
)
410+
)
411+
val linkColorHex =
412+
"#%08X".format(
413+
getColor(
414+
context,
415+
if (isDarkMode) ResR.color.tooltip_link_color_dark
416+
else ResR.color.tooltip_link_color_light
417+
)
418+
)
395419

396-
val hexColor = textColor.toHexColor()
397-
398-
val debugHtml = """
399-
<h3>Plugin Tooltip Debug Info</h3>
400-
<b>Version:</b> <small>${tooltip.lastChange}</small><br/>
401-
<b>Row:</b> ${tooltip.rowId}<br/>
402-
<b>ID:</b> ${tooltip.id}<br/>
403-
<b>Category:</b> ${tooltip.category}<br/>
404-
<b>Tag:</b> ${tooltip.tag}<br/>
405-
<br/>
406-
<b>Raw Summary:</b><br/>
407-
<small>${Html.escapeHtml(tooltip.summary)}</small><br/>
408-
<br/>
409-
<b>Raw Detail:</b><br/>
410-
<small>${Html.escapeHtml(tooltip.detail)}</small><br/>
411-
<br/>
412-
<b>Buttons:</b> ${
413-
if (tooltip.buttons.isEmpty()) "None" else tooltip.buttons.joinToString(
414-
"<br/>"
415-
) { "${it.first}" }
420+
val buttonsFormatted = if (tooltip.buttons.isEmpty()) {
421+
"None"
422+
} else {
423+
val separator = context.getString(R.string.tooltip_debug_button_separator)
424+
tooltip.buttons.joinToString(separator) {
425+
context.getString(R.string.tooltip_debug_button_item_template_simple, it.first)
426+
}
416427
}
417-
""".trimIndent()
418428

419-
val styledHtml = context.getString(R.string.tooltip_html_template, hexColor, debugHtml)
429+
val debugHtml = context.getString(
430+
R.string.tooltip_debug_plugin_html,
431+
tooltip.lastChange,
432+
tooltip.rowId,
433+
tooltip.id,
434+
tooltip.category,
435+
tooltip.tag,
436+
Html.escapeHtml(tooltip.summary),
437+
Html.escapeHtml(tooltip.detail),
438+
buttonsFormatted
439+
)
440+
441+
val styledHtml = context.getString(R.string.tooltip_html_template, bodyColorHex, debugHtml, linkColorHex)
420442

421443
webView.settings.javaScriptEnabled = false // No need for JS in debug view
422444
webView.setBackgroundColor(Color.TRANSPARENT)

resources/src/main/res/values/colors.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323
<color name="splash_bg">@android:color/white</color>
2424
<color name="splash_icon_tint">@android:color/black</color>
2525
<color name="primary_blue">#233490</color>
26+
27+
<!-- Tooltip (HTML content text and link colors) -->
28+
<color name="tooltip_text_color_light">#000000</color>
29+
<color name="tooltip_link_color_light">#233490</color>
30+
<color name="tooltip_text_color_dark">#E4E2E6</color>
31+
<color name="tooltip_link_color_dark">#00BE00</color>
2632
</resources>

resources/src/main/res/values/strings.xml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@
796796
color: %1$s;
797797
}
798798
a{
799-
color: #233490;
799+
color: %3$s;
800800
text-decoration: underline;
801801
}
802802
</style>
@@ -808,6 +808,40 @@
808808
</html>
809809
]]></string>
810810

811+
<!-- Tooltip HTML Components -->
812+
<string name="tooltip_buttons_separator"><![CDATA[<br>]]></string>
813+
<string name="tooltip_detail_links_template"><![CDATA[%1$s<br><br>%2$s]]></string>
814+
815+
<!-- Tooltip Debug Dialog -->
816+
<string name="tooltip_debug_dialog_title">Tooltip Debug Info</string>
817+
<string name="tooltip_debug_metadata_html"><![CDATA[<b>Version</b> <small>%1$s</small><br/>
818+
<b>Row:</b> %2$d<br/>
819+
<b>ID:</b> %3$d<br/>
820+
<b>Category:</b> %4$s<br/>
821+
<b>Tag:</b> %5$s<br/>
822+
<b>Raw Summary:</b> %6$s<br/>
823+
<b>Raw Detail:</b> %7$s<br/>
824+
<b>Buttons:</b> %8$s<br/>]]></string>
825+
<string name="tooltip_debug_plugin_html"><![CDATA[<h3>Plugin Tooltip Debug Info</h3>
826+
<b>Version:</b> <small>%1$s</small><br/>
827+
<b>Row:</b> %2$d<br/>
828+
<b>ID:</b> %3$d<br/>
829+
<b>Category:</b> %4$s<br/>
830+
<b>Tag:</b> %5$s<br/>
831+
<br/>
832+
<b>Raw Summary:</b><br/>
833+
<small>%6$s</small><br/>
834+
<br/>
835+
<b>Raw Detail:</b><br/>
836+
<small>%7$s</small><br/>
837+
<br/>
838+
<b>Buttons:</b> %8$s]]></string>
839+
840+
<!-- Tooltip Debug Button Formatting -->
841+
<string name="tooltip_debug_button_separator"><![CDATA[<br/>]]></string>
842+
<string name="tooltip_debug_button_item_template_simple">• %1$s</string>
843+
<string name="tooltip_debug_button_item_template">\'%1$s \u2192 %2$s\'</string>
844+
811845
<string name="info_icon_content_description">Info icon</string>
812846
<string name="feedback_button_description">Send feedback about this tooltip</string>
813847
<string name="unknown_error">"An unknown error occurred."</string>
@@ -939,7 +973,7 @@
939973
<string name="debugger_setup_description_footer"><![CDATA[Some devices (including older Xiaomi devices) also require giving Code on the Go the “Unrestricted battery usage permission”. <a href="http://localhost:6174/i/debug-enable.html">See more information including troubleshooting tips</a>.]]></string>
940974

941975
<!-- Tooltip Links HTML Template -->
942-
<string name="tooltip_links_html_template"><![CDATA[<a href="%1$s" style="color:#233490;text-decoration:underline;">%2$s</a>]]></string>
976+
<string name="tooltip_links_html_template"><![CDATA[<a href="%1$s" style="color:%2$s;text-decoration:underline;">%3$s</a>]]></string>
943977
<string name="title_alert">Send feedback</string>
944978

945979
<!-- Strings from BaseApplication.java -->

0 commit comments

Comments
 (0)