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