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.ActionBehavior;
     20 import com.android.tv.settings.ActionKey;
     21 import com.android.tv.settings.BaseSettingsActivity;
     22 import com.android.tv.settings.R;
     23 import com.android.tv.settings.dialog.old.Action;
     24 import com.android.tv.settings.dialog.old.ActionAdapter;
     25 import com.android.tv.settings.dialog.old.ContentFragment;
     26 
     27 import android.content.Context;
     28 import android.net.ConnectivityManager;
     29 import android.net.IpConfiguration.IpAssignment;
     30 import android.net.IpConfiguration.ProxySettings;
     31 import android.net.NetworkInfo;
     32 import android.net.Uri;
     33 import android.net.wifi.WifiConfiguration;
     34 import android.net.wifi.WifiInfo;
     35 import android.net.wifi.WifiManager;
     36 
     37 import android.os.Bundle;
     38 import android.util.Log;
     39 import android.os.Handler;
     40 import android.content.Intent;
     41 
     42 /**
     43  * Activity to view the status and modify the configuration of the currently
     44  * connected wifi network.
     45  */
     46 
     47 public class WifiConfigurationActivity extends BaseSettingsActivity
     48         implements ActionAdapter.Listener, ConnectivityListener.Listener {
     49 
     50     protected static final String TAG = "WifiConfigurationActivity";
     51     private static final boolean DEBUG = false;
     52 
     53     private static final int INET_CONDITION_THRESHOLD = 50;
     54     private static final int REQUEST_CODE_ADVANCED_OPTIONS = 1;
     55 
     56     private ConnectivityListener mConnectivityListener;
     57     private ConnectivityStatusIconUriGetter mWifiStatusIconUriGetter;
     58     private ConnectivityManager mConnectivityManager;
     59     private WifiManager mWifiManager;
     60     private boolean mInetConnected;
     61     private Handler mHandler;
     62 
     63     private Runnable mMainRefreshView = new Runnable() {
     64         @Override
     65         public void run() {
     66             updateView();
     67         }
     68     };
     69 
     70     @Override
     71     protected void onCreate(Bundle savedInstanceState) {
     72         mConnectivityListener = new ConnectivityListener(this, this);
     73         mWifiStatusIconUriGetter =
     74             ConnectivityStatusIconUriGetter.createWifiStatusIconUriGetter(this);
     75         mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     76         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
     77         super.onCreate(savedInstanceState);
     78         if (DEBUG) Log.d(TAG, "onCreate");
     79         mHandler = new Handler();
     80     }
     81 
     82     @Override
     83     protected void onResume() {
     84         super.onResume();
     85         if (DEBUG) Log.d(TAG, "onResume");
     86         mConnectivityListener.start();
     87     }
     88 
     89     @Override
     90     protected void onPause() {
     91         mConnectivityListener.stop();
     92         super.onPause();
     93         if (DEBUG) Log.d(TAG, "onPause");
     94     }
     95 
     96     @Override
     97     protected Object getInitialState() {
     98         if (DEBUG) Log.d(TAG, "getInitialState");
     99         return ActionType.CONECTIVITY_SETTINGS_MAIN;
    100     }
    101 
    102     @Override
    103     public void onConnectivityChange(Intent intent) {
    104         if (DEBUG) Log.d(TAG, "onConnectivityChange  intent " + intent);
    105         String intentAction = intent.getAction();
    106         boolean inetConnectedChanged = false;
    107         if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION)
    108                 || intentAction.equals(ConnectivityManager.INET_CONDITION_ACTION)) {
    109             int connectionStatus = intent.getIntExtra(ConnectivityManager.EXTRA_INET_CONDITION, 0);
    110             boolean ic = connectionStatus > INET_CONDITION_THRESHOLD ? true : false;
    111             if (ic != mInetConnected) {
    112                 inetConnectedChanged = true;
    113                 mInetConnected = ic;
    114             }
    115             if (DEBUG) Log.d(TAG, "onConnectivityChange  mInetConnected " + mInetConnected);
    116         }
    117         if (inetConnectedChanged) {
    118             mHandler.post(mMainRefreshView);
    119         } else {
    120             updateIconUriIfNecessary();
    121         }
    122     }
    123 
    124     private void updateIconUriIfNecessary() {
    125         if(mContentFragment instanceof ContentFragment) {
    126             ContentFragment cf = (ContentFragment) mContentFragment;
    127             Uri oldUri = cf.getIconResourceUri();
    128             Uri newUri = Uri.parse(mWifiStatusIconUriGetter.getUri());
    129             if (!oldUri.equals(newUri)) {
    130                 cf.setIcon(newUri);
    131             }
    132         }
    133     }
    134 
    135     private WifiInfo getWifiInfo() {
    136         NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
    137         if (networkInfo == null || networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
    138             return null;
    139         } else {
    140             return mWifiManager.getConnectionInfo();
    141         }
    142     }
    143 
    144     @Override
    145     protected void refreshActionList() {
    146         if (DEBUG) Log.d(TAG, "refreshActionList");
    147         mActions.clear();
    148         switch ((ActionType) mState) {
    149             case CONECTIVITY_SETTINGS_MAIN:
    150                 mActions.add(ActionType.CONECTIVITY_SETTINGS_STATUS_INFO.toAction(mResources));
    151                 mActions.add(ActionType.CONECTIVITY_SETTINGS_ADVANCED_OPTIONS.toAction(mResources));
    152                 mActions.add(ActionType.CONECTIVITY_SETTINGS_FORGET_NETWORK.toAction(mResources));
    153                 break;
    154             case CONECTIVITY_SETTINGS_STATUS_INFO: {
    155                 boolean isConnected = false;
    156                 WifiInfo wifiInfo = getWifiInfo();
    157                 if (wifiInfo != null) {
    158                     NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
    159                     if (networkInfo != null &&
    160                         networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
    161                         mInetConnected) {
    162                         isConnected = true;
    163                     }
    164                 }
    165 
    166                 if (!isConnected) {
    167                     mActions.add(ActionType.CONECTIVITY_SETTINGS_CONNECTION.
    168                                      toInfo(mResources, R.string.not_connected));
    169                 } else {
    170                     // If we're on a wifi-network and the status is good...
    171                     mActions.add(
    172                         ActionType.CONECTIVITY_SETTINGS_CONNECTION.
    173                             toInfo(mResources, R.string.connected));
    174 
    175                     int ip = wifiInfo.getIpAddress();
    176                     mActions.add(ActionType.CONECTIVITY_SETTINGS_IP_ADDRESS.
    177                         toInfo(mResources,
    178                                  String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff),
    179                                                (ip >> 16 & 0xff), (ip >> 24 & 0xff))));
    180 
    181                     mActions.add(ActionType.CONECTIVITY_SETTINGS_MAC_ADDRESS.
    182                         toInfo(mResources, wifiInfo.getMacAddress()));
    183 
    184                     String[] signalLevels =
    185                         getResources().getStringArray(R.array.wifi_signal_strength);
    186                     int strength =
    187                         WifiManager.
    188                             calculateSignalLevel(wifiInfo.getRssi(), signalLevels.length);
    189                     mActions.add(ActionType.CONECTIVITY_SETTINGS_SIGNAL_STRENGTH.
    190                         toInfo(mResources, signalLevels[strength]));
    191                 }
    192                 break;
    193             }
    194             case CONECTIVITY_SETTINGS_ADVANCED_OPTIONS: {
    195                 WifiInfo wifiInfo = getWifiInfo();
    196                 if (wifiInfo != null) {
    197                     WifiConfiguration wifiConfiguration =
    198                         WifiConfigHelper.getWifiConfiguration(
    199                             mWifiManager, wifiInfo.getNetworkId());
    200                     if (wifiConfiguration != null) {
    201                         int proxySettingsResourceId =
    202                             (wifiConfiguration.getProxySettings() == ProxySettings.NONE) ?
    203                                 R.string.wifi_action_proxy_none :
    204                                 R.string.wifi_action_proxy_manual;
    205                         mActions.add(ActionType.CONECTIVITY_SETTINGS_PROXY_SETTINGS.
    206                                             toAction(mResources, proxySettingsResourceId));
    207 
    208                         int ipSettingsResourceId =
    209                            (wifiConfiguration.getIpAssignment() == IpAssignment.STATIC) ?
    210                                 R.string.wifi_action_static :
    211                                 R.string.wifi_action_dhcp;
    212                         mActions.add(ActionType.CONECTIVITY_SETTINGS_IP_SETTINGS.
    213                                             toAction(mResources, ipSettingsResourceId));
    214                     }
    215                 } else {
    216                     mActions.add(ActionType.CONECTIVITY_SETTINGS_CONNECTION.
    217                                      toInfo(mResources, R.string.not_connected));
    218                 }
    219                 break;
    220             }
    221 
    222             case CONECTIVITY_SETTINGS_FORGET_NETWORK: {
    223                 String okKey =
    224                     new ActionKey<ActionType, ActionBehavior>(
    225                         ActionType.CONECTIVITY_SETTINGS_FORGET_NETWORK, ActionBehavior.OK).getKey();
    226                 mActions.add(
    227                     new Action.Builder()
    228                         .key(okKey)
    229                         .title(getString(R.string.wifi_forget_network))
    230                         .build());
    231                 String cancelKey =
    232                     new ActionKey<ActionType, ActionBehavior>(
    233                             ActionType.CONECTIVITY_SETTINGS_FORGET_NETWORK,
    234                             ActionBehavior.CANCEL).getKey();
    235                 mActions.add(
    236                     new Action.Builder()
    237                         .key(cancelKey)
    238                         .title(getString(R.string.settings_cancel))
    239                         .build());
    240                 break;
    241             }
    242         }
    243     }
    244 
    245     private String getNetworkName() {
    246         NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
    247         String name = getString(R.string.connectivity_wifi);
    248         if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
    249             WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    250             if (wifiInfo != null) {
    251                 name = WifiInfo.removeDoubleQuotes(wifiInfo.getSSID());
    252             }
    253         }
    254         return name;
    255     }
    256 
    257     @Override
    258     protected void updateView() {
    259         refreshActionList();
    260         if (DEBUG) Log.d(TAG, "updateView  mState " + (ActionType) mState);
    261         switch ((ActionType) mState) {
    262             case CONECTIVITY_SETTINGS_MAIN: {
    263                 setView(getNetworkName(), null, null,
    264                         Uri.parse(mWifiStatusIconUriGetter.getUri()));
    265                 break;
    266             }
    267             case CONECTIVITY_SETTINGS_STATUS_INFO: {
    268                 setView(getString(R.string.wifi_action_status_info), getNetworkName(), null,
    269                         Uri.parse(mWifiStatusIconUriGetter.getUri()));
    270                 break;
    271             }
    272             case CONECTIVITY_SETTINGS_ADVANCED_OPTIONS: {
    273                 setView(getString(R.string.wifi_action_advanced_options_title),
    274                         getNetworkName(), null, Uri.parse(mWifiStatusIconUriGetter.getUri()));
    275                 break;
    276             }
    277             case CONECTIVITY_SETTINGS_FORGET_NETWORK: {
    278                 setView(R.string.wifi_forget_network, getNetworkName(),
    279                         R.string.wifi_forget_network_description,
    280                         Uri.parse(mWifiStatusIconUriGetter.getUri()));
    281                 break;
    282             }
    283         }
    284     }
    285 
    286     @Override
    287     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    288         if (requestCode == REQUEST_CODE_ADVANCED_OPTIONS && resultCode == RESULT_OK) {
    289             updateView();
    290         } else {
    291             super.onActivityResult(requestCode, resultCode, data);
    292         }
    293     }
    294 
    295     @Override
    296     protected void setProperty(boolean enable) {
    297     }
    298 
    299     private int getNetworkId() {
    300         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    301         if (wifiInfo != null) {
    302               return wifiInfo.getNetworkId();
    303         }
    304         return -1;
    305     }
    306 
    307     @Override
    308     public void onActionClicked(Action action) {
    309         if (DEBUG) Log.d(TAG, "onActionClicked " + action.getKey());
    310 
    311         ActionKey<ActionType, ActionBehavior> actionKey =
    312             new ActionKey<ActionType, ActionBehavior>(
    313                 ActionType.class, ActionBehavior.class, action.getKey());
    314         final ActionType type = actionKey.getType();
    315         final ActionBehavior behavior = actionKey.getBehavior();
    316 
    317         switch (type) {
    318             case CONECTIVITY_SETTINGS_STATUS_INFO:
    319                 switch (behavior) {
    320                     case INIT:
    321                         setState(type, true);
    322                         break;
    323                 }
    324                 break;
    325             case CONECTIVITY_SETTINGS_ADVANCED_OPTIONS:
    326                 switch (behavior) {
    327                     case INIT:
    328                         setState(type, true);
    329                         break;
    330                 }
    331                 break;
    332             case CONECTIVITY_SETTINGS_PROXY_SETTINGS:
    333                 switch (behavior) {
    334                     case INIT: {
    335                         int networkId = getNetworkId();
    336                         if (networkId != -1) {
    337                             startActivityForResult(
    338                                 EditProxySettingsActivity.createIntent(this, networkId),
    339                                 REQUEST_CODE_ADVANCED_OPTIONS);
    340                         }
    341                         break;
    342                     }
    343                 }
    344                 break;
    345             case CONECTIVITY_SETTINGS_IP_SETTINGS:
    346                 switch (behavior) {
    347                     case INIT: {
    348                         int networkId = getNetworkId();
    349                         if (networkId != -1) {
    350                             startActivityForResult(
    351                                 EditIpSettingsActivity.createIntent(this, networkId),
    352                                 REQUEST_CODE_ADVANCED_OPTIONS);
    353                         }
    354                         break;
    355                     }
    356                 }
    357                 break;
    358             case CONECTIVITY_SETTINGS_FORGET_NETWORK: {
    359                 switch (behavior) {
    360                     case INIT:
    361                         setState(type, true);
    362                         break;
    363                     case OK: {
    364                         int networkId = getNetworkId();
    365                         if (networkId != -1) {
    366                             mWifiManager.forget(networkId, null);
    367                         }
    368                         setResult(RESULT_OK);
    369                         finish();
    370                         break;
    371                     }
    372                     case CANCEL:
    373                         goBack();
    374                         break;
    375                 }
    376                 break;
    377             }
    378         }
    379     }
    380 }
    381