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.VisibleForTesting; 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 setting dns1. 38 */ 39 public class Dns1State implements State { 40 private final FragmentActivity mActivity; 41 private Fragment mFragment; 42 43 public Dns1State(FragmentActivity activity) { 44 mActivity = activity; 45 } 46 47 @Override 48 public void processForward() { 49 mFragment = new Dns1Fragment(); 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 Dns1Fragment(); 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 dns1. 72 */ 73 public static class Dns1Fragment extends WifiConnectivityGuidedStepFragment { 74 @VisibleForTesting 75 StateMachine mStateMachine; 76 @VisibleForTesting 77 AdvancedOptionsFlowInfo mAdvancedOptionsFlowInfo; 78 private GuidedAction mAction; 79 80 @Override 81 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 82 return new GuidanceStylist.Guidance( 83 getString(R.string.title_wifi_dns1), 84 getString(R.string.wifi_dns1_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(List<GuidedAction> actions, Bundle savedInstanceState) { 112 String title = getString(R.string.wifi_dns1_hint); 113 if (mAdvancedOptionsFlowInfo.containsPage(AdvancedOptionsFlowInfo.DNS1)) { 114 title = mAdvancedOptionsFlowInfo.get(AdvancedOptionsFlowInfo.DNS1); 115 } else if (mAdvancedOptionsFlowInfo.getInitialDns(0) != null) { 116 title = mAdvancedOptionsFlowInfo.getInitialDns(0).getHostAddress(); 117 } 118 119 Context context = getActivity(); 120 mAction = new GuidedAction.Builder(context) 121 .title(title) 122 .editable(true) 123 .id(GuidedAction.ACTION_ID_CONTINUE) 124 .build(); 125 actions.add(mAction); 126 } 127 128 @Override 129 public void onViewCreated(View view, Bundle savedInstanceState) { 130 openInEditMode(mAction); 131 } 132 133 @Override 134 public long onGuidedActionEditedAndProceed(GuidedAction action) { 135 if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) { 136 mAdvancedOptionsFlowInfo.put(AdvancedOptionsFlowInfo.DNS1, 137 action.getTitle()); 138 mStateMachine.getListener().onComplete(StateMachine.CONTINUE); 139 } 140 return action.getId(); 141 } 142 } 143 } 144