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.net.wifi.WifiManager;
20 import android.os.Bundle;
21 
22 import androidx.fragment.app.Fragment;
23 import androidx.fragment.app.FragmentActivity;
24 import androidx.lifecycle.ViewModelProviders;
25 
26 import com.android.tv.settings.R;
27 import com.android.tv.settings.connectivity.setup.AdvancedOptionsFlowInfo;
28 import com.android.tv.settings.connectivity.setup.MessageFragment;
29 import com.android.tv.settings.connectivity.util.State;
30 import com.android.tv.settings.connectivity.util.StateMachine;
31 
32 /**
33  * State responsible for showing the save page.
34  */
35 public class SaveState implements State {
36     private final FragmentActivity mActivity;
37     private Fragment mFragment;
38 
SaveState(FragmentActivity activity)39     public SaveState(FragmentActivity activity) {
40         mActivity = activity;
41     }
42 
43     @Override
processForward()44     public void processForward() {
45         EditSettingsInfo editSettingsInfo =
46                     ViewModelProviders.of(mActivity).get(EditSettingsInfo.class);
47         NetworkConfiguration networkConfig = editSettingsInfo.getNetworkConfiguration();
48         AdvancedOptionsFlowInfo advInfo =
49                     ViewModelProviders.of(mActivity).get(AdvancedOptionsFlowInfo.class);
50         networkConfig.setIpConfiguration(advInfo.getIpConfiguration());
51         mFragment = SaveWifiConfigurationFragment.newInstance(
52                     mActivity.getString(R.string.wifi_saving, networkConfig.getPrintableName()));
53         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
54         listener.onFragmentChange(mFragment, true);
55     }
56 
57     @Override
processBackward()58     public void processBackward() {
59         StateMachine stateMachine = ViewModelProviders.of(mActivity).get(StateMachine.class);
60         stateMachine.back();
61     }
62 
63     @Override
getFragment()64     public Fragment getFragment() {
65         return mFragment;
66     }
67 
68     /**
69      * Saves a wifi network's configuration.
70      */
71     public static class SaveWifiConfigurationFragment extends MessageFragment
72             implements WifiManager.ActionListener {
73 
newInstance(String title)74         public static SaveWifiConfigurationFragment newInstance(String title) {
75             SaveWifiConfigurationFragment
76                     fragment = new SaveWifiConfigurationFragment();
77             Bundle args = new Bundle();
78             addArguments(args, title, true);
79             fragment.setArguments(args);
80             return fragment;
81         }
82 
83         @Override
onCreate(Bundle icicle)84         public void onCreate(Bundle icicle) {
85             super.onCreate(icicle);
86             EditSettingsInfo editSettingsInfo =
87                     ViewModelProviders.of(getActivity()).get(EditSettingsInfo.class);
88             NetworkConfiguration configuration = editSettingsInfo.getNetworkConfiguration();
89             configuration.save(this);
90         }
91 
92         @Override
onSuccess()93         public void onSuccess() {
94             FragmentActivity activity = getActivity();
95             if (activity != null) {
96                 StateMachine sm = ViewModelProviders
97                         .of(activity).get(StateMachine.class);
98                 sm.getListener().onComplete(this, StateMachine.RESULT_SUCCESS);
99             }
100         }
101 
102         @Override
onFailure(int reason)103         public void onFailure(int reason) {
104             FragmentActivity activity = getActivity();
105             if (activity != null) {
106                 StateMachine sm = ViewModelProviders
107                         .of(activity).get(StateMachine.class);
108                 sm.getListener().onComplete(this, StateMachine.RESULT_FAILURE);
109             }
110         }
111     }
112 }
113