1 /* 2 * Copyright (C) 2012 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 package com.android.settings.wifi; 17 18 import android.app.AlertDialog; 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.NetworkInfo; 24 import android.net.NetworkInfo.DetailedState; 25 import android.net.wifi.WifiInfo; 26 import android.net.wifi.WifiManager; 27 import android.net.wifi.WpsInfo; 28 import android.os.Bundle; 29 import android.os.Handler; 30 import android.os.Message; 31 import android.view.View; 32 import android.widget.Button; 33 import android.widget.ProgressBar; 34 import android.widget.TextView; 35 36 import java.util.Timer; 37 import java.util.TimerTask; 38 39 import com.android.settings.R; 40 41 42 /** 43 * Dialog to show WPS progress. 44 */ 45 public class WpsDialog extends AlertDialog { 46 47 private final static String TAG = "WpsDialog"; 48 49 private View mView; 50 private TextView mTextView; 51 private ProgressBar mTimeoutBar; 52 private ProgressBar mProgressBar; 53 private Button mButton; 54 private Timer mTimer; 55 56 private static final int WPS_TIMEOUT_S = 120; 57 58 private WifiManager mWifiManager; 59 private WifiManager.Channel mChannel; 60 private WifiManager.WpsListener mWpsListener; 61 private int mWpsSetup; 62 63 private final IntentFilter mFilter; 64 private BroadcastReceiver mReceiver; 65 66 private Context mContext; 67 private Handler mHandler = new Handler(); 68 69 private enum DialogState { 70 WPS_INIT, 71 WPS_START, 72 WPS_COMPLETE, 73 CONNECTED, //WPS + IP config is done 74 WPS_FAILED 75 } 76 DialogState mDialogState = DialogState.WPS_INIT; 77 WpsDialog(Context context, int wpsSetup)78 public WpsDialog(Context context, int wpsSetup) { 79 super(context); 80 mContext = context; 81 mWpsSetup = wpsSetup; 82 83 class WpsListener implements WifiManager.WpsListener { 84 public void onStartSuccess(String pin) { 85 if (pin != null) { 86 updateDialog(DialogState.WPS_START, String.format( 87 mContext.getString(R.string.wifi_wps_onstart_pin), pin)); 88 } else { 89 updateDialog(DialogState.WPS_START, mContext.getString( 90 R.string.wifi_wps_onstart_pbc)); 91 } 92 } 93 public void onCompletion() { 94 updateDialog(DialogState.WPS_COMPLETE, 95 mContext.getString(R.string.wifi_wps_complete)); 96 } 97 98 public void onFailure(int reason) { 99 String msg; 100 switch (reason) { 101 case WifiManager.WPS_OVERLAP_ERROR: 102 msg = mContext.getString(R.string.wifi_wps_failed_overlap); 103 break; 104 case WifiManager.WPS_WEP_PROHIBITED: 105 msg = mContext.getString(R.string.wifi_wps_failed_wep); 106 break; 107 case WifiManager.WPS_TKIP_ONLY_PROHIBITED: 108 msg = mContext.getString(R.string.wifi_wps_failed_tkip); 109 break; 110 case WifiManager.IN_PROGRESS: 111 msg = mContext.getString(R.string.wifi_wps_in_progress); 112 break; 113 default: 114 msg = mContext.getString(R.string.wifi_wps_failed_generic); 115 break; 116 } 117 updateDialog(DialogState.WPS_FAILED, msg); 118 } 119 } 120 121 mWpsListener = new WpsListener(); 122 123 124 mFilter = new IntentFilter(); 125 mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 126 mReceiver = new BroadcastReceiver() { 127 @Override 128 public void onReceive(Context context, Intent intent) { 129 handleEvent(context, intent); 130 } 131 }; 132 } 133 134 @Override onCreate(Bundle savedInstanceState)135 protected void onCreate(Bundle savedInstanceState) { 136 mView = getLayoutInflater().inflate(R.layout.wifi_wps_dialog, null); 137 138 mTextView = (TextView) mView.findViewById(R.id.wps_dialog_txt); 139 mTextView.setText(R.string.wifi_wps_setup_msg); 140 141 mTimeoutBar = ((ProgressBar) mView.findViewById(R.id.wps_timeout_bar)); 142 mTimeoutBar.setMax(WPS_TIMEOUT_S); 143 mTimeoutBar.setProgress(0); 144 145 mProgressBar = ((ProgressBar) mView.findViewById(R.id.wps_progress_bar)); 146 mProgressBar.setVisibility(View.GONE); 147 148 mButton = ((Button) mView.findViewById(R.id.wps_dialog_btn)); 149 mButton.setText(R.string.wifi_cancel); 150 mButton.setOnClickListener(new View.OnClickListener() { 151 @Override 152 public void onClick(View v) { 153 dismiss(); 154 } 155 }); 156 157 mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); 158 mChannel = mWifiManager.initialize(mContext, mContext.getMainLooper(), null); 159 160 setView(mView); 161 super.onCreate(savedInstanceState); 162 } 163 164 @Override onStart()165 protected void onStart() { 166 /* 167 * increment timeout bar per second. 168 */ 169 mTimer = new Timer(false); 170 mTimer.schedule(new TimerTask() { 171 @Override 172 public void run() { 173 mHandler.post(new Runnable() { 174 175 @Override 176 public void run() { 177 mTimeoutBar.incrementProgressBy(1); 178 } 179 }); 180 } 181 }, 1000, 1000); 182 183 mContext.registerReceiver(mReceiver, mFilter); 184 185 WpsInfo wpsConfig = new WpsInfo(); 186 wpsConfig.setup = mWpsSetup; 187 mWifiManager.startWps(mChannel, wpsConfig, mWpsListener); 188 } 189 190 @Override onStop()191 protected void onStop() { 192 if (mDialogState != DialogState.WPS_COMPLETE) { 193 mWifiManager.cancelWps(mChannel, null); 194 } 195 196 if (mReceiver != null) { 197 mContext.unregisterReceiver(mReceiver); 198 mReceiver = null; 199 } 200 201 if (mTimer != null) { 202 mTimer.cancel(); 203 } 204 } 205 updateDialog(DialogState state, String msg)206 private void updateDialog(DialogState state, String msg) { 207 if (mDialogState.ordinal() >= state.ordinal()) { 208 //ignore. 209 return; 210 } 211 mDialogState = state; 212 213 switch(state) { 214 case WPS_COMPLETE: 215 mTimeoutBar.setVisibility(View.GONE); 216 mProgressBar.setVisibility(View.VISIBLE); 217 break; 218 case CONNECTED: 219 case WPS_FAILED: 220 mButton.setText(mContext.getString(R.string.dlg_ok)); 221 mTimeoutBar.setVisibility(View.GONE); 222 mProgressBar.setVisibility(View.GONE); 223 if (mReceiver != null) { 224 mContext.unregisterReceiver(mReceiver); 225 mReceiver = null; 226 } 227 break; 228 } 229 mTextView.setText(msg); 230 } 231 handleEvent(Context context, Intent intent)232 private void handleEvent(Context context, Intent intent) { 233 String action = intent.getAction(); 234 if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { 235 NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( 236 WifiManager.EXTRA_NETWORK_INFO); 237 final NetworkInfo.DetailedState state = info.getDetailedState(); 238 if (state == DetailedState.CONNECTED && 239 mDialogState == DialogState.WPS_COMPLETE) { 240 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 241 if (wifiInfo != null) { 242 String msg = String.format(mContext.getString( 243 R.string.wifi_wps_connected), wifiInfo.getSSID()); 244 updateDialog(DialogState.CONNECTED, msg); 245 } 246 } 247 } 248 } 249 250 } 251