Visibility icon in password field is toggled according to the visibility state; bette...
authorDavid A. Velasco <dvelasco@solidgear.es>
Fri, 17 May 2013 12:33:31 +0000 (14:33 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Fri, 17 May 2013 12:33:31 +0000 (14:33 +0200)
res/layout-land/account_setup.xml
res/layout/account_setup.xml
src/com/owncloud/android/authentication/AuthenticatorActivity.java

index a4b5a5e..52f5392 100644 (file)
                                        android:hint="@string/auth_username"\r
                                        android:inputType="textNoSuggestions" />\r
                \r
-                               <FrameLayout\r
+                               <EditText\r
+                                   android:id="@+id/account_password"\r
                                        android:layout_width="match_parent"\r
                                        android:layout_height="wrap_content"\r
-                                       >\r
-                                       <EditText\r
-                                               android:id="@+id/account_password"\r
-                                               android:layout_width="match_parent"\r
-                                               android:layout_height="wrap_content"\r
-                                               android:ems="10"\r
-                                               android:hint="@string/auth_password"\r
-                                               android:inputType="textPassword"/>\r
-                                       \r
-                                       <ImageView\r
-                                               android:id="@+id/viewPasswordButton"\r
-                                               android:layout_width="wrap_content"\r
-                                               android:layout_height="wrap_content"\r
-                                               android:layout_gravity="right|center_vertical"\r
-                                               android:src="@android:drawable/ic_menu_view"\r
-                                               android:onClick="onViewPasswordClick"\r
-                                               android:visibility="invisible" />\r
-                               </FrameLayout>\r
-                    \r
+                                       android:ems="10"\r
+                                       android:hint="@string/auth_password"\r
+                                       android:inputType="textPassword"\r
+                                       android:drawablePadding="5dp"\r
+                                       />\r
+                       \r
                                <TextView\r
                                        android:id="@+id/auth_status_text"\r
                                        android:layout_width="match_parent"\r
index 65237a8..01593fa 100644 (file)
             android:hint="@string/auth_username"\r
             android:inputType="textNoSuggestions" />\r
 \r
-        <FrameLayout\r
-            android:layout_width="match_parent"\r
-            android:layout_height="wrap_content" >\r
-\r
-            <EditText\r
-                android:id="@+id/account_password"\r
-                android:layout_width="match_parent"\r
-                android:layout_height="wrap_content"\r
-                android:ems="10"\r
-                android:hint="@string/auth_password"\r
-                android:inputType="textPassword" />\r
-\r
-            <ImageView\r
-                android:id="@+id/viewPasswordButton"\r
-                android:layout_width="wrap_content"\r
-                android:layout_height="wrap_content"\r
-                android:layout_gravity="right|center_vertical"\r
-                android:onClick="onViewPasswordClick"\r
-                android:src="@android:drawable/ic_menu_view"\r
-                android:visibility="invisible" />\r
-        </FrameLayout>\r
-\r
+               <EditText\r
+                   android:id="@+id/account_password"\r
+                       android:layout_width="match_parent"\r
+                       android:layout_height="wrap_content"\r
+                       android:ems="10"\r
+                       android:hint="@string/auth_password"\r
+                       android:inputType="textPassword"\r
+                       android:drawablePadding="5dp"\r
+                       />\r
+        \r
         <TextView\r
             android:id="@+id/auth_status_text"\r
             android:layout_width="match_parent"\r
index 80cf894..77fa724 100644 (file)
@@ -59,7 +59,6 @@ import android.view.inputmethod.EditorInfo;
 import android.widget.CheckBox;\r
 import android.widget.EditText;\r
 import android.widget.Button;\r
-import android.widget.ImageView;\r
 import android.widget.TextView;\r
 import android.widget.Toast;\r
 import android.widget.TextView.OnEditorActionListener;\r
@@ -93,6 +92,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private static final String KEY_SERVER_STATUS_TEXT = "SERVER_STATUS_TEXT";\r
     private static final String KEY_SERVER_STATUS_ICON = "SERVER_STATUS_ICON";\r
     private static final String KEY_IS_SSL_CONN = "IS_SSL_CONN";\r
+    private static final String KEY_PASSWORD_VISIBLE = "PASSWORD_VISIBLE";\r
     private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT";\r
     private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON";\r
     \r
@@ -129,7 +129,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private byte mAction;\r
     private Account mAccount;\r
     \r
-    private ImageView mViewPasswordButton;\r
     private EditText mHostUrlInput;\r
     private EditText mUsernameInput;\r
     private EditText mPasswordInput;\r
@@ -154,7 +153,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         \r
         /// set view and get references to view elements\r
         setContentView(R.layout.account_setup);\r
-        mViewPasswordButton = (ImageView) findViewById(R.id.viewPasswordButton);\r
         mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);\r
         mUsernameInput = (EditText) findViewById(R.id.account_username);\r
         mPasswordInput = (EditText) findViewById(R.id.account_password);\r
