1 /*
2  * Copyright (C) 2014 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.content.Intent;
21 import android.net.IpConfiguration;
22 import android.net.wifi.WifiConfiguration;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 
27 import androidx.fragment.app.Fragment;
28 import androidx.fragment.app.FragmentTransaction;
29 import androidx.lifecycle.ViewModelProviders;
30 
31 import com.android.settingslib.RestrictedLockUtils;
32 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
33 import com.android.settingslib.RestrictedLockUtilsInternal;
34 import com.android.tv.settings.R;
35 import com.android.tv.settings.connectivity.setup.AdvancedWifiOptionsFlow;
36 import com.android.tv.settings.connectivity.util.State;
37 import com.android.tv.settings.connectivity.util.StateMachine;
38 import com.android.tv.settings.core.instrumentation.InstrumentedActivity;
39 
40 /**
41  * Allows the modification of advanced Wi-Fi settings
42  */
43 public class EditProxySettingsActivity extends InstrumentedActivity implements
44         State.FragmentChangeListener {
45 
46     private static final String TAG = "EditProxySettings";
47 
48     private static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID;
49     private static final String EXTRA_NETWORK_ID = "network_id";
50     private static final String EXTRA_IFACE = "interface_name";
51     private static final String EXTRA_IP_CONFIG = "ip_config";
52 
53     /**
54      * Create a new Intent for WiFi network.
55      */
createWifiIntent(Context context, int networkId)56     public static Intent createWifiIntent(Context context, int networkId) {
57         return new Intent(context, EditProxySettingsActivity.class)
58                 .putExtra(EXTRA_NETWORK_ID, networkId);
59     }
60 
61     /**
62      * Create a new Intent for Ethernet network.
63      */
createEthernetIntent(Context context, String iface, IpConfiguration initialConfig)64     public static Intent createEthernetIntent(Context context, String iface,
65             IpConfiguration initialConfig) {
66         return new Intent(context, EditProxySettingsActivity.class)
67                 .putExtra(EXTRA_NETWORK_ID, NETWORK_ID_ETHERNET)
68                 .putExtra(EXTRA_IFACE, iface)
69                 .putExtra(EXTRA_IP_CONFIG, initialConfig);
70     }
71 
72     private State mSaveState;
73     private State mSaveSuccessState;
74     private State mSaveFailedState;
75     private StateMachine mStateMachine;
76     private final StateMachine.Callback mStateMachineCallback = result -> {
77         setResult(result);
78         finish();
79     };
80 
81     @Override
onCreate(Bundle savedInstanceState)82     protected void onCreate(Bundle savedInstanceState) {
83         super.onCreate(savedInstanceState);
84         setContentView(R.layout.wifi_container);
85         mStateMachine = ViewModelProviders.of(this).get(StateMachine.class);
86         mStateMachine.setCallback(mStateMachineCallback);
87         mSaveState = new SaveState(this);
88         mSaveSuccessState = new SaveSuccessState(this);
89         mSaveFailedState = new SaveFailedState(this);
90         final Intent intent = getIntent();
91         int networkId = intent.getIntExtra(EXTRA_NETWORK_ID, NETWORK_ID_ETHERNET);
92         NetworkConfiguration netConfig;
93         if (networkId == NETWORK_ID_ETHERNET) {
94             final String iface = intent.getStringExtra(EXTRA_IFACE);
95             final IpConfiguration initialConfig = intent.getParcelableExtra(EXTRA_IP_CONFIG);
96             netConfig = new EthernetConfig(this, iface, initialConfig);
97         } else {
98             final UserManager userManager = UserManager.get(this);
99             if (userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) {
100                 EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
101                         this, UserManager.DISALLOW_CONFIG_WIFI, UserHandle.myUserId());
102                 if (admin != null) {
103                     RestrictedLockUtils.sendShowAdminSupportDetailsIntent(this, admin);
104                 }
105                 finish();
106                 return;
107             }
108 
109             netConfig = new WifiConfig(this);
110             ((WifiConfig) netConfig).load(networkId);
111             if (((WifiConfig) netConfig).isLockedDown(this)) {
112                 EnforcedAdmin admin = RestrictedLockUtils.getProfileOrDeviceOwner(this,
113                         UserHandle.of(UserHandle.myUserId()));
114                 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(this, admin);
115                 finish();
116                 return;
117             }
118         }
119         EditSettingsInfo editSettingsInfo = ViewModelProviders.of(this).get(EditSettingsInfo.class);
120         editSettingsInfo.setNetworkConfiguration(netConfig);
121         AdvancedWifiOptionsFlow.createFlow(this, false, true, netConfig,
122                 null, mSaveState, AdvancedWifiOptionsFlow.START_PROXY_SETTINGS_PAGE);
123 
124         /* Save */
125         mStateMachine.addState(
126                 mSaveState,
127                 StateMachine.RESULT_SUCCESS,
128                 mSaveSuccessState
129         );
130         mStateMachine.addState(
131                 mSaveState,
132                 StateMachine.RESULT_FAILURE,
133                 mSaveFailedState
134         );
135         mStateMachine.start(true);
136     }
137 
138     @Override
onBackPressed()139     public void onBackPressed() {
140         mStateMachine.back();
141     }
142 
updateView(Fragment fragment, boolean movingForward)143     private void updateView(Fragment fragment, boolean movingForward) {
144         if (fragment != null) {
145             FragmentTransaction updateTransaction = getSupportFragmentManager().beginTransaction();
146             if (movingForward) {
147                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
148             } else {
149                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
150             }
151             updateTransaction.replace(R.id.wifi_container, fragment, TAG);
152             updateTransaction.commitAllowingStateLoss();
153         }
154     }
155 
156     @Override
onFragmentChange(Fragment newFragment, boolean movingForward)157     public void onFragmentChange(Fragment newFragment, boolean movingForward) {
158         updateView(newFragment, movingForward);
159     }
160 }
161