Skip to content

Commit

Permalink
fix issue #2: [Android 4.1.x - 4.4.x] some video URLs don't load
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Mar 7, 2020
1 parent 613c4b3 commit 7201d10
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String, Handler> getHandlerMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7201d10

Please sign in to comment.