1 /*
2  * Copyright (C) 2018 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;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 
22 import androidx.fragment.app.Fragment;
23 import androidx.fragment.app.FragmentActivity;
24 import androidx.leanback.widget.GuidanceStylist;
25 import androidx.leanback.widget.GuidedAction;
26 import androidx.lifecycle.ViewModelProviders;
27 
28 import com.android.tv.settings.R;
29 import com.android.tv.settings.connectivity.setup.WifiConnectivityGuidedStepFragment;
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 showing the save failure page.
37  */
38 public class SaveFailedState implements State {
39     private final FragmentActivity mActivity;
40     private Fragment mFragment;
41 
SaveFailedState(FragmentActivity activity)42     public SaveFailedState(FragmentActivity activity) {
43         mActivity = activity;
44     }
45 
46     @Override
processForward()47     public void processForward() {
48         mFragment = new SaveFailedFragment();
49         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
50         if (listener != null) {
51             listener.onFragmentChange(mFragment, true);
52         }
53     }
54 
55     @Override
processBackward()56     public void processBackward() {
57         StateMachine sm = ViewModelProviders.of(mActivity).get(StateMachine.class);
58         sm.back();
59     }
60 
61     @Override
getFragment()62     public Fragment getFragment() {
63         return mFragment;
64     }
65 
66     /**
67      * Fragment that needs user to enter dns1.
68      */
69     public static class SaveFailedFragment extends WifiConnectivityGuidedStepFragment {
70         private StateMachine mStateMachine;
71 
72         @Override
onCreateGuidance(Bundle savedInstanceState)73         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
74             return new GuidanceStylist.Guidance(
75                     getString(R.string.title_wifi_could_not_save),
76                     null,
77                     null,
78                     null);
79         }
80 
81         @Override
onCreate(Bundle savedInstanceState)82         public void onCreate(Bundle savedInstanceState) {
83             mStateMachine = ViewModelProviders
84                     .of(getActivity())
85                     .get(StateMachine.class);
86             super.onCreate(savedInstanceState);
87         }
88 
89         @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)90         public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
91             Context context = getActivity();
92             actions.add(new GuidedAction.Builder(context)
93                     .title(getString(R.string.wifi_action_ok))
94                     .id(GuidedAction.ACTION_ID_CONTINUE)
95                     .build());
96         }
97 
98         @Override
onGuidedActionClicked(GuidedAction action)99         public void onGuidedActionClicked(GuidedAction action) {
100             if (action.getId() == GuidedAction.ACTION_ID_CONTINUE) {
101                 mStateMachine.getListener().onComplete(StateMachine.FAIL);
102             }
103         }
104     }
105 }
106