@@ -214,6 +212,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mIsSslConn = savedInstanceState.getBoolean(KEY_IS_SSL_CONN);\r
             mAuthStatusText = savedInstanceState.getInt(KEY_AUTH_STATUS_TEXT);\r
             mAuthStatusIcon = savedInstanceState.getInt(KEY_AUTH_STATUS_ICON);\r
+            if (savedInstanceState.getBoolean(KEY_PASSWORD_VISIBLE, false)) {\r
+                showPassword();\r
+            }\r
             \r
             /// server data\r
             String ocVersion = savedInstanceState.getString(KEY_OC_VERSION);\r
@@ -259,13 +260,24 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         mHostUrlInput.setOnTouchListener(new RightDrawableOnTouchListener() {\r
             @Override\r
             public boolean onDrawableTouch(final MotionEvent event) {\r
-                AuthenticatorActivity.this.onRefreshClick(mHostUrlInput);\r
+                if (event.getAction() == MotionEvent.ACTION_UP) {\r
+                    AuthenticatorActivity.this.onRefreshClick();\r
+                }\r
                 return true;\r
             }\r
         });\r
         mPasswordInput.setOnFocusChangeListener(this);\r
         mPasswordInput.setImeOptions(EditorInfo.IME_ACTION_DONE);\r
         mPasswordInput.setOnEditorActionListener(this);\r
+        mPasswordInput.setOnTouchListener(new RightDrawableOnTouchListener() {\r
+            @Override\r
+            public boolean onDrawableTouch(final MotionEvent event) {\r
+                if (event.getAction() == MotionEvent.ACTION_UP) {\r
+                    AuthenticatorActivity.this.onViewPasswordClick();\r
+                }\r
+                return true;\r
+            }\r
+        });\r
     }\r
     \r
     /**\r
@@ -287,6 +299,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         outState.putBoolean(KEY_SERVER_CHECKED, mServerIsChecked);\r
         outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mOcServerChkOperation != null));\r
         outState.putBoolean(KEY_IS_SSL_CONN, mIsSslConn);\r
+        outState.putBoolean(KEY_PASSWORD_VISIBLE, isPasswordVisible());\r
         outState.putInt(KEY_AUTH_STATUS_ICON, mAuthStatusIcon);\r
         outState.putInt(KEY_AUTH_STATUS_TEXT, mAuthStatusText);\r
         \r
@@ -434,15 +447,40 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      */\r
     private void onPasswordFocusChanged(TextView passwordInput, boolean hasFocus) {\r
         if (hasFocus) {\r
-            mViewPasswordButton.setVisibility(View.VISIBLE);\r
+            showViewPasswordButton();\r
         } else {\r
-            int input_type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
-            passwordInput.setInputType(input_type);\r
-            mViewPasswordButton.setVisibility(View.INVISIBLE);\r
+            hidePassword();\r
+            hidePasswordButton();\r
         }\r
     }\r
 \r
 \r
+    private void showViewPasswordButton() {\r
+        int drawable = android.R.drawable.ic_menu_view;\r
+        if (isPasswordVisible()) {\r
+            drawable = android.R.drawable.ic_secure;\r
+        }\r
+        mPasswordInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable, 0);\r
+    }\r
+\r
+    private boolean isPasswordVisible() {\r
+        return ((mPasswordInput.getInputType() & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);\r
+    }\r
+    \r
+    private void hidePasswordButton() {\r
+        mPasswordInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);\r
+    }\r
+\r
+    private void showPassword() {\r
+        mPasswordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);\r
+        showViewPasswordButton();\r
+    }\r
+    \r
+    private void hidePassword() {\r
+        mPasswordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);\r
+        showViewPasswordButton();\r
+    }\r
+    \r
     \r
     /**\r
      * Cancels the authenticator activity\r
@@ -1088,30 +1126,25 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * \r
      * @param view      Refresh 'button'\r
      */\r
