Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e249ea0

Browse files
committed
Converted library to pure java implementation for extra portability
1 parent ba4466c commit e249ea0

File tree

24 files changed

+209
-247
lines changed

24 files changed

+209
-247
lines changed

library/build.gradle

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
apply plugin: 'com.android.library'
2-
3-
android {
4-
compileSdkVersion compileSdkVer
5-
buildToolsVersion buildToolsVer
6-
7-
defaultConfig {
8-
minSdkVersion minSdkVer
9-
targetSdkVersion targetSdkVer
10-
versionCode versionCode
11-
versionName versionName
12-
}
13-
buildTypes {
14-
release {
15-
minifyEnabled false
16-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17-
}
18-
}
19-
}
1+
apply plugin: 'java'
202

213
dependencies {
22-
// Should be easy enough to replace with a non-android annotations class if want to make it
23-
// a totally java project
24-
implementation 'com.android.support:support-annotations:27.1.1'
254
testImplementation 'junit:junit:4.12'
265
testImplementation 'org.mockito:mockito-core:2.10.0'
276
}
7+
8+
sourceCompatibility = "1.7"
9+
targetCompatibility = "1.7"

library/src/androidTest/java/com/stealthcopter/networktools/ApplicationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
88
*/
99
public class ApplicationTest extends ApplicationTestCase<Application> {
10-
public ApplicationTest() {
11-
super(Application.class);
12-
}
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
1313
}

library/src/main/java/com/stealthcopter/networktools/ARPInfo.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stealthcopter.networktools;
22

