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