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