From 3e0cfa5179ac4ad11c8fc2c4e9ffea712ae78c99 Mon Sep 17 00:00:00 2001
From: Sergey Volkov <s.volkov@netris.ru>
Date: Tue, 24 Sep 2019 17:28:16 +0300
Subject: [PATCH] feat(android): add passwordToggle properties to textView

passwordVisibilityToggleColor
passwordVisibilityToggleEnabled
passwordVisibilityToggleTintColor
---
 .../ti/modules/titanium/ui/TextFieldProxy.java     |  4 ++
 .../ti/modules/titanium/ui/widget/TiUIText.java    | 43 ++++++++++++++++++++++
 .../src/java/org/appcelerator/titanium/TiC.java    | 15 ++++++++
 apidoc/Titanium/UI/TextField.yml                   | 31 ++++++++++++++++
 4 files changed, 93 insertions(+)

diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/TextFieldProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/TextFieldProxy.java
index 11a1096..f8aa7be 100644
--- a/android/modules/ui/src/java/ti/modules/titanium/ui/TextFieldProxy.java
+++ b/android/modules/ui/src/java/ti/modules/titanium/ui/TextFieldProxy.java
@@ -40,6 +40,9 @@ import android.app.Activity;
 		TiC.PROPERTY_KEYBOARD_TYPE,
 		TiC.PROPERTY_MAX_LENGTH,
 		TiC.PROPERTY_PASSWORD_MASK,
+		TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_COLOR,
+		TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_ENABLED,
+		TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_TINT_COLOR,
 		TiC.PROPERTY_TEXT_ALIGN,
 		TiC.PROPERTY_VALUE,
 		TiC.PROPERTY_VERTICAL_ALIGN,
@@ -56,6 +59,7 @@ public class TextFieldProxy extends TiViewProxy
 		defaultValues.put(TiC.PROPERTY_MAX_LENGTH, -1);
 		defaultValues.put(TiC.PROPERTY_FULLSCREEN, true);
 		defaultValues.put(TiC.PROPERTY_HINT_TYPE, UIModule.HINT_TYPE_STATIC);
+		defaultValues.put(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_ENABLED, false);
 	}
 
 	@Override
diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java
index fea4eb6..ea6131e 100644
--- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java
+++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java
@@ -24,6 +24,7 @@ import ti.modules.titanium.ui.AttributedStringProxy;
 import ti.modules.titanium.ui.UIModule;
 
 import android.content.Context;
+import android.content.res.ColorStateList;
 import android.graphics.Color;
 import android.graphics.Rect;
 import android.graphics.Typeface;
@@ -272,9 +273,44 @@ public class TiUIText extends TiUIView implements TextWatcher, OnEditorActionLis
 			}
 		}
 
+		if (d.containsKey(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_ENABLED)) {
+			textInputLayout.setPasswordVisibilityToggleEnabled(
+				TiConvert.toBoolean(d.get(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_ENABLED), false));
+		}
+
+		setPasswordToggleTintList(d);
+
 		disableChangeEvent = false;
 	}
 
