From 7201d109622d267e0ef110c7da6542b4827460fa Mon Sep 17 00:00:00 2001 From: "Warren R. Bank" Date: Sat, 7 Mar 2020 06:26:00 -0800 Subject: [PATCH] fix issue #2: [Android 4.1.x - 4.4.x] some video URLs don't load --- .../exoplayer_airplay_receiver/MainApp.java | 18 ++++- .../service/NetworkingService.java | 2 +- .../utils/SystemUtils.java | 2 +- .../utils/TLSSocketFactory.java | 73 +++++++++++++++++++ android-studio-project/constants.gradle | 4 +- 5 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/TLSSocketFactory.java diff --git a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/MainApp.java b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/MainApp.java index 87dc54a..6720475 100644 --- a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/MainApp.java +++ b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/MainApp.java @@ -1,11 +1,15 @@ package com.github.warren_bank.exoplayer_airplay_receiver; -import java.util.concurrent.ConcurrentHashMap; +import com.github.warren_bank.exoplayer_airplay_receiver.utils.TLSSocketFactory; import android.app.Application; +import android.os.Build; import android.os.Handler; import android.os.Message; +import java.util.concurrent.ConcurrentHashMap; +import javax.net.ssl.HttpsURLConnection; + public class MainApp extends Application { private static MainApp instance; @@ -33,6 +37,18 @@ public static void broadcastMessage(Message msg) { public void onCreate() { super.onCreate(); instance = this; + + if ( + (Build.VERSION.SDK_INT >= 16) && + (Build.VERSION.SDK_INT < 20) + ) { + try { + TLSSocketFactory socketFactory = new TLSSocketFactory(); + + HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory); + } + catch(Exception e) {} + } } public ConcurrentHashMap getHandlerMap() { diff --git a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/service/NetworkingService.java b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/service/NetworkingService.java index 1b5db95..36149e4 100644 --- a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/service/NetworkingService.java +++ b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/service/NetworkingService.java @@ -73,7 +73,7 @@ public void onCreate() { toast.show(); showNotification(); - airplayName = android.os.Build.MODEL + "@" + airplayName; + airplayName = Build.MODEL + "@" + airplayName; new Thread() { public void run() { diff --git a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/SystemUtils.java b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/SystemUtils.java index 1c5d66b..dd9b862 100644 --- a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/SystemUtils.java +++ b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/SystemUtils.java @@ -16,7 +16,7 @@ public class SystemUtils { * source: https://stackoverflow.com/a/28747907 */ public static boolean isScreenOn(Context context) { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); boolean screenOn = false; for (Display display : dm.getDisplays()) { diff --git a/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/TLSSocketFactory.java b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/TLSSocketFactory.java new file mode 100644 index 0000000..a9d9b92 --- /dev/null +++ b/android-studio-project/ExoPlayer-AirPlay-Receiver/src/main/java/com/github/warren_bank/exoplayer_airplay_receiver/utils/TLSSocketFactory.java @@ -0,0 +1,73 @@ +package com.github.warren_bank.exoplayer_airplay_receiver.utils; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + +/** + * https://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/ + */ +public class TLSSocketFactory extends SSLSocketFactory { + + private SSLSocketFactory internalSSLSocketFactory; + + public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, null, null); + internalSSLSocketFactory = context.getSocketFactory(); + } + + @Override + public String[] getDefaultCipherSuites() { + return internalSSLSocketFactory.getDefaultCipherSuites(); + } + + @Override + public String[] getSupportedCipherSuites() { + return internalSSLSocketFactory.getSupportedCipherSuites(); + } + + @Override + public Socket createSocket() throws IOException { + return enableTLSOnSocket(internalSSLSocketFactory.createSocket()); + } + + @Override + public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { + return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose)); + } + + @Override + public Socket createSocket(String host, int port) throws IOException, UnknownHostException { + return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); + } + + @Override + public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { + return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort)); + } + + @Override + public Socket createSocket(InetAddress host, int port) throws IOException { + return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); + } + + @Override + public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { + return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort)); + } + + private Socket enableTLSOnSocket(Socket socket) { + if(socket != null && (socket instanceof SSLSocket)) { + ((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"}); + } + return socket; + } +} diff --git a/android-studio-project/constants.gradle b/android-studio-project/constants.gradle index 23d1df7..7dd3b09 100644 --- a/android-studio-project/constants.gradle +++ b/android-studio-project/constants.gradle @@ -1,6 +1,6 @@ project.ext { - releaseVersionCode = Integer.parseInt("001000716", 10) - releaseVersion = '001.00.07-16API' + releaseVersionCode = Integer.parseInt("001000816", 10) + releaseVersion = '001.00.08-16API' minSdkVersion = 16 targetSdkVersion = 28 compileSdkVersion = 28