Home | History | Annotate | Download | only in wizard
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package android.support.v17.leanback.supportleanbackshowcase.app.wizard;
     16 
     17 import android.app.FragmentManager;
     18 import android.graphics.Color;
     19 import android.graphics.drawable.Drawable;
     20 import android.os.Bundle;
     21 import android.support.annotation.NonNull;
     22 import android.support.v17.leanback.app.GuidedStepFragment;
     23 import android.support.v17.leanback.supportleanbackshowcase.R;
     24 import android.support.v17.leanback.widget.GuidanceStylist;
     25 import android.support.v17.leanback.widget.GuidedAction;
     26 import android.support.v17.leanback.widget.GuidedActionsStylist;
     27 import android.widget.Toast;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 /**
     33  * Displays the second screen of the rental wizard which requires the user to confirm his purchase.
     34  */
     35 public class WizardExample2ndStepFragment extends WizardExampleBaseStepFragment {
     36 
     37     private static final String ARG_HD = "hd";
     38     private static final int ACTION_ID_CONFIRM = 1;
     39     private static final int ACTION_ID_PAYMENT_METHOD = ACTION_ID_CONFIRM + 1;
     40     private static final int ACTION_ID_NEW_PAYMENT = ACTION_ID_PAYMENT_METHOD + 1;
     41 
     42     protected static ArrayList<String> sCards = new ArrayList();
     43     protected static int sSelectedCard = -1;
     44 
     45     static {
     46         sCards.add("Visa-1234");
     47         sCards.add("Master-4321");
     48     }
     49 
     50 
     51     public static GuidedStepFragment build(boolean hd, WizardExampleBaseStepFragment previousFragment) {
     52         GuidedStepFragment fragment = new WizardExample2ndStepFragment();
     53         // Reuse the same arguments this fragment was given.
     54         Bundle args = previousFragment.getArguments();
     55         args.putBoolean(ARG_HD, hd);
     56         fragment.setArguments(args);
     57         return fragment;
     58     }
     59 
     60     @NonNull
     61     @Override
     62     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
     63         GuidanceStylist.Guidance guidance = new GuidanceStylist.Guidance(mMovie.getTitle(),
     64                 getString(R.string.wizard_example_rental_period),
     65                 mMovie.getBreadcrump(), null);
     66         return guidance;
     67 
     68     }
     69 
     70     @Override
     71     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
     72         boolean rentHighDefinition = getArguments().getBoolean(ARG_HD);
     73 
     74         GuidedAction action = new GuidedAction.Builder(getActivity())
     75                 .id(ACTION_ID_CONFIRM)
     76                 .title(R.string.wizard_example_rent)
     77                 .description(rentHighDefinition ? mMovie.getPriceHd() : mMovie.getPriceSd())
     78                 .editable(false)
     79                 .build();
     80         action.setEnabled(false);
     81         actions.add(action);
     82         List<GuidedAction> subActions = new ArrayList();
     83         action = new GuidedAction.Builder(getActivity())
     84                 .id(ACTION_ID_PAYMENT_METHOD)
     85                 .title(R.string.wizard_example_payment_method)
     86                 .editTitle("")
     87                 .description(R.string.wizard_example_input_credit)
     88                 .subActions(subActions)
     89                 .build();
     90         actions.add(action);
     91     }
     92 
     93     @Override
     94     public void onResume() {
     95         super.onResume();
     96         GuidedAction payment = findActionById(ACTION_ID_PAYMENT_METHOD);
     97 
     98         List<GuidedAction> paymentSubActions = payment.getSubActions();
     99         paymentSubActions.clear();
    100         for (int i = 0; i < sCards.size(); i++) {
    101             paymentSubActions.add(new GuidedAction.Builder(getActivity())
    102                             .title(sCards.get(i))
    103                             .description("")
    104                             .checkSetId(GuidedAction.DEFAULT_CHECK_SET_ID)
    105                             .build()
    106             );
    107         }
    108         paymentSubActions.add(new GuidedAction.Builder(getActivity())
    109                 .id(ACTION_ID_NEW_PAYMENT)
    110                 .title("Add New Card")
    111                 .description("")
    112                 .editable(false)
    113                 .build()
    114         );
    115         if ( sSelectedCard >= 0 && sSelectedCard < sCards.size() ) {
    116             payment.setDescription(sCards.get(sSelectedCard));
    117             findActionById(ACTION_ID_CONFIRM).setEnabled(true);
    118         } else
    119             findActionById(ACTION_ID_CONFIRM).setEnabled(false);
    120         notifyActionChanged(findActionPositionById(ACTION_ID_CONFIRM));
    121     }
    122 
    123     @Override
    124     public boolean onSubGuidedActionClicked(GuidedAction action) {
    125 
    126         if (action.isChecked()) {
    127             String payment = action.getTitle().toString();
    128             if ( (sSelectedCard = sCards.indexOf(payment)) != -1 ) {
    129                 findActionById(ACTION_ID_PAYMENT_METHOD).setDescription(payment);
    130                 notifyActionChanged(findActionPositionById(ACTION_ID_PAYMENT_METHOD));
    131                 findActionById(ACTION_ID_CONFIRM).setEnabled(true);
    132                 notifyActionChanged(findActionPositionById(ACTION_ID_CONFIRM));
    133             }
    134             return true;
    135         } else {
    136             FragmentManager fm = getFragmentManager();
    137             GuidedStepFragment fragment = new WizardNewPaymentStepFragment();
    138             fragment.setArguments(getArguments());
    139             add(fm, fragment);
    140             return false;
    141         }
    142     }
    143 
    144     @Override
    145     public void onGuidedActionClicked(GuidedAction action) {
    146         if (ACTION_ID_CONFIRM == action.getId()) {
    147             GuidedStepFragment fragment = new WizardExample3rdStepFragment();
    148             fragment.setArguments(getArguments());
    149             add(getFragmentManager(), fragment);
    150         }
    151     }
    152 }
    153