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