• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.net.ConnectivityManager;
24 import android.net.NetworkScoreManager;
25 import android.net.wifi.WifiInfo;
26 import android.net.wifi.WifiManager;
27 import android.text.TextUtils;
28 
29 import androidx.annotation.VisibleForTesting;
30 
31 import com.android.settings.R;
32 import com.android.settings.widget.SummaryUpdater;
33 import com.android.settingslib.wifi.WifiStatusTracker;
34 
35 /**
36  * Helper class that listeners to wifi callback and notify client when there is update in
37  * wifi summary info.
38  */
39 public final class WifiSummaryUpdater extends SummaryUpdater {
40 
41     private final WifiStatusTracker mWifiTracker;
42     private final BroadcastReceiver mReceiver;
43 
44     private static final IntentFilter INTENT_FILTER;
45     static {
46         INTENT_FILTER = new IntentFilter();
47         INTENT_FILTER.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
48         INTENT_FILTER.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
49         INTENT_FILTER.addAction(WifiManager.RSSI_CHANGED_ACTION);
50     }
51 
WifiSummaryUpdater(Context context, OnSummaryChangeListener listener)52     public WifiSummaryUpdater(Context context, OnSummaryChangeListener listener) {
53         this(context, listener, null);
54     }
55 
56     @VisibleForTesting
WifiSummaryUpdater(Context context, OnSummaryChangeListener listener, WifiStatusTracker wifiTracker)57     public WifiSummaryUpdater(Context context, OnSummaryChangeListener listener,
58         WifiStatusTracker wifiTracker) {
59         super(context, listener);
60         mWifiTracker = wifiTracker != null ? wifiTracker :
61                 new WifiStatusTracker(context, context.getSystemService(WifiManager.class),
62                 context.getSystemService(NetworkScoreManager.class),
63                 context.getSystemService(ConnectivityManager.class),
64                         this::notifyChangeIfNeeded);
65         mReceiver = new BroadcastReceiver() {
66             @Override
67             public void onReceive(Context context, Intent intent) {
68                 mWifiTracker.handleBroadcast(intent);
69                 notifyChangeIfNeeded();
70             }
71         };
72     }
73 
74     @Override
register(boolean register)75     public void register(boolean register) {
76         if (register) {
77             mWifiTracker.fetchInitialState();
78             notifyChangeIfNeeded();
79             mContext.registerReceiver(mReceiver, INTENT_FILTER);
80         } else {
81             mContext.unregisterReceiver(mReceiver);
82         }
83         mWifiTracker.setListening(register);
84     }
85 
86     @Override
getSummary()87     public String getSummary() {
88         if (!mWifiTracker.enabled) {
89             return mContext.getString(R.string.switch_off_text);
90         }
91         if (!mWifiTracker.connected) {
92             return mContext.getString(R.string.disconnected);
93         }
94         String ssid = WifiInfo.sanitizeSsid(mWifiTracker.ssid);
95         if (TextUtils.isEmpty(mWifiTracker.statusLabel)) {
96             return ssid;
97         }
98         return mContext.getResources().getString(
99                 com.android.settingslib.R.string.preference_summary_default_combination,
100                 ssid, mWifiTracker.statusLabel);
101     }
102 
103     /**
104      * return true if Wi-Fi connected.
105      */
isWifiConnected()106     public boolean isWifiConnected() {
107         return mWifiTracker.connected;
108     }
109 }
110