1 /*
2  * Copyright (C) 2019 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.settings.wifi.slice;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.text.TextUtils;
23 import android.widget.Toast;
24 
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.settings.R;
28 import com.android.settings.slices.SliceBackgroundWorker;
29 import com.android.settings.wifi.WifiDialogActivity;
30 import com.android.wifitrackerlib.WifiEntry;
31 import com.android.wifitrackerlib.WifiEntry.ConnectCallback;
32 
33 /**
34  * This receiver helps connect to Wi-Fi network
35  *
36  * @deprecated this is not used after V and will be removed.
37  */
38 @Deprecated(forRemoval = true)
39 public class ConnectToWifiHandler extends BroadcastReceiver {
40 
41     static final String KEY_CHOSEN_WIFIENTRY_KEY = "key_chosen_wifientry_key";
42     static final String KEY_WIFI_SLICE_URI = "key_wifi_slice_uri";
43 
44     @Override
onReceive(Context context, Intent intent)45     public void onReceive(Context context, Intent intent) {
46         if (context == null || intent == null) {
47             return;
48         }
49         final String key = intent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY);
50         if (TextUtils.isEmpty(key)) {
51             return;
52         }
53         if (intent.getParcelableExtra(KEY_WIFI_SLICE_URI) == null) {
54             return;
55         }
56         final WifiScanWorker worker = getWifiScanWorker(intent);
57         if (worker == null) {
58             return;
59         }
60         final WifiEntry wifiEntry = worker.getWifiEntry(key);
61         if (wifiEntry == null) {
62             return;
63         }
64         wifiEntry.connect(new WifiEntryConnectCallback(context, wifiEntry));
65     }
66 
67     @VisibleForTesting
getWifiScanWorker(Intent intent)68     WifiScanWorker getWifiScanWorker(Intent intent) {
69         return SliceBackgroundWorker.getInstance(intent.getParcelableExtra(KEY_WIFI_SLICE_URI));
70     }
71 
72     @VisibleForTesting
73     static class WifiEntryConnectCallback implements WifiEntry.ConnectCallback {
74         final Context mContext;
75         final WifiEntry mWifiEntry;
76 
WifiEntryConnectCallback(Context context, WifiEntry connectWifiEntry)77         WifiEntryConnectCallback(Context context, WifiEntry connectWifiEntry) {
78             mContext = context;
79             mWifiEntry = connectWifiEntry;
80         }
81 
82         @Override
onConnectResult(@onnectStatus int status)83         public void onConnectResult(@ConnectStatus int status) {
84             if (status == ConnectCallback.CONNECT_STATUS_FAILURE_NO_CONFIG) {
85                 final Intent intent = new Intent(mContext, WifiDialogActivity.class)
86                         .putExtra(WifiDialogActivity.KEY_CHOSEN_WIFIENTRY_KEY, mWifiEntry.getKey());
87                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
88                 mContext.startActivity(intent);
89             } else if (status == CONNECT_STATUS_FAILURE_UNKNOWN) {
90                 Toast.makeText(mContext, R.string.wifi_failed_connect_message,
91                         Toast.LENGTH_SHORT).show();
92             }
93         }
94     }
95 }
96