3-
import android.support.annotation.Nullable;
4-
53
import java.io.BufferedReader;
64
import java.io.FileReader;
75
import java.io.IOException;
@@ -32,7 +30,6 @@ private ARPInfo() {
3230
* @param ip - IP address to search for
3331
* @return the MAC from the ARP cache or null in format "01:23:45:67:89:ab"
3432
*/
35-
@Nullable
3633
public static String getMACFromIPAddress(String ip) {
3734
if (ip == null) {
3835
return null;
@@ -60,7 +57,6 @@ public static String getMACFromIPAddress(String ip) {
6057
* @param macAddress in format "01:23:45:67:89:ab" to search for
6158
* @return the IP address found or null in format "192.168.0.1"
6259
*/
63-
@Nullable
6460
public static String getIPAddressFromMAC(String macAddress) {
6561
if (macAddress == null) {
6662
return null;

library/src/main/java/com/stealthcopter/networktools/IPTools.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stealthcopter.networktools;
22

3-
import android.support.annotation.Nullable;
4-
53
import java.net.Inet4Address;
64
import java.net.InetAddress;
75
import java.net.NetworkInterface;
@@ -56,7 +54,6 @@ public static boolean isIPv6Address(final String address) {
5654
/**
5755
* @return The first local IPv4 address, or null
5856
*/
59-
@Nullable
6057
public static InetAddress getLocalIPv4Address() {
6158
ArrayList<InetAddress> localAddresses = getLocalIPv4Addresses();
6259
return localAddresses.size() > 0 ? localAddresses.get(0) : null;
@@ -93,7 +90,7 @@ public static ArrayList<InetAddress> getLocalIPv4Addresses() {
9390

9491
/**
9592
* Check if the provided ip address refers to the localhost
96-
*
93+
* <p>
9794
* https://stackoverflow.com/a/2406819/315998
9895
*
9996
* @param addr - address to check
@@ -116,7 +113,7 @@ public static boolean isIpAddressLocalhost(InetAddress addr) {
116113

117114
/**
118115
* Check if the provided ip address refers to the localhost
119-
*
116+
* <p>
120117
* https://stackoverflow.com/a/2406819/315998
121118
*
122119
* @param addr - address to check

library/src/main/java/com/stealthcopter/networktools/MACTools.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stealthcopter.networktools;
22

3-
import android.support.annotation.NonNull;
4-
53
import java.util.regex.Pattern;
64

75
public class MACTools {
@@ -19,9 +17,8 @@ private MACTools() {
1917
* Validates a provided MAC address
2018
*
2119
* @param macAddress - the MAC address to check
22-
*
2320
* @return - true if it is valid MAC address in IEEE802 format (either hyphen or colon seperated)
24-
* eg: "01:23:45:67:89:AB:CD:EF" or "01-23-45-67-89-AB-CD-EF"
21+
* eg: "01:23:45:67:89:AB:CD:EF" or "01-23-45-67-89-AB-CD-EF"
2522
*/
2623
public static boolean isValidMACAddress(final String macAddress) {
2724
return macAddress != null && PATTERN_MAC.matcher(macAddress).matches();
@@ -33,11 +30,10 @@ public static boolean isValidMACAddress(final String macAddress) {
3330
*
3431
* @param macStr - MAC string in IEEE802 format (either hyphen or colon seperated)
3532
* eg: "01:23:45:67:89:AB:CD:EF" or "01-23-45-67-89-AB-CD-EF"
36-
*
3733
* @return - MAC formatted in bytes
3834
* @throws IllegalArgumentException - if mac address is invalid
3935
*/
40-
public static byte[] getMacBytes(@NonNull String macStr) throws IllegalArgumentException {
36+
public static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
4137

4238
if (macStr == null) throw new IllegalArgumentException("Mac Address cannot be null");
4339

library/src/main/java/com/stealthcopter/networktools/Ping.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stealthcopter.networktools;
22

3-
import android.support.annotation.NonNull;
4-
53
import com.stealthcopter.networktools.ping.PingResult;
64
import com.stealthcopter.networktools.ping.PingStats;
75
import com.stealthcopter.networktools.ping.PingTools;
@@ -18,9 +16,11 @@ public class Ping {
1816
private Ping() {
1917
}
2018

21-
public interface PingListener{
19+
public interface PingListener {
2220
void onResult(PingResult pingResult);
21+
2322
void onFinished(PingStats pingStats);
23+
2424
void onError(Exception e);
2525
}
2626

@@ -33,59 +33,64 @@ public interface PingListener{
3333

3434
/**
3535
* Set the address to ping
36-
*
36+
* <p>
3737
* Note that a lookup is not performed here so that we do not accidentally perform a network
3838
* request on the UI thread.
3939
*
4040
* @param address - Address to be pinged
4141
* @return this object to allow chaining
4242
*/
43-
public static Ping onAddress(@NonNull String address) {
43+
public static Ping onAddress(String address) {
4444
Ping ping = new Ping();
4545
ping.setAddressString(address);
4646
return ping;
4747
}
4848

4949
/**
5050
* Set the address to ping
51+
*
5152
* @param ia - Address to be pinged
5253
* @return this object to allow chaining
5354
*/
54-
public static Ping onAddress(@NonNull InetAddress ia) {
55+
public static Ping onAddress(InetAddress ia) {
5556
Ping ping = new Ping();
5657
ping.setAddress(ia);
5758
return ping;
5859
}
5960

6061
/**
6162
* Set the timeout
63+
*
6264
* @param timeOutMillis - the timeout for each ping in milliseconds
6365
* @return this object to allow chaining
6466
*/
65-
public Ping setTimeOutMillis(int timeOutMillis){
66-
if (timeOutMillis<0) throw new IllegalArgumentException("Times cannot be less than 0");
67+
public Ping setTimeOutMillis(int timeOutMillis) {
68+
if (timeOutMillis < 0) throw new IllegalArgumentException("Times cannot be less than 0");
6769
this.timeOutMillis = timeOutMillis;
6870
return this;
6971
}
7072

7173
/**
7274
* Set the delay between each ping
75+
*
7376
* @param delayBetweenScansMillis - the timeout for each ping in milliseconds
7477
* @return this object to allow chaining
7578
*/
76-
public Ping setDelayMillis(int delayBetweenScansMillis){
77-
if (delayBetweenScansMillis<0) throw new IllegalArgumentException("Delay cannot be less than 0");
79+
public Ping setDelayMillis(int delayBetweenScansMillis) {
80+
if (delayBetweenScansMillis < 0)
81+
throw new IllegalArgumentException("Delay cannot be less than 0");
7882
this.delayBetweenScansMillis = delayBetweenScansMillis;
7983
return this;
8084
}
8185

8286
/**
8387
* Set number of times to ping the address
88+
*
8489
* @param noTimes - number of times, 0 = continuous
8590
* @return this object to allow chaining
8691
*/
87-
public Ping setTimes(int noTimes){
88-
if (noTimes<0) throw new IllegalArgumentException("Times cannot be less than 0");
92+
public Ping setTimes(int noTimes) {
93+
if (noTimes < 0) throw new IllegalArgumentException("Times cannot be less than 0");
8994
this.times = noTimes;
9095
return this;
9196
}
@@ -96,6 +101,7 @@ private void setAddress(InetAddress address) {
96101

97102
/**
98103
* Set the address string which will be resolved to an address by resolveAddressString()
104+
*
99105
* @param addressString - String of the address to be pinged
100106
*/
101107
private void setAddressString(String addressString) {
@@ -108,7 +114,7 @@ private void setAddressString(String addressString) {
108114
* @throws UnknownHostException - if host cannot be found
109115
*/
110116
private void resolveAddressString() throws UnknownHostException {
111-
if (address == null && addressString !=null){
117+
if (address == null && addressString != null) {
112118
address = InetAddress.getByName(addressString);
113119
}
114120
}
@@ -122,9 +128,10 @@ public void cancel() {
122128

123129
/**
124130
* Perform a synchronous ping and return a result, will ignore number of times.
125-
*
131+
* <p>
126132
* Note that this should be performed on a background thread as it will perform a network
127133
* request
134+
*
128135
* @return - ping result
129136
*/
130137
public PingResult doPing() throws UnknownHostException {
@@ -135,10 +142,11 @@ public PingResult doPing() throws UnknownHostException {
135142

136143
/**
137144
* Perform an asynchronous ping
145+
*
138146
* @param pingListener - the listener to fire PingResults to.
139147
* @return - this so we can cancel if needed
140148
*/
141-
public Ping doPing(final PingListener pingListener){
149+
public Ping doPing(final PingListener pingListener) {
142150

143151
new Thread(new Runnable() {
144152
@Override
@@ -151,7 +159,7 @@ public void run() {
151159
return;
152160
}
153161

154-
if (address == null){
162+
if (address == null) {
155163
pingListener.onError(new NullPointerException("Address is null"));
156164
return;
157165
}
@@ -166,24 +174,23 @@ public void run() {
166174
int noPings = times;
167175

168176
// times == 0 is the case that we can continuous scanning
169-
while(noPings>0 || times == 0){
177+
while (noPings > 0 || times == 0) {
170178
PingResult pingResult = PingTools.doPing(address, timeOutMillis);
171179

172-
if (pingListener!=null){
180+
if (pingListener != null) {
173181
pingListener.onResult(pingResult);
174182
}
175183

176184
// Update ping stats
177185
pingsCompleted++;
178186

179-
if (pingResult.hasError()){
187+
if (pingResult.hasError()) {
180188
noLostPackets++;
181-
}
182-
else{
189+
} else {
183190
float timeTaken = pingResult.getTimeTaken();
184191
totalPingTime += timeTaken;
185-
if (maxPingTime == - 1 || timeTaken > maxPingTime) maxPingTime = timeTaken;
186-
if (minPingTime == - 1 || timeTaken < minPingTime) minPingTime = timeTaken;
192+
if (maxPingTime == -1 || timeTaken > maxPingTime) maxPingTime = timeTaken;
193+
if (minPingTime == -1 || timeTaken < minPingTime) minPingTime = timeTaken;
187194
}
188195

189196
noPings--;
@@ -196,7 +203,7 @@ public void run() {
196203
}
197204
}
198205

199-
if (pingListener!=null){
206+
if (pingListener != null) {
200207
pingListener.onFinished(new PingStats(address, pingsCompleted, noLostPackets, totalPingTime, minPingTime, maxPingTime));
201208
}
202209
}

0 commit comments

Comments
 (0)