• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.net.wifi.WifiManager.ActionListener;
26 import android.os.Bundle;
27 import android.support.annotation.VisibleForTesting;
28 import android.util.Log;
29 
30 import com.android.settings.SetupWizardUtils;
31 import com.android.settingslib.wifi.AccessPoint;
32 import com.android.setupwizardlib.util.WizardManagerHelper;
33 
34 public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialogListener,
35         DialogInterface.OnDismissListener {
36 
37     private static final String TAG = "WifiDialogActivity";
38 
39     private static final int RESULT_CONNECTED = RESULT_FIRST_USER;
40     private static final int RESULT_FORGET = RESULT_FIRST_USER + 1;
41 
42     private static final String KEY_ACCESS_POINT_STATE = "access_point_state";
43     private static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";
44 
45     /**
46      * Boolean extra indicating whether this activity should connect to an access point on the
47      * caller's behalf. If this is set to false, the caller should check
48      * {@link #KEY_WIFI_CONFIGURATION} in the result data and save that using
49      * {@link WifiManager#connect(WifiConfiguration, ActionListener)}. Default is true.
50      */
51     @VisibleForTesting
52     static final String KEY_CONNECT_FOR_CALLER = "connect_for_caller";
53 
54     @Override
onCreate(Bundle savedInstanceState)55     protected void onCreate(Bundle savedInstanceState) {
56         final Intent intent = getIntent();
57         if (WizardManagerHelper.isSetupWizardIntent(intent)) {
58             setTheme(SetupWizardUtils.getTransparentTheme(intent));
59         }
60 
61         super.onCreate(savedInstanceState);
62 
63         final Bundle accessPointState = intent.getBundleExtra(KEY_ACCESS_POINT_STATE);
64         AccessPoint accessPoint = null;
65         if (accessPointState != null) {
66             accessPoint = new AccessPoint(this, accessPointState);
67         }
68 
69         WifiDialog dialog = WifiDialog.createModal(
70                 this, this, accessPoint, WifiConfigUiBase.MODE_CONNECT);
71         dialog.show();
72         dialog.setOnDismissListener(this);
73     }
74 
75     @Override
finish()76     public void finish() {
77         super.finish();
78         overridePendingTransition(0, 0);
79     }
80 
81     @Override
onForget(WifiDialog dialog)82     public void onForget(WifiDialog dialog) {
83         final WifiManager wifiManager = getSystemService(WifiManager.class);
84         final AccessPoint accessPoint = dialog.getController().getAccessPoint();
85         if (accessPoint != null) {
86             if (!accessPoint.isSaved()) {
87                 if (accessPoint.getNetworkInfo() != null &&
88                         accessPoint.getNetworkInfo().getState() != NetworkInfo.State.DISCONNECTED) {
89                     // Network is active but has no network ID - must be ephemeral.
90                     wifiManager.disableEphemeralNetwork(
91                             AccessPoint.convertToQuotedString(accessPoint.getSsidStr()));
92                 } else {
93                     // Should not happen, but a monkey seems to trigger it
94                     Log.e(TAG, "Failed to forget invalid network " + accessPoint.getConfig());
95                 }
96             } else {
97                 wifiManager.forget(accessPoint.getConfig().networkId, null /* listener */);
98             }
99         }
100 
101         Intent resultData = new Intent();
102         if (accessPoint != null) {
103             Bundle accessPointState = new Bundle();
104             accessPoint.saveWifiState(accessPointState);
105             resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
106         }
107         setResult(RESULT_FORGET);
108         finish();
109     }
110 
111     @Override
onSubmit(WifiDialog dialog)112     public void onSubmit(WifiDialog dialog) {
113         final WifiConfiguration config = dialog.getController().getConfig();
114         final AccessPoint accessPoint = dialog.getController().getAccessPoint();
115         final WifiManager wifiManager = getSystemService(WifiManager.class);
116 
117         if (getIntent().getBooleanExtra(KEY_CONNECT_FOR_CALLER, true)) {
118             if (config == null) {
119                 if (accessPoint != null && accessPoint.isSaved()) {
120                     wifiManager.connect(accessPoint.getConfig(), null /* listener */);
121                 }
122             } else {
123                 wifiManager.save(config, null /* listener */);
124                 if (accessPoint != null) {
125                     // accessPoint is null for "Add network"
126                     NetworkInfo networkInfo = accessPoint.getNetworkInfo();
127                     if (networkInfo == null || !networkInfo.isConnected()) {
128                         wifiManager.connect(config, null /* listener */);
129                     }
130                 }
131             }
132         }
133 
134         Intent resultData = new Intent();
135         if (accessPoint != null) {
136             Bundle accessPointState = new Bundle();
137             accessPoint.saveWifiState(accessPointState);
138             resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
139         }
140         if (config != null) {
141             resultData.putExtra(KEY_WIFI_CONFIGURATION, config);
142         }
143         setResult(RESULT_CONNECTED, resultData);
144         finish();
145     }
146 
147     @Override
onDismiss(DialogInterface dialogInterface)148     public void onDismiss(DialogInterface dialogInterface) {
149         finish();
150     }
151 }
152