summaryrefslogtreecommitdiff
path: root/src/com/android/settings/wifi/WifiDialogActivity.java
blob: 58e9a7b5bb912257a1db500526cd076f711a5f6d (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
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2015 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.settings.wifi;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process;
import android.os.SimpleClock;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settings.SetupWizardUtils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.core.lifecycle.ObservableActivity;
import com.android.settingslib.wifi.AccessPoint;
import com.android.wifitrackerlib.NetworkDetailsTracker;
import com.android.wifitrackerlib.WifiEntry;

import com.google.android.setupcompat.util.WizardManagerHelper;
import com.google.android.setupdesign.util.ThemeHelper;

import java.time.Clock;
import java.time.ZoneOffset;

/**
 * The activity shows a Wi-fi editor dialog.
 *
 * TODO(b/152571756): This activity supports both WifiTrackerLib and SettingsLib because this is an
 *                    exported UI component, some other APPs (e.g., SetupWizard) still use
 *                    SettingsLib. Remove the SettingsLib compatible part after these APPs use
 *                    WifiTrackerLib.
 */
public class WifiDialogActivity extends ObservableActivity implements WifiDialog.WifiDialogListener,
        WifiDialog2.WifiDialog2Listener, DialogInterface.OnDismissListener {

    private static final String TAG = "WifiDialogActivity";

    // For the callers which support WifiTrackerLib.
    public static final String KEY_CHOSEN_WIFIENTRY_KEY = "key_chosen_wifientry_key";

    // For the callers which support SettingsLib.
    public static final String KEY_ACCESS_POINT_STATE = "access_point_state";

    /**
     * Boolean extra indicating whether this activity should connect to an access point on the
     * caller's behalf. If this is set to false, the caller should check
     * {@link #KEY_WIFI_CONFIGURATION} in the result data and save that using
     * {@link WifiManager#connect(WifiConfiguration, ActionListener)}. Default is true.
     */
    @VisibleForTesting
    static final String KEY_CONNECT_FOR_CALLER = "connect_for_caller";

    public static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";

    private static final int RESULT_CONNECTED = RESULT_FIRST_USER;
    private static final int RESULT_FORGET = RESULT_FIRST_USER + 1;

    private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;

    // Max age of tracked WifiEntries.
    private static final long MAX_SCAN_AGE_MILLIS = 15_000;
    // Interval between initiating NetworkDetailsTracker scans.
    private static final long SCAN_INTERVAL_MILLIS = 10_000;

    private WifiDialog mDialog;
    private AccessPoint mAccessPoint;

    private WifiDialog2 mDialog2;

    // The received intent supports a key of WifiTrackerLib or SettingsLib.
    private boolean mIsWifiTrackerLib;

    private Intent mIntent;
    private NetworkDetailsTracker mNetworkDetailsTracker;
    private HandlerThread mWorkerThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mIntent = getIntent();
        if (WizardManagerHelper.isSetupWizardIntent(mIntent)) {
            setTheme(SetupWizardUtils.getTransparentTheme(this, mIntent));
        }

        super.onCreate(savedInstanceState);

        mIsWifiTrackerLib = !TextUtils.isEmpty(mIntent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY));

        if (mIsWifiTrackerLib) {
            mWorkerThread = new HandlerThread(
                    TAG + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
                    Process.THREAD_PRIORITY_BACKGROUND);
            mWorkerThread.start();
            final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) {
                @Override
                public long millis() {
                    return SystemClock.elapsedRealtime();
                }
            };
            mNetworkDetailsTracker = FeatureFactory.getFactory(this)
                    .getWifiTrackerLibProvider()
                    .createNetworkDetailsTracker(
                            getLifecycle(),
                            this,
                            new Handler(Looper.getMainLooper()),
                            mWorkerThread.getThreadHandler(),
                            elapsedRealtimeClock,
                            MAX_SCAN_AGE_MILLIS,
                            SCAN_INTERVAL_MILLIS,
                            mIntent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY));
        } else {
            final Bundle accessPointState = mIntent.getBundleExtra(KEY_ACCESS_POINT_STATE);
            if (accessPointState != null) {
                mAccessPoint = new AccessPoint(this, accessPointState);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mDialog2 != null || mDialog != null) {
            return;
        }

        if (WizardManagerHelper.isAnySetupWizard(getIntent())) {
            final int targetStyle = ThemeHelper.isSetupWizardDayNightEnabled(this)
                    ? R.style.SuwAlertDialogThemeCompat_DayNight :
                    R.style.SuwAlertDialogThemeCompat_Light;
            if (mIsWifiTrackerLib) {
                mDialog2 = WifiDialog2.createModal(this, this,
                        mNetworkDetailsTracker.getWifiEntry(),
                        WifiConfigUiBase2.MODE_CONNECT, targetStyle);
            } else {
                mDialog = WifiDialog.createModal(this, this, mAccessPoint,
                        WifiConfigUiBase.MODE_CONNECT, targetStyle);
            }
        } else {
            if (mIsWifiTrackerLib) {
                mDialog2 = WifiDialog2.createModal(this, this,
                        mNetworkDetailsTracker.getWifiEntry(), WifiConfigUiBase2.MODE_CONNECT);
            } else {
                mDialog = WifiDialog.createModal(
                        this, this, mAccessPoint, WifiConfigUiBase.MODE_CONNECT);
            }
        }

        if (mIsWifiTrackerLib) {
            mDialog2.show();
            mDialog2.setOnDismissListener(this);
        } else {
            mDialog.show();
            mDialog.setOnDismissListener(this);
        }
    }

    @Override
    public void finish() {
        overridePendingTransition(0, 0);

        super.finish();
    }

    @Override
    public void onDestroy() {
        if (mIsWifiTrackerLib) {
            if (mDialog2 != null && mDialog2.isShowing()) {
                mDialog2 = null;
            }
            mWorkerThread.quit();
        } else {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog = null;
            }
        }

        super.onDestroy();
    }

    @Override
    public void onForget(WifiDialog2 dialog) {
        final WifiEntry wifiEntry = dialog.getController().getWifiEntry();
        if (wifiEntry != null && wifiEntry.canForget()) {
            wifiEntry.forget(null /* callback */);
        }

        setResult(RESULT_FORGET);
        finish();
    }

    @Override
    public void onForget(WifiDialog dialog) {
        final WifiManager wifiManager = getSystemService(WifiManager.class);
        final AccessPoint accessPoint = dialog.getController().getAccessPoint();
        if (accessPoint != null) {
            if (!accessPoint.isSaved()) {
                if (accessPoint.getNetworkInfo() != null &&
                        accessPoint.getNetworkInfo().getState() != NetworkInfo.State.DISCONNECTED) {
                    // Network is active but has no network ID - must be ephemeral.
                    wifiManager.disableEphemeralNetwork(
                            AccessPoint.convertToQuotedString(accessPoint.getSsidStr()));
                } else {
                    // Should not happen, but a monkey seems to trigger it
                    Log.e(TAG, "Failed to forget invalid network " + accessPoint.getConfig());
                }
            } else {
                wifiManager.forget(accessPoint.getConfig().networkId, null /* listener */);
            }
        }

        Intent resultData = new Intent();
        if (accessPoint != null) {
            Bundle accessPointState = new Bundle();
            accessPoint.saveWifiState(accessPointState);
            resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
        }
        setResult(RESULT_FORGET);
        finish();
    }

    @Override
    public void onSubmit(WifiDialog2 dialog) {
        final WifiEntry wifiEntry = dialog.getController().getWifiEntry();
        final WifiConfiguration config = dialog.getController().getConfig();

        if (getIntent().getBooleanExtra(KEY_CONNECT_FOR_CALLER, true)) {
            if (config == null && wifiEntry != null && wifiEntry.canConnect()) {
                wifiEntry.connect(null /* callback */);
            } else {
                getSystemService(WifiManager.class).connect(config, null /* listener */);
            }
        }

        final Intent resultData = new Intent();
        if (config != null) {
            resultData.putExtra(KEY_WIFI_CONFIGURATION, config);
        }
        setResult(RESULT_CONNECTED, resultData);
        finish();
    }

    @Override
    public void onSubmit(WifiDialog dialog) {
        final WifiConfiguration config = dialog.getController().getConfig();
        final AccessPoint accessPoint = dialog.getController().getAccessPoint();
        final WifiManager wifiManager = getSystemService(WifiManager.class);

        if (getIntent().getBooleanExtra(KEY_CONNECT_FOR_CALLER, true)) {
            if (config == null) {
                if (accessPoint != null && accessPoint.isSaved()) {
                    wifiManager.connect(accessPoint.getConfig(), null /* listener */);
                }
            } else {
                wifiManager.save(config, null /* listener */);
                if (accessPoint != null) {
                    // accessPoint is null for "Add network"
                    NetworkInfo networkInfo = accessPoint.getNetworkInfo();
                    if (networkInfo == null || !networkInfo.isConnected()) {
                        wifiManager.connect(config, null /* listener */);
                    }
                }
            }
        }

        Intent resultData = new Intent();
        if (accessPoint != null) {
            Bundle accessPointState = new Bundle();
            accessPoint.saveWifiState(accessPointState);
            resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
        }
        if (config != null) {
            resultData.putExtra(KEY_WIFI_CONFIGURATION, config);
        }
        setResult(RESULT_CONNECTED, resultData);
        finish();
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        mDialog2 = null;
        mDialog = null;
        finish();
    }

    @Override
    public void onScan(WifiDialog2 dialog, String ssid) {
        Intent intent = WifiDppUtils.getEnrolleeQrCodeScannerIntent(dialog.getContext(), ssid);
        WizardManagerHelper.copyWizardManagerExtras(mIntent, intent);

        // Launch QR code scanner to join a network.
        startActivityForResult(intent, REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
    }

    @Override
    public void onScan(WifiDialog dialog, String ssid) {
        Intent intent = WifiDppUtils.getEnrolleeQrCodeScannerIntent(dialog.getContext(), ssid);
        WizardManagerHelper.copyWizardManagerExtras(mIntent, intent);

        // Launch QR code scanner to join a network.
        startActivityForResult(intent, REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER) {
            if (resultCode != RESULT_OK) {
                return;
            }

            setResult(RESULT_CONNECTED, data);
            finish();
        }
    }
}