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
17package com.android.tv.settings.connectivity.setup;
18
19import android.arch.lifecycle.ViewModelProviders;
20import android.content.Context;
21import android.os.Bundle;
22import android.support.v17.leanback.widget.GuidanceStylist;
23import android.support.v17.leanback.widget.GuidedAction;
24import android.support.v17.leanback.widget.GuidedActionsStylist;
25import android.support.v4.app.Fragment;
26import android.support.v4.app.FragmentActivity;
27import android.view.View;
28
29import com.android.tv.settings.R;
30import com.android.tv.settings.connectivity.util.AdvancedOptionsFlowUtil;
31import com.android.tv.settings.connectivity.util.State;
32import com.android.tv.settings.connectivity.util.StateMachine;
33
34import java.util.List;
35
36/**
37 * State responsible for showing proxy bypass page.
38 */
39public class ProxyBypassState implements State {
40    private final FragmentActivity mActivity;
41    private Fragment mFragment;
42
43    public ProxyBypassState(FragmentActivity activity) {
44        mActivity = activity;
45    }
46
47    @Override
48    public void processForward() {
49        mFragment = new ProxyBypassFragment();
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 ProxyBypassFragment();
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 proxy bypass.
72     */
73    public static class ProxyBypassFragment extends WifiConnectivityGuidedStepFragment {
74        private StateMachine mStateMachine;
75        private AdvancedOptionsFlowInfo mAdvancedOptionsFlowInfo;
76        private GuidedAction mAction;
77
78        @Override
79        public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
80            return new GuidanceStylist.Guidance(
81                    getString(R.string.title_wifi_proxy_bypass),
82                    getString(R.string.proxy_exclusionlist_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            String title = getString(R.string.proxy_exclusionlist_hint);
111            if (mAdvancedOptionsFlowInfo.containsPage(AdvancedOptionsFlowInfo.PROXY_BYPASS)) {
112                title = mAdvancedOptionsFlowInfo.get(AdvancedOptionsFlowInfo.PROXY_BYPASS);
113            }  else if (mAdvancedOptionsFlowInfo.getInitialProxyInfo() != null) {
114                title = mAdvancedOptionsFlowInfo.getInitialProxyInfo().getExclusionListAsString();
115            }
116
117            Context context = getActivity();
118            mAction = new GuidedAction.Builder(context)
119                    .title(title)
120                    .editable(true)
121                    .id(GuidedAction.ACTION_ID_CONTINUE)
122                    .build();
123            actions.add(mAction);
124        }
125
126        @Override
127        public void onViewCreated(View view, Bundle savedInstanceState) {
128            openInEditMode(mAction);
129        }
130
131        @Override
132        public long onGuidedActionEditedAndProceed(GuidedAction action) {
133            if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
134                mAdvancedOptionsFlowInfo.put(AdvancedOptionsFlowInfo.PROXY_BYPASS,
135                        action.getTitle());
136                int proxySettingsResult = AdvancedOptionsFlowUtil.processProxySettings(
137                        getActivity());
138                if (proxySettingsResult == 0) {
139                    if (mAdvancedOptionsFlowInfo.isSettingsFlow()) {
140                        mStateMachine.getListener().onComplete(StateMachine.ADVANCED_FLOW_COMPLETE);
141                    } else {
142                        mStateMachine.getListener().onComplete(StateMachine.IP_SETTINGS);
143                    }
144                } else {
145                    mStateMachine.getListener().onComplete(StateMachine.PROXY_SETTINGS_INVALID);
146                }
147            }
148            return action.getId();
149        }
150    }
151}
152