FOTA v.0.9.2.7版本的 MD5 值为:2305fdf4520259063967289da5df0dc6
以下内容为反编译后的 NetworkUtils.java 源代码,内容仅作参考
package com.incall.apps.master.service.network;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.os.Build;
import android.provider.Settings;
import cn.hutool.core.util.StrUtil;
import com.incall.apps.libbase.CarStatusManager;
import com.incall.apps.softmanager.util.log.LogUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class NetworkUtils {
private static final String TAG = "LBC.NetworkUtils";
public static long lastTimeStamp;
public static long lastTotalRxBytes;
public interface INetworkAvailableCallback {
void onAvailable();
void onUnavailable();
}
private NetworkUtils() {
}
public static boolean isNetworkAvailable(Context context) {
return isConnected(context);
}
public static boolean isConnected(Context mContext) {
boolean isConnect = isAndroidApiConnected(mContext);
LogUtil.i(TAG, "isConnected: isAndroidApiConnected = " + isConnect);
if (!isConnect) {
boolean isConnect2 = CarStatusManager.getInstance().isTboxConnected();
LogUtil.i(TAG, "isConnected: isTboxConnected = " + isConnect2);
return isConnect2;
}
return isConnect;
}
public static boolean isAndroidApiConnected(Context mContext) {
ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService("connectivity");
if (manager != null) {
if (Build.VERSION.SDK_INT >= 23) {
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
if (networkCapabilities != null) {
NetworkInfo info = getNetworkInfo(mContext);
LogUtil.i(TAG, "NetworkInfo type: " + info.getType());
if (info.getType() == 9 && "enable".equals(Settings.Global.getString(mContext.getContentResolver(), "tbox_data_channel_status"))) {
LogUtil.i(TAG, "tbox data channel status is enable");
return true;
} else if (info.getType() == 1) {
return networkCapabilities.hasTransport(1) || networkCapabilities.hasTransport(0) || networkCapabilities.hasTransport(3);
} else {
return false;
}
}
} else {
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
return false;
}
public static boolean is4gConnected(Context context) {
NetworkInfo info = getNetworkInfo(context);
if (info != null && isNetworkAvailable(context)) {
return info.getType() == 9 || info.getType() == 0;
}
return false;
}
public static boolean isWifiConnected(Context context) {
NetworkInfo info = getNetworkInfo(context);
return info != null && isNetworkAvailable(context) && info.getType() == 1;
}
private static ConnectivityManager getConnectivityManager(Context context) {
if (context == null) {
return null;
}
return (ConnectivityManager) context.getSystemService("connectivity");
}
private static NetworkInfo getNetworkInfo(Context context) {
ConnectivityManager connectivityManager = getConnectivityManager(context);
if (connectivityManager == null) {
return null;
}
return connectivityManager.getActiveNetworkInfo();
}
public static String getNetworkSpeed(Context context) {
int uid = getAppUid(context);
int speed = getNetSpeed(uid);
String speedStr = speedFormat(speed);
LogUtil.i(TAG, "getNetworkSpeed uid = " + uid + " speed = " + speed + " speedStr = " + speedStr);
return speedStr;
}
private static int getAppUid(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 128);
int uid = applicationInfo.uid;
return uid;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
private static int getNetSpeed(int uid) {
long nowTotalRxBytes = getTotalRxBytes(uid);
long nowTimeStamp = System.currentTimeMillis();
long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000) / (nowTimeStamp - lastTimeStamp);
lastTimeStamp = nowTimeStamp;
lastTotalRxBytes = nowTotalRxBytes;
int result = (int) speed;
return result;
}
private static long getTotalRxBytes(int uid) {
if (TrafficStats.getUidRxBytes(uid) == -1) {
return 0L;
}
return TrafficStats.getTotalRxBytes() / 1024;
}
public static boolean isNetworkOnline() {
Runtime runtime = Runtime.getRuntime();
Process ipProcess = null;
try {
try {
ipProcess = runtime.exec("ping -c 1 -w 4 202.108.22.5");
InputStream input = ipProcess.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
while (true) {
String content = in.readLine();
if (content == null) {
break;
}
stringBuffer.append(content);
}
int exitValue = ipProcess.waitFor();
if (exitValue == 0) {
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
return true;
} else if (stringBuffer.indexOf("100% packet loss") != -1) {
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
return false;
} else {
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
return true;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
return false;
}
} catch (Throwable th) {
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
throw th;
}
}
private static String speedFormat(int speed) {
if (speed > 1024) {
int partA = speed / 1024;
int partB = (speed - (partA * 1024)) / 100;
String result = partA + StrUtil.DOT + partB + "M/S";
return result;
}
String result2 = speed + "KB/S";
return result2;
}
}