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