GuidedStepFragment: multiple fixes:

1. Handle Keyboard ENTER key correctly. Go through the same logic
that press enter icon in soft IME.

2. Add theme attribute for actions elevation value where SetupWraith
need override as 0dip

3. Make EditText not focusable if action is not editable.

Change-Id: I0e77487ba655c2d4ab729f180fe057b28e19e7e2
diff --git a/v17/leanback/res/layout/lb_guidedstep_fragment.xml b/v17/leanback/res/layout/lb_guidedstep_fragment.xml
index 36504fe..fff77e4 100644
--- a/v17/leanback/res/layout/lb_guidedstep_fragment.xml
+++ b/v17/leanback/res/layout/lb_guidedstep_fragment.xml
@@ -34,7 +34,7 @@
         android:orientation="horizontal"
         android:clipToPadding="false"
         android:clipChildren="false"
-        android:paddingLeft="@dimen/lb_guidedactions_section_shadow_width"
+        android:paddingStart="@dimen/lb_guidedactions_section_shadow_width"
         android:layout_width="0dp"
         android:layout_weight="?attr/guidedActionContentWidthWeight"
         android:layout_height="match_parent"
@@ -47,7 +47,7 @@
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="?attr/guidedActionsBackground"
-            android:elevation="@dimen/lb_guidedactions_elevation" />
+            android:elevation="?attr/guidedActionsElevation" />
 
         <android.support.v17.leanback.widget.NonOverlappingLinearLayout
             android:id="@+id/action_fragment"
