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.app.FragmentManager;
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 import androidx.core.content.res.ResourcesCompat;
27 import androidx.leanback.app.GuidedStepFragment;
28 import androidx.leanback.widget.GuidanceStylist.Guidance;
29 import androidx.leanback.widget.GuidedAction;
30 
31 import java.util.List;
32 
33 /**
34  * Activity that showcases different aspects of GuidedStepFragments in half
35  * screen mode. This is achieved by setting the theme for this activity
36  * to {@code Theme.Example.Leanback.GuidedStep.Half}.
37  */
38 public class GuidedStepHalfScreenActivity extends Activity {
39     private static final String TAG = "leanback.GuidedStepSupportHalfScreenActivity";
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         Log.v(TAG, "onCreate");
44         super.onCreate(savedInstanceState);
45         setContentView(R.layout.guided_step_activity);
46         GuidedStepFragment.addAsRoot(this, new FirstStepFragment(), R.id.lb_guidedstep_host);
47     }
48 
49     public static class FirstStepFragment extends GuidedStepFragment {
50 
51        @Override
onCreateGuidance(Bundle savedInstanceState)52         public Guidance onCreateGuidance(Bundle savedInstanceState) {
53             String title = getString(R.string.guidedstep_first_title);
54             String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
55             String description = getString(R.string.guidedstep_first_description);
56             final Context context = getActivity();
57             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
58                     R.drawable.ic_main_icon, context.getTheme());
59             return new Guidance(title, description, breadcrumb, icon);
60         }
61 
62         @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)63         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
64             Context context = getActivity();
65             actions.add(new GuidedAction.Builder(context)
66                     .clickAction(GuidedAction.ACTION_ID_CONTINUE)
67                     .description("Just do it")
68                     .build());
69             actions.add(new GuidedAction.Builder(context)
70                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
71                     .description("Never mind")
72                     .build());
73         }
74 
FirstStepFragment()75         public FirstStepFragment() {
76             setEntranceTransitionType(GuidedStepFragment.SLIDE_FROM_BOTTOM);
77         }
78 
79         /**
80          * This fragment could be used by an activity using theme
81          * {@code Theme.Leanback.GuidedStep.Half} or something else (BrowseActivity).
82          * In order to provide a consistent half screen experience under
83          * both scenarios, we override onProvideTheme method.
84          */
85         @Override
onProvideTheme()86         public int onProvideTheme() {
87             return R.style.Theme_Example_Leanback_GuidedStep_Half;
88         }
89 
90         @Override
onGuidedActionClicked(GuidedAction action)91         public void onGuidedActionClicked(GuidedAction action) {
92             FragmentManager fm = getFragmentManager();
93             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
94                 GuidedStepFragment.add(fm, new SecondStepFragment(), R.id.lb_guidedstep_host);
95             } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL){
96                 finishGuidedStepFragments();
97             }
98         }
99     }
100 
101     public static class SecondStepFragment extends GuidedStepFragment {
102 
103         @Override
onProvideTheme()104         public int onProvideTheme() {
105             return R.style.Theme_Example_Leanback_GuidedStep_Half;
106         }
107 
108         @Override
onCreateGuidance(Bundle savedInstanceState)109         public Guidance onCreateGuidance(Bundle savedInstanceState) {
110             String title = getString(R.string.guidedstep_second_title);
111             String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
112             String description = getString(R.string.guidedstep_second_description);
113             final Context context = getActivity();
114             Drawable icon = ResourcesCompat.getDrawable(context.getResources(),
115                     R.drawable.ic_main_icon, context.getTheme());
116             return new Guidance(title, description, breadcrumb, icon);
117         }
118 
119         @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)120         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
121             Context context = getActivity();
122             actions.add(new GuidedAction.Builder(context)
123                     .clickAction(GuidedAction.ACTION_ID_FINISH)
124                     .description("Done")
125                     .build());
126             actions.add(new GuidedAction.Builder(context)
127                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
128                     .description("Never mind")
129                     .build());
130         }
131 
132         @Override
onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState)133         public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
134             actions.add(new GuidedAction.Builder(getActivity())
135                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
136                     .description("Cancel")
137                     .build());
138         }
139 
140         @Override
onGuidedActionClicked(GuidedAction action)141         public void onGuidedActionClicked(GuidedAction action) {
142             FragmentManager fm = getFragmentManager();
143             fm.popBackStack();
144         }
145     }
146 }
147