Leanback Demo: demo of edit title

Bug 24673827

Change-Id: I1e4d9378cc862d748055387a71129692bd4cf53b
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java
index dac7f53..63149b4 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepActivity.java
@@ -27,6 +27,7 @@
 import android.support.v17.leanback.widget.GuidedAction;
 import android.support.v17.leanback.widget.GuidanceStylist;
 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
@@ -41,8 +42,9 @@
     private static final int CONTINUE = 1;
     private static final int BACK = 2;
 
-    private static final int FIRST_NAME = 1;
-    private static final int LAST_NAME = 2;
+    private static final int FIRST_NAME = 3;
+    private static final int LAST_NAME = 4;
+    private static final int PAYMENT = 5;
 
     private static final int OPTION_CHECK_SET_ID = 10;
     private static final int DEFAULT_OPTION = 0;
@@ -96,6 +98,17 @@
                 .build());
     }
 
+    private static void addEditableAction(List<GuidedAction> actions, long id, String title,
+            String editTitle, String desc) {
+        actions.add(new GuidedAction.Builder()
+                .id(id)
+                .title(title)
+                .editTitle(editTitle)
+                .description(desc)
+                .editable(true)
+                .build());
+    }
+
     private static void addCheckedAction(List<GuidedAction> actions, int iconResId, Context context,
             String title, String desc) {
         actions.add(new GuidedAction.Builder()
@@ -158,16 +171,30 @@
         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
             addEditableAction(actions, FIRST_NAME, "Pat", "Your first name");
             addEditableAction(actions, LAST_NAME, "Smith", "Your last name");
+            addEditableAction(actions, PAYMENT, "Payment", "", "Input credit card number");
+            addAction(actions, CONTINUE, "Continue", "Continue");
         }
 
         @Override
         public void onGuidedActionClicked(GuidedAction action) {
-            if (action.getId() == LAST_NAME) {
+            if (action.getId() == CONTINUE) {
                 FragmentManager fm = getFragmentManager();
                 GuidedStepFragment.add(fm, new ThirdStepFragment());
             }
         }
 
+        @Override
+        public void onGuidedActionEdited(GuidedAction action) {
+            CharSequence editTitle = action.getEditTitle();
+            if (TextUtils.isDigitsOnly(editTitle) && editTitle.length() == 16) {
+                editTitle = editTitle.subSequence(editTitle.length() - 4, editTitle.length());
+                action.setDescription("Visa XXXX-XXXX-XXXX-"+editTitle);
+            } else if (editTitle.length() == 0){
+                action.setDescription("Input credit card number");
+            } else {
+                action.setDescription("Error credit card number");
+            }
+        }
     }
 
     public static class ThirdStepFragment extends GuidedStepFragment {
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java
index 6974323..432c6c0 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/GuidedStepSupportActivity.java
@@ -29,6 +29,7 @@
 import android.support.v17.leanback.widget.GuidedAction;
 import android.support.v17.leanback.widget.GuidanceStylist;
 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
@@ -43,8 +44,9 @@
     private static final int CONTINUE = 1;
     private static final int BACK = 2;
 
-    private static final int FIRST_NAME = 1;
-    private static final int LAST_NAME = 2;
+    private static final int FIRST_NAME = 3;
+    private static final int LAST_NAME = 4;
+    private static final int PAYMENT = 5;
 
     private static final int OPTION_CHECK_SET_ID = 10;
     private static final int DEFAULT_OPTION = 0;
@@ -98,6 +100,17 @@
                 .build());
     }
 
