|
67 | 67 | * |
68 | 68 | * <p>Supported on Android 2.3+ and OpenJDK 7+. There are no public APIs to recover the trust |
69 | 69 | * manager that was used to create an {@link SSLSocketFactory}. |
| 70 | + * |
| 71 | + * <h3>Android Cleartext Permit Detection</h3> |
| 72 | + * |
| 73 | + * <p>Supported on Android 6.0+ via {@code NetworkSecurityPolicy}. |
70 | 74 | */ |
71 | 75 | public class Platform { |
72 | 76 | private static final Platform PLATFORM = findPlatform(); |
@@ -128,6 +132,10 @@ public void log(String message) { |
128 | 132 | System.out.println(message); |
129 | 133 | } |
130 | 134 |
|
| 135 | + public boolean isCleartextTrafficPermitted() { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
131 | 139 | public static List<String> alpnProtocolNames(List<Protocol> protocols) { |
132 | 140 | List<String> names = new ArrayList<>(protocols.size()); |
133 | 141 | for (int i = 0, size = protocols.size(); i < size; i++) { |
@@ -298,6 +306,25 @@ public Android(Class<?> sslParametersClass, OptionalMethod<Socket> setUseSession |
298 | 306 | } while (i < newline); |
299 | 307 | } |
300 | 308 | } |
| 309 | + |
| 310 | + @Override public boolean isCleartextTrafficPermitted() { |
| 311 | + try { |
| 312 | + Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy"); |
| 313 | + Method getInstanceMethod = networkPolicyClass.getMethod("getInstance"); |
| 314 | + Object networkSecurityPolicy = getInstanceMethod.invoke(null); |
| 315 | + Method isCleartextTrafficPermittedMethod = networkPolicyClass |
| 316 | + .getMethod("isCleartextTrafficPermitted"); |
| 317 | + boolean cleartextPermitted = (boolean) isCleartextTrafficPermittedMethod |
| 318 | + .invoke(networkSecurityPolicy); |
| 319 | + return cleartextPermitted; |
| 320 | + } catch (ClassNotFoundException e) { |
| 321 | + return super.isCleartextTrafficPermitted(); |
| 322 | + } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException |
| 323 | + | InvocationTargetException e) { |
| 324 | + throw new AssertionError(); |
| 325 | + } |
| 326 | + } |
| 327 | + |
301 | 328 | } |
302 | 329 |
|
303 | 330 | /** |
|
0 commit comments