11package com .stringcare .library ;
22
3- /*
4- * Credits to Narvelan:
5- * https://github.com/StringCare/AndroidLibrary/issues/34
6- */
7-
83import android .content .Context ;
94import android .support .v7 .widget .AppCompatTextView ;
105import android .text .Html ;
116import android .util .AttributeSet ;
127
13- import java .util .Formatter ;
8+ /*
9+ * Credits to Narvelan:
10+ * https://github.com/StringCare/AndroidLibrary/issues/34
11+ */
1412
1513public class SCTextView extends AppCompatTextView {
1614
1715 private String text ;
1816 private Boolean isHTML ;
1917 private Boolean androidTreatment ;
20- private Boolean visible ;
18+ private Boolean revealed ;
2119
2220 public SCTextView (Context context ) {
2321 super (context );
2422 isHTML = null ;
25- visible = null ;
23+ revealed = null ;
2624 androidTreatment = null ;
2725 }
2826
2927 public SCTextView (Context context , AttributeSet attrs ) {
3028 super (context , attrs );
3129 isHTML = null ;
32- visible = null ;
30+ revealed = null ;
3331 androidTreatment = null ;
3432 loadText (attrs );
3533 }
3634
3735 public SCTextView (Context context , AttributeSet attrs , int defStyleAttr ) {
3836 super (context , attrs , defStyleAttr );
3937 isHTML = null ;
40- visible = null ;
38+ revealed = null ;
4139 androidTreatment = null ;
4240 loadText (attrs );
4341 }
4442
4543 /**
46- * Defines initial vars
44+ * Sets the initial parameters
4745 *
4846 * @param attrs {AttributeSet}
4947 */
5048 private void loadText (final AttributeSet attrs ) {
51- text = attrs .getAttributeValue ("http://schemas.android.com/apk/res/android" , "text" );
49+ String mFalse = "false" ;
50+
51+ String mResourceAndroidSchema = "http://schemas.android.com/apk/res/android" ;
52+ String textName = "text" ;
53+
54+ String mResourceSchema = "http://schemas.android.com/apk/res-auto" ;
55+ String htmlName = "htmlSupport" ;
56+ String revealValue = "reveal" ;
57+ String androidTreatmentName = "androidTreatment" ;
58+
59+ text = attrs .getAttributeValue (mResourceAndroidSchema , textName );
5260 if (isHTML == null ) {
53- isHTML = !"false" .equalsIgnoreCase (attrs .getAttributeValue ("http://schemas.android.com/apk/res-auto" , "html" ));
61+ isHTML = !mFalse .equalsIgnoreCase (attrs .getAttributeValue (mResourceSchema , htmlName ));
5462 }
55- if (visible == null ) {
56- visible = !"false" .equalsIgnoreCase (attrs .getAttributeValue ("http://schemas.android.com/apk/res-auto" , "visible" ));
63+ if (revealed == null ) {
64+ revealed = !mFalse .equalsIgnoreCase (attrs .getAttributeValue (mResourceSchema , revealValue ));
5765 }
5866 if (androidTreatment == null ) {
59- androidTreatment = !"false" .equalsIgnoreCase (attrs .getAttributeValue ("http://schemas.android.com/apk/res-auto" , "androidTreatment" ));
67+ androidTreatment = !mFalse .equalsIgnoreCase (attrs .getAttributeValue (mResourceSchema , androidTreatmentName ));
6068 }
6169
6270 reloadText ();
@@ -68,16 +76,16 @@ private void loadText(final AttributeSet attrs) {
6876 private void reloadText () {
6977 if (text != null ) {
7078 try {
71- final Integer val = Integer .parseInt (text .substring (1 ));
72- if (!visible ) {
79+ final int val = Integer .parseInt (text .substring (1 ));
80+ if (!isRevealingValue () ) {
7381 setText (getContext ().getString (val ));
7482 return ;
7583 }
7684 SC .onContextReady (new ContextListener () {
7785 @ Override
7886 public void contextReady () {
79- String value = SC .reveal (val , androidTreatment );
80- if (isHTML ) {
87+ String value = SC .reveal (val , usesAndroidTreatment () );
88+ if (isHtmlEnabled () ) {
8189 setText (Html .fromHtml (value ));
8290 } else {
8391 setText (value );
@@ -91,12 +99,12 @@ public void contextReady() {
9199 }
92100
93101 /**
94- * Enables de-obfuscation before print the value
102+ * Reveals the value before print it
95103 *
96- * @param visible {true|false}
104+ * @param revealed {true|false}
97105 */
98- public void visible (boolean visible ) {
99- this .visible = visible ;
106+ public void setRevealed (boolean revealed ) {
107+ this .revealed = revealed ;
100108 reloadText ();
101109 }
102110
@@ -105,40 +113,46 @@ public void visible(boolean visible) {
105113 *
106114 * @param enabled {true|false}
107115 */
108- public void htmlEnabled (boolean enabled ) {
116+ public void setHtmlSupport (boolean enabled ) {
109117 isHTML = enabled ;
110118 reloadText ();
111119 }
112120
113121 /**
114- * Returns true if is the value must be print as HTML or plain text
122+ * Enables the Android treatment
123+ *
124+ * @param enabled {true|false}
125+ */
126+ public void setAndroidTreatment (boolean enabled ) {
127+ androidTreatment = enabled ;
128+ reloadText ();
129+ }
130+
131+ /**
132+ * Returns true if is the value must be print as HTML
115133 *
116134 * @return Boolean
117135 */
118136 public boolean isHtmlEnabled () {
119- return isHTML ;
137+ return Boolean . TRUE . equals ( isHTML ) ;
120138 }
121139
122140 /**
123- * Returns true if is de-obfuscating the value before print it
141+ * Returns true if the value must be treated as the Android system does
124142 *
125143 * @return Boolean
126144 */
127- public boolean isVisible () {
128- return visible ;
145+ public boolean usesAndroidTreatment () {
146+ return Boolean . TRUE . equals ( androidTreatment ) ;
129147 }
130148
131- public String escapeUnicode (String input ) {
132- StringBuilder b = new StringBuilder (input .length ());
133- Formatter f = new Formatter (b );
134- for (char c : input .toCharArray ()) {
135- if (c < 128 ) {
136- b .append (c );
137- } else {
138- f .format ("\\ u%04x" , (int ) c );
139- }
140- }
141- return b .toString ();
149+ /**
150+ * Returns true if the value should be setRevealed before print it
151+ *
152+ * @return Boolean
153+ */
154+ public boolean isRevealingValue () {
155+ return Boolean .TRUE .equals (revealed );
142156 }
143157
144158}
0 commit comments