1 package com.android.nfc;
2 
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.DialogInterface;
8 import android.content.Intent;
9 import android.content.IntentFilter;
10 import android.net.wifi.WifiConfiguration;
11 import android.net.wifi.WifiManager;
12 import android.os.Bundle;
13 import android.os.Handler;
14 import android.view.View;
15 import android.widget.Toast;
16 
17 public class ConfirmConnectToWifiNetworkActivity extends Activity
18         implements View.OnClickListener, DialogInterface.OnDismissListener {
19 
20     public static final int ENABLE_WIFI_TIMEOUT_MILLIS = 5000;
21     private WifiConfiguration mCurrentWifiConfiguration;
22     private AlertDialog mAlertDialog;
23     private boolean mEnableWifiInProgress;
24     private Handler mHandler;
25 
26     @Override
onCreate(Bundle savedInstanceState)27     protected void onCreate(Bundle savedInstanceState) {
28 
29         Intent intent = getIntent();
30         mCurrentWifiConfiguration =
31                 intent.getParcelableExtra(NfcWifiProtectedSetup.EXTRA_WIFI_CONFIG);
32 
33         String printableSsid = mCurrentWifiConfiguration.getPrintableSsid();
34         mAlertDialog = new AlertDialog.Builder(this,  AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
35                 .setTitle(R.string.title_connect_to_network)
36                 .setMessage(
37                         String.format(getResources().getString(R.string.prompt_connect_to_network),
38                         printableSsid))
39                 .setOnDismissListener(this)
40                 .setNegativeButton(com.android.internal.R.string.cancel, null)
41                 .setPositiveButton(R.string.wifi_connect, null)
42                 .create();
43 
44         mEnableWifiInProgress = false;
45         mHandler = new Handler();
46 
47         IntentFilter intentFilter = new IntentFilter();
48         intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
49         registerReceiver(mBroadcastReceiver, intentFilter);
50 
51         mAlertDialog.show();
52 
53         super.onCreate(savedInstanceState);
54 
55         mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this);
56     }
57 
58 
59     @Override
onClick(View v)60     public void onClick(View v) {
61         WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
62 
63         if (!wifiManager.isWifiEnabled()) {
64             wifiManager.setWifiEnabled(true);
65             mEnableWifiInProgress = true;
66 
67             mHandler.postDelayed(new Runnable() {
68                 @Override
69                 public void run() {
70                     if (getAndClearEnableWifiInProgress()) {
71                         showFailToast();
72                         ConfirmConnectToWifiNetworkActivity.this.finish();
73                     }
74                 }
75             }, ENABLE_WIFI_TIMEOUT_MILLIS);
76 
77         } else {
78             doConnect(wifiManager);
79         }
80 
81         mAlertDialog.dismiss();
82     }
83 
doConnect(WifiManager wifiManager)84     private void doConnect(WifiManager wifiManager) {
85         int networkId = wifiManager.addNetwork(mCurrentWifiConfiguration);
86 
87         if (networkId < 0) {
88             showFailToast();
89         } else {
90 
91             wifiManager.connect(networkId,
92                     new WifiManager.ActionListener() {
93                         @Override
94                         public void onSuccess() {
95                             Toast.makeText(ConfirmConnectToWifiNetworkActivity.this,
96                                     R.string.status_wifi_connected, Toast.LENGTH_SHORT).show();
97                         }
98 
99                         @Override
100                         public void onFailure(int reason) {
101                             showFailToast();
102                         }
103                     });
104         }
105     }
106 
107 
showFailToast()108     private void showFailToast() {
109         Toast.makeText(ConfirmConnectToWifiNetworkActivity.this,
110                 R.string.status_unable_to_connect, Toast.LENGTH_SHORT).show();
111     }
112 
113     @Override
onDismiss(DialogInterface dialog)114     public void onDismiss(DialogInterface dialog) {
115         if (!mEnableWifiInProgress) {
116             finish();
117         }
118     }
119 
120 
121     @Override
onDestroy()122     protected void onDestroy() {
123         ConfirmConnectToWifiNetworkActivity.this.unregisterReceiver(mBroadcastReceiver);
124         super.onDestroy();
125     }
126 
127     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
128         @Override
129         public void onReceive(Context context, Intent intent) {
130             String action = intent.getAction();
131             if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
132                 int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
133                 if (mCurrentWifiConfiguration != null
134                         && wifiState == WifiManager.WIFI_STATE_ENABLED) {
135                     if (getAndClearEnableWifiInProgress()) {
136                         doConnect(
137                                 (WifiManager) ConfirmConnectToWifiNetworkActivity.this
138                                         .getSystemService(Context.WIFI_SERVICE));
139                     }
140                 }
141             }
142         }
143     };
144 
getAndClearEnableWifiInProgress()145     private boolean getAndClearEnableWifiInProgress() {
146         boolean enableWifiInProgress;
147 
148         synchronized (this)  {
149             enableWifiInProgress = mEnableWifiInProgress;
150             mEnableWifiInProgress = false;
151         }
152 
153         return enableWifiInProgress;
154     }
155 }
156