nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | package com.android.nfc; |
| 2 | |
| 3 | import android.app.Activity; |
| 4 | import android.app.AlertDialog; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 5 | import android.app.AppOpsManager; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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.Bundle; |
| 14 | import android.os.Handler; |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 15 | import android.os.Process; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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 |
| 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, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT) |
| 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 |
| 62 | public void onClick(View v) { |
| 63 | WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 64 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 65 | if (!isChangeWifiStateGranted()) { |
| 66 | showFailToast(); |
| 67 | } else if (!wifiManager.isWifiEnabled()) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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 | |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 88 | private boolean isChangeWifiStateGranted() { |
| 89 | AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); |
| 90 | int modeChangeWifiState = appOps.checkOpNoThrow(AppOpsManager.OP_CHANGE_WIFI_STATE, |
| 91 | Process.NFC_UID, getPackageName()); |
| 92 | return modeChangeWifiState == AppOpsManager.MODE_ALLOWED; |
| 93 | } |
| 94 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 95 | private void doConnect(WifiManager wifiManager) { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 96 | mCurrentWifiConfiguration.hiddenSSID = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 117 | finish(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | private void showFailToast() { |
| 122 | Toast.makeText(ConfirmConnectToWifiNetworkActivity.this, |
| 123 | R.string.status_unable_to_connect, Toast.LENGTH_SHORT).show(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onDismiss(DialogInterface dialog) { |
| 128 | if (!mEnableWifiInProgress) { |
| 129 | finish(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | @Override |
| 135 | protected void onDestroy() { |
Nikhil Chhabra | 865363d | 2018-01-09 12:04:47 +0530 | [diff] [blame] | 136 | mAlertDialog.dismiss(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | private boolean getAndClearEnableWifiInProgress() { |
| 160 | boolean enableWifiInProgress; |
| 161 | |
| 162 | synchronized (this) { |
| 163 | enableWifiInProgress = mEnableWifiInProgress; |
| 164 | mEnableWifiInProgress = false; |
| 165 | } |
| 166 | |
| 167 | return enableWifiInProgress; |
| 168 | } |
| 169 | } |