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 static com.android.settings.wifi.slice.WifiSlice.DEFAULT_EXPANDED_ROW_COUNT;
20 
21 import android.content.Context;
22 import android.net.Uri;
23 import android.net.wifi.WifiManager;
24 import android.text.TextUtils;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.lifecycle.Lifecycle;
28 import androidx.lifecycle.LifecycleOwner;
29 import androidx.lifecycle.LifecycleRegistry;
30 
31 import com.android.settings.slices.SliceBackgroundWorker;
32 import com.android.settings.wifi.WifiPickerTrackerHelper;
33 import com.android.wifitrackerlib.WifiEntry;
34 import com.android.wifitrackerlib.WifiEntry.WifiEntryCallback;
35 import com.android.wifitrackerlib.WifiPickerTracker;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /**
41  * {@link SliceBackgroundWorker} for Wi-Fi, used by {@link WifiSlice}.
42  *
43  * @deprecated this is not used after V and will be removed.
44  */
45 @Deprecated(forRemoval = true)
46 public class WifiScanWorker extends SliceBackgroundWorker<WifiSliceItem> implements
47         WifiPickerTracker.WifiPickerTrackerCallback, LifecycleOwner, WifiEntryCallback {
48 
49     private static final String TAG = "WifiScanWorker";
50 
51     @VisibleForTesting
52     final LifecycleRegistry mLifecycleRegistry;
53     @VisibleForTesting
54     protected WifiPickerTracker mWifiPickerTracker;
55     protected WifiPickerTrackerHelper mWifiPickerTrackerHelper;
56 
WifiScanWorker(Context context, Uri uri)57     public WifiScanWorker(Context context, Uri uri) {
58         super(context, uri);
59 
60         mLifecycleRegistry = new LifecycleRegistry(this);
61 
62         mWifiPickerTrackerHelper = new WifiPickerTrackerHelper(mLifecycleRegistry, context, this);
63         mWifiPickerTracker = mWifiPickerTrackerHelper.getWifiPickerTracker();
64 
65         mLifecycleRegistry.markState(Lifecycle.State.INITIALIZED);
66         mLifecycleRegistry.markState(Lifecycle.State.CREATED);
67     }
68 
69     @Override
onSlicePinned()70     protected void onSlicePinned() {
71         mLifecycleRegistry.markState(Lifecycle.State.STARTED);
72         mLifecycleRegistry.markState(Lifecycle.State.RESUMED);
73         updateResults();
74     }
75 
76     @Override
onSliceUnpinned()77     protected void onSliceUnpinned() {
78         mLifecycleRegistry.markState(Lifecycle.State.STARTED);
79         mLifecycleRegistry.markState(Lifecycle.State.CREATED);
80     }
81 
82     @Override
close()83     public void close() {
84         mLifecycleRegistry.markState(Lifecycle.State.DESTROYED);
85     }
86 
87     @Override
getLifecycle()88     public Lifecycle getLifecycle() {
89         return mLifecycleRegistry;
90     }
91 
92     /** Called when the state of Wifi has changed. */
93     @Override
onWifiStateChanged()94     public void onWifiStateChanged() {
95         notifySliceChange();
96     }
97 
98     /**
99      * Update the results when data changes
100      */
101     @Override
onWifiEntriesChanged()102     public void onWifiEntriesChanged() {
103         updateResults();
104     }
105 
106     /**
107      * Indicates the state of the WifiEntry has changed and clients may retrieve updates through
108      * the WifiEntry getter methods.
109      */
110     @Override
onUpdated()111     public void onUpdated() {
112         updateResults();
113     }
114 
getApRowCount()115     protected int getApRowCount() {
116         return DEFAULT_EXPANDED_ROW_COUNT;
117     }
118 
119     @Override
onNumSavedSubscriptionsChanged()120     public void onNumSavedSubscriptionsChanged() {
121         // Do nothing.
122     }
123 
124     @Override
onNumSavedNetworksChanged()125     public void onNumSavedNetworksChanged() {
126         // Do nothing.
127     }
128 
129     /**
130      * To get the WifiEntry of key.
131      */
getWifiEntry(String key)132     public WifiEntry getWifiEntry(String key) {
133         // Get specified WifiEntry.
134         WifiEntry keyWifiEntry = null;
135         final WifiEntry connectedWifiEntry = mWifiPickerTracker.getConnectedWifiEntry();
136         if (connectedWifiEntry != null && TextUtils.equals(key, connectedWifiEntry.getKey())) {
137             keyWifiEntry = connectedWifiEntry;
138         } else {
139             for (WifiEntry wifiEntry : mWifiPickerTracker.getWifiEntries()) {
140                 if (TextUtils.equals(key, wifiEntry.getKey())) {
141                     keyWifiEntry = wifiEntry;
142                     break;
143                 }
144             }
145         }
146         return keyWifiEntry;
147     }
148 
149     @VisibleForTesting
updateResults()150     void updateResults() {
151         if (mWifiPickerTracker.getWifiState() != WifiManager.WIFI_STATE_ENABLED
152                 || mLifecycleRegistry.getCurrentState() != Lifecycle.State.RESUMED) {
153             super.updateResults(null);
154             return;
155         }
156 
157         final List<WifiSliceItem> resultList = new ArrayList<>();
158         final WifiEntry connectedWifiEntry = mWifiPickerTracker.getConnectedWifiEntry();
159         if (connectedWifiEntry != null) {
160             connectedWifiEntry.setListener(this);
161             resultList.add(new WifiSliceItem(getContext(), connectedWifiEntry));
162         }
163         for (WifiEntry wifiEntry : mWifiPickerTracker.getWifiEntries()) {
164             if (resultList.size() >= getApRowCount()) {
165                 break;
166             }
167             if (wifiEntry.getLevel() != WifiEntry.WIFI_LEVEL_UNREACHABLE) {
168                 wifiEntry.setListener(this);
169                 resultList.add(new WifiSliceItem(getContext(), wifiEntry));
170             }
171         }
172         super.updateResults(resultList);
173     }
174 
175     /** Enables/disables the carrier network if the carrier network provision disabled */
setCarrierNetworkEnabledIfNeeded(boolean enabled, int subId)176     public void setCarrierNetworkEnabledIfNeeded(boolean enabled, int subId) {
177         if (!mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(subId)) {
178             mWifiPickerTrackerHelper.setCarrierNetworkEnabled(enabled);
179         }
180     }
181 
182     /** Connect to the carrier network */
connectCarrierNetwork()183     public void connectCarrierNetwork() {
184         mWifiPickerTrackerHelper.connectCarrierNetwork(null /* ConnectCallback */);
185     }
186 }
187