WiFiUtils.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package com.epson.mobilephone.common.wifidirect;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.ConnectivityManager;
  6. import android.net.DhcpInfo;
  7. import android.net.Network;
  8. import android.net.NetworkInfo;
  9. import android.net.wifi.SupplicantState;
  10. import android.net.wifi.WifiConfiguration;
  11. import android.net.wifi.WifiInfo;
  12. import android.net.wifi.WifiManager;
  13. import android.os.Build;
  14. import android.os.Handler;
  15. import android.os.HandlerThread;
  16. import android.widget.Toast;
  17. import org.apache.commons.lang.ClassUtils;
  18. import java.io.IOException;
  19. import java.net.InetAddress;
  20. import java.net.UnknownHostException;
  21. import java.util.List;
  22. import java.util.UUID;
  23. public class WiFiUtils {
  24. static final int INVALID_NETWORKID = -1;
  25. static final String PREFIX_SIMPLEAP_DIRECT = "DIRECT-";
  26. static final String PREFIX_SIMPLEAP_DISALBED = "DISABLED-";
  27. static final String PREFIX_SIMPLEAP_UNUSED = "UNUSED-";
  28. static final String SUFIX_SIMPLEAP_DISALBED = "-DISABLED";
  29. private static final String TAG = "WiFiUtils";
  30. static final String WifiSsid_NONE = "<unknown ssid>";
  31. private static HandlerThread htforToast = new HandlerThread("Thread for Toast");
  32. private static WiFiUtils instance;
  33. /* renamed from: cm */
  34. private ConnectivityManager f158cm = null;
  35. /* renamed from: wm */
  36. private WifiManager f159wm = null;
  37. private WiFiUtils(Context context) {
  38. f159wm = (WifiManager) context.getApplicationContext().getApplicationContext().getSystemService(Service.WIFI_SERVICE);
  39. f158cm = (ConnectivityManager) context.getApplicationContext().getApplicationContext().getSystemService("connectivity");
  40. }
  41. public static WiFiUtils getInstance(Context context) {
  42. WiFiUtils wiFiUtils = instance;
  43. if (wiFiUtils != null) {
  44. return wiFiUtils;
  45. }
  46. instance = new WiFiUtils(context);
  47. return instance;
  48. }
  49. public static String replaceUnusedSSID(String str) {
  50. if (str == null || !str.startsWith("DIRECT-")) {
  51. return str;
  52. }
  53. return PREFIX_SIMPLEAP_UNUSED + str.substring(7);
  54. }
  55. public static String removeQuotationsInSSID(String str) {
  56. return (str == null || !str.startsWith("\"") || !str.endsWith("\"")) ? str : str.substring(1, str.length() - 1);
  57. }
  58. @Deprecated
  59. public int getExistSimpleApDisabled(String str) {
  60. int networkId = getNetworkId(replaceUnusedSSID(str));
  61. if (networkId == -1) {
  62. networkId = getNetworkId(str + SUFIX_SIMPLEAP_DISALBED);
  63. }
  64. if (networkId != -1) {
  65. return networkId;
  66. }
  67. return getNetworkId(PREFIX_SIMPLEAP_DISALBED + str);
  68. }
  69. @Deprecated
  70. public int getCurNetworkId() {
  71. WifiInfo connectionInfo = f159wm.getConnectionInfo();
  72. if (connectionInfo == null || connectionInfo.getNetworkId() == -1 || !SupplicantState.COMPLETED.equals(connectionInfo.getSupplicantState())) {
  73. return -1;
  74. }
  75. return connectionInfo.getNetworkId();
  76. }
  77. public String getCurSSID() {
  78. String removeQuotationsInSSID;
  79. WifiInfo connectionInfo = f159wm.getConnectionInfo();
  80. if (connectionInfo == null || !SupplicantState.COMPLETED.equals(connectionInfo.getSupplicantState())) {
  81. return null;
  82. }
  83. if (Build.VERSION.SDK_INT <= 28 && connectionInfo.getNetworkId() != -1 && (removeQuotationsInSSID = removeQuotationsInSSID(getSSID(connectionInfo.getNetworkId()))) != null && removeQuotationsInSSID.length() > 0) {
  84. return removeQuotationsInSSID;
  85. }
  86. String removeQuotationsInSSID2 = removeQuotationsInSSID(connectionInfo.getSSID());
  87. if (removeQuotationsInSSID2 != null && removeQuotationsInSSID2.length() > 0 && !WifiSsid_NONE.equals(removeQuotationsInSSID2)) {
  88. return removeQuotationsInSSID2;
  89. }
  90. EPLog.e(TAG, "Error getCurSSID() : Do you have location permission?");
  91. return null;
  92. }
  93. public String getCurP2PMacAdder() {
  94. WifiInfo connectionInfo = f159wm.getConnectionInfo();
  95. if (connectionInfo != null) {
  96. return connectionInfo.getBSSID();
  97. }
  98. return null;
  99. }
  100. @Deprecated
  101. public int getNetworkId(String str) {
  102. List<WifiConfiguration> configuredNetworks;
  103. if (!(str == null || (configuredNetworks = f159wm.getConfiguredNetworks()) == null)) {
  104. for (WifiConfiguration next : configuredNetworks) {
  105. if (removeQuotationsInSSID(str).equals(removeQuotationsInSSID(next.SSID))) {
  106. return next.networkId;
  107. }
  108. }
  109. }
  110. return -1;
  111. }
  112. @Deprecated
  113. public WifiConfiguration getWifiConfig(int i) {
  114. List<WifiConfiguration> configuredNetworks = f159wm.getConfiguredNetworks();
  115. if (configuredNetworks == null) {
  116. return null;
  117. }
  118. for (WifiConfiguration next : configuredNetworks) {
  119. if (next.networkId == i) {
  120. return next;
  121. }
  122. }
  123. return null;
  124. }
  125. @Deprecated
  126. public String getSSID(int i) {
  127. WifiConfiguration wifiConfig;
  128. if (i == -1 || (wifiConfig = getWifiConfig(i)) == null) {
  129. return null;
  130. }
  131. return removeQuotationsInSSID(wifiConfig.SSID);
  132. }
  133. public int createSimpleAP(String str, String str2) {
  134. int i;
  135. WifiConfiguration wifiConfiguration = new WifiConfiguration();
  136. int networkId = getNetworkId(str);
  137. if (Build.VERSION.SDK_INT > 25 && networkId != -1) {
  138. if (!removeNetwork(networkId)) {
  139. return -1;
  140. }
  141. networkId = -1;
  142. }
  143. wifiConfiguration.SSID = "\"" + str + "\"";
  144. wifiConfiguration.preSharedKey = "\"" + str2 + "\"";
  145. wifiConfiguration.status = 2;
  146. wifiConfiguration.allowedGroupCiphers.set(2);
  147. wifiConfiguration.allowedGroupCiphers.set(3);
  148. wifiConfiguration.allowedKeyManagement.set(1);
  149. wifiConfiguration.allowedPairwiseCiphers.set(1);
  150. wifiConfiguration.allowedPairwiseCiphers.set(2);
  151. wifiConfiguration.allowedProtocols.set(1);
  152. wifiConfiguration.networkId = networkId;
  153. if (networkId == -1) {
  154. EPLog.i(TAG, "call addNetwork!!");
  155. i = f159wm.addNetwork(wifiConfiguration);
  156. if (i == -1) {
  157. EPLog.w(TAG, "Failed addNetwork. Try ramdam ssid!");
  158. wifiConfiguration.SSID = "\"" + UUID.randomUUID().toString().replace("-", "") + "\"";
  159. i = f159wm.addNetwork(wifiConfiguration);
  160. if (i != -1) {
  161. wifiConfiguration.SSID = "\"" + str + "\"";
  162. wifiConfiguration.networkId = i;
  163. i = f159wm.updateNetwork(wifiConfiguration);
  164. }
  165. }
  166. } else {
  167. EPLog.i(TAG, "call updateNetwork!!");
  168. i = f159wm.updateNetwork(wifiConfiguration);
  169. }
  170. if (i == -1) {
  171. EPLog.e(TAG, "Failed addNetwork or updateNetwork");
  172. return -1;
  173. }
  174. int curNetworkId = getCurNetworkId();
  175. if (curNetworkId != -1) {
  176. f159wm.disableNetwork(curNetworkId);
  177. }
  178. if (wifiManager_connect(i)) {
  179. return i;
  180. }
  181. EPLog.e(TAG, "Failed wifiManager_connect");
  182. return -1;
  183. }
  184. public boolean enableSimpleAP(int i, String str) {
  185. WifiConfiguration wifiConfig = getWifiConfig(i);
  186. if (wifiConfig == null) {
  187. return false;
  188. }
  189. EPLog.i(TAG, "Enable Wifi Profile: SSID = " + wifiConfig.SSID);
  190. if (Build.VERSION.SDK_INT < 23) {
  191. wifiConfig.SSID = "\"" + str + "\"";
  192. if (f159wm.updateNetwork(wifiConfig) == -1) {
  193. EPLog.e(TAG, "Failed updateNetwork");
  194. return false;
  195. }
  196. }
  197. int curNetworkId = getCurNetworkId();
  198. if (curNetworkId != -1) {
  199. f159wm.disableNetwork(curNetworkId);
  200. }
  201. if (wifiManager_connect(i)) {
  202. return true;
  203. }
  204. EPLog.e(TAG, "Failed wifiManager_connect");
  205. return true;
  206. }
  207. private boolean wifiManager_connect(int i) {
  208. if (f159wm.enableNetwork(i, true)) {
  209. EPLog.e(TAG, "Failed enableNetwork");
  210. return false;
  211. } else if (saveWifiConfiguration(f159wm)) {
  212. return true;
  213. } else {
  214. EPLog.e(TAG, "Failed saveConfiguration");
  215. return false;
  216. }
  217. }
  218. public boolean removeSimpleAP(int i) {
  219. if (i == -1) {
  220. return false;
  221. }
  222. if (i == getCurNetworkId() && !this.f159wm.disconnect()) {
  223. EPLog.e(TAG, "Failed disconnect");
  224. return false;
  225. } else if (!removeNetwork(i)) {
  226. return false;
  227. } else {
  228. reConnectNetwork();
  229. return true;
  230. }
  231. }
  232. public boolean removeNetwork(int i) {
  233. WifiConfiguration wifiConfig = getWifiConfig(i);
  234. if (wifiConfig == null) {
  235. return false;
  236. }
  237. EPLog.d(TAG, "Remove Wifi Profile : SSID = " + wifiConfig.SSID + " networkid = " + wifiConfig.networkId + " status = " + wifiConfig.status);
  238. if (f159wm.removeNetwork(i)) {
  239. EPLog.e(TAG, "Failed removeNetwork");
  240. return false;
  241. } else if (saveWifiConfiguration(f159wm)) {
  242. return true;
  243. } else {
  244. EPLog.e(TAG, "Failed saveConfiguration");
  245. return false;
  246. }
  247. }
  248. public boolean disableSimpleAP(Context context, int i) {
  249. EPLog.d(TAG, "Enter disableSimpleAP()");
  250. if (Build.VERSION.SDK_INT > 25) {
  251. ManageDefaultNetwork.getInstance(context).resetDefaultNetwork();
  252. return true;
  253. } else if (!invalidateSimpleAP(context, i)) {
  254. return false;
  255. } else {
  256. return true;
  257. }
  258. }
  259. public boolean invalidateSimpleAP(Context context, int i) {
  260. boolean z;
  261. EPLog.d(TAG, "Enter invalidateSimpleAP()");
  262. if (i == -1) {
  263. return false;
  264. }
  265. if (i != getCurNetworkId()) {
  266. z = false;
  267. } else if (f159wm.disableNetwork(i)) {
  268. EPLog.e(TAG, "Failed disableNetwork");
  269. return false;
  270. } else if (f159wm.disconnect()) {
  271. EPLog.e(TAG, "Failed disconnect");
  272. return false;
  273. } else {
  274. waitDisconnect();
  275. displayToastThroughHandlerThread(context, R.string.str_notice_wifi_disconnected);
  276. z = true;
  277. }
  278. if (Build.VERSION.SDK_INT < 23) {
  279. WifiConfiguration wifiConfig = getWifiConfig(i);
  280. if (wifiConfig == null) {
  281. return false;
  282. }
  283. EPLog.d(TAG, "Disable Wifi Profile : SSID = " + wifiConfig.SSID + " networkid = " + wifiConfig.networkId + " status = " + wifiConfig.status);
  284. if (removeQuotationsInSSID(wifiConfig.SSID).matches("^DIRECT-..-.*")) {
  285. wifiConfig.SSID = "\"" + replaceUnusedSSID(removeQuotationsInSSID(wifiConfig.SSID)) + "\"";
  286. } else if (Build.VERSION.SDK_INT >= 21) {
  287. wifiConfig.SSID = "\"DISABLED-" + removeQuotationsInSSID(wifiConfig.SSID) + "\"";
  288. } else {
  289. wifiConfig.SSID = "\"" + removeQuotationsInSSID(wifiConfig.SSID) + SUFIX_SIMPLEAP_DISALBED + "\"";
  290. }
  291. for (WifiConfiguration next : f159wm.getConfiguredNetworks()) {
  292. if (next.networkId != i && wifiConfig.SSID.equals(next.SSID)) {
  293. EPLog.i(TAG, "Delete network for Backup : " + next.SSID);
  294. f159wm.removeNetwork(next.networkId);
  295. }
  296. }
  297. if (f159wm.updateNetwork(wifiConfig) == -1) {
  298. EPLog.e(TAG, "Failed updateNetwork");
  299. return false;
  300. } else if (!saveWifiConfiguration(f159wm)) {
  301. EPLog.e(TAG, "Failed saveConfiguration");
  302. return false;
  303. }
  304. }
  305. if (z) {
  306. reConnectNetwork();
  307. }
  308. return true;
  309. }
  310. public void reConnectNetwork() {
  311. EPLog.i(TAG, "called reConnectNetwork");
  312. for (WifiConfiguration next : f159wm.getConfiguredNetworks()) {
  313. if (WiFiControl.isSimpleAP(removeQuotationsInSSID(next.SSID))) {
  314. f159wm.disableNetwork(next.networkId);
  315. } else if (!(next == null || next.status == 2)) {
  316. f159wm.enableNetwork(next.networkId, false);
  317. }
  318. }
  319. if (!saveWifiConfiguration(f159wm)) {
  320. EPLog.e(TAG, "Failed saveConfiguration");
  321. }
  322. if (f159wm.reassociate()) {
  323. EPLog.e(TAG, "Failed reassociate");
  324. }
  325. }
  326. private boolean saveWifiConfiguration(WifiManager wifiManager) {
  327. if (Build.VERSION.SDK_INT >= 26) {
  328. EPLog.d(TAG, "Needless call saveConfiguration");
  329. return true;
  330. } else if (wifiManager.saveConfiguration()) {
  331. return true;
  332. } else {
  333. EPLog.e(TAG, "Failed saveConfiguration");
  334. return false;
  335. }
  336. }
  337. private void waitDisconnect() {
  338. int i = 0;
  339. while (true) {
  340. if (i >= 5000) {
  341. break;
  342. }
  343. try {
  344. if (!isConnectedWifi()) {
  345. break;
  346. }
  347. EPLog.w(TAG, "Waiting wifi disconnected");
  348. Thread.sleep((long) 200);
  349. i += 200;
  350. } catch (InterruptedException e) {
  351. e.printStackTrace();
  352. return;
  353. }
  354. }
  355. if (i >= 5000) {
  356. EPLog.e(TAG, "TIMEOUT wifi disconnected");
  357. }
  358. }
  359. public void waitConnected() {
  360. int i = 0;
  361. while (true) {
  362. if (i >= 10000) {
  363. break;
  364. }
  365. try {
  366. if (isConnectedWifi()) {
  367. break;
  368. }
  369. EPLog.w(TAG, "Waiting wifi connected");
  370. Thread.sleep((long) 500);
  371. i += 500;
  372. } catch (InterruptedException e) {
  373. e.printStackTrace();
  374. return;
  375. }
  376. }
  377. if (i >= 10000) {
  378. EPLog.e(TAG, "TIMEOUT wifi connected");
  379. }
  380. }
  381. static void displayToastThroughHandlerThread(final Context context, final int i) {
  382. if (htforToast.getState() == Thread.State.NEW) {
  383. htforToast.start();
  384. }
  385. new Handler(htforToast.getLooper()).post(new Runnable() {
  386. public void run() {
  387. Context context = context;
  388. Toast.makeText(context, context.getString(i), 0).show();
  389. }
  390. });
  391. }
  392. public boolean isConnectedWifi() {
  393. int type;
  394. if (Build.VERSION.SDK_INT >= 21) {
  395. Network[] allNetworks = f158cm.getAllNetworks();
  396. int i = 0;
  397. while (i < allNetworks.length) {
  398. NetworkInfo networkInfo = f158cm.getNetworkInfo(allNetworks[i]);
  399. if (networkInfo == null || (!((type = networkInfo.getType()) == 1 || type == 9) || !networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) {
  400. i++;
  401. } else {
  402. EPLog.i(TAG, String.format("isConnectedWifi = true : TypeName = %s", new Object[]{networkInfo.getTypeName()}));
  403. return true;
  404. }
  405. }
  406. } else {
  407. for (NetworkInfo networkInfo2 : f158cm.getAllNetworkInfo()) {
  408. int type2 = networkInfo2.getType();
  409. if ((type2 == 1 || type2 == 9) && networkInfo2.isConnected()) {
  410. EPLog.i(TAG, String.format("isConnectedWifi = true : TypeName = %s", new Object[]{networkInfo2.getTypeName()}));
  411. return true;
  412. }
  413. }
  414. }
  415. return false;
  416. }
  417. public static int setAutoGoTimeout(String str, int i) {
  418. if (str == null) {
  419. return -1;
  420. }
  421. escprLib escprlib = new escprLib();
  422. int epsWrapperInitDriver = escprlib.epsWrapperInitDriver(192);
  423. if (epsWrapperInitDriver != -1050 && epsWrapperInitDriver != 0) {
  424. return epsWrapperInitDriver;
  425. }
  426. int epsWrapperSetAutoGoTimeOut = escprlib.epsWrapperSetAutoGoTimeOut(str.toCharArray(), i);
  427. EPLog.i(TAG, "epsWrapperSetAutoGoTimeOut ret = " + epsWrapperSetAutoGoTimeOut);
  428. escprlib.epsWrapperReleaseDriver();
  429. return epsWrapperSetAutoGoTimeOut;
  430. }
  431. public static void showOsWifiSettings(Activity activity, int i) {
  432. activity.startActivityForResult(new Intent("android.settings.WIFI_SETTINGS"), i);
  433. }
  434. public void pingWiFiDirectPrinter() {
  435. DhcpInfo dhcpInfo = f159wm.getDhcpInfo();
  436. if (dhcpInfo != null) {
  437. StringBuffer stringBuffer = new StringBuffer();
  438. putAddress(stringBuffer, dhcpInfo.serverAddress);
  439. try {
  440. InetAddress byName = InetAddress.getByName(stringBuffer.toString());
  441. if (byName == null) {
  442. return;
  443. }
  444. if (byName.isReachable(1000)) {
  445. EPLog.d(TAG, "Success ping " + stringBuffer);
  446. return;
  447. }
  448. EPLog.d(TAG, "Failed ping " + stringBuffer);
  449. } catch (UnknownHostException e) {
  450. e.printStackTrace();
  451. } catch (IOException e2) {
  452. e2.printStackTrace();
  453. }
  454. }
  455. }
  456. private static void putAddress(StringBuffer stringBuffer, int i) {
  457. stringBuffer.append(i & 255);
  458. stringBuffer.append(ClassUtils.PACKAGE_SEPARATOR_CHAR);
  459. int i2 = i >>> 8;
  460. stringBuffer.append(i2 & 255);
  461. stringBuffer.append(ClassUtils.PACKAGE_SEPARATOR_CHAR);
  462. int i3 = i2 >>> 8;
  463. stringBuffer.append(i3 & 255);
  464. stringBuffer.append(ClassUtils.PACKAGE_SEPARATOR_CHAR);
  465. stringBuffer.append((i3 >>> 8) & 255);
  466. }
  467. }