Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2017 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.android.tv.settings.connectivity.setup;
     18 
     19 import android.arch.lifecycle.ViewModelProviders;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.support.annotation.NonNull;
     23 import android.support.v17.leanback.widget.GuidanceStylist;
     24 import android.support.v17.leanback.widget.GuidedAction;
     25 import android.support.v17.leanback.widget.GuidedActionsStylist;
     26 import android.support.v4.app.Fragment;
     27 import android.support.v4.app.FragmentActivity;
     28 import android.view.View;
     29 
     30 import com.android.tv.settings.R;
     31 import com.android.tv.settings.connectivity.util.State;
     32 import com.android.tv.settings.connectivity.util.StateMachine;
     33 
     34 import java.util.List;
     35 
     36 /**
     37  * State responsible for entering gateway.
     38  */
     39 public class GatewayState implements State {
     40     private final FragmentActivity mActivity;
     41     private Fragment mFragment;
     42 
     43     public GatewayState(FragmentActivity activity) {
     44         mActivity = activity;
     45     }
     46 
     47     @Override
     48     public void processForward() {
     49         mFragment = new GatewayFragment();
     50         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
     51         if (listener != null) {
     52             listener.onFragmentChange(mFragment, true);
     53         }
     54     }
     55 
     56     @Override
     57     public void processBackward() {
     58         mFragment = new GatewayFragment();
     59         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
     60         if (listener != null) {
     61             listener.onFragmentChange(mFragment, false);
     62         }
     63     }
     64 
     65     @Override
     66     public Fragment getFragment() {
     67         return mFragment;
     68     }
     69 
     70     /**
     71      * Fragment that needs user to enter gateway.
     72      */
     73     public static class GatewayFragment extends WifiConnectivityGuidedStepFragment {
     74         private StateMachine mStateMachine;
     75         private AdvancedOptionsFlowInfo mAdvancedOptionsFlowInfo;
     76         private GuidedAction mAction;
     77 
     78         @NonNull
     79         @Override
     80         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
     81             String title = getString(R.string.title_wifi_gateway);
     82             return new GuidanceStylist.Guidance(
     83                     title,
     84                     getString(R.string.wifi_gateway_description),
     85                     null,
     86                     null);
     87         }
     88 
     89         @Override
     90         public void onCreate(Bundle savedInstanceState) {
     91             mAdvancedOptionsFlowInfo = ViewModelProviders
     92                     .of(getActivity())
     93                     .get(AdvancedOptionsFlowInfo.class);
     94             mStateMachine = ViewModelProviders
     95                     .of(getActivity())
     96                     .get(StateMachine.class);
     97             super.onCreate(savedInstanceState);
     98         }
     99 
    100         @Override
    101         public GuidedActionsStylist onCreateActionsStylist() {
    102             return new GuidedActionsStylist() {
    103                 @Override
    104                 public int onProvideItemLayoutId() {
    105                     return R.layout.setup_text_input_item;
    106                 }
    107             };
    108         }
    109 
    110         @Override
    111         public void onCreateActions(@NonNull List<GuidedAction> actions,
    112                 Bundle savedInstanceState) {
    113             String title = getString(R.string.wifi_gateway_hint);
    114             if (mAdvancedOptionsFlowInfo.containsPage(AdvancedOptionsFlowInfo.GATEWAY)) {
    115                 title = mAdvancedOptionsFlowInfo.get(AdvancedOptionsFlowInfo.GATEWAY);
    116             } else if (mAdvancedOptionsFlowInfo.getInitialGateway() != null) {
    117                 title = mAdvancedOptionsFlowInfo.getInitialGateway().getHostAddress();
    118             }
    119 
    120             Context context = getActivity();
    121             mAction = new GuidedAction.Builder(context)
    122                     .title(title)
    123                     .editable(true)
    124                     .id(GuidedAction.ACTION_ID_CONTINUE)
    125                     .build();
    126             actions.add(mAction);
    127         }
    128 
    129         @Override
    130         public void onViewCreated(View view, Bundle savedInstanceState) {
    131             openInEditMode(mAction);
    132         }
    133 
    134         @Override
    135         public long onGuidedActionEditedAndProceed(GuidedAction action) {
    136             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
    137                 mAdvancedOptionsFlowInfo.put(AdvancedOptionsFlowInfo.GATEWAY,
    138                         action.getTitle());
    139                 mStateMachine.getListener().onComplete(StateMachine.CONTINUE);
    140             }
    141             return action.getId();
    142         }
    143     }
    144 }
    145