Skip to content

Commit

Permalink
Merge branch 'main' into dev/barometer-955
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelbeltran committed Jul 12, 2024
2 parents fc5e91e + 5e9afd5 commit 1e319aa
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ flutter {
dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation "com.google.truth:truth:1.4.3"
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
api 'androidx.test:core:1.6.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ flutter {
dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import android.os.Build
import java.net.*

/** Reports network info such as wifi name and address. */
internal class NetworkInfo(private val wifiManager: WifiManager,
private val connectivityManager: ConnectivityManager? = null
internal class NetworkInfo(
private val wifiManager: WifiManager,
private val connectivityManager: ConnectivityManager? = null
) {

// Using deprecated `connectionInfo` call here to be able to get info on demand
Expand All @@ -25,7 +26,8 @@ internal class NetworkInfo(private val wifiManager: WifiManager,
var ipAddress: String? = null

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)?.linkAddresses
val linkAddresses =
connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)?.linkAddresses

val ipV4Address = linkAddresses?.firstOrNull { linkAddress ->
linkAddress.address.hostAddress?.contains('.')
Expand All @@ -35,64 +37,52 @@ internal class NetworkInfo(private val wifiManager: WifiManager,
ipAddress = ipV4Address
} else {
@Suppress("DEPRECATION")
val interfaceIp = wifiInfo!!.ipAddress
val interfaceIp = wifiInfo.ipAddress
if (interfaceIp != 0) ipAddress = formatIPAddress(interfaceIp)
}
return ipAddress
}

fun getWifiSubnetMask(): String {
val ip = getWifiIPAddress()
var subnet = ""
try {
val inetAddress = InetAddress.getByName(ip)
subnet = getIPv4Subnet(inetAddress)
} catch (ignored: Exception) {
}
val inetAddress = InetAddress.getByName(ip)
val subnet = getIPv4Subnet(inetAddress)
return subnet
}

fun getBroadcastIP(): String? {
var broadcastIP: String? = null
val currentWifiIpAddress = getWifiIPAddress()
val inetAddress = InetAddress.getByName(currentWifiIpAddress)
try {
val networkInterface = NetworkInterface.getByInetAddress(inetAddress)
networkInterface.interfaceAddresses.forEach { interfaceAddress ->
if (!interfaceAddress.address.isLoopbackAddress) {
if (interfaceAddress.broadcast != null) {
broadcastIP = interfaceAddress.broadcast.hostAddress
}
val networkInterface = NetworkInterface.getByInetAddress(inetAddress)
networkInterface.interfaceAddresses.forEach { interfaceAddress ->
if (!interfaceAddress.address.isLoopbackAddress) {
if (interfaceAddress.broadcast != null) {
return interfaceAddress.broadcast.hostAddress
}
}
} catch (ignored: Exception) {

}
return broadcastIP
return null
}

fun getIpV6(): String? {
try {
val ip = getWifiIPAddress()
val ni = NetworkInterface.getByInetAddress(InetAddress.getByName(ip))
for (interfaceAddress in ni.interfaceAddresses) {
val address = interfaceAddress.address
if (!address.isLoopbackAddress && address is Inet6Address) {
val ipaddress = address.getHostAddress()
if (ipaddress != null) {
return ipaddress.split("%").toTypedArray()[0]
}
val ip = getWifiIPAddress()
val ni = NetworkInterface.getByInetAddress(InetAddress.getByName(ip))
for (interfaceAddress in ni.interfaceAddresses) {
val address = interfaceAddress.address
if (!address.isLoopbackAddress && address is Inet6Address) {
val ipaddress = address.getHostAddress()
if (ipaddress != null) {
return ipaddress.split("%").toTypedArray()[0]
}
}
} catch (ignored: SocketException) {

}
return null
}

fun getGatewayIPAddress(): String? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)
val linkAddresses =
connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)
val dhcpServer = linkAddresses?.dhcpServerAddress?.hostAddress

dhcpServer
Expand All @@ -115,39 +105,32 @@ internal class NetworkInfo(private val wifiManager: WifiManager,
)

private fun getIPv4Subnet(inetAddress: InetAddress): String {
try {
val ni = NetworkInterface.getByInetAddress(inetAddress)
val intAddresses = ni.interfaceAddresses
for (ia in intAddresses) {
if (!ia.address.isLoopbackAddress && ia.address is Inet4Address) {
val networkPrefix =
getIPv4SubnetFromNetPrefixLength(ia.networkPrefixLength.toInt())
if (networkPrefix != null) {
return networkPrefix.hostAddress!!
}
val ni = NetworkInterface.getByInetAddress(inetAddress)
val intAddresses = ni.interfaceAddresses
for (ia in intAddresses) {
if (!ia.address.isLoopbackAddress && ia.address is Inet4Address) {
val networkPrefix =
getIPv4SubnetFromNetPrefixLength(ia.networkPrefixLength.toInt())
if (networkPrefix != null) {
return networkPrefix.hostAddress!!
}
}
} catch (ignored: Exception) {
}
return ""
}

private fun getIPv4SubnetFromNetPrefixLength(netPrefixLength: Int): InetAddress? {
try {
var shift = 1 shl 31
for (i in netPrefixLength - 1 downTo 1) {
shift = shift shr 1
}
val subnet = ((shift shr 24 and 255)
.toString() + "."
+ (shift shr 16 and 255)
+ "."
+ (shift shr 8 and 255)
+ "."
+ (shift and 255))
return InetAddress.getByName(subnet)
} catch (ignored: Exception) {
var shift = 1 shl 31
for (i in netPrefixLength - 1 downTo 1) {
shift = shift shr 1
}
return null
val subnet = ((shift shr 24 and 255)
.toString() + "."
+ (shift shr 16 and 255)
+ "."
+ (shift shr 8 and 255)
+ "."
+ (shift and 255))
return InetAddress.getByName(subnet)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class PackageInfoPlugin : MethodCallHandler, FlutterPlugin {

val infoMap = HashMap<String, String>()
infoMap.apply {
put("appName", info.applicationInfo.loadLabel(packageManager).toString())
put("appName", info.applicationInfo?.loadLabel(packageManager)?.toString() ?: "")
put("packageName", applicationContext!!.packageName)
put("version", info.versionName)
put("version", info.versionName ?: "")
put("buildNumber", getLongVersionCode(info).toString())
if (buildSignature != null) put("buildSignature", buildSignature)
if (installerPackage != null) put("installerStore", installerPackage)
Expand Down Expand Up @@ -105,7 +105,7 @@ class PackageInfoPlugin : MethodCallHandler, FlutterPlugin {
)
val signatures = packageInfo.signatures

if (signatures.isNullOrEmpty() || packageInfo.signatures.first() == null) {
if (signatures.isNullOrEmpty() || signatures.first() == null) {
null
} else {
signatureToSha256(signatures.first().toByteArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:package_info_plus_example/main.dart';

const android14SDK = 34;

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

Expand All @@ -25,12 +28,19 @@ void main() {
expect(info.installerStore, null);
} else {
if (Platform.isAndroid) {
final androidVersionInfo = await DeviceInfoPlugin().androidInfo;

expect(info.appName, 'package_info_example');
expect(info.buildNumber, '4');
expect(info.buildSignature, isNotEmpty);
expect(info.packageName, 'io.flutter.plugins.packageinfoexample');
expect(info.version, '1.2.3');
expect(info.installerStore, null);
// Since Android 14 (API 34) OS returns com.android.shell when app is installed via package installer
if (androidVersionInfo.version.sdkInt >= android14SDK) {
expect(info.installerStore, 'com.android.shell');
} else {
expect(info.installerStore, null);
}
} else if (Platform.isIOS) {
expect(info.appName, 'Package Info Plus Example');
expect(info.buildNumber, '4');
Expand Down Expand Up @@ -75,13 +85,22 @@ void main() {
expect(find.text('not available'), findsOneWidget);
} else {
if (Platform.isAndroid) {
final androidVersionInfo = await DeviceInfoPlugin().androidInfo;

expect(find.text('package_info_example'), findsOneWidget);
expect(find.text('4'), findsOneWidget);
expect(
find.text('io.flutter.plugins.packageinfoexample'), findsOneWidget);
find.text('io.flutter.plugins.packageinfoexample'),
findsOneWidget,
);
expect(find.text('1.2.3'), findsOneWidget);
expect(find.text('Not set'), findsNothing);
expect(find.text('not available'), findsOneWidget);
// Since Android 14 (API 34) OS returns com.android.shell when app is installed via package installer
if (androidVersionInfo.version.sdkInt >= android14SDK) {
expect(find.text('com.android.shell'), findsOneWidget);
} else {
expect(find.text('not available'), findsOneWidget);
}
} else if (Platform.isIOS) {
expect(find.text('Package Info Plus Example'), findsOneWidget);
expect(find.text('4'), findsOneWidget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.3.3
device_info_plus: ^10.1.0
integration_test:
sdk: flutter
flutter_driver:
Expand Down

0 comments on commit 1e319aa

Please sign in to comment.