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 entering proxy port.
     37  */
     38 public class ProxyPortState implements State {
     39     private final FragmentActivity mActivity;
     40     private Fragment mFragment;
     41 
     42     public ProxyPortState(FragmentActivity activity) {
     43         mActivity = activity;
     44     }
     45 
     46     @Override
     47     public void processForward() {
     48         mFragment = new ProxyPortFragment();
     49         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
     50         listener.onFragmentChange(mFragment, true);
     51     }
     52 
     53     @Override
     54     public void processBackward() {
     55         mFragment = new ProxyPortFragment();
     56         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
     57         listener.onFragmentChange(mFragment, false);
     58     }
     59 
     60     @Override
     61     public Fragment getFragment() {
     62         return mFragment;
     63     }
     64 
     65     /**
     66      * The fragment that needs user to enter proxy port.
     67      */
     68     public static class ProxyPortFragment extends WifiConnectivityGuidedStepFragment {
     69         private StateMachine mStateMachine;
     70         private AdvancedOptionsFlowInfo mAdvancedOptionsFlowInfo;
     71         private GuidedAction mAction;
     72 
     73         @Override
     74         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
     75             String title = getString(R.string.title_wifi_proxy_port);
     76             return new GuidanceStylist.Guidance(
     77                     title,
     78                     getString(R.string.proxy_port_description),
     79                     null,
     80                     null);
     81         }
     82 
     83         @Override
     84         public void onCreate(Bundle savedInstanceState) {
     85             mAdvancedOptionsFlowInfo = ViewModelProviders
     86                     .of(getActivity())
     87                     .get(AdvancedOptionsFlowInfo.class);
     88             mStateMachine = ViewModelProviders
     89                     .of(getActivity())
     90                     .get(StateMachine.class);
     91             super.onCreate(savedInstanceState);
     92         }
     93 
     94         @Override
     95         public GuidedActionsStylist onCreateActionsStylist() {
     96             return new GuidedActionsStylist() {
     97                 @Override
     98                 public int onProvideItemLayoutId() {
     99                     return R.layout.setup_text_input_item;
    100                 }
    101             };
    102         }
    103 
    104         @Override
    105         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
    106             String title = getString(R.string.proxy_port_hint);
    107             if (mAdvancedOptionsFlowInfo.containsPage(AdvancedOptionsFlowInfo.PROXY_PORT)) {
    108                 title = mAdvancedOptionsFlowInfo.get(AdvancedOptionsFlowInfo.PROXY_PORT);
    109             }  else if (mAdvancedOptionsFlowInfo.getInitialProxyInfo() != null) {
    110                 title = Integer.toString(mAdvancedOptionsFlowInfo.getInitialProxyInfo().getPort());
    111             }
    112 
    113             Context context = getActivity();
    114             mAction = new GuidedAction.Builder(context)
    115                     .title(title)
    116                     .editable(true)
    117                     .id(GuidedAction.ACTION_ID_CONTINUE)
    118                     .build();
    119             actions.add(mAction);
    120         }
    121 
    122         @Override
    123         public void onViewCreated(View view, Bundle savedInstanceState) {
    124             openInEditMode(mAction);
    125         }
    126 
    127         @Override
    128         public long onGuidedActionEditedAndProceed(GuidedAction action) {
    129             mAdvancedOptionsFlowInfo.put(AdvancedOptionsFlowInfo.PROXY_PORT,
    130                     action.getTitle());
    131             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
    132                 mStateMachine.getListener().onComplete(StateMachine.CONTINUE);
    133             }
    134             return action.getId();
    135         }
    136     }
    137 }
    138