123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- package com.epson.mobilephone.common.wifidirect;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.DhcpInfo;
- import android.net.Network;
- import android.net.NetworkInfo;
- import android.net.wifi.SupplicantState;
- import android.net.wifi.WifiConfiguration;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.os.Build;
- import android.os.Handler;
- import android.os.HandlerThread;
- import android.widget.Toast;
- import org.apache.commons.lang.ClassUtils;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.List;
- import java.util.UUID;
- public class WiFiUtils {
- static final int INVALID_NETWORKID = -1;
- static final String PREFIX_SIMPLEAP_DIRECT = "DIRECT-";
- static final String PREFIX_SIMPLEAP_DISALBED = "DISABLED-";
- static final String PREFIX_SIMPLEAP_UNUSED = "UNUSED-";
- static final String SUFIX_SIMPLEAP_DISALBED = "-DISABLED";
- private static final String TAG = "WiFiUtils";
- static final String WifiSsid_NONE = "<unknown ssid>";
- private static HandlerThread htforToast = new HandlerThread("Thread for Toast");
- private static WiFiUtils instance;
- /* renamed from: cm */
- private ConnectivityManager f158cm = null;
- /* renamed from: wm */
- private WifiManager f159wm = null;
- private WiFiUtils(Context context) {
- f159wm = (WifiManager) context.getApplicationContext().getApplicationContext().getSystemService(Service.WIFI_SERVICE);
- f158cm = (ConnectivityManager) context.getApplicationContext().getApplicationContext().getSystemService("connectivity");
- }
- public static WiFiUtils getInstance(Context context) {
- WiFiUtils wiFiUtils = instance;
- if (wiFiUtils != null) {
- return wiFiUtils;
- }
- instance = new WiFiUtils(context);
- return instance;
- }
- public static String replaceUnusedSSID(String str) {
- if (str == null || !str.startsWith("DIRECT-")) {
- return str;
- }
- return PREFIX_SIMPLEAP_UNUSED + str.substring(7);
- }
- public static String removeQuotationsInSSID(String str) {
- return (str == null || !str.startsWith("\"") || !str.endsWith("\"")) ? str : str.substring(1, str.length() - 1);
- }
- @Deprecated
- public int getExistSimpleApDisabled(String str) {
- int networkId = getNetworkId(replaceUnusedSSID(str));
- if (networkId == -1) {
- networkId = getNetworkId(str + SUFIX_SIMPLEAP_DISALBED);
- }
- if (networkId != -1) {
- return networkId;
- }
- return getNetworkId(PREFIX_SIMPLEAP_DISALBED + str);
- }
- @Deprecated
- public int getCurNetworkId() {
- WifiInfo connectionInfo = f159wm.getConnectionInfo();
- if (connectionInfo == null || connectionInfo.getNetworkId() == -1 || !SupplicantState.COMPLETED.equals(connectionInfo.getSupplicantState())) {
- return -1;
- }
- return connectionInfo.getNetworkId();
- }
- public String getCurSSID() {
- String removeQuotationsInSSID;
- WifiInfo connectionInfo = f159wm.getConnectionInfo();
- if (connectionInfo == null || !SupplicantState.COMPLETED.equals(connectionInfo.getSupplicantState())) {
- return null;
- }
- if (Build.VERSION.SDK_INT <= 28 && connectionInfo.getNetworkId() != -1 && (removeQuotationsInSSID = removeQuotationsInSSID(getSSID(connectionInfo.getNetworkId()))) != null && removeQuotationsInSSID.length() > 0) {
- return removeQuotationsInSSID;
- }
- String removeQuotationsInSSID2 = removeQuotationsInSSID(connectionInfo.getSSID());
- if (removeQuotationsInSSID2 != null && removeQuotationsInSSID2.length() > 0 && !WifiSsid_NONE.equals(removeQuotationsInSSID2)) {
- return removeQuotationsInSSID2;
- }
- EPLog.e(TAG, "Error getCurSSID() : Do you have location permission?");
- return null;
- }
- public String getCurP2PMacAdder() {
- WifiInfo connectionInfo = f159wm.getConnectionInfo();
- if (connectionInfo != null) {
- return connectionInfo.getBSSID();
- }
- return null;
- }
- @Deprecated
- public int getNetworkId(String str) {
- List<WifiConfiguration> configuredNetworks;
- if (!(str == null || (configuredNetworks = f159wm.getConfiguredNetworks()) == null)) {
- for (WifiConfiguration next : configuredNetworks) {
- if (removeQuotationsInSSID(str).equals(removeQuotationsInSSID(next.SSID))) {
- return next.networkId;
- }
- }
- }
- return -1;
- }
- @Deprecated
- public WifiConfiguration getWifiConfig(int i) {
- List<WifiConfiguration> configuredNetworks = f159wm.getConfiguredNetworks();
- if (configuredNetworks == null) {
- return null;
- }
- for (WifiConfiguration next : configuredNetworks) {
- if (next.networkId == i) {
- return next;
- }
- }
- return null;
- }
- @Deprecated
- public String getSSID(int i) {
- WifiConfiguration wifiConfig;
- if (i == -1 || (wifiConfig = getWifiConfig(i)) == null) {
- return null;
- }
- return removeQuotationsInSSID(wifiConfig.SSID);
- }
- public int createSimpleAP(String str, String str2) {
- int i;
- WifiConfiguration wifiConfiguration = new WifiConfiguration();
- int networkId = getNetworkId(str);
- if (Build.VERSION.SDK_INT > 25 && networkId != -1) {
- if (!removeNetwork(networkId)) {
- return -1;
- }
- networkId = -1;
- }
- wifiConfiguration.SSID = "\"" + str + "\"";
- wifiConfiguration.preSharedKey = "\"" + str2 + "\"";
- wifiConfiguration.status = 2;
- wifiConfiguration.allowedGroupCiphers.set(2);
- wifiConfiguration.allowedGroupCiphers.set(3);
- wifiConfiguration.allowedKeyManagement.set(1);
- wifiConfiguration.allowedPairwiseCiphers.set(1);
- wifiConfiguration.allowedPairwiseCiphers.set(2);
- wifiConfiguration.allowedProtocols.set(1);
- wifiConfiguration.networkId = networkId;
- if (networkId == -1) {
- EPLog.i(TAG, "call addNetwork!!");
- i = f159wm.addNetwork(wifiConfiguration);
- if (i == -1) {
- EPLog.w(TAG, "Failed addNetwork. Try ramdam ssid!");
- wifiConfiguration.SSID = "\"" + UUID.randomUUID().toString().replace("-", "") + "\"";
- i = f159wm.addNetwork(wifiConfiguration);
- if (i != -1) {
- wifiConfiguration.SSID = "\"" + str + "\"";
- wifiConfiguration.networkId = i;
- i = f159wm.updateNetwork(wifiConfiguration);
- }
- }
- } else {
- EPLog.i(TAG, "call updateNetwork!!");
- i = f159wm.updateNetwork(wifiConfiguration);
- }
- if (i == -1) {
- EPLog.e(TAG, "Failed addNetwork or updateNetwork");
- return -1;
- }
- int curNetworkId = getCurNetworkId();
- if (curNetworkId != -1) {
- f159wm.disableNetwork(curNetworkId);
- }
- if (wifiManager_connect(i)) {
- return i;
- }
- EPLog.e(TAG, "Failed wifiManager_connect");
- return -1;
- }
- public boolean enableSimpleAP(int i, String str) {
- WifiConfiguration wifiConfig = getWifiConfig(i);
- if (wifiConfig == null) {
- return false;
- }
- EPLog.i(TAG, "Enable Wifi Profile: SSID = " + wifiConfig.SSID);
- if (Build.VERSION.SDK_INT < 23) {
- wifiConfig.SSID = "\"" + str + "\"";
- if (f159wm.updateNetwork(wifiConfig) == -1) {
- EPLog.e(TAG, "Failed updateNetwork");
- return false;
- }
- }
- int curNetworkId = getCurNetworkId();
- if (curNetworkId != -1) {
- f159wm.disableNetwork(curNetworkId);
- }
- if (wifiManager_connect(i)) {
- return true;
- }
- EPLog.e(TAG, "Failed wifiManager_connect");
- return true;
- }
- private boolean wifiManager_connect(int i) {
- if (!this.f159wm.enableNetwork(i, true)) {
- EPLog.e(TAG, "Failed enableNetwork");
- return false;
- } else if (saveWifiConfiguration(f159wm)) {
- return true;
- } else {
- EPLog.e(TAG, "Failed saveConfiguration");
- return false;
- }
- }
- public boolean removeSimpleAP(int i) {
- if (i == -1) {
- return false;
- }
- if (i == getCurNetworkId() && !this.f159wm.disconnect()) {
- EPLog.e(TAG, "Failed disconnect");
- return false;
- } else if (!removeNetwork(i)) {
- return false;
- } else {
- reConnectNetwork();
- return true;
- }
- }
- public boolean removeNetwork(int i) {
- WifiConfiguration wifiConfig = getWifiConfig(i);
- if (wifiConfig == null) {
- return false;
- }
- EPLog.d(TAG, "Remove Wifi Profile : SSID = " + wifiConfig.SSID + " networkid = " + wifiConfig.networkId + " status = " + wifiConfig.status);
- if (!this.f159wm.removeNetwork(i)) {
- EPLog.e(TAG, "Failed removeNetwork");
- return false;
- } else if (saveWifiConfiguration(f159wm)) {
- return true;
- } else {
- EPLog.e(TAG, "Failed saveConfiguration");
- return false;
- }
- }
- public boolean disableSimpleAP(Context context, int i) {
- EPLog.d(TAG, "Enter disableSimpleAP()");
- if (Build.VERSION.SDK_INT > 25) {
- ManageDefaultNetwork.getInstance(context).resetDefaultNetwork();
- return true;
- } else if (!invalidateSimpleAP(context, i)) {
- return false;
- } else {
- return true;
- }
- }
- public boolean invalidateSimpleAP(Context context, int i) {
- boolean z;
- EPLog.d(TAG, "Enter invalidateSimpleAP()");
- if (i == -1) {
- return false;
- }
- if (i != getCurNetworkId()) {
- z = false;
- } else if (!this.f159wm.disableNetwork(i)) {
- EPLog.e(TAG, "Failed disableNetwork");
- return false;
- } else if (!this.f159wm.disconnect()) {
- EPLog.e(TAG, "Failed disconnect");
- return false;
- } else {
- waitDisconnect();
- displayToastThroughHandlerThread(context, R.string.str_notice_wifi_disconnected);
- z = true;
- }
- if (Build.VERSION.SDK_INT < 23) {
- WifiConfiguration wifiConfig = getWifiConfig(i);
- if (wifiConfig == null) {
- return false;
- }
- EPLog.d(TAG, "Disable Wifi Profile : SSID = " + wifiConfig.SSID + " networkid = " + wifiConfig.networkId + " status = " + wifiConfig.status);
- if (removeQuotationsInSSID(wifiConfig.SSID).matches("^DIRECT-..-.*")) {
- wifiConfig.SSID = "\"" + replaceUnusedSSID(removeQuotationsInSSID(wifiConfig.SSID)) + "\"";
- } else if (Build.VERSION.SDK_INT >= 21) {
- wifiConfig.SSID = "\"DISABLED-" + removeQuotationsInSSID(wifiConfig.SSID) + "\"";
- } else {
- wifiConfig.SSID = "\"" + removeQuotationsInSSID(wifiConfig.SSID) + SUFIX_SIMPLEAP_DISALBED + "\"";
- }
- for (WifiConfiguration next : f159wm.getConfiguredNetworks()) {
- if (next.networkId != i && wifiConfig.SSID.equals(next.SSID)) {
- EPLog.i(TAG, "Delete network for Backup : " + next.SSID);
- f159wm.removeNetwork(next.networkId);
- }
- }
- if (f159wm.updateNetwork(wifiConfig) == -1) {
- EPLog.e(TAG, "Failed updateNetwork");
- return false;
- } else if (!saveWifiConfiguration(f159wm)) {
- EPLog.e(TAG, "Failed saveConfiguration");
- return false;
- }
- }
- if (z) {
- reConnectNetwork();
- }
- return true;
- }
- public void reConnectNetwork() {
- EPLog.i(TAG, "called reConnectNetwork");
- for (WifiConfiguration next : f159wm.getConfiguredNetworks()) {
- if (WiFiControl.isSimpleAP(removeQuotationsInSSID(next.SSID))) {
- f159wm.disableNetwork(next.networkId);
- } else if (!(next == null || next.status == 2)) {
- f159wm.enableNetwork(next.networkId, false);
- }
- }
- if (!saveWifiConfiguration(f159wm)) {
- EPLog.e(TAG, "Failed saveConfiguration");
- }
- if (!this.f159wm.reassociate()) {
- EPLog.e(TAG, "Failed reassociate");
- }
- }
- private boolean saveWifiConfiguration(WifiManager wifiManager) {
- if (Build.VERSION.SDK_INT >= 26) {
- EPLog.d(TAG, "Needless call saveConfiguration");
- return true;
- } else if (wifiManager.saveConfiguration()) {
- return true;
- } else {
- EPLog.e(TAG, "Failed saveConfiguration");
- return false;
- }
- }
- private void waitDisconnect() {
- int i = 0;
- while (true) {
- if (i >= 5000) {
- break;
- }
- try {
- if (!isConnectedWifi()) {
- break;
- }
- EPLog.w(TAG, "Waiting wifi disconnected");
- Thread.sleep((long) 200);
- i += 200;
- } catch (InterruptedException e) {
- e.printStackTrace();
- return;
- }
- }
- if (i >= 5000) {
- EPLog.e(TAG, "TIMEOUT wifi disconnected");
- }
- }
- public void waitConnected() {
- int i = 0;
- while (true) {
- if (i >= 10000) {
- break;
- }
- try {
- if (isConnectedWifi()) {
- break;
- }
- EPLog.w(TAG, "Waiting wifi connected");
- Thread.sleep((long) 500);
- i += 500;
- } catch (InterruptedException e) {
- e.printStackTrace();
- return;
- }
- }
- if (i >= 10000) {
- EPLog.e(TAG, "TIMEOUT wifi connected");
- }
- }
- static void displayToastThroughHandlerThread(final Context context, final int i) {
- if (htforToast.getState() == Thread.State.NEW) {
- htforToast.start();
- }
- new Handler(htforToast.getLooper()).post(new Runnable() {
- public void run() {
- Context context = context;
- Toast.makeText(context, context.getString(i), 0).show();
- }
- });
- }
- public boolean isConnectedWifi() {
- int type;
- if (Build.VERSION.SDK_INT >= 21) {
- Network[] allNetworks = f158cm.getAllNetworks();
- int i = 0;
- while (i < allNetworks.length) {
- NetworkInfo networkInfo = f158cm.getNetworkInfo(allNetworks[i]);
- if (networkInfo == null || (!((type = networkInfo.getType()) == 1 || type == 9) || !networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) {
- i++;
- } else {
- EPLog.i(TAG, String.format("isConnectedWifi = true : TypeName = %s", new Object[]{networkInfo.getTypeName()}));
- return true;
- }
- }
- } else {
- for (NetworkInfo networkInfo2 : f158cm.getAllNetworkInfo()) {
- int type2 = networkInfo2.getType();
- if ((type2 == 1 || type2 == 9) && networkInfo2.isConnected()) {
- EPLog.i(TAG, String.format("isConnectedWifi = true : TypeName = %s", new Object[]{networkInfo2.getTypeName()}));
- return true;
- }
- }
- }
- return false;
- }
- public static int setAutoGoTimeout(String str, int i) {
- if (str == null) {
- return -1;
- }
- escprLib escprlib = new escprLib();
- int epsWrapperInitDriver = escprlib.epsWrapperInitDriver(192);
- if (epsWrapperInitDriver != -1050 && epsWrapperInitDriver != 0) {
- return epsWrapperInitDriver;
- }
- int epsWrapperSetAutoGoTimeOut = escprlib.epsWrapperSetAutoGoTimeOut(str.toCharArray(), i);
- EPLog.i(TAG, "epsWrapperSetAutoGoTimeOut ret = " + epsWrapperSetAutoGoTimeOut);
- escprlib.epsWrapperReleaseDriver();
- return epsWrapperSetAutoGoTimeOut;
- }
- public static void showOsWifiSettings(Activity activity, int i) {
- activity.startActivityForResult(new Intent("android.settings.WIFI_SETTINGS"), i);
- }
- public void pingWiFiDirectPrinter() {
- DhcpInfo dhcpInfo = f159wm.getDhcpInfo();
- if (dhcpInfo != null) {
- StringBuffer stringBuffer = new StringBuffer();
- putAddress(stringBuffer, dhcpInfo.serverAddress);
- try {
- InetAddress byName = InetAddress.getByName(stringBuffer.toString());
- if (byName == null) {
- return;
- }
- if (byName.isReachable(1000)) {
- EPLog.d(TAG, "Success ping " + stringBuffer);
- return;
- }
- EPLog.d(TAG, "Failed ping " + stringBuffer);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- }
- private static void putAddress(StringBuffer stringBuffer, int i) {
- stringBuffer.append(i & 255);
- stringBuffer.append(ClassUtils.PACKAGE_SEPARATOR_CHAR);
- int i2 = i >>> 8;
- stringBuffer.append(i2 & 255);
- stringBuffer.append(ClassUtils.PACKAGE_SEPARATOR_CHAR);
- int i3 = i2 >>> 8;
- stringBuffer.append(i3 & 255);
- stringBuffer.append(ClassUtils.PACKAGE_SEPARATOR_CHAR);
- stringBuffer.append((i3 >>> 8) & 255);
- }
- }
|