• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 com.android.tv.settings.MenuItem;
20 import com.android.tv.settings.R;
21 import com.android.tv.settings.util.UriUtils;
22 
23 import android.content.Context;
24 import android.net.ConnectivityManager;
25 import android.net.NetworkInfo;
26 import android.net.wifi.WifiConfiguration;
27 import android.net.wifi.WifiInfo;
28 import android.net.wifi.WifiManager;
29 import android.util.Log;
30 
31 /**
32  * Gets an icon uri based on the current connectivity status.
33  */
34 public class ConnectivityStatusIconUriGetter implements MenuItem.UriGetter {
35 
36     private static final String TAG = "ConnectivityStatusIconUriGetter";
37     private static final boolean DEBUG = true;
38 
39     private final Context mContext;
40     private final int mEthernetDrawableResourceId;
41     private final int mDisconnectedDrawableResourceId;
42     private final ConnectivityManager mConnectivityManager;
43     private final WifiManager mWifiManager;
44 
createWifiStatusIconUriGetter(Context context)45     public static ConnectivityStatusIconUriGetter createWifiStatusIconUriGetter(Context context) {
46         return new ConnectivityStatusIconUriGetter(context, R.drawable.ic_settings_wifi_4,
47                 R.drawable.ic_settings_wifi_4);
48     }
49 
ConnectivityStatusIconUriGetter(Context context)50     public ConnectivityStatusIconUriGetter(Context context) {
51         this(context, R.drawable.ic_settings_ethernet_active,
52                 R.drawable.ic_settings_connection_status);
53     }
54 
ConnectivityStatusIconUriGetter(Context context, int ethernetDrawableResourceId, int disconnectedDrawableResourceId)55     public ConnectivityStatusIconUriGetter(Context context, int ethernetDrawableResourceId,
56             int disconnectedDrawableResourceId) {
57         mContext = context;
58         mEthernetDrawableResourceId = ethernetDrawableResourceId;
59         mDisconnectedDrawableResourceId = disconnectedDrawableResourceId;
60         mConnectivityManager = (ConnectivityManager) mContext.getSystemService(
61                 Context.CONNECTIVITY_SERVICE);
62         mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
63     }
64 
65     @Override
getUri()66     public String getUri() {
67         String uri = UriUtils.getAndroidResourceUri(mContext, getConnectionStatusResId());
68         if (DEBUG) {
69             Log.d(TAG, "Returning connectivity status icon: " + uri);
70         }
71         return uri;
72     }
73 
getConnectionStatusResId()74     private int getConnectionStatusResId() {
75         NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
76         if (networkInfo == null) {
77             return mDisconnectedDrawableResourceId;
78         }
79 
80         switch (networkInfo.getType()) {
81             case ConnectivityManager.TYPE_WIFI:
82                 return getWifiDrawableResourceId();
83             case ConnectivityManager.TYPE_ETHERNET:
84                 return mEthernetDrawableResourceId;
85             default:
86                 return mDisconnectedDrawableResourceId;
87         }
88     }
89 
getWifiDrawableResourceId()90     private int getWifiDrawableResourceId() {
91         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
92         boolean isOpen = isOpenNetwork(mWifiManager, wifiInfo);
93         int strength = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 4);
94 
95         switch (strength) {
96             case 0:
97                 return isOpen ? R.drawable.ic_settings_wifi_active_1
98                         : R.drawable.ic_settings_wifi_secure_active_1;
99             case 1:
100                 return isOpen ? R.drawable.ic_settings_wifi_active_2
101                         : R.drawable.ic_settings_wifi_secure_active_2;
102             case 2:
103                 return isOpen ? R.drawable.ic_settings_wifi_active_3
104                         : R.drawable.ic_settings_wifi_secure_active_3;
105             case 3:
106                 return isOpen ? R.drawable.ic_settings_wifi_active_4
107                         : R.drawable.ic_settings_wifi_secure_active_4;
108         }
109         return mDisconnectedDrawableResourceId;
110     }
111 
isOpenNetwork(WifiManager wifiMan, WifiInfo wifiInfo)112     private boolean isOpenNetwork(WifiManager wifiMan, WifiInfo wifiInfo) {
113         WifiConfiguration network = WifiConfigHelper.getWifiConfiguration(wifiMan,
114                 wifiInfo.getNetworkId());
115         if (network != null) {
116             return WifiSecurity.isOpen(network);
117         }
118         Log.w(TAG, "Could not determine if network is secure or not! Defaulting to open icon.");
119         return true;
120     }
121 }
122