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.v4.app.Fragment;
25import android.support.v4.app.FragmentActivity;
26
27import com.android.tv.settings.R;
28import com.android.tv.settings.connectivity.util.AdvancedOptionsFlowUtil;
29import com.android.tv.settings.connectivity.util.State;
30import com.android.tv.settings.connectivity.util.StateMachine;
31
32import java.util.List;
33
34/**
35 * State responsible for showing first page of advanced flow.
36 */
37public class AdvancedOptionsState implements State {
38    private final FragmentActivity mActivity;
39    private Fragment mFragment;
40
41    public AdvancedOptionsState(FragmentActivity activity) {
42        mActivity = activity;
43    }
44
45    @Override
46    public void processForward() {
47        mFragment = new AdvancedOptionsFragment();
48        FragmentChangeListener listener = (FragmentChangeListener) mActivity;
49        if (listener != null) {
50            listener.onFragmentChange(mFragment, true);
51        }
52    }
53
54    @Override
55    public void processBackward() {
56        mFragment = new AdvancedOptionsFragment();
57        FragmentChangeListener listener = (FragmentChangeListener) mActivity;
58        if (listener != null) {
59            listener.onFragmentChange(mFragment, false);
60        }
61    }
62
63    @Override
64    public Fragment getFragment() {
65        return mFragment;
66    }
67
68    /**
69     * Fragment that makes user to decide whether to use advanced flow to set up network.
70     */
71    public static class AdvancedOptionsFragment extends WifiConnectivityGuidedStepFragment {
72        private StateMachine mStateMachine;
73        private AdvancedOptionsFlowInfo mAdvancedOptionsFlowInfo;
74
75        @Override
76        public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
77            String title = getString(
78                    R.string.title_wifi_advanced_options,
79                    mAdvancedOptionsFlowInfo.getPrintableSsid()
80            );
81            return new GuidanceStylist.Guidance(title, null, null, null);
82        }
83
84        @Override
85        public void onCreate(Bundle savedInstanceState) {
86            mAdvancedOptionsFlowInfo = ViewModelProviders
87                    .of(getActivity())
88                    .get(AdvancedOptionsFlowInfo.class);
89            mStateMachine = ViewModelProviders
90                    .of(getActivity())
91                    .get(StateMachine.class);
92            super.onCreate(savedInstanceState);
93        }
94
95        @Override
96        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
97            Context context = getActivity();
98            actions.add(new GuidedAction.Builder(context)
99                    .title(R.string.wifi_action_advanced_no)
100                    .id(GuidedAction.ACTION_ID_NO)
101                    .build());
102            actions.add(new GuidedAction.Builder(context)
103                    .title(R.string.wifi_action_advanced_yes)
104                    .id(GuidedAction.ACTION_ID_YES)
105                    .build());
106        }
107
108        @Override
109        public void onGuidedActionClicked(GuidedAction action) {
110            if (action.getId() == GuidedAction.ACTION_ID_NO) {
111                AdvancedOptionsFlowUtil.processProxySettings(getActivity());
112                AdvancedOptionsFlowUtil.processIpSettings(getActivity());
113                mStateMachine.getListener().onComplete(StateMachine.ADVANCED_FLOW_COMPLETE);
114            } else if (action.getId() == GuidedAction.ACTION_ID_YES) {
115                mStateMachine.getListener().onComplete(StateMachine.CONTINUE);
116            }
117        }
118    }
119}
120