summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/connectivity/WifiDetailsFragment.java
blob: 92d21c8e6600ec6894c2576c0358c04831c2a24a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.tv.settings.connectivity;

import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;

import android.app.tvsettings.TvSettingsEnums;
import android.content.Context;
import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.leanback.app.GuidedStepSupportFragment;
import androidx.leanback.widget.GuidanceStylist;
import androidx.leanback.widget.GuidedAction;
import androidx.preference.ListPreference;
import androidx.preference.Preference;

import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.wifi.AccessPoint;
import com.android.tv.settings.R;
import com.android.tv.settings.SettingsPreferenceFragment;

import java.util.List;

/**
 * Fragment for displaying the details of a single wifi network
 */
public class WifiDetailsFragment extends SettingsPreferenceFragment
        implements ConnectivityListener.Listener, ConnectivityListener.WifiNetworkListener {

    private static final String ARG_ACCESS_POINT_STATE = "apBundle";

    private static final String KEY_CONNECTION_STATUS = "connection_status";
    private static final String KEY_IP_ADDRESS = "ip_address";
    private static final String KEY_MAC_ADDRESS = "mac_address";
    private static final String KEY_SIGNAL_STRENGTH = "signal_strength";
    private static final String KEY_RANDOM_MAC = "random_mac";
    private static final String KEY_PROXY_SETTINGS = "proxy_settings";
    private static final String KEY_IP_SETTINGS = "ip_settings";
    private static final String KEY_FORGET_NETWORK = "forget_network";

    private static final String VALUE_MAC_RANDOM = "random";
    private static final String VALUE_MAC_DEVICE = "device";

    private Preference mConnectionStatusPref;
    private Preference mIpAddressPref;
    private Preference mMacAddressPref;
    private Preference mSignalStrengthPref;
    private ListPreference mRandomMacPref;
    private RestrictedPreference mProxySettingsPref;
    private RestrictedPreference mIpSettingsPref;
    private RestrictedPreference mForgetNetworkPref;

    private ConnectivityListener mConnectivityListener;
    private AccessPoint mAccessPoint;

    public static void prepareArgs(@NonNull Bundle args, AccessPoint accessPoint) {
        final Bundle apBundle = new Bundle();
        accessPoint.saveWifiState(apBundle);
        args.putParcelable(ARG_ACCESS_POINT_STATE, apBundle);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mConnectivityListener = new ConnectivityListener(
                getContext(), this, getSettingsLifecycle());

        mAccessPoint = new AccessPoint(getContext(),
                getArguments().getBundle(ARG_ACCESS_POINT_STATE));
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        mConnectivityListener.setWifiListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.wifi_details, null);

        getPreferenceScreen().setTitle(mAccessPoint.getSsid());

        mConnectionStatusPref = findPreference(KEY_CONNECTION_STATUS);
        mIpAddressPref = findPreference(KEY_IP_ADDRESS);
        mMacAddressPref = findPreference(KEY_MAC_ADDRESS);
        mSignalStrengthPref = findPreference(KEY_SIGNAL_STRENGTH);
        mRandomMacPref = findPreference(KEY_RANDOM_MAC);
        mProxySettingsPref = findPreference(KEY_PROXY_SETTINGS);
        mIpSettingsPref = findPreference(KEY_IP_SETTINGS);
        mForgetNetworkPref = findPreference(KEY_FORGET_NETWORK);
    }

    @Override
    public void onConnectivityChange() {
        update();
    }

    @Override
    public void onWifiListChanged() {
        final List<AccessPoint> accessPoints = mConnectivityListener.getAvailableNetworks();
        for (final AccessPoint accessPoint : accessPoints) {
            if (TextUtils.equals(mAccessPoint.getSsidStr(), accessPoint.getSsidStr())
                    && mAccessPoint.getSecurity() == accessPoint.getSecurity()) {
                // Make sure we're not holding on to the one we inflated from the bundle, because
                // it won't be updated
                mAccessPoint = accessPoint;
                break;
            }
        }
        update();
    }

    private void update() {
        if (!isAdded()) {
            return;
        }

        final boolean active = mAccessPoint.isActive();

        mConnectionStatusPref.setSummary(active ? R.string.connected : R.string.not_connected);
        mIpAddressPref.setVisible(active);
        mSignalStrengthPref.setVisible(active);

        if (active) {
            mIpAddressPref.setSummary(mConnectivityListener.getWifiIpAddress());
            mSignalStrengthPref.setSummary(getSignalStrength());
        }

        // Mac address related Preferences (info entry and random mac setting entry)
        String macAddress = mConnectivityListener.getWifiMacAddress(mAccessPoint);
        if (active && !TextUtils.isEmpty(macAddress)) {
            mMacAddressPref.setVisible(true);
            updateMacAddressPref(macAddress);
            updateRandomMacPref();
        } else {
            mMacAddressPref.setVisible(false);
            mRandomMacPref.setVisible(false);
        }

        WifiConfiguration wifiConfiguration = mAccessPoint.getConfig();
        if (wifiConfiguration != null) {
            final int networkId = wifiConfiguration.networkId;
            ProxySettings proxySettings = wifiConfiguration.getIpConfiguration().getProxySettings();
            mProxySettingsPref.setSummary(proxySettings == ProxySettings.NONE
                    ? R.string.wifi_action_proxy_none : R.string.wifi_action_proxy_manual);
            mProxySettingsPref.setIntent(EditProxySettingsActivity.createWifiIntent(getContext(),
                    networkId));

            IpAssignment ipAssignment = wifiConfiguration.getIpConfiguration().getIpAssignment();
            mIpSettingsPref.setSummary(ipAssignment == IpAssignment.STATIC
                    ? R.string.wifi_action_static : R.string.wifi_action_dhcp);
            mIpSettingsPref.setIntent(EditIpSettingsActivity.createWifiIntent(getContext(),
                    networkId));

            mForgetNetworkPref.setFragment(ForgetNetworkConfirmFragment.class.getName());
            ForgetNetworkConfirmFragment.prepareArgs(mForgetNetworkPref.getExtras(), mAccessPoint);
        }

        mProxySettingsPref.setVisible(wifiConfiguration != null);
        mProxySettingsPref.setOnPreferenceClickListener(
                preference -> {
                    logEntrySelected(TvSettingsEnums.NETWORK_AP_INFO_PROXY_SETTINGS);
                    return false;
                });
        mIpSettingsPref.setVisible(wifiConfiguration != null);
        mIpSettingsPref.setOnPreferenceClickListener(
                preference -> {
                    logEntrySelected(TvSettingsEnums.NETWORK_AP_INFO_IP_SETTINGS);
                    return false;
                });
        mForgetNetworkPref.setVisible(wifiConfiguration != null);
        mForgetNetworkPref.setOnPreferenceClickListener(
                preference -> {
                    logEntrySelected(TvSettingsEnums.NETWORK_AP_INFO_FORGET_NETWORK);
                    return false;
                });

        boolean canModifyNetwork = !WifiConfigHelper.isNetworkLockedDown(
                getContext(), wifiConfiguration);
        if (canModifyNetwork) {
            mProxySettingsPref.setDisabledByAdmin(null);
            mIpSettingsPref.setDisabledByAdmin(null);
            mForgetNetworkPref.setDisabledByAdmin(null);

            mProxySettingsPref.setEnabled(true);
            mIpSettingsPref.setEnabled(true);
            mForgetNetworkPref.setEnabled(true);
        } else {
            EnforcedAdmin admin = RestrictedLockUtils.getProfileOrDeviceOwner(getContext(),
                    UserHandle.of(UserHandle.myUserId()));
            mProxySettingsPref.setDisabledByAdmin(admin);
            mIpSettingsPref.setDisabledByAdmin(admin);
            mForgetNetworkPref.setDisabledByAdmin(admin);
        }
    }

    private String getSignalStrength() {
        String[] signalLevels = getResources().getStringArray(R.array.wifi_signal_strength);
        int strength = mConnectivityListener.getWifiSignalStrength(signalLevels.length);
        return signalLevels[strength];
    }

    private void updateMacAddressPref(String macAddress) {
        if (WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
            mMacAddressPref.setSummary(R.string.mac_address_not_available);
        } else {
            mMacAddressPref.setSummary(macAddress);
        }
        if (mAccessPoint == null || mAccessPoint.getConfig() == null) {
            return;
        }
        // For saved Passpoint network, framework doesn't have the field to keep the MAC choice
        // persistently, so Passpoint network will always use the default value so far, which is
        // randomized MAC address, so don't need to modify title.
        if (mAccessPoint.isPasspoint() || mAccessPoint.isPasspointConfig()) {
            return;
        }
        mMacAddressPref.setTitle(
                (mConnectivityListener.isWifiMacAddressRandomized(mAccessPoint))
                        ? R.string.title_randomized_mac_address
                        : R.string.title_mac_address);
    }

    private void updateRandomMacPref() {
        mRandomMacPref.setVisible(mConnectivityListener.isMacAddressRandomizationSupported());
        boolean isMacRandomized = mConnectivityListener.isWifiMacAddressRandomized(mAccessPoint);
        mRandomMacPref.setValue(isMacRandomized ? VALUE_MAC_RANDOM : VALUE_MAC_DEVICE);
        if (mAccessPoint.isEphemeral() || mAccessPoint.isPasspoint()
                || mAccessPoint.isPasspointConfig()) {
            mRandomMacPref.setSelectable(false);
            mRandomMacPref.setSummary(R.string.mac_address_ephemeral_summary);
        } else {
            mRandomMacPref.setSelectable(true);
            mRandomMacPref.setSummary(mRandomMacPref.getEntries()[isMacRandomized ? 0 : 1]);
        }
        mRandomMacPref.setOnPreferenceChangeListener(
                (pref, newValue) -> {
                    mConnectivityListener.applyMacRandomizationSetting(
                            mAccessPoint,
                            VALUE_MAC_RANDOM.equals(newValue));
                    // The above call should trigger a connectivity change which will refresh
                    // the UI.
                    return true;
                });
    }

    @Override
    protected int getPageId() {
        return TvSettingsEnums.NETWORK_AP_INFO;
    }

    public static class ForgetNetworkConfirmFragment extends GuidedStepSupportFragment {

        private AccessPoint mAccessPoint;

        public static void prepareArgs(@NonNull Bundle args, AccessPoint accessPoint) {
            final Bundle apBundle = new Bundle();
            accessPoint.saveWifiState(apBundle);
            args.putParcelable(ARG_ACCESS_POINT_STATE, apBundle);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            mAccessPoint = new AccessPoint(getContext(),
                    getArguments().getBundle(ARG_ACCESS_POINT_STATE));
            super.onCreate(savedInstanceState);
        }

        @NonNull
        @Override
        public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
            return new GuidanceStylist.Guidance(
                    getString(R.string.wifi_forget_network),
                    getString(R.string.wifi_forget_network_description),
                    mAccessPoint.getSsidStr(),
                    getContext().getDrawable(R.drawable.ic_wifi_signal_4_white_132dp));
        }

        @Override
        public void onCreateActions(@NonNull List<GuidedAction> actions,
                Bundle savedInstanceState) {
            final Context context = getContext();
            actions.add(new GuidedAction.Builder(context)
                    .clickAction(GuidedAction.ACTION_ID_OK)
                    .build());
            actions.add(new GuidedAction.Builder(context)
                    .clickAction(GuidedAction.ACTION_ID_CANCEL)
                    .build());
        }

        @Override
        public void onGuidedActionClicked(GuidedAction action) {
            if (action.getId() == GuidedAction.ACTION_ID_OK) {
                WifiManager wifiManager =
                        (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
                wifiManager.forget(mAccessPoint.getConfig().networkId, null);
            }
            getFragmentManager().popBackStack();
        }
    }
}