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 ip address. 37 */ 38 public class IpAddressState implements State { 39 private final FragmentActivity mActivity; 40 private Fragment mFragment; 41 42 public IpAddressState(FragmentActivity activity) { 43 mActivity = activity; 44 } 45 46 @Override 47 public void processForward() { 48 mFragment = new IpAddressFragment(); 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 IpAddressFragment(); 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 in advanced flow that needs to enter ip address. 71 */ 72 public static class IpAddressFragment 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 String title = getString(R.string.title_wifi_ip_address); 80 return new GuidanceStylist.Guidance( 81 title, 82 getString(R.string.wifi_ip_address_description), 83 null, 84 null); 85 } 86 87 @Override 88 public void onCreate(Bundle savedInstanceState) { 89 mAdvancedOptionsFlowInfo = ViewModelProviders 90 .of(getActivity()) 91 .get(AdvancedOptionsFlowInfo.class); 92 mStateMachine = ViewModelProviders 93 .of(getActivity()) 94 .get(StateMachine.class); 95 super.onCreate(savedInstanceState); 96 } 97 98 @Override 99 public GuidedActionsStylist onCreateActionsStylist() { 100 return new GuidedActionsStylist() { 101 @Override 102 public int onProvideItemLayoutId() { 103 return R.layout.setup_text_input_item; 104 } 105 }; 106 } 107 108 @Override 109 public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) { 110 Context context = getActivity(); 111 String title = getString(R.string.wifi_ip_address_hint); 112 if (mAdvancedOptionsFlowInfo.containsPage(AdvancedOptionsFlowInfo.IP_ADDRESS)) { 113 title = mAdvancedOptionsFlowInfo.get(AdvancedOptionsFlowInfo.IP_ADDRESS); 114 } else if (mAdvancedOptionsFlowInfo.getInitialLinkAddress() != null) { 115 title = mAdvancedOptionsFlowInfo.getInitialLinkAddress() 116 .getAddress().getHostAddress(); 117 } 118 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.IP_ADDRESS, 136 action.getTitle()); 137 mStateMachine.getListener().onComplete(StateMachine.CONTINUE); 138 } 139 return action.getId(); 140 } 141 } 142 } 143