Allow action item layouts that do not supply editable actions.

Fix cast operations that were throwing exceptions; make the Leanback
edit text object public to allow reuse.

b/23039755

Change-Id: Id9ceda139efeb352210831dbed0f1dfbbcccd76a
diff --git a/v17/leanback/api/current.txt b/v17/leanback/api/current.txt
index 388ce92..8bc4297 100644
--- a/v17/leanback/api/current.txt
+++ b/v17/leanback/api/current.txt
@@ -874,6 +874,13 @@
     method public android.support.v17.leanback.widget.GuidedAction.Builder title(java.lang.String);
   }
 
+  public class GuidedActionEditText extends android.widget.EditText implements android.support.v17.leanback.widget.ImeKeyMonitor {
+    ctor public GuidedActionEditText(android.content.Context);
+    ctor public GuidedActionEditText(android.content.Context, android.util.AttributeSet);
+    ctor public GuidedActionEditText(android.content.Context, android.util.AttributeSet, int);
+    method public void setImeKeyListener(android.support.v17.leanback.widget.ImeKeyMonitor.ImeKeyListener);
+  }
+
   public class GuidedActionsStylist implements android.support.v17.leanback.widget.FragmentAnimationProvider {
     ctor public GuidedActionsStylist();
     method public android.support.v17.leanback.widget.VerticalGridView getActionsGridView();
@@ -904,6 +911,7 @@
     method public android.widget.ImageView getChevronView();
     method public android.view.View getContentView();
     method public android.widget.TextView getDescriptionView();
+    method public android.widget.EditText getEditableTitleView();
     method public android.widget.ImageView getIconView();
     method public android.widget.TextView getTitleView();
     field public final android.view.View view;
diff --git a/v17/leanback/res/layout/lb_guidedactions_item.xml b/v17/leanback/res/layout/lb_guidedactions_item.xml
index 1d81470..3babaa9 100644
--- a/v17/leanback/res/layout/lb_guidedactions_item.xml
+++ b/v17/leanback/res/layout/lb_guidedactions_item.xml
@@ -34,7 +34,7 @@
         android:id="@+id/guidedactions_item_content"
         style="?attr/guidedActionItemContentStyle" >
 
-        <android.support.v17.leanback.widget.LeanbackEditText
+        <android.support.v17.leanback.widget.GuidedActionEditText
             android:id="@+id/guidedactions_item_title"
             style="?attr/guidedActionItemTitleStyle" />
 
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 1edd0d1..975357e 100644
--- a/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
+++ b/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
@@ -237,12 +237,12 @@
         v.setOnClickListener(mOnClickListener);
         v.setOnFocusChangeListener(mActionOnFocusListener);
 
-        final EditText edit = (EditText)vh.getTitleView();
+        final EditText edit = vh.getEditableTitleView();
         if (edit != null) {
             edit.setPrivateImeOptions("EscapeNorth=1;");
             edit.setOnEditorActionListener(mActionEditListener);
-            ImeKeyMonitor monitor = (ImeKeyMonitor)edit;
-            if (monitor != null) {
+            if (edit instanceof ImeKeyMonitor) {
+                ImeKeyMonitor monitor = (ImeKeyMonitor)edit;
                 monitor.setImeKeyListener(mActionEditListener);
             }
         }
@@ -263,7 +263,7 @@
         avh.setAction(action);
         mStylist.onBindViewHolder(avh.mStylistViewHolder, action);
 
-        final EditText edit = (EditText)avh.mStylistViewHolder.getTitleView();
+        final EditText edit = avh.mStylistViewHolder.getEditableTitleView();
         if (edit != null) {
             int next = getNextEditableActionIndex(action);
             int flag = (next == -1) ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT;
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/LeanbackEditText.java b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionEditText.java
similarity index 85%
rename from v17/leanback/src/android/support/v17/leanback/widget/LeanbackEditText.java
rename to v17/leanback/src/android/support/v17/leanback/widget/GuidedActionEditText.java
index 97c4477..8e052fb 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/LeanbackEditText.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionEditText.java
@@ -21,22 +21,20 @@
 
 /**
  * A custom EditText that satisfies the IME key monitoring requirements of GuidedStepFragment.
- *
- * @hide
  */
-public class LeanbackEditText extends EditText implements ImeKeyMonitor {
+public class GuidedActionEditText extends EditText implements ImeKeyMonitor {
 
     private ImeKeyListener mKeyListener;
 
-    public LeanbackEditText(Context ctx) {
+    public GuidedActionEditText(Context ctx) {
         this(ctx, null);
     }
 
-    public LeanbackEditText(Context ctx, AttributeSet attrs) {
+    public GuidedActionEditText(Context ctx, AttributeSet attrs) {
         this(ctx, attrs, android.R.attr.editTextStyle);
     }
 
-    public LeanbackEditText(Context ctx, AttributeSet attrs, int defStyleAttr) {
+    public GuidedActionEditText(Context ctx, AttributeSet attrs, int defStyleAttr) {
         super(ctx, attrs, defStyleAttr);
     }
 
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 525e2fc..07dd4c9 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
@@ -40,6 +40,7 @@
 import android.view.ViewPropertyAnimator;
 import android.view.ViewTreeObserver;
 import android.view.WindowManager;
+import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -168,6 +169,14 @@
         }
 
         /**
+         * Convenience method to return an editable version of the title, if possible,
+         * or null if the title view isn't an EditText.
+         */
+        public EditText getEditableTitleView() {
+            return (mTitleView instanceof EditText) ? (EditText)mTitleView : null;
+        }
+
+        /**
          * Returns the description view within this view holder's view.
          */
         public TextView getDescriptionView() {
@@ -310,7 +319,10 @@
      * Subclasses may override to provide their own customized layouts. The base implementation
      * returns {@link android.support.v17.leanback.R.layout#lb_guidedactions_item}. If overridden,
      * the substituted layout should contain matching IDs for any views that should be managed by
-     * the base class; this can be achieved by starting with a copy of the base layout file.
+     * the base class; this can be achieved by starting with a copy of the base layout file. Note
+     * that in order for the item to support editing, the title view should both subclass {@link
+     * android.widget.EditText} and implement {@link ImeKeyMonitor}; see {@link
+     * GuidedActionEditText}.
      * @return The resource ID of the layout to be inflated to define the view to display an
      * individual GuidedAction.
      */