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.content.Intent; 23 import android.graphics.drawable.Drawable; 24 import android.os.Bundle; 25 import android.support.v17.leanback.app.GuidedStepFragment; 26 import android.support.v17.leanback.widget.GuidedAction; 27 import android.support.v17.leanback.widget.GuidanceStylist; 28 import android.support.v17.leanback.widget.GuidanceStylist.Guidance; 29 30 import java.util.List; 31 import java.util.ArrayList; 32 33 /** 34 * Activity that showcases different aspects of GuidedStepFragments. 35 */ 36 public class DetailsPresenterSelectionActivity extends Activity { 37 38 private static final int OPTION_CHECK_SET_ID = 10; 39 40 private static final long ACTION_ID_SWITCH_LEGACY_ON = 10000; 41 private static final long ACTION_ID_SWITCH_LEGACY_OFF = 10001; 42 43 public static boolean USE_LEGACY_PRESENTER = false; 44 45 private static final String[] OPTION_NAMES = { "Use new details presenter", "Use legacy details presenter" }; 46 private static final String[] OPTION_DESCRIPTIONS = { "Use new details presenter", 47 "Use legacy details presenter"}; 48 private static final long[] OPTION_IDS = {ACTION_ID_SWITCH_LEGACY_OFF, ACTION_ID_SWITCH_LEGACY_ON}; 49 50 @Override onCreate(Bundle savedInstanceState)51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 GuidedStepFragment.addAsRoot(this, new SetupFragment(), android.R.id.content); 54 } 55 addAction(List<GuidedAction> actions, long id, String title, String desc)56 private static void addAction(List<GuidedAction> actions, long id, String title, String desc) { 57 actions.add(new GuidedAction.Builder() 58 .id(id) 59 .title(title) 60 .description(desc) 61 .build()); 62 } 63 addCheckedAction(List<GuidedAction> actions, Context context, long id, String title, String desc, boolean checked)64 private static void addCheckedAction(List<GuidedAction> actions, Context context, 65 long id, String title, String desc, boolean checked) { 66 actions.add(new GuidedAction.Builder() 67 .title(title) 68 .description(desc) 69 .id(id) 70 .checkSetId(OPTION_CHECK_SET_ID) 71 .checked(checked) 72 .build()); 73 } 74 75 private static class SetupFragment extends GuidedStepFragment { 76 77 @Override onCreateGuidance(Bundle savedInstanceState)78 public Guidance onCreateGuidance(Bundle savedInstanceState) { 79 String title = getString(R.string.guidedstep_second_title); 80 String breadcrumb = getString(R.string.guidedstep_second_breadcrumb); 81 String description = getString(R.string.guidedstep_second_description); 82 Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon); 83 return new Guidance(title, description, breadcrumb, icon); 84 } 85 86 @Override onCreateGuidanceStylist()87 public GuidanceStylist onCreateGuidanceStylist() { 88 return new GuidanceStylist() { 89 @Override 90 public int onProvideLayoutId() { 91 return R.layout.guidedstep_second_guidance; 92 } 93 }; 94 } 95 96 @Override onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)97 public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) { 98 for (int i = 0; i < OPTION_NAMES.length; i++) { 99 boolean checked = false; 100 if (OPTION_IDS[i] == ACTION_ID_SWITCH_LEGACY_ON) { 101 if (USE_LEGACY_PRESENTER) { 102 checked = true; 103 } 104 } else if (OPTION_IDS[i] == ACTION_ID_SWITCH_LEGACY_OFF) { 105 if (!USE_LEGACY_PRESENTER) { 106 checked = true; 107 } 108 } 109 addCheckedAction(actions, getActivity(), OPTION_IDS[i], OPTION_NAMES[i], 110 OPTION_DESCRIPTIONS[i], checked); 111 } 112 } 113 114 @Override onGuidedActionClicked(GuidedAction action)115 public void onGuidedActionClicked(GuidedAction action) { 116 if (action.getId() == ACTION_ID_SWITCH_LEGACY_ON) { 117 USE_LEGACY_PRESENTER = action.isChecked(); 118 } else if (action.getId() == ACTION_ID_SWITCH_LEGACY_OFF) { 119 USE_LEGACY_PRESENTER = !action.isChecked(); 120 } 121 getActivity().finish(); 122 } 123 124 } 125 126 } 127