1 /*
2  * Copyright (C) 2021 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.tv.settings.connectivity;
18 
19 import android.content.Context;
20 import android.net.ConnectivityManager;
21 import android.net.NetworkInfo;
22 import android.net.wifi.WifiInfo;
23 import android.net.wifi.WifiManager;
24 
25 import androidx.annotation.Nullable;
26 
27 import java.util.Optional;
28 
29 /**
30  * Provides information about the currently connected network synchronously.
31  */
32 public class ActiveNetworkProvider {
33     private final ConnectivityManager mConnectivityManager;
34     private final Optional<WifiManager> mWifiManagerOptional;
35     @Nullable private NetworkInfo mNetworkInfo;
36 
ActiveNetworkProvider(Context context)37     ActiveNetworkProvider(Context context) {
38         mConnectivityManager = context.getSystemService(ConnectivityManager.class);
39         mWifiManagerOptional = Optional.ofNullable(context.getSystemService(WifiManager.class));
40         mNetworkInfo = null;
41     }
42 
updateActiveNetwork()43     void updateActiveNetwork() {
44         mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
45     }
46 
isConnected()47     public boolean isConnected() {
48         return mNetworkInfo != null
49                 && (mNetworkInfo.isConnected() || mNetworkInfo.isConnectedOrConnecting());
50     }
51 
isTypeCellular()52     public boolean isTypeCellular() {
53         return isConnected() && mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
54     }
55 
isTypeEthernet()56     public boolean isTypeEthernet() {
57         return isConnected() && mNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET;
58     }
59 
isTypeWifi()60     public boolean isTypeWifi() {
61         return isConnected() && mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI;
62     }
63 
isWifiEnabled()64     public boolean isWifiEnabled() {
65         return mWifiManagerOptional.map(WifiManager::isWifiEnabled).orElse(false);
66     }
67 
68     @Nullable
getSsid()69     public String getSsid() {
70         if (!isTypeWifi()) {
71             return null;
72         }
73         return mWifiManagerOptional
74                 .map(WifiManager::getConnectionInfo)
75                 .map(WifiInfo::getSSID)
76                 .map(ActiveNetworkProvider::sanitizeSsid)
77                 .orElse(null);
78     }
79 
sanitizeSsid(@ullable String string)80     private static String sanitizeSsid(@Nullable String string) {
81         return removeDoubleQuotes(string);
82     }
83 
removeDoubleQuotes(@ullable String string)84     private static String removeDoubleQuotes(@Nullable String string) {
85         if (string == null) return null;
86         final int length = string.length();
87         if ((length > 1) && (string.charAt(0) == '"') && (string.charAt(length - 1) == '"')) {
88             return string.substring(1, length - 1);
89         }
90         return string;
91     }
92 }
93