-    public void onRefreshClick(View view) {\r
+    public void onRefreshClick() {\r
         checkOcServer();\r
     }\r
     \r
     \r
+    \r
     /**\r
      * Called when the eye icon in the password field is clicked.\r
      * \r
      * Toggles the visibility of the password in the field. \r
-     * \r
-     * @param view      'View password' 'button'\r
      */\r
-    public void onViewPasswordClick(View view) {\r
+    public void onViewPasswordClick() {\r
         int selectionStart = mPasswordInput.getSelectionStart();\r
         int selectionEnd = mPasswordInput.getSelectionEnd();\r
-        int input_type = mPasswordInput.getInputType();\r
-        if ((input_type & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {\r
-            input_type = InputType.TYPE_CLASS_TEXT\r
-                    | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
+        if (isPasswordVisible()) {\r
+            hidePassword();\r
         } else {\r
-            input_type = InputType.TYPE_CLASS_TEXT\r
-                    | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;\r
+            showPassword();\r
         }\r
-        mPasswordInput.setInputType(input_type);\r
         mPasswordInput.setSelection(selectionStart, selectionEnd);\r
     }    \r
     \r
@@ -1142,13 +1175,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mOAuthTokenEndpointText.setVisibility(View.VISIBLE);\r
             mUsernameInput.setVisibility(View.GONE);\r
             mPasswordInput.setVisibility(View.GONE);\r
-            mViewPasswordButton.setVisibility(View.GONE);\r
         } else {\r
             mOAuthAuthEndpointText.setVisibility(View.GONE);\r
             mOAuthTokenEndpointText.setVisibility(View.GONE);\r
             mUsernameInput.setVisibility(View.VISIBLE);\r
             mPasswordInput.setVisibility(View.VISIBLE);\r
-            mViewPasswordButton.setVisibility(View.INVISIBLE);\r
         }     \r
 \r
     }    \r
@@ -1196,23 +1227,21 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
          */\r
         @Override\r
         public boolean onTouch(View view, MotionEvent event) {\r
-            if (event.getAction() == MotionEvent.ACTION_DOWN) {\r
-                Drawable rightDrawable = null;\r
-                if (view instanceof TextView) {\r
-                    Drawable[] drawables = ((TextView)view).getCompoundDrawables();\r
-                    if (drawables.length > 2) {\r
-                        rightDrawable = drawables[2];\r
-                    }\r
+            Drawable rightDrawable = null;\r
+            if (view instanceof TextView) {\r
+                Drawable[] drawables = ((TextView)view).getCompoundDrawables();\r
+                if (drawables.length > 2) {\r
+                    rightDrawable = drawables[2];\r
                 }\r
-                if (rightDrawable != null) {\r
-                    final int x = (int) event.getX();\r
-                    final int y = (int) event.getY();\r
-                    final Rect bounds = rightDrawable.getBounds();\r
-                    if (x >= (view.getRight() - bounds.width() - fuzz) && x <= (view.getRight() - view.getPaddingRight() + fuzz)\r
-                        && y >= (view.getPaddingTop() - fuzz) && y <= (view.getHeight() - view.getPaddingBottom()) + fuzz) {\r
-                        \r
-                        return onDrawableTouch(event);\r
-                    }\r
+            }\r
+            if (rightDrawable != null) {\r
+                final int x = (int) event.getX();\r
+                final int y = (int) event.getY();\r
+                final Rect bounds = rightDrawable.getBounds();\r
+                if (x >= (view.getRight() - bounds.width() - fuzz) && x <= (view.getRight() - view.getPaddingRight() + fuzz)\r
+                    && y >= (view.getPaddingTop() - fuzz) && y <= (view.getHeight() - view.getPaddingBottom()) + fuzz) {\r
+                    \r
+                    return onDrawableTouch(event);\r
                 }\r
             }\r
             return false;\r