• 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.wifi.WifiManager;
24 import android.support.annotation.VisibleForTesting;
25 import com.android.settings.R;
26 import com.android.settings.widget.SummaryUpdater;
27 import com.android.settingslib.wifi.WifiStatusTracker;
28 
29 import static android.net.wifi.WifiInfo.removeDoubleQuotes;
30 
31 /**
32  * Helper class that listeners to wifi callback and notify client when there is update in
33  * wifi summary info.
34  */
35 public final class WifiSummaryUpdater extends SummaryUpdater {
36 
37     private final WifiStatusTracker mWifiTracker;
38     private final BroadcastReceiver mReceiver;
39 
40     private static final IntentFilter INTENT_FILTER;
41     static {
42         INTENT_FILTER = new IntentFilter();
43         INTENT_FILTER.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
44         INTENT_FILTER.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
45         INTENT_FILTER.addAction(WifiManager.RSSI_CHANGED_ACTION);
46     }
47 
WifiSummaryUpdater(Context context, OnSummaryChangeListener listener)48     public WifiSummaryUpdater(Context context, OnSummaryChangeListener listener) {
49         this(context, listener, new WifiStatusTracker(context.getSystemService(WifiManager.class)));
50     }
51 
52     @VisibleForTesting
WifiSummaryUpdater(Context context, OnSummaryChangeListener listener, WifiStatusTracker wifiTracker)53     public WifiSummaryUpdater(Context context, OnSummaryChangeListener listener,
54         WifiStatusTracker wifiTracker) {
55         super(context, listener);
56         mWifiTracker = wifiTracker;
57         mReceiver = new BroadcastReceiver() {
58             @Override
59             public void onReceive(Context context, Intent intent) {
60                 mWifiTracker.handleBroadcast(intent);
61                 notifyChangeIfNeeded();
62             }
63         };
64     }
65 
66     @Override
register(boolean register)67     public void register(boolean register) {
68         if (register) {
69             mContext.registerReceiver(mReceiver, INTENT_FILTER);
70         } else {
71             mContext.unregisterReceiver(mReceiver);
72         }
73     }
74 
75     @Override
getSummary()76     public String getSummary() {
77         if (!mWifiTracker.enabled) {
78             return mContext.getString(R.string.switch_off_text);
79         }
80         if (!mWifiTracker.connected) {
81             return mContext.getString(R.string.disconnected);
82         }
83         return removeDoubleQuotes(mWifiTracker.ssid);
84     }
85 
86 }
87