Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/caarmen-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed Mar 14, 2019
2 parents 63d8bfc + 5354ea2 commit d26db0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions networkmonitor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ android {
applicationId "ca.rmen.android.networkmonitor"
minSdkVersion 14
targetSdkVersion 28
versionName "1.31.0"
versionCode 13100
versionName "1.31.1"
versionCode 13101
archivesBaseName += "-" + versionName
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,18 @@ public void onAppWarningOkClicked() {
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions();
}
NetMonService.start(this);
}

private void requestPermissions() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_LOCATION);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_LOCATION);
} else {
onPermissionGranted();
}
}

private void onPermissionGranted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import ca.rmen.android.networkmonitor.Constants;
import ca.rmen.android.networkmonitor.app.prefs.NetMonPreferences;
import ca.rmen.android.networkmonitor.app.service.NetMonNotification;
Expand All @@ -61,6 +64,7 @@ private enum NetworkTestResult {
}

private static final int PORT = 80;
private static final int HTTPS_PORT = 443;
private static final int DURATION_SLOW = 5000;

// The maximum connection and read timeout for a connection test, in ms. We may actually set a lower timeout if the user has set the app to test very frequently (ex: every 10 seconds).
Expand Down Expand Up @@ -185,7 +189,7 @@ private NetworkTestResult getSocketTestResult() {

/**
* Try to open a connection to an HTTP server, and execute a simple GET request. If we can read a response to the GET request, we consider that the network
* is up. This test uses an HttpURLConnection.
* is up. This test uses an HttpsURLConnection.
*
* @return {@link NetworkTestResult#PASS} if we were able to read a response to a GET request quickly, {@link NetworkTestResult#FAIL} if any error occurred
* trying to execute the GET, or {@link NetworkTestResult#SLOW} if we were able to read a response, but it took too long.
Expand All @@ -194,16 +198,25 @@ private NetworkTestResult getHttpTestResult() {
Log.v(TAG, "getHttpTestResult BEGIN");
InputStream inputStream = null;
try {
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
long before = System.currentTimeMillis();
String host = NetMonPreferences.getInstance(mContext).getTestServer().trim();
URL url = new URL("http", host, PORT, "/");
URL url = new URL("https", host, HTTPS_PORT, "/");
URLConnection connection = url.openConnection();
Log.v(TAG, "Opened connection");
connection.setConnectTimeout(mTimeout);
connection.setReadTimeout(mTimeout);
connection.addRequestProperty("Cache-Control", "no-cache");
connection.setUseCaches(false);
if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setInstanceFollowRedirects(false);
((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
}
Log.v(TAG, "Will open input stream");
inputStream = connection.getInputStream();
long after = System.currentTimeMillis();
Expand Down

0 comments on commit d26db0f

Please sign in to comment.