+	private void setPasswordToggleTintList(KrollDict d)
+	{
+		int toggleColor = Color.TRANSPARENT;
+		int toggleTintColor = Color.TRANSPARENT;
+
+		if (d.containsKey(TiC.PROPERTY_COLOR)) {
+			toggleTintColor = TiConvert.toColor(d, TiC.PROPERTY_COLOR);
+		}
+
+		if (d.containsKey(TiC.PROPERTY_HINT_TEXT_COLOR)) {
+			toggleColor = TiConvert.toColor(d, TiC.PROPERTY_HINT_TEXT_COLOR);
+		}
+
+		if (d.containsKey(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_COLOR)) {
+			toggleColor = TiConvert.toColor(d, TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_COLOR);
+		}
+
+		if (d.containsKey(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_TINT_COLOR)) {
+			toggleTintColor = TiConvert.toColor(d, TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_TINT_COLOR);
+		}
+
+		int stateToUse = android.R.attr.state_checked;
+		int[][] textColorStates = new int[][] { new int[] { stateToUse }, new int[] {} };
+		int[] textColors = { toggleTintColor, toggleColor };
+		ColorStateList colorStateList = new ColorStateList(textColorStates, textColors);
+		textInputLayout.setPasswordVisibilityToggleTintList(colorStateList);
+	}
+
 	private void updateTextField()
 	{
 		if (!field) {
@@ -319,6 +355,11 @@ public class TiUIText extends TiUIView implements TextWatcher, OnEditorActionLis
 		if (Log.isDebugModeEnabled()) {
 			Log.d(TAG, "Property: " + key + " old: " + oldValue + " new: " + newValue, Log.DEBUG_MODE);
 		}
+		if (key.equals(TiC.PROPERTY_COLOR) || key.equals(TiC.PROPERTY_HINT_TEXT_COLOR)
+			|| key.equals(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_COLOR)
+			|| key.equals(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_TINT_COLOR)) {
+			setPasswordToggleTintList(proxy.getProperties());
+		}
 		if (key.equals(TiC.PROPERTY_AUTOFILL_TYPE) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
 			tv.setAutofillHints(TiConvert.toString(newValue));
 		} else if (key.equals(TiC.PROPERTY_ENABLED)) {
@@ -406,6 +447,8 @@ public class TiUIText extends TiUIView implements TextWatcher, OnEditorActionLis
 			if (!TiConvert.toBoolean(newValue, true)) {
 				tv.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
 			}
+		} else if (key.equals(TiC.PROPERTY_PASSWORD_VISIBILITY_TOGGLE_ENABLED)) {
+			textInputLayout.setPasswordVisibilityToggleEnabled(TiConvert.toBoolean(newValue, false));
 		} else if (key.equals(TiC.PROPERTY_LINES)) {
 			if (!field) {
 				this.viewHeightInLines = TiConvert.toInt(newValue, 0);
diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiC.java b/android/titanium/src/java/org/appcelerator/titanium/TiC.java
index d063743..a212ad0 100644
--- a/android/titanium/src/java/org/appcelerator/titanium/TiC.java
+++ b/android/titanium/src/java/org/appcelerator/titanium/TiC.java
@@ -2582,6 +2582,21 @@ public class TiC
 	/**
 	 * @module.api
 	 */
+	public static final String PROPERTY_PASSWORD_VISIBILITY_TOGGLE_COLOR = "passwordVisibilityToggleColor";
+
+	/**
+	 * @module.api
+	 */
+	public static final String PROPERTY_PASSWORD_VISIBILITY_TOGGLE_ENABLED = "passwordVisibilityToggleEnabled";
+
+	/**
+	 * @module.api
+	 */
+	public static final String PROPERTY_PASSWORD_VISIBILITY_TOGGLE_TINT_COLOR = "passwordVisibilityToggleTintColor";
+
+	/**
+	 * @module.api
+	 */
 	public static final String PROPERTY_PATH = "path";
 
 	/**
diff --git a/apidoc/Titanium/UI/TextField.yml b/apidoc/Titanium/UI/TextField.yml
index cc6558a..80d8cef 100644
--- a/apidoc/Titanium/UI/TextField.yml
+++ b/apidoc/Titanium/UI/TextField.yml
@@ -485,6 +485,37 @@ properties:
     osver: { ios: { min: "12.0" } }
     platforms: [iphone, ipad]
 
+  - name: passwordVisibilityToggleColor
+    summary: Color of password visibility toggle
+    description: |
+      Color applied to password visibility toggle when field's input is not visible.
+      This property only works when <Titanium.UI.TextField.passwordVisibilityToggleEnabled> is set `true`.
+    type: String
+    default: value of <Titanium.UI.TextField.hintTextColor> property
+    since: {android: "8.3.0"}
+    platforms: [android]
+
+  - name: passwordVisibilityToggleEnabled
+    summary: Determines whether the password visibility toggle functionality is enabled or not
+    description: |
+      When enabled, a button is placed at the end of the TextField which enables the user to switch
+      between the field's input being visibly disguised or not.
+      This property only works when <Titanium.UI.TextField.passwordMask> is set `true`.
+    type: boolean
+    default: false
+    since: {android: "8.3.0"}
+    platforms: [android]
+
+  - name: passwordVisibilityToggleTintColor
+    summary: Tint color of password visibility toggle
+    description: |
+      Color applied to password visibility toggle when field's input is visible.
+      This property only works when <Titanium.UI.TextField.passwordVisibilityToggleEnabled> is set `true`.
+    type: String
+    default: value of <Titanium.UI.TextField.color> property
+    since: {android: "8.3.0"}
+    platforms: [android]
+
   - name: returnKeyType
     summary: Specifies the text to display on the keyboard `Return` key when this field is focused.
     type: Number
-- 
2.7.4