diff --git a/v17/leanback/res/values/attrs.xml b/v17/leanback/res/values/attrs.xml
index 6aa0c7e..9ce1094 100644
--- a/v17/leanback/res/values/attrs.xml
+++ b/v17/leanback/res/values/attrs.xml
@@ -352,6 +352,10 @@
              {@link android.support.v17.leanback.R.style#Widget_Leanback_GuidedActionsSelectorStyle}. -->
         <attr name="guidedActionsSelectorStyle" format="reference" />
 
+        <!-- Theme attribute for the shadow elevation of GuidedActions. Default is
+             {@link android.support.v17.leanback.R.dimen#lb_guidedactions_elevation}.-->
+        <attr name="guidedActionsElevation" format="dimension|reference" />
+
         <!-- Theme attribute for the background of GuidedActions. Default is
              {@link android.support.v17.leanback.R.color#lb_guidedactions_background}.-->
         <attr name="guidedActionsBackground" format="reference" />
diff --git a/v17/leanback/res/values/themes.xml b/v17/leanback/res/values/themes.xml
index 2af6b3b..d00bc36 100644
--- a/v17/leanback/res/values/themes.xml
+++ b/v17/leanback/res/values/themes.xml
@@ -129,6 +129,7 @@
         <item name="guidanceBreadcrumbStyle">@style/Widget.Leanback.GuidanceBreadcrumbStyle</item>
         <item name="guidanceDescriptionStyle">@style/Widget.Leanback.GuidanceDescriptionStyle</item>
 
+        <item name="guidedActionsElevation">@dimen/lb_guidedactions_elevation</item>
         <item name="guidedActionsBackground">@color/lb_guidedactions_background</item>
         <item name="guidedButtonActionsBackground">@color/lb_guidedbuttonactions_background</item>
         <item name="guidedActionsSelectorStyle">@style/Widget.Leanback.GuidedActionsSelectorStyle</item>
diff --git a/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java b/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
index b70c50d..639ee7f 100644
--- a/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
+++ b/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
@@ -422,38 +422,35 @@
 
                     switch (event.getAction()) {
                         case KeyEvent.ACTION_DOWN:
+                            if (DEBUG) {
+                                Log.d(TAG, "Enter Key down");
+                            }
                             if (!mKeyPressed) {
                                 mKeyPressed = true;
-
                                 playSound(v, AudioManager.FX_KEY_CLICK);
-
-                                if (DEBUG) {
-                                    Log.d(TAG, "Enter Key down");
-                                }
-
                                 mStylist.onAnimateItemPressed(avh.mStylistViewHolder,
                                         mKeyPressed);
-                                handled = true;
                             }
+                            handled = true;
                             break;
                         case KeyEvent.ACTION_UP:
+                            if (DEBUG) {
+                                Log.d(TAG, "Enter Key up");
+                            }
+                            // Sometimes we are losing ACTION_DOWN for the first ENTER after pressed
+                            // Escape in IME.
                             if (mKeyPressed) {
                                 mKeyPressed = false;
-
-                                if (DEBUG) {
-                                    Log.d(TAG, "Enter Key up");
-                                }
-
                                 mStylist.onAnimateItemPressed(avh.mStylistViewHolder, mKeyPressed);
-                                if (action.isEditable() || action.isDescriptionEditable()) {
-                                    if (DEBUG_EDIT) Log.v(TAG_EDIT, "openIme click");
-                                    openIme(avh);
-                                } else {
-                                    handleCheckedActions(avh, action);
-                                    mClickListener.onGuidedActionClicked(action);
-                                }
-                                handled = true;
                             }
+                            if (action.isEditable() || action.isDescriptionEditable()) {
+                                if (DEBUG_EDIT) Log.v(TAG_EDIT, "openIme click");
+                                openIme(avh);
+                            } else {
+                                handleCheckedActions(avh, action);
+                                mClickListener.onGuidedActionClicked(action);
+                            }
+                            handled = true;
                             break;
                         default:
                             break;
@@ -507,53 +504,63 @@
             boolean handled = false;
             if (actionId == EditorInfo.IME_ACTION_NEXT ||
                 actionId == EditorInfo.IME_ACTION_DONE) {
-
-                ActionViewHolder avh = findSubChildViewHolder(v);
-                updateTextIntoAction(avh, v);
-                mClickListener.onGuidedActionClicked(avh.getAction());
-                long nextActionId = finishEditing(avh);
-                if (nextActionId != GuidedAction.ACTION_ID_CURRENT
-                        && nextActionId != avh.getAction().getId()) {
-                    int next = getNextActionIndex(avh.getAction(), nextActionId);
-                    if (next != -1) {
-                        ActionViewHolder vh = (ActionViewHolder) mRecyclerView
-                                .findViewHolderForPosition(next);
-                        if (vh != null) {
-                            handled = true;
-                            if (vh.getAction().isEditable() ||
-                                    vh.getAction().isDescriptionEditable()) {
-                                if (DEBUG_EDIT) Log.v(TAG_EDIT, "openIme next/done");
-                                mStylist.setEditingMode(vh.mStylistViewHolder,
-                                        vh.getAction(), true);
-                                // open Ime on next action.
-                                openIme(vh);
-                            } else {
-                                // close IME and focus to next (not editable) action
-                                closeIme(v);
-                                vh.mStylistViewHolder.view.requestFocus();
-                            }
-                        }
-                    }
-                }
-                if (!handled) {
-                    if (DEBUG_EDIT) Log.v(TAG_EDIT, "closeIme no next action");
-                    handled = true;
-                    closeIme(v);
-                    // requestFocus() otherwise the focus might be stolen by other fragments.
-                    avh.mStylistViewHolder.view.requestFocus();
-                }
+                fillAndGoNext(v);
+                handled = true;
             } else if (actionId == EditorInfo.IME_ACTION_NONE) {
                 if (DEBUG_EDIT) Log.v(TAG_EDIT, "closeIme escape north");
                 // Escape north handling: stay on current item, but close editor
                 handled = true;
-                ActionViewHolder avh = findSubChildViewHolder(v);
-                finishEditing(avh);
-                avh.mStylistViewHolder.view.requestFocus();
-                closeIme(v);
+                fillAndStay(v);
             }
             return handled;
         }
 
+        private void fillAndStay(TextView v) {
+            ActionViewHolder avh = findSubChildViewHolder(v);
+            updateTextIntoAction(avh, v);
+            finishEditing(avh);
+            closeIme(v);
+            avh.mStylistViewHolder.view.requestFocus();
+        }
+
+        private void fillAndGoNext(TextView v) {
+            boolean handled = false;
+            ActionViewHolder avh = findSubChildViewHolder(v);
+            updateTextIntoAction(avh, v);
+            mClickListener.onGuidedActionClicked(avh.getAction());
+            long nextActionId = finishEditing(avh);
+            if (nextActionId != GuidedAction.ACTION_ID_CURRENT
+                    && nextActionId != avh.getAction().getId()) {
+                int next = getNextActionIndex(avh.getAction(), nextActionId);
+                if (next != -1) {
+                    ActionViewHolder vh = (ActionViewHolder) mRecyclerView
+                            .findViewHolderForPosition(next);
+                    if (vh != null) {
+                        handled = true;
+                        if (vh.getAction().isEditable() ||
+                                vh.getAction().isDescriptionEditable()) {
+                            if (DEBUG_EDIT) Log.v(TAG_EDIT, "openIme of next Action");
+                            mStylist.setEditingMode(vh.mStylistViewHolder,
+                                    vh.getAction(), true);
+                            // open Ime on next action.
+                            openIme(vh);
+                        } else {
+                            if (DEBUG_EDIT) Log.v(TAG_EDIT, "closeIme and focus to next Action");
+                            // close IME and focus to next (not editable) action
+                            closeIme(v);
+                            vh.mStylistViewHolder.view.requestFocus();
+                        }
+                    }
+                }
+            }
+            if (!handled) {
+                if (DEBUG_EDIT) Log.v(TAG_EDIT, "closeIme no next action");
+                handled = true;
+                closeIme(v);
+                avh.mStylistViewHolder.view.requestFocus();
+            }
+        }
+
         private void updateTextIntoAction(ActionViewHolder avh, TextView v) {
             GuidedAction action = avh.getAction();
             if (v == avh.mStylistViewHolder.getDescriptionView()) {
@@ -575,15 +582,10 @@
         public boolean onKeyPreIme(EditText editText, int keyCode, KeyEvent event) {
             if (DEBUG_EDIT) Log.v(TAG_EDIT, "IME key: " + keyCode);
             if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
-                ActionViewHolder avh = findSubChildViewHolder(editText);
-                updateTextIntoAction(avh, editText);
-                editText.clearFocus();
-                finishEditing(avh);
-                avh.mStylistViewHolder.view.requestFocus();
-                if (mImeOpened) {
-                    mImeOpened = false;
-                    mEditListener.onImeClose();
-                }
+                fillAndStay(editText);
+            } else if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() ==
+                    KeyEvent.ACTION_UP) {
+                fillAndGoNext(editText);
             }
             return false;
         }
@@ -611,7 +613,6 @@
                 mImeOpened = false;
                 InputMethodManager mgr = (InputMethodManager)
                         v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-                v.clearFocus();
                 mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
                 mEditListener.onImeClose();
             }
diff --git a/v17/leanback/src/android/support/v17/leanback/app/GuidedStepFragment.java b/v17/leanback/src/android/support/v17/leanback/app/GuidedStepFragment.java
index f5bf497..89a1a13 100644
--- a/v17/leanback/src/android/support/v17/leanback/app/GuidedStepFragment.java
+++ b/v17/leanback/src/android/support/v17/leanback/app/GuidedStepFragment.java
@@ -127,6 +127,7 @@
  * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedActionContentWidthWeightTwoPanels
  * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedActionsBackground
  * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedButtonActionsBackground
+ * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedActionsElevation
  * @see GuidanceStylist
  * @see GuidanceStylist.Guidance
  * @see GuidedAction
diff --git a/v17/leanback/src/android/support/v17/leanback/app/GuidedStepSupportFragment.java b/v17/leanback/src/android/support/v17/leanback/app/GuidedStepSupportFragment.java
index 5fb8521..dbc7b30 100644
--- a/v17/leanback/src/android/support/v17/leanback/app/GuidedStepSupportFragment.java
+++ b/v17/leanback/src/android/support/v17/leanback/app/GuidedStepSupportFragment.java
@@ -129,6 +129,7 @@
  * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedActionContentWidthWeightTwoPanels
  * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedActionsBackground
  * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedButtonActionsBackground
+ * @attr ref android.support.v17.leanback.R.styleable#LeanbackGuidedStepTheme_guidedActionsElevation
  * @see GuidanceStylist
  * @see GuidanceStylist.Guidance
  * @see GuidedAction
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
index dc57553..e8c5801 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
@@ -468,6 +468,7 @@
         if (vh.mTitleView != null) {
             vh.mTitleView.setText(action.getTitle());
             vh.mTitleView.setAlpha(action.isEnabled() ? mEnabledTextAlpha : mDisabledTextAlpha);
+            vh.mTitleView.setFocusable(action.isEditable());
         }
         if (vh.mDescriptionView != null) {
             vh.mDescriptionView.setText(action.getDescription());
@@ -475,6 +476,7 @@
                     View.GONE : View.VISIBLE);
             vh.mDescriptionView.setAlpha(action.isEnabled() ? mEnabledDescriptionAlpha :
                 mDisabledDescriptionAlpha);
+            vh.mDescriptionView.setFocusable(action.isDescriptionEditable());
         }
         // Clients might want the check mark view to be gone entirely, in which case, ignore it.
         if (vh.mCheckmarkView != null && vh.mCheckmarkView.getVisibility() != View.GONE) {