Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2015 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.app.Activity;
     20 import android.content.DialogInterface;
     21 import android.content.Intent;
     22 import android.net.NetworkInfo;
     23 import android.net.wifi.WifiConfiguration;
     24 import android.net.wifi.WifiManager;
     25 import android.os.Bundle;
     26 import android.util.Log;
     27 
     28 import com.android.settings.SetupWizardUtils;
     29 import com.android.settingslib.wifi.AccessPoint;
     30 import com.android.setupwizardlib.util.WizardManagerHelper;
     31 
     32 public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialogListener,
     33         DialogInterface.OnDismissListener {
     34 
     35     private static final String TAG = "WifiDialogActivity";
     36 
     37     private static final int RESULT_CONNECTED = RESULT_FIRST_USER;
     38     private static final int RESULT_FORGET = RESULT_FIRST_USER + 1;
     39 
     40     private static final String KEY_ACCESS_POINT_STATE = "access_point_state";
     41     private static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";
     42 
     43     @Override
     44     protected void onCreate(Bundle savedInstanceState) {
     45         final Intent intent = getIntent();
     46         if (WizardManagerHelper.isSetupWizardIntent(intent)) {
     47             setTheme(SetupWizardUtils.getTransparentTheme(intent));
     48         }
     49 
     50         super.onCreate(savedInstanceState);
     51 
     52         final Bundle accessPointState = intent.getBundleExtra(KEY_ACCESS_POINT_STATE);
     53         AccessPoint accessPoint = null;
     54         if (accessPointState != null) {
     55             accessPoint = new AccessPoint(this, accessPointState);
     56         }
     57 
     58         WifiDialog dialog = new WifiDialog(this, this, accessPoint, WifiConfigUiBase.MODE_CONNECT);
     59         dialog.show();
     60         dialog.setOnDismissListener(this);
     61     }
     62 
     63     @Override
     64     public void finish() {
     65         super.finish();
     66         overridePendingTransition(0, 0);
     67     }
     68 
     69     @Override
     70     public void onForget(WifiDialog dialog) {
     71         final WifiManager wifiManager = getSystemService(WifiManager.class);
     72         final AccessPoint accessPoint = dialog.getController().getAccessPoint();
     73         if (accessPoint != null) {
     74             if (!accessPoint.isSaved()) {
     75                 if (accessPoint.getNetworkInfo() != null &&
     76                         accessPoint.getNetworkInfo().getState() != NetworkInfo.State.DISCONNECTED) {
     77                     // Network is active but has no network ID - must be ephemeral.
     78                     wifiManager.disableEphemeralNetwork(
     79                             AccessPoint.convertToQuotedString(accessPoint.getSsidStr()));
     80                 } else {
     81                     // Should not happen, but a monkey seems to trigger it
     82                     Log.e(TAG, "Failed to forget invalid network " + accessPoint.getConfig());
     83                 }
     84             } else {
     85                 wifiManager.forget(accessPoint.getConfig().networkId, null /* listener */);
     86             }
     87         }
     88 
     89         Intent resultData = new Intent();
     90         if (accessPoint != null) {
     91             Bundle accessPointState = new Bundle();
     92             accessPoint.saveWifiState(accessPointState);
     93             resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
     94         }
     95         setResult(RESULT_FORGET);
     96         finish();
     97     }
     98 
     99     @Override
    100     public void onSubmit(WifiDialog dialog) {
    101         final WifiConfiguration config = dialog.getController().getConfig();
    102         final AccessPoint accessPoint = dialog.getController().getAccessPoint();
    103         final WifiManager wifiManager = getSystemService(WifiManager.class);
    104 
    105         if (config == null) {
    106             if (accessPoint != null && accessPoint.isSaved()) {
    107                 wifiManager.connect(accessPoint.getConfig(), null /* listener */);
    108             }
    109         } else {
    110             wifiManager.save(config, null /* listener */);
    111             if (accessPoint != null) {
    112                 // accessPoint is null for "Add network"
    113                 NetworkInfo networkInfo = accessPoint.getNetworkInfo();
    114                 if (networkInfo == null || !networkInfo.isConnected()) {
    115                     wifiManager.connect(config, null /* listener */);
    116                 }
    117             }
    118         }
    119 
    120         Intent resultData = new Intent();
    121         if (accessPoint != null) {
    122             Bundle accessPointState = new Bundle();
    123             accessPoint.saveWifiState(accessPointState);
    124             resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
    125         }
    126         if (config != null) {
    127             resultData.putExtra(KEY_WIFI_CONFIGURATION, config);
    128         }
    129         setResult(RESULT_CONNECTED, resultData);
    130         finish();
    131     }
    132 
    133     @Override
    134     public void onDismiss(DialogInterface dialogInterface) {
    135         finish();
    136     }
    137 }
    138