diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 05d1ef0e..73ae4699 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -50,7 +50,7 @@ import android.widget.Toast; import com.dev.devapi.api.SysApi; import com.xypower.common.FileDownloader; -import com.xypower.common.InetAddressUtils; +import com.xypower.common.NetworkUtils; import com.xypower.common.MicroPhotoContext; import com.xypower.mpapp.v2.Camera2VideoActivity; @@ -672,7 +672,7 @@ public class MicroPhotoService extends Service { public void run() { String ip = server; - if (!InetAddressUtils.isIPv4Address(ip) && !InetAddressUtils.isIPv6Address(ip)) { + if (!NetworkUtils.isIPv4Address(ip) && !NetworkUtils.isIPv6Address(ip)) { // It is a domain InetAddress addr = null; try { diff --git a/common/src/main/java/com/xypower/common/InetAddressUtils.java b/common/src/main/java/com/xypower/common/InetAddressUtils.java deleted file mode 100644 index 859cd810..00000000 --- a/common/src/main/java/com/xypower/common/InetAddressUtils.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.xypower.common; - -import android.text.TextUtils; - -import java.util.regex.Pattern; - -public class InetAddressUtils { - // String regex = "^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$"; - private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); - - private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); - - private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile("^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$"); - - public static boolean isIPv4Address(final String input) { - if (TextUtils.isEmpty(input)) { - return false; - } - return IPV4_PATTERN.matcher(input).matches(); - } - - public static boolean isIPv6StdAddress(final String input) { - if (TextUtils.isEmpty(input)) { - return false; - } - return IPV6_STD_PATTERN.matcher(input).matches(); - } - - public static boolean isIPv6HexCompressedAddress(final String input) { - if (TextUtils.isEmpty(input)) { - return false; - } - return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches(); - } - - public static boolean isIPv6Address(final String input) { - if (TextUtils.isEmpty(input)) { - return false; - } - return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input); - } -} diff --git a/common/src/main/java/com/xypower/common/NetworkUtils.java b/common/src/main/java/com/xypower/common/NetworkUtils.java new file mode 100644 index 00000000..58065629 --- /dev/null +++ b/common/src/main/java/com/xypower/common/NetworkUtils.java @@ -0,0 +1,167 @@ +package com.xypower.common; + +import android.annotation.SuppressLint; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.net.Uri; +import android.telephony.TelephonyManager; +import android.text.TextUtils; + +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.URI; +import java.util.Enumeration; +import java.util.regex.Pattern; + +public class NetworkUtils { + // String regex = "^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$"; + private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); + + private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); + + private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile("^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$"); + + public static boolean isIPv4Address(final String input) { + if (TextUtils.isEmpty(input)) { + return false; + } + return IPV4_PATTERN.matcher(input).matches(); + } + + public static boolean isIPv6StdAddress(final String input) { + if (TextUtils.isEmpty(input)) { + return false; + } + return IPV6_STD_PATTERN.matcher(input).matches(); + } + + public static boolean isIPv6HexCompressedAddress(final String input) { + if (TextUtils.isEmpty(input)) { + return false; + } + return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches(); + } + + public static boolean isIPv6Address(final String input) { + if (TextUtils.isEmpty(input)) { + return false; + } + return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input); + } + + public static String getMobileNetworkIp(Context context) { + ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); + @SuppressLint("MissingPermission") NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo(); + + if (networkInfos == null || networkInfos.length == 0) { + return null; + } + + for (NetworkInfo networkInfo : networkInfos) { + if (networkInfo == null || networkInfo.getType() != ConnectivityManager.TYPE_MOBILE || !networkInfo.isConnected()) { + continue; + } + + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { + NetworkInterface intf = en.nextElement(); + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (inetAddress.isLoopbackAddress()) { + continue; + } + if (inetAddress instanceof Inet4Address) { + return inetAddress.getHostAddress(); + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + return null; + } + + + public static int addAPN(Context context, String name, String desc, String numeric, String user, String pwd) { + int id = -1; + String NUMERIC = getSIMInfo(context); + if (NUMERIC == null) { + return -1; + } + Uri APN_URI = null; + try { + APN_URI = Uri.parse("content://telephony/carriers"); + } catch (Exception ex) { + ex.printStackTrace(); + } + + ContentResolver resolver = context.getContentResolver(); + ContentValues values = new ContentValues(); + values.put("name", desc); //apn description + values.put("apn", name); + values.put("type", "default"); //apn Type + values.put("numeric", numeric); + values.put("mcc", numeric.substring(0, 3)); + values.put("mnc", numeric.substring(3, numeric.length())); + values.put("proxy", ""); + values.put("port", ""); + values.put("mmsproxy", ""); + values.put("mmsport", ""); + values.put("user", user); + values.put("server", ""); //服务器 + values.put("password", pwd); //密码 + values.put("mmsc", ""); //MMSC + Cursor c = null; + Uri newRow = resolver.insert(APN_URI, values); + if (newRow != null) { + c = resolver.query(newRow, null, null, null, null); + int idIndex = c.getColumnIndex("_id"); + c.moveToFirst(); + id = c.getShort(idIndex); + } + if (c != null) + c.close(); + return id; + } + + protected static String getSIMInfo(Context context) { + TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + return iPhoneManager.getSimOperator(); + } + + // 设置接入点 + public static void SetAPN(Context context, int id) { + Uri CURRENT_APN_URI = null; + try { + CURRENT_APN_URI = Uri.parse("content://telephony/carriers"); + } catch (Exception ex) { + ex.printStackTrace(); + } + ContentResolver resolver = context.getContentResolver(); + ContentValues values = new ContentValues(); + values.put("apn_id", id); + resolver.update(CURRENT_APN_URI, values, null, null); + } + + /* + public static void checkAPN(Context context) { + // 检查当前连接的APN + Cursor cr = context.getContentResolver().query(APN_URI, null, null, null, null); + while (cr != null && cr.moveToNext()) { + if(cr.getString(cr.getColumnIndex("apn")).equals("abc")){ + APN.hasAPN=true; + break; + } + } + } + + */ + +} diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 6c558c02..f7c4742f 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -4,12 +4,10 @@ import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.Network; -import android.net.NetworkInfo; import android.os.Build; import android.os.Environment; import android.os.PowerManager; import android.os.SystemClock; -import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Pair; import android.util.Base64; @@ -18,7 +16,7 @@ import com.dev.devapi.api.SysApi; import com.xypower.common.FileDownloader; import com.xypower.common.FileUploader; import com.xypower.common.HttpRequest; -import com.xypower.common.InetAddressUtils; +import com.xypower.common.NetworkUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MD5Util; import com.xypower.common.MicroPhotoContext; @@ -27,7 +25,6 @@ import com.xypower.common.ZipUtils; import org.json.JSONArray; import org.json.JSONObject; -import org.w3c.dom.Text; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -42,9 +39,7 @@ import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; -import java.nio.file.CopyOption; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -353,6 +348,8 @@ public class AppMaster { long lastHb --- v2 20240714 added long lastHbResp long lastRecv + + int delayedUploads }; */ @@ -394,7 +391,7 @@ public class AppMaster { stats.add(new Pair("i1RebootTimesWk", Integer.toString(items[11]))); } - if (items.length >= 12 && items.length <= 18) { + if (items.length > 12 && items.length <= 18) { // v2 long ts = (((long)items[12]) & 0xFFFFFFFFl) | ((((long)items[13]) << 32) & 0xFFFFFFFF00000000l); @@ -406,6 +403,8 @@ public class AppMaster { ts = (((long)items[16]) & 0xFFFFFFFFl) | ((((long)items[17]) << 32) & 0xFFFFFFFF00000000l); stats.add(new Pair("lastRecvTime", Long.toString(ts))); } + + } catch (Exception ex) { ex.printStackTrace(); } @@ -848,7 +847,7 @@ public class AppMaster { if (server.startsWith("http://") || server.startsWith("https://")) { url = server; } else { - if ((InetAddressUtils.isIPv4Address(server) || InetAddressUtils.isIPv6Address(server)) && port > 0) { + if ((NetworkUtils.isIPv4Address(server) || NetworkUtils.isIPv6Address(server)) && port > 0) { url = "http://" + server + ":" + Integer.toString(port) + "/"; } } @@ -857,7 +856,7 @@ public class AppMaster { } private boolean updateCma(String ip, int port) { - if ((InetAddressUtils.isIPv4Address(ip) && InetAddressUtils.isIPv6Address(ip)) || port <= 0) { + if ((NetworkUtils.isIPv4Address(ip) && NetworkUtils.isIPv6Address(ip)) || port <= 0) { return false; } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java index fe7728ee..40280db3 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -29,6 +29,7 @@ import androidx.core.app.ActivityCompat; import com.dev.devapi.api.SysApi; import com.xypower.common.FilesUtils; import com.xypower.common.MicroPhotoContext; +import com.xypower.common.NetworkUtils; import com.xypower.common.RegexUtil; import com.xypower.mpmaster.MpMasterService; @@ -603,11 +604,11 @@ public class SimUtil { } - boolean b1 = tm.isDataConnectionAllowed(); - // boolean b2 = tm.isDataCapable(); - boolean b3 = tm.isDataRoamingEnabled(); + result.append(isActiveSlot ? ",默认" : ""); - result.append(isActiveSlot ? ",默认" : ","); + if (isActiveSlot) { + result.append(",IP=" + NetworkUtils.getMobileNetworkIp(context)); + } result.append(" "); }