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.State;
29import com.android.tv.settings.connectivity.util.StateMachine;
30
31import java.util.List;
32
33/**
34 * State responsible for handling the failed connection.
35 */
36public class ConnectFailedState implements State {
37    private final FragmentActivity mActivity;
38    private Fragment mFragment;
39
40    public ConnectFailedState(FragmentActivity activity) {
41        mActivity = activity;
42    }
43
44    @Override
45    public void processForward() {
46        mFragment = new ConnectFailedFragment();
47        FragmentChangeListener listener = (FragmentChangeListener) mActivity;
48        if (listener != null) {
49            listener.onFragmentChange(mFragment, true);
50        }
51    }
52
53    @Override
54    public void processBackward() {
55        StateMachine stateMachine = ViewModelProviders.of(mActivity).get(StateMachine.class);
56        stateMachine.back();
57    }
58
59    @Override
60    public Fragment getFragment() {
61        return mFragment;
62    }
63
64    /**
65     * Fragment that notifies the user the connection to network is failed.
66     */
67    public static class ConnectFailedFragment extends WifiConnectivityGuidedStepFragment {
68        private static final int ACTION_ID_TRY_AGAIN = 100001;
69        private static final int ACTION_ID_VIEW_AVAILABLE_NETWORK = 100002;
70        private StateMachine mStateMachine;
71        private UserChoiceInfo mUserChoiceInfo;
72
73        @Override
74        public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
75            String title = getString(
76                    R.string.title_wifi_could_not_connect,
77                    mUserChoiceInfo.getWifiConfiguration().getPrintableSsid());
78            return new GuidanceStylist.Guidance(title, null, null, null);
79        }
80
81        @Override
82        public void onCreate(Bundle savedInstanceState) {
83            mStateMachine = ViewModelProviders
84                    .of(getActivity())
85                    .get(StateMachine.class);
86            mUserChoiceInfo = ViewModelProviders
87                    .of(getActivity())
88                    .get(UserChoiceInfo.class);
89            super.onCreate(savedInstanceState);
90        }
91
92        @Override
93        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
94            Context context = getActivity();
95            actions.add(new GuidedAction.Builder(context)
96                    .title(R.string.wifi_action_try_again)
97                    .id(ACTION_ID_TRY_AGAIN)
98                    .build());
99            actions.add(new GuidedAction.Builder(context)
100                    .title(R.string.wifi_action_view_available_networks)
101                    .id(ACTION_ID_VIEW_AVAILABLE_NETWORK)
102                    .build());
103        }
104
105        @Override
106        public void onGuidedActionClicked(GuidedAction action) {
107            if (action.getId() == ACTION_ID_TRY_AGAIN) {
108                mStateMachine.getListener()
109                        .onComplete(StateMachine.TRY_AGAIN);
110            } else if (action.getId() == ACTION_ID_VIEW_AVAILABLE_NETWORK) {
111                mStateMachine.getListener()
112                        .onComplete(StateMachine.SELECT_WIFI);
113            }
114        }
115    }
116}
117