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