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