Browse Source

Fix proxy / network on main thread

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 years ago
parent
commit
af472a8687
1 changed files with 29 additions and 9 deletions
  1. 29 9
      app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java

+ 29 - 9
app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java

@@ -74,15 +74,9 @@ public class RestModule {
     Proxy provideProxy(AppPreferences appPreferences) {
         if (!TextUtils.isEmpty(appPreferences.getProxyType()) && !"No proxy".equals(appPreferences.getProxyType())
                 && !TextUtils.isEmpty(appPreferences.getProxyHost())) {
-            if (Proxy.Type.SOCKS.equals(Proxy.Type.valueOf(appPreferences.getProxyType()))) {
-                return (new Proxy(Proxy.Type.valueOf(appPreferences.getProxyType()),
-                        InetSocketAddress.createUnresolved(appPreferences.getProxyHost(), Integer.parseInt(
-                                appPreferences.getProxyPort()))));
-            } else {
-                return (new Proxy(Proxy.Type.valueOf(appPreferences.getProxyType()),
-                        new InetSocketAddress(appPreferences.getProxyHost(),
-                                Integer.parseInt(appPreferences.getProxyPort()))));
-            }
+            GetProxyRunnable getProxyRunnable = new GetProxyRunnable(appPreferences);
+            new Thread(getProxyRunnable).start();
+            return getProxyRunnable.getProxyValue();
         } else {
             return Proxy.NO_PROXY;
         }
@@ -215,4 +209,30 @@ public class RestModule {
             return chain.proceed(request);
         }
     }
+
+    private class GetProxyRunnable implements Runnable {
+        private volatile Proxy proxy;
+        private AppPreferences appPreferences;
+
+        public GetProxyRunnable(AppPreferences appPreferences) {
+            this.appPreferences = appPreferences;
+        }
+
+        @Override
+        public void run() {
+            if (Proxy.Type.SOCKS.equals(Proxy.Type.valueOf(appPreferences.getProxyType()))) {
+                proxy = new Proxy(Proxy.Type.valueOf(appPreferences.getProxyType()),
+                        InetSocketAddress.createUnresolved(appPreferences.getProxyHost(), Integer.parseInt(
+                                appPreferences.getProxyPort())));
+            } else {
+                proxy = new Proxy(Proxy.Type.valueOf(appPreferences.getProxyType()),
+                        new InetSocketAddress(appPreferences.getProxyHost(),
+                                Integer.parseInt(appPreferences.getProxyPort())));
+            }
+        }
+
+        public Proxy getProxyValue() {
+            return proxy;
+        }
+    }
 }