1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.leanback;
18 
19 import android.app.Activity;
20 import android.graphics.drawable.Drawable;
21 import android.os.Bundle;
22 
23 import androidx.leanback.app.GuidedStepFragment;
24 import androidx.leanback.widget.GuidanceStylist;
25 import androidx.leanback.widget.GuidanceStylist.Guidance;
26 import androidx.leanback.widget.GuidedAction;
27 
28 import java.util.List;
29 
30 /**
31  * Activity that showcases different aspects of GuidedStepFragments.
32  */
33 public class DetailsPresenterSelectionActivity extends Activity {
34 
35     private static final int OPTION_CHECK_SET_ID = 10;
36 
37     private static final long ACTION_ID_SWITCH_LEGACY_ON = 10000;
38     private static final long ACTION_ID_SWITCH_LEGACY_OFF = 10001;
39 
40     public static boolean USE_LEGACY_PRESENTER = false;
41 
42     private static final String[] OPTION_NAMES = { "Use new details presenter", "Use legacy details presenter" };
43     private static final String[] OPTION_DESCRIPTIONS = { "Use new details presenter",
44             "Use legacy details presenter"};
45     private static final long[] OPTION_IDS = {ACTION_ID_SWITCH_LEGACY_OFF, ACTION_ID_SWITCH_LEGACY_ON};
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         GuidedStepFragment.addAsRoot(this, new SetupFragment(), android.R.id.content);
51     }
52 
addCheckedAction(List<GuidedAction> actions, long id, String title, String desc, boolean checked)53     private static void addCheckedAction(List<GuidedAction> actions,
54             long id, String title, String desc, boolean checked) {
55         actions.add(new GuidedAction.Builder(null)
56                 .title(title)
57                 .description(desc)
58                 .id(id)
59                 .checkSetId(OPTION_CHECK_SET_ID)
60                 .checked(checked)
61                 .build());
62     }
63 
64     /**
65      * Fragment hosted in DetailsPresenterSelectionActivity.
66      */
67     public static class SetupFragment extends GuidedStepFragment {
68 
69         @Override
onCreateGuidance(Bundle savedInstanceState)70         public Guidance onCreateGuidance(Bundle savedInstanceState) {
71             String title = getString(R.string.guidedstep_second_title);
72             String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
73             String description = getString(R.string.guidedstep_second_description);
74             Drawable icon = getActivity().getResources().getDrawable(R.drawable.ic_main_icon);
75             return new Guidance(title, description, breadcrumb, icon);
76         }
77 
78         @Override
onCreateGuidanceStylist()79         public GuidanceStylist onCreateGuidanceStylist() {
80             return new GuidanceStylist() {
81                 @Override
82                 public int onProvideLayoutId() {
83                     return R.layout.guidedstep_second_guidance;
84                 }
85             };
86         }
87 
88         @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)89         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
90             for (int i = 0; i < OPTION_NAMES.length; i++) {
91                 boolean checked = false;
92                 if (OPTION_IDS[i] == ACTION_ID_SWITCH_LEGACY_ON) {
93                     if (USE_LEGACY_PRESENTER) {
94                         checked = true;
95                     }
96                 } else if (OPTION_IDS[i] == ACTION_ID_SWITCH_LEGACY_OFF) {
97                     if (!USE_LEGACY_PRESENTER) {
98                         checked = true;
99                     }
100                 }
101                 addCheckedAction(actions, OPTION_IDS[i], OPTION_NAMES[i],
102                         OPTION_DESCRIPTIONS[i], checked);
103             }
104         }
105 
106         @Override
onGuidedActionClicked(GuidedAction action)107         public void onGuidedActionClicked(GuidedAction action) {
108             if (action.getId() == ACTION_ID_SWITCH_LEGACY_ON) {
109                 USE_LEGACY_PRESENTER = action.isChecked();
110             } else if (action.getId() == ACTION_ID_SWITCH_LEGACY_OFF) {
111                 USE_LEGACY_PRESENTER = !action.isChecked();
112             }
113             getActivity().finish();
114         }
115 
116     }
117 
118 }
119