This change uses WifiTrackerLib's WifiPickerTracker & WifiEntry to replace SettingLib's WifiTracker & AccessPoint. This change includes 1. WifiScanWorker has the callbacks similar to a lifecycle component but it's not a lifecycle component. Let WifiScanWorker implements LifecycleOwner and provides #getLifecycle() for WifiPickerTracker. 2. Remove captive portal related code because WifiEntry#connect will handle captive portal login if it's necessary. 3. Create WifiSliceItem to wrap WifiEntry because WifiEntry is an abstract object and it does not provide copy constructor. Without copy construcor, Wi-Fi Slice may show unexpected information when a WifiEntry is updated. Bug: 155613549 Bug: 152571756 Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.wifi.slice make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.wifi Change-Id: I2d66ea4905daca3244ec4cf8f2935cda817480b1
93 lines
3.3 KiB
Java
93 lines
3.3 KiB
Java
/*
|
|
* Copyright (C) 2019 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.slice;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.text.TextUtils;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
import com.android.settings.R;
|
|
import com.android.settings.slices.SliceBackgroundWorker;
|
|
import com.android.settings.wifi.WifiDialogActivity;
|
|
import com.android.wifitrackerlib.WifiEntry;
|
|
import com.android.wifitrackerlib.WifiEntry.ConnectCallback;
|
|
|
|
/**
|
|
* This receiver helps connect to Wi-Fi network
|
|
*/
|
|
public class ConnectToWifiHandler extends BroadcastReceiver {
|
|
|
|
static final String KEY_CHOSEN_WIFIENTRY_KEY = "key_chosen_wifientry_key";
|
|
static final String KEY_WIFI_SLICE_URI = "key_wifi_slice_uri";
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (context == null || intent == null) {
|
|
return;
|
|
}
|
|
final String key = intent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY);
|
|
if (TextUtils.isEmpty(key)) {
|
|
return;
|
|
}
|
|
if (intent.getParcelableExtra(KEY_WIFI_SLICE_URI) == null) {
|
|
return;
|
|
}
|
|
final WifiScanWorker worker = getWifiScanWorker(intent);
|
|
if (worker == null) {
|
|
return;
|
|
}
|
|
final WifiEntry wifiEntry = worker.getWifiEntry(key);
|
|
if (wifiEntry == null) {
|
|
return;
|
|
}
|
|
wifiEntry.connect(new WifiEntryConnectCallback(context, wifiEntry));
|
|
}
|
|
|
|
@VisibleForTesting
|
|
WifiScanWorker getWifiScanWorker(Intent intent) {
|
|
return SliceBackgroundWorker.getInstance(intent.getParcelableExtra(KEY_WIFI_SLICE_URI));
|
|
}
|
|
|
|
@VisibleForTesting
|
|
static class WifiEntryConnectCallback implements WifiEntry.ConnectCallback {
|
|
final Context mContext;
|
|
final WifiEntry mWifiEntry;
|
|
|
|
WifiEntryConnectCallback(Context context, WifiEntry connectWifiEntry) {
|
|
mContext = context;
|
|
mWifiEntry = connectWifiEntry;
|
|
}
|
|
|
|
@Override
|
|
public void onConnectResult(@ConnectStatus int status) {
|
|
if (status == ConnectCallback.CONNECT_STATUS_FAILURE_NO_CONFIG) {
|
|
final Intent intent = new Intent(mContext, WifiDialogActivity.class)
|
|
.putExtra(WifiDialogActivity.KEY_CHOSEN_WIFIENTRY_KEY, mWifiEntry.getKey());
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
mContext.startActivity(intent);
|
|
} else if (status == CONNECT_STATUS_FAILURE_UNKNOWN) {
|
|
Toast.makeText(mContext, R.string.wifi_failed_connect_message,
|
|
Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
}
|
|
}
|