• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // CHECKSTYLE:OFF Generated code
2 /* This file is auto-generated from GuidedStepActivity.java.  DO NOT MODIFY. */
3 
4 /*
5  * Copyright (C) 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 package com.example.android.leanback;
21 
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.text.InputType;
27 import android.text.TextUtils;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.inputmethod.EditorInfo;
33 
34 import androidx.core.content.res.ResourcesCompat;
35 import androidx.fragment.app.Fragment;
36 import androidx.fragment.app.FragmentActivity;
37 import androidx.fragment.app.FragmentManager;
38 import androidx.leanback.app.GuidedStepSupportFragment;
39 import androidx.leanback.widget.GuidanceStylist;
40 import androidx.leanback.widget.GuidanceStylist.Guidance;
41 import androidx.leanback.widget.GuidedAction;
42 import androidx.leanback.widget.GuidedActionsStylist;
43 import androidx.leanback.widget.GuidedDatePickerAction;
44 
45 import java.util.ArrayList;
46 import java.util.Calendar;
47 import java.util.List;
48 
49 /**
50  * Activity that showcases different aspects of GuidedStepSupportFragments.
51  */
52 public class GuidedStepSupportActivity extends FragmentActivity {
53 
54     private static final int BACK = 2;
55 
56     private static final int FIRST_NAME = 3;
57     private static final int LAST_NAME = 4;
58     private static final int PASSWORD = 5;
59     private static final int PAYMENT = 6;
60     private static final int NEW_PAYMENT = 7;
61     private static final int PAYMENT_EXPIRE = 8;
62     private static final int REFRESH = 9;
63 
64     private static final long RADIO_ID_BASE = 0;
65     private static final long CHECKBOX_ID_BASE = 100;
66 
67     private static final long DEFAULT_OPTION = RADIO_ID_BASE;
68 
69     private static final String[] OPTION_NAMES = { "Option A", "Option B", "Option C" };
70     private static final String[] OPTION_DESCRIPTIONS = { "Here's one thing you can do",
71             "Here's another thing you can do", "Here's one more thing you can do" };
72 
73     private static final String TAG = GuidedStepSupportActivity.class.getSimpleName();
74 
75     @Override
onCreate(Bundle savedInstanceState)76     protected void onCreate(Bundle savedInstanceState) {
77         Log.v(TAG, "onCreate");
78         super.onCreate(savedInstanceState);
79         setContentView(R.layout.guided_step_activity);
80         if (savedInstanceState == null) {
81             GuidedStepSupportFragment.addAsRoot(this, new FirstStepFragment(), R.id.lb_guidedstep_host);
82         }
83     }
84 
85     @Override
onConfigurationChanged(Configuration newConfig)86     public void onConfigurationChanged(Configuration newConfig) {
87         Log.v(TAG, "onConfigurationChanged");
88         super.onConfigurationChanged(newConfig);
89     }
90 
91     @Override
onSaveInstanceState(Bundle outState)92     protected void onSaveInstanceState(Bundle outState) {
93         Log.v(TAG, "onSaveInstanceState");
94         super.onSaveInstanceState(outState);
95     }
96 
97     @Override
onRestoreInstanceState(Bundle savedInstanceState)98     protected void onRestoreInstanceState(Bundle savedInstanceState) {
99         Log.v(TAG, "onRestoreInstanceState");
100         super.onRestoreInstanceState(savedInstanceState);
101     }
102 
addAction(List<GuidedAction> actions, long id, String title, String desc)103     private static GuidedAction addAction(List<GuidedAction> actions, long id, String title,
104             String desc) {
105         GuidedAction action;
106         actions.add(action = new GuidedAction.Builder(null)
107                 .id(id)
108                 .title(title)
109                 .description(desc)
110                 .build());
111         return action;
112     }
113 
addAction(List<GuidedAction> actions, long id, String title, String desc, List<GuidedAction> subActions)114     private static GuidedAction addAction(List<GuidedAction> actions, long id, String title,
115             String desc, List<GuidedAction> subActions) {
116         GuidedAction action;
117         actions.add(action = new GuidedAction.Builder(null)
118                 .id(id)
119                 .title(title)
120                 .description(desc)
121                 .subActions(subActions)
122                 .build());
123         return action;
124     }
125 
addEditableAction(Context context, List<GuidedAction> actions, long id, String title, String desc)126     private static GuidedAction addEditableAction(Context context, List<GuidedAction> actions,
127             long id, String title, String desc) {
128         GuidedAction action;
129         actions.add(action = new GuidedAction.Builder(context)
130                 .id(id)
131                 .title(title)
132                 .description(desc)
133                 .editable(true)
134                 .icon(R.drawable.lb_ic_search_mic)
135                 .build());
136         return action;
137     }
138 
addEditableAction(List<GuidedAction> actions, long id, String title, String editTitle, String desc)139     private static GuidedAction addEditableAction(List<GuidedAction> actions, long id, String title,
140             String editTitle, String desc) {
141         GuidedAction action;
142         actions.add(action = new GuidedAction.Builder(null)
143                 .id(id)
144                 .title(title)
145                 .editTitle(editTitle)
146                 .description(desc)
147                 .editable(true)
148                 .build());
149         return action;
150     }
151 
addEditableAction(List<GuidedAction> actions, long id, String title, String editTitle, int editInputType, String desc, String editDesc)152     private static GuidedAction addEditableAction(List<GuidedAction> actions, long id, String title,
153             String editTitle, int editInputType, String desc, String editDesc) {
154         GuidedAction action;
155         actions.add(action = new GuidedAction.Builder(null)
156                 .id(id)
157                 .title(title)
158                 .editTitle(editTitle)
159                 .editInputType(editInputType)
160                 .description(desc)
161                 .editDescription(editDesc)
162                 .editable(true)
163                 .build());
164         return action;
165     }
166 
addDatePickerAction(List<GuidedAction> actions, long id, String title)167     private static GuidedDatePickerAction addDatePickerAction(List<GuidedAction> actions, long id,
168             String title) {
169         GuidedDatePickerAction action;
170         actions.add(action = new GuidedDatePickerAction.Builder(null)
171                 .id(id)
172                 .title(title)
173                 .datePickerFormat("MY")
174                 .build());
175         return action;
176     }
177 
addEditableDescriptionAction(List<GuidedAction> actions, long id, String title, String desc, String editDescription, int descriptionEditInputType)178     private static GuidedAction addEditableDescriptionAction(List<GuidedAction> actions, long id,
179             String title, String desc, String editDescription, int descriptionEditInputType) {
180         GuidedAction action;
181         actions.add(action = new GuidedAction.Builder(null)
182                 .id(id)
183                 .title(title)
184                 .description(desc)
185                 .editDescription(editDescription)
186                 .descriptionEditInputType(descriptionEditInputType)
187                 .descriptionEditable(true)
188                 .build());
189         return action;
190     }
191 
addCheckedAction(List<GuidedAction> actions, long id, String title, String desc, int checkSetId)192     private static GuidedAction addCheckedAction(List<GuidedAction> actions, long id,
193             String title, String desc, int checkSetId) {
194         GuidedAction action;
195         actions.add(action = new GuidedAction.Builder(null)
196                 .id(id)
197                 .title(title)
198                 .description(desc)
199                 .checkSetId(checkSetId)
200                 .build());
201         return action;
202     }
203 
204     public static class FirstStepFragment extends GuidedStepSupportFragment {
205 
206         @Override
onProvideTheme()207         public int onProvideTheme() {
208             return R.style.Theme_Example_Leanback_GuidedStep_First;
209         }
210 
211         @Override
onCreateGuidance(Bundle savedInstanceState)212         public Guidance onCreateGuidance(Bundle savedInstanceState) {
213             String title = getString(R.string.guidedstep_first_title);
214             String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
215             String description = getString(R.string.guidedstep_first_description);
216             final Context context = getActivity();
217             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
218                     R.drawable.ic_main_icon, context.getTheme());
219             return new Guidance(title, description, breadcrumb, icon);
220         }
221 
222         @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)223         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
224             Context context = getActivity();
225             actions.add(new GuidedAction.Builder(context)
226                     .clickAction(GuidedAction.ACTION_ID_CONTINUE)
227                     .description("Let's do it")
228                     .build());
229             actions.add(new GuidedAction.Builder(context)
230                     .id(REFRESH)
231                     .title("Refresh")
232                     .build());
233             actions.add(new GuidedAction.Builder(context)
234                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
235                     .description("Never mind")
236                     .build());
237         }
238 
239         @Override
onGuidedActionClicked(GuidedAction action)240         public void onGuidedActionClicked(GuidedAction action) {
241             FragmentManager fm = getFragmentManager();
242             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
243                 GuidedStepSupportFragment.add(fm, new SecondStepFragment(), R.id.lb_guidedstep_host);
244             } else if (action.getId() == REFRESH) {
245                 // swap actions position and change content:
246                 Context context = getActivity();
247                 ArrayList<GuidedAction> newActions = new ArrayList();
248                 newActions.add(new GuidedAction.Builder(context)
249                         .id(REFRESH)
250                         .title("Refresh done")
251                         .build());
252                 newActions.add(new GuidedAction.Builder(context)
253                         .clickAction(GuidedAction.ACTION_ID_CONTINUE)
254                         .description("Let's do it")
255                         .build());
256                 newActions.add(new GuidedAction.Builder(context)
257                         .clickAction(GuidedAction.ACTION_ID_CANCEL)
258                         .description("Never mind")
259                         .build());
260                 //setActionsDiffCallback(null);
261                 setActions(newActions);
262             } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL){
263                 finishGuidedStepSupportFragments();
264             }
265         }
266     }
267 
268     public interface NewPaymentFragmentTarget {
onNewPaymentFragmentStarted()269         void onNewPaymentFragmentStarted();
onNewPaymentAdded(int selection)270         void onNewPaymentAdded(int selection);
271     }
272 
273     static ArrayList<String> sCards = new ArrayList<String>();
274     static int sSelectedCard = -1;
275     static {
276         sCards.add("Visa-1234");
277         sCards.add("Master-4321");
278     }
279 
280     public static class NewPaymentStepFragment extends GuidedStepSupportFragment {
281 
282         NewPaymentFragmentTarget mNewPaymentTarget;
283 
284         @Override
onCreate(Bundle savedInstance)285         public void onCreate(Bundle savedInstance) {
286             super.onCreate(savedInstance);
287             Fragment targetFragment = getTargetFragment();
288             if (targetFragment instanceof NewPaymentFragmentTarget) {
289                 mNewPaymentTarget = ((NewPaymentFragmentTarget) targetFragment);
290                 mNewPaymentTarget.onNewPaymentFragmentStarted();
291             }
292         }
293 
294         @Override
onCreateGuidance(Bundle savedInstanceState)295         public Guidance onCreateGuidance(Bundle savedInstanceState) {
296             String title = getString(R.string.guidedstep_newpayment_title);
297             String breadcrumb = getString(R.string.guidedstep_newpayment_breadcrumb);
298             String description = getString(R.string.guidedstep_newpayment_description);
299             final Context context = getActivity();
300             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
301                     R.drawable.ic_main_icon, context.getTheme());
302             return new Guidance(title, description, breadcrumb, icon);
303         }
304 
305         @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)306         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
307             addEditableAction(actions, NEW_PAYMENT, "Input credit card number", "",
308                     InputType.TYPE_CLASS_NUMBER,
309                     "Input credit card number", "Input credit card number");
310             addDatePickerAction(actions, PAYMENT_EXPIRE, "Exp:");
311         }
312 
313         @Override
onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState)314         public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
315             Context context = getActivity();
316             actions.add(new GuidedAction.Builder(context).clickAction(GuidedAction.ACTION_ID_OK)
317                     .build());
318             actions.get(actions.size() - 1).setEnabled(false);
319         }
320 
321         @Override
onGuidedActionClicked(GuidedAction action)322         public void onGuidedActionClicked(GuidedAction action) {
323             if (action.getId() == GuidedAction.ACTION_ID_OK) {
324                 CharSequence desc = findActionById(NEW_PAYMENT).getDescription();
325                 String cardNumber = desc.subSequence(desc.length() - 4, desc.length()).toString();
326                 String card;
327                 if ((Integer.parseInt(cardNumber) & 1) == 0) {
328                     card = "Visa "+cardNumber;
329                 } else {
330                     card = "Master "+cardNumber;
331                 }
332                 int selection = sCards.size();
333                 sCards.add(card);
334                 if (mNewPaymentTarget != null) {
335                     mNewPaymentTarget.onNewPaymentAdded(selection);
336                 }
337                 popBackStackToGuidedStepSupportFragment(NewPaymentStepFragment.class,
338                         FragmentManager.POP_BACK_STACK_INCLUSIVE);
339             }
340         }
341 
342         @Override
onGuidedActionEditedAndProceed(GuidedAction action)343         public long onGuidedActionEditedAndProceed(GuidedAction action) {
344             if (action.getId() == NEW_PAYMENT) {
345                 CharSequence editTitle = action.getEditTitle();
346                 if (isCardNumberValid(editTitle)) {
347                     editTitle = editTitle.subSequence(editTitle.length() - 4, editTitle.length());
348                     action.setDescription("Visa XXXX-XXXX-XXXX-" + editTitle);
349                     updateOkButton(isExpDateValid(findActionById(PAYMENT_EXPIRE)));
350                     return GuidedAction.ACTION_ID_NEXT;
351                 } else if (editTitle.length() == 0) {
352                     action.setDescription("Input credit card number");
353                     updateOkButton(false);
354                     return GuidedAction.ACTION_ID_CURRENT;
355                 } else {
356                     action.setDescription("Error credit card number");
357                     updateOkButton(false);
358                     return GuidedAction.ACTION_ID_CURRENT;
359                 }
360             } else if (action.getId() == PAYMENT_EXPIRE) {
361                 updateOkButton(isExpDateValid(action) &&
362                         isCardNumberValid(findActionById(NEW_PAYMENT).getEditTitle()));
363             }
364             return GuidedAction.ACTION_ID_NEXT;
365         }
366 
isCardNumberValid(CharSequence number)367         boolean isCardNumberValid(CharSequence number) {
368             return TextUtils.isDigitsOnly(number) && number.length() == 16;
369         }
370 
isExpDateValid(GuidedAction action)371         boolean isExpDateValid(GuidedAction action) {
372             long date = ((GuidedDatePickerAction) action).getDate();
373             Calendar c = Calendar.getInstance();
374             c.setTimeInMillis(date);
375             return Calendar.getInstance().before(c);
376         }
377 
updateOkButton(boolean enabled)378         void updateOkButton(boolean enabled) {
379             findButtonActionById(GuidedAction.ACTION_ID_OK).setEnabled(enabled);
380             notifyButtonActionChanged(findButtonActionPositionById(GuidedAction.ACTION_ID_OK));
381         }
382     }
383 
384     public static class SecondStepFragment extends GuidedStepSupportFragment
385             implements NewPaymentFragmentTarget {
386 
387 
388         boolean mExpandPaymentListInOnCreateView;
389 
390         @Override
onNewPaymentAdded(int selection)391         public void onNewPaymentAdded(int selection) {
392             // if a new payment is added, we dont need expand the sub actions list.
393             mExpandPaymentListInOnCreateView = false;
394             sSelectedCard = selection;
395             updatePaymentAction(findActionById(PAYMENT));
396             findButtonActionById(GuidedAction.ACTION_ID_CONTINUE).setEnabled(sSelectedCard != -1);
397         }
398 
399         @Override
onNewPaymentFragmentStarted()400         public void onNewPaymentFragmentStarted() {
401             // if a new payment fragment is opened, when come back we should expand the payment
402             // sub actions list unless user created a new payment in onNewPaymentAdded
403             mExpandPaymentListInOnCreateView = true;
404         }
405 
406         @Override
onCreateActionsStylist()407         public GuidedActionsStylist onCreateActionsStylist() {
408             return new GuidedActionsStylist() {
409                 @Override
410                 protected void setupImeOptions(GuidedActionsStylist.ViewHolder vh,
411                         GuidedAction action) {
412                     if (action.getId() == PASSWORD) {
413                         vh.getEditableDescriptionView().setImeActionLabel("Confirm!",
414                                 EditorInfo.IME_ACTION_DONE);
415                     } else {
416                         super.setupImeOptions(vh, action);
417                     }
418                 }
419             };
420         }
421 
422         @Override
423         public Guidance onCreateGuidance(Bundle savedInstanceState) {
424             String title = getString(R.string.guidedstep_second_title);
425             String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
426             String description = getString(R.string.guidedstep_second_description);
427             final Context context = getActivity();
428             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
429                     R.drawable.ic_main_icon, context.getTheme());
430             return new Guidance(title, description, breadcrumb, icon);
431         }
432 
433         @Override
434         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
435             addEditableAction(getActivity(), actions, FIRST_NAME, "Pat", "Your first name");
436             addEditableAction(getActivity(), actions, LAST_NAME, "Smith", "Your last name");
437             List<GuidedAction> subActions = new ArrayList<GuidedAction>();
438             updatePaymentAction(addAction(actions, PAYMENT, "Select Payment", "", subActions));
439             addEditableDescriptionAction(actions, PASSWORD, "Password", "", "",
440                     InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
441         }
442 
443         @Override
444         public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
445             actions.add(new GuidedAction.Builder(getActivity())
446                     .clickAction(GuidedAction.ACTION_ID_CONTINUE)
447                     .description("Continue")
448                     .enabled(isPasswordValid() && isPaymentValid())
449                     .build());
450         }
451 
452         @Override
453         public void onGuidedActionClicked(GuidedAction action) {
454             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
455                 FragmentManager fm = getFragmentManager();
456                 GuidedStepSupportFragment.add(fm, new ThirdStepFragment(), R.id.lb_guidedstep_host);
457             }
458         }
459 
460         void updatePaymentAction(GuidedAction paymentAction) {
461             List<GuidedAction> subActions = paymentAction.getSubActions();
462             subActions.clear();
463             for (int i = 0; i < sCards.size(); i++) {
464                 addCheckedAction(subActions, -1, sCards.get(i), "",
465                         GuidedAction.DEFAULT_CHECK_SET_ID);
466                 if (i == sSelectedCard) {
467                     subActions.get(i).setChecked(true);
468                 }
469             }
470             addAction(subActions, NEW_PAYMENT, "Add New Card", "");
471             paymentAction.setDescription(sSelectedCard == -1 ? "" : sCards.get(sSelectedCard));
472         }
473 
474         @Override
475         public long onGuidedActionEditedAndProceed(GuidedAction action) {
476             if (action.getId() == PASSWORD) {
477                 CharSequence password = action.getEditDescription();
478                 if (password.length() > 0) {
479                     if (isPaymentValid()) {
480                         updateContinue(true);
481                         return GuidedAction.ACTION_ID_NEXT;
482                     } else {
483                         updateContinue(false);
484                         return GuidedAction.ACTION_ID_CURRENT;
485                     }
486                 } else {
487                     updateContinue(false);
488                     return GuidedAction.ACTION_ID_CURRENT;
489                 }
490             }
491             return GuidedAction.ACTION_ID_NEXT;
492         }
493 
494         @Override
495         public boolean onSubGuidedActionClicked(GuidedAction action) {
496             if (action.isChecked()) {
497                 String payment = action.getTitle().toString();
498                 for (int i = 0; i < sCards.size(); i++) {
499                     if (payment.equals(sCards.get(i))) {
500                         sSelectedCard = i;
501                         findActionById(PAYMENT).setDescription(payment);
502                         notifyActionChanged(findActionPositionById(PAYMENT));
503                         updateContinue(isPasswordValid());
504                         break;
505                     }
506                 }
507                 return true;
508             } else {
509                 FragmentManager fm = getFragmentManager();
510                 NewPaymentStepFragment newPaymentFragment = new NewPaymentStepFragment();
511                 newPaymentFragment.setTargetFragment(this, 0);
512                 GuidedStepSupportFragment.add(fm, newPaymentFragment, R.id.lb_guidedstep_host);
513                 return false;
514             }
515         }
516 
517         @Override
518         public View onCreateView(LayoutInflater inflater, ViewGroup container,
519                 Bundle savedInstanceState) {
520             View view = super.onCreateView(inflater, container, savedInstanceState);
521             if (mExpandPaymentListInOnCreateView) {
522                 expandAction(findActionById(PAYMENT), false);
523             }
524             return view;
525         }
526 
527         boolean isPaymentValid() {
528             CharSequence paymentType = findActionById(PAYMENT).getDescription();
529             return (paymentType.length() >= 4 &&
530                     paymentType.subSequence(0, 4).toString().equals("Visa")) ||
531                     (paymentType.length() >= 6 &&
532                     paymentType.subSequence(0, 6).toString().equals("Master"));
533         }
534 
535         boolean isPasswordValid() {
536             return findActionById(PASSWORD).getEditDescription().length() > 0;
537         }
538 
539         void updateContinue(boolean enabled) {
540             findButtonActionById(GuidedAction.ACTION_ID_CONTINUE).setEnabled(enabled);
541             notifyButtonActionChanged(findButtonActionPositionById(
542                     GuidedAction.ACTION_ID_CONTINUE));
543         }
544     }
545 
546     public static class ThirdStepFragment extends GuidedStepSupportFragment {
547 
548         private long mSelectedOption = DEFAULT_OPTION;
549 
550         @Override
551         public Guidance onCreateGuidance(Bundle savedInstanceState) {
552             String title = getString(R.string.guidedstep_third_title);
553             String breadcrumb = getString(R.string.guidedstep_third_breadcrumb);
554             String description = getString(R.string.guidedstep_third_description);
555             final Context context = getActivity();
556             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
557                     R.drawable.ic_main_icon, context.getTheme());
558             return new Guidance(title, description, breadcrumb, icon);
559         }
560 
561         @Override
562         public GuidanceStylist onCreateGuidanceStylist() {
563             return new GuidanceStylist() {
564                 @Override
565                 public int onProvideLayoutId() {
566                     return R.layout.guidedstep_second_guidance;
567                 }
568             };
569         }
570 
571         @Override
572         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
573             String desc = "The description can be quite long as well.  "
574                     + "Just be sure to set multilineDescription to true in the GuidedAction."
575                     + "For testing purpose we make this line even longer since "
576                     + "multilineDescriptionMinLines will be set to 2.";
577             actions.add(new GuidedAction.Builder(getActivity())
578                     .title("Note that Guided Actions can have titles that are quite long.")
579                     .description(desc)
580                     .multilineDescription(true)
581                     .infoOnly(true)
582                     .enabled(true)
583                     .focusable(false)
584                     .build());
585             for (int i = 0; i < OPTION_NAMES.length; i++) {
586                 addCheckedAction(actions, RADIO_ID_BASE + i, OPTION_NAMES[i],
587                         OPTION_DESCRIPTIONS[i], GuidedAction.DEFAULT_CHECK_SET_ID);
588                 if (i == DEFAULT_OPTION) {
589                     actions.get(actions.size() -1).setChecked(true);
590                 }
591             }
592             for (int i = 0; i < OPTION_NAMES.length; i++) {
593                 addCheckedAction(actions, CHECKBOX_ID_BASE + i, OPTION_NAMES[i],
594                         OPTION_DESCRIPTIONS[i], GuidedAction.CHECKBOX_CHECK_SET_ID);
595             }
596         }
597 
598         @Override
599         public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
600             actions.add(new GuidedAction.Builder(getActivity())
601                     .clickAction(GuidedAction.ACTION_ID_CONTINUE)
602                     .build());
603         }
604 
605         @Override
606         public void onGuidedActionClicked(GuidedAction action) {
607             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
608                 FragmentManager fm = getFragmentManager();
609                 FourthStepFragment f = new FourthStepFragment();
610                 Bundle arguments = new Bundle();
611                 arguments.putLong(FourthStepFragment.EXTRA_OPTION, mSelectedOption);
612                 f.setArguments(arguments);
613                 GuidedStepSupportFragment.add(fm, f, R.id.lb_guidedstep_host);
614             } else if (action.getCheckSetId() == GuidedAction.DEFAULT_CHECK_SET_ID) {
615                 mSelectedOption = action.getId();
616             }
617         }
618 
619     }
620 
621     public static class FourthStepFragment extends GuidedStepSupportFragment {
622         public static final String EXTRA_OPTION = "extra_option";
623 
624         public FourthStepFragment() {
625         }
626 
627         public long getOption() {
628             Bundle b = getArguments();
629             if (b == null) return 0;
630             return b.getLong(EXTRA_OPTION, 0);
631         }
632 
633         @Override
634         public Guidance onCreateGuidance(Bundle savedInstanceState) {
635             String title = getString(R.string.guidedstep_fourth_title);
636             String breadcrumb = getString(R.string.guidedstep_fourth_breadcrumb);
637             String description = "You chose: " + OPTION_NAMES[(int) getOption()];
638             final Context context = getActivity();
639             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
640                     R.drawable.ic_main_icon, context.getTheme());
641             return new Guidance(title, description, breadcrumb, icon);
642         }
643 
644         @Override
645         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
646             actions.add(new GuidedAction.Builder(getActivity())
647                     .clickAction(GuidedAction.ACTION_ID_FINISH)
648                     .description("All Done...")
649                     .build());
650             addAction(actions, BACK, "Start Over", "Let's try this again...");
651         }
652 
653         @Override
654         public void onGuidedActionClicked(GuidedAction action) {
655             if (action.getId() == GuidedAction.ACTION_ID_FINISH) {
656                 finishGuidedStepSupportFragments();
657             } else if (action.getId() == BACK) {
658                 // pop 4, 3, 2
659                 popBackStackToGuidedStepSupportFragment(SecondStepFragment.class,
660                         FragmentManager.POP_BACK_STACK_INCLUSIVE);
661             }
662         }
663 
664     }
665 
666 }
667