Home | History | Annotate | Download | only in connectivity
      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 
     22 import android.content.Context;
     23 import android.net.ConnectivityManager;
     24 import android.net.NetworkInfo;
     25 import android.net.wifi.WifiInfo;
     26 import android.net.wifi.WifiManager;
     27 
     28 /**
     29  * Gets a text string based on the current connectivity status.
     30  */
     31 public class ConnectivityStatusTextGetter implements MenuItem.TextGetter {
     32 
     33     private final Context mContext;
     34     private final int mEthernetStringResourceId;
     35     private final int mWifiStringResourceId;
     36     private final boolean mUseSsid;
     37     private final ConnectivityManager mConnectivityManager;
     38     private final WifiManager mWifiManager;
     39 
     40     public static ConnectivityStatusTextGetter createEthernetStatusTextGetter(Context context) {
     41         return new ConnectivityStatusTextGetter(context, R.string.connected,
     42                 R.string.not_connected, false);
     43     }
     44 
     45     public static ConnectivityStatusTextGetter createWifiStatusTextGetter(Context context) {
     46         return new ConnectivityStatusTextGetter(context, R.string.not_connected,
     47                 R.string.not_connected, true);
     48     }
     49 
     50     public ConnectivityStatusTextGetter(Context context) {
     51         this(context, R.string.connectivity_ethernet, R.string.connectivity_wifi, true);
     52     }
     53 
     54     public ConnectivityStatusTextGetter(Context context, int ethernetStringResourceId,
     55             int wifiStringResourceId, boolean useSsid) {
     56         mContext = context;
     57         mEthernetStringResourceId = ethernetStringResourceId;
     58         mWifiStringResourceId = wifiStringResourceId;
     59         mUseSsid = useSsid;
     60         mConnectivityManager = (ConnectivityManager) mContext.getSystemService(
     61                 Context.CONNECTIVITY_SERVICE);
     62         mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
     63     }
     64 
     65     @Override
     66     public String getText() {
     67         NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
     68 
     69         if (networkInfo != null) {
     70             switch (networkInfo.getType()) {
     71                 case ConnectivityManager.TYPE_WIFI:
     72                     return getWifiString();
     73                 case ConnectivityManager.TYPE_ETHERNET:
     74                     return mContext.getString(mEthernetStringResourceId);
     75                 default:
     76                     break;
     77             }
     78         }
     79 
     80         return mContext.getString(R.string.not_connected);
     81     }
     82 
     83     private String getWifiString() {
     84         if (mUseSsid) {
     85             WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
     86             if (wifiInfo != null) {
     87                 String ssid = wifiInfo.getSSID();
     88                 if (ssid != null) {
     89                     return WifiInfo.removeDoubleQuotes(ssid);
     90                 }
     91             }
     92         }
     93         return mContext.getString(mWifiStringResourceId);
     94     }
     95 }
     96