+    private static void addEditableAction(List<GuidedAction> actions, long id, String title,
+            String editTitle, String desc) {
+        actions.add(new GuidedAction.Builder()
+                .id(id)
+                .title(title)
+                .editTitle(editTitle)
+                .description(desc)
+                .editable(true)
+                .build());
+    }
+
     private static void addCheckedAction(List<GuidedAction> actions, int iconResId, Context context,
             String title, String desc) {
         actions.add(new GuidedAction.Builder()
@@ -160,16 +173,30 @@
         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
             addEditableAction(actions, FIRST_NAME, "Pat", "Your first name");
             addEditableAction(actions, LAST_NAME, "Smith", "Your last name");
+            addEditableAction(actions, PAYMENT, "Payment", "", "Input credit card number");
+            addAction(actions, CONTINUE, "Continue", "Continue");
         }
 
         @Override
         public void onGuidedActionClicked(GuidedAction action) {
-            if (action.getId() == LAST_NAME) {
+            if (action.getId() == CONTINUE) {
                 FragmentManager fm = getFragmentManager();
                 GuidedStepSupportFragment.add(fm, new ThirdStepFragment());
             }
         }
 
+        @Override
+        public void onGuidedActionEdited(GuidedAction action) {
+            CharSequence editTitle = action.getEditTitle();
+            if (TextUtils.isDigitsOnly(editTitle) && editTitle.length() == 16) {
+                editTitle = editTitle.subSequence(editTitle.length() - 4, editTitle.length());
+                action.setDescription("Visa XXXX-XXXX-XXXX-"+editTitle);
+            } else if (editTitle.length() == 0){
+                action.setDescription("Input credit card number");
+            } else {
+                action.setDescription("Error credit card number");
+            }
+        }
     }
 
     public static class ThirdStepFragment extends GuidedStepSupportFragment {
diff --git a/samples/SupportLeanbackShowcase/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/wizard/WizardExample2ndStepFragment.java b/samples/SupportLeanbackShowcase/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/wizard/WizardExample2ndStepFragment.java
index 3b369ac..c1df8a1 100644
--- a/samples/SupportLeanbackShowcase/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/wizard/WizardExample2ndStepFragment.java
+++ b/samples/SupportLeanbackShowcase/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/wizard/WizardExample2ndStepFragment.java
@@ -17,6 +17,7 @@
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.support.annotation.NonNull;
 import android.support.v17.leanback.app.GuidedStepFragment;
 import android.support.v17.leanback.supportleanbackshowcase.R;
@@ -69,19 +70,29 @@
         action = new GuidedAction.Builder()
                 .id(ACTION_ID_PAYMENT_METHOD)
                 .title(getString(R.string.wizard_example_payment_method))
-                .description(getString(R.string.wizard_example_visa_balance))
-                .editable(false)
+                .editTitle("")
+                .description(getString(R.string.wizard_example_input_credit))
+                .editable(true)
                 .build();
         actions.add(action);
     }
 
     @Override
-    public void onGuidedActionClicked(GuidedAction action) {
-        if (ACTION_ID_PAYMENT_METHOD == action.getId()) {
-            Toast.makeText(getActivity(),
-                    getString(R.string.wizard_example_toast_payment_method_clicked),
-                    Toast.LENGTH_SHORT).show();
+    public void onGuidedActionEdited(GuidedAction action) {
+        CharSequence editTitle = action.getEditTitle();
+        if (TextUtils.isDigitsOnly(editTitle) && editTitle.length() == 16) {
+            action.setDescription(getString(R.string.wizard_example_visa,
+                    editTitle.subSequence(editTitle.length() - 4, editTitle.length())));
+        } else if (editTitle.length() == 0) {
+            action.setDescription(getString(R.string.wizard_example_input_credit));
         } else {
+            action.setDescription(getString(R.string.wizard_example_input_credit_wrong));
+        }
+    }
+
+    @Override
+    public void onGuidedActionClicked(GuidedAction action) {
+        if (ACTION_ID_CONFIRM == action.getId()) {
             GuidedStepFragment fragment = new WizardExample3rdStepFragment();
             fragment.setArguments(getArguments());
             add(getFragmentManager(), fragment);
diff --git a/samples/SupportLeanbackShowcase/app/src/main/res/values/strings.xml b/samples/SupportLeanbackShowcase/app/src/main/res/values/strings.xml
index 9a46e54..a25b964 100644
--- a/samples/SupportLeanbackShowcase/app/src/main/res/values/strings.xml
+++ b/samples/SupportLeanbackShowcase/app/src/main/res/values/strings.xml
@@ -51,6 +51,8 @@
     <string name="wizard_example_watch_now">Watch now</string>
     <string name="wizard_example_later">Later</string>
     <string name="wizard_example_watch_now_clicked">\'Watch now\' clicked.</string>
-    <string name="wizard_example_visa_balance">Visa - 1234 Balance $60.00</string>
-    <string name="wizard_example_just_a_second">Just a second...</string>
+    <string name="wizard_example_input_credit">Input credit card number</string>
+    <string name="wizard_example_visa">Visa XXXX-XXXX-XXXX-%s</string>
+    <string name="wizard_example_input_credit_wrong">Error credit card number</string>
+    s<string name="wizard_example_just_a_second">Just a second...</string>
 </resources>
\ No newline at end of file