@@ -2,7 +2,7 @@ package info.hannes.logcat.base
22
33import android.content.Context
44import android.content.res.ColorStateList
5- import android.content.res.TypedArray
5+ import android.content.res.Resources
66import android.graphics.Color
77import android.view.LayoutInflater
88import android.view.View
@@ -87,32 +87,54 @@ class LogListAdapter(private var completeLogs: MutableList<String>, filter: Stri
8787 * @param position
8888 */
8989 override fun onBindViewHolder (holder : LogViewHolder , position : Int ) {
90- holder.logContent.text = filterLogs[position].also {
91- if (it.contains(" ${LogBaseFragment .ERROR_LINE } " ) || it.startsWith(LogBaseFragment .ERROR_LINE )) {
92- holder.logContent.setTextColor(Color .RED )
93- } else if (it.contains(" ${LogBaseFragment .ASSERT_LINE } " ) || it.startsWith(LogBaseFragment .ASSERT_LINE )) {
94- holder.logContent.setTextColor(Color .RED )
95- } else if (it.contains(" ${LogBaseFragment .INFO_LINE } " ) || it.startsWith(LogBaseFragment .INFO_LINE )) {
96- holder.logContent.setTextColor(Color .BLACK )
97- } else if (it.contains(" ${LogBaseFragment .WARNING_LINE } " ) || it.startsWith(LogBaseFragment .WARNING_LINE )) {
98- holder.logContent.setTextColor(Color .MAGENTA )
99- } else if (it.contains(" ${LogBaseFragment .VERBOSE_LINE } " ) || it.startsWith(LogBaseFragment .VERBOSE_LINE )) {
100- holder.logContent.setTextColor(Color .GRAY )
101- } else {
102- holder.logContent.setTextColor(getColorAttr(holder.logContent.context, android.R .attr.textColorSecondary))
103- }
90+ val text = filterLogs[position]
91+ holder.logContent.let {
92+ it.text = text
93+
94+ when {
95+ text.contains(" ${LogBaseFragment .ERROR_LINE } " ) || text.startsWith(LogBaseFragment .ERROR_LINE ) -> {
96+ getAttrColorStateList(it.context, R .attr.colorErrorLine) ? : ColorStateList .valueOf(Color .RED )
97+ }
98+ text.contains(" ${LogBaseFragment .ASSERT_LINE } " ) || text.startsWith(LogBaseFragment .ASSERT_LINE ) -> {
99+ getAttrColorStateList(it.context, R .attr.colorAssertLine) ? : ColorStateList .valueOf(Color .RED )
100+ }
101+ text.contains(" ${LogBaseFragment .INFO_LINE } " ) || text.startsWith(LogBaseFragment .INFO_LINE ) -> {
102+ getAttrColorStateList(it.context, R .attr.colorInfoLine) ? : getAttrColorStateList(it.context, android.R .attr.textColorPrimary)
103+ }
104+ text.contains(" ${LogBaseFragment .WARNING_LINE } " ) || text.startsWith(LogBaseFragment .WARNING_LINE ) -> {
105+ getAttrColorStateList(it.context, R .attr.colorWarningLine) ? : ColorStateList .valueOf(Color .MAGENTA )
106+ }
107+ text.contains(" ${LogBaseFragment .VERBOSE_LINE } " ) || text.startsWith(LogBaseFragment .VERBOSE_LINE ) -> {
108+ getAttrColorStateList(it.context, R .attr.colorVerboseLine) ? : ColorStateList .valueOf(Color .GRAY )
109+ }
110+ else -> {
111+ getAttrColorStateList(it.context, R .attr.colorDebugLine) ? : getAttrColorStateList(it.context, android.R .attr.textColorSecondary)
112+ }
113+ }?.let { colors: ColorStateList -> it.setTextColor(colors) }
104114 }
105115 }
106116
107117 override fun getItemCount (): Int = filterLogs.size
108118
109119 companion object {
110- fun getColorAttr (context : Context , attr : Int ): ColorStateList ? {
111- val ta: TypedArray = context.obtainStyledAttributes(intArrayOf(attr))
112- return try {
113- ta.getColorStateList(0 )
114- } finally {
120+
121+ /* *
122+ * Retrieve the ColorStateList for the given attribute. The value may be either a single solid
123+ * color or a reference to a color or complex [android.content.res.ColorStateList]
124+ * description.
125+ *
126+ * @param context
127+ * @param attr
128+ * @return
129+ */
130+ fun getAttrColorStateList (context : Context , attr : Int ): ColorStateList ? {
131+ try {
132+ val ta = context.obtainStyledAttributes(intArrayOf(attr))
133+ val colorStateList = ta.getColorStateList(0 )
115134 ta.recycle()
135+ return colorStateList
136+ } catch (e: Resources .NotFoundException ) {
137+ return null
116138 }
117139 }
118140 }
0 commit comments