From 87f160f4c45c7452d6d2e4787c9cb884a6a8c380 Mon Sep 17 00:00:00 2001 From: Gregor Santner Date: Fri, 26 May 2017 12:13:28 +0200 Subject: [PATCH] Some small improvements --- .../service/FetchPodsService.java | 112 +++++++++--------- .../web/CustomWebViewClient.java | 15 ++- 2 files changed, 67 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/service/FetchPodsService.java b/app/src/main/java/com/github/dfa/diaspora_android/service/FetchPodsService.java index f9619f67..30d50554 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/service/FetchPodsService.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/service/FetchPodsService.java @@ -39,73 +39,75 @@ import javax.net.ssl.HttpsURLConnection; import info.guardianproject.netcipher.NetCipher; public class FetchPodsService extends Service { - public static final String EXTRA_PODLIST = "pods"; public static final String MESSAGE_PODS_RECEIVED = "com.github.dfa.diaspora.podsreceived"; - public static final String PODDY_PODLIST_URL = "https://raw.githubusercontent.com/Diaspora-for-Android/dandelion/master/app/src/main/res/raw/podlist.json"; - + public static final String EXTRA_PODLIST = "pods"; public FetchPodsService() { } @Override public int onStartCommand(Intent intent, int flags, int startId) { - getPods(); + new GetPodsTask(this).execute(); return super.onStartCommand(intent, flags, startId); } - private void getPods() { - AsyncTask getPodsAsync = new AsyncTask() { - @Override - protected DiasporaPodList doInBackground(Void... params) { - StringBuilder sb = new StringBuilder(); - BufferedReader br = null; - try { - HttpsURLConnection con = NetCipher.getHttpsURLConnection(PODDY_PODLIST_URL); - if (con.getResponseCode() == HttpsURLConnection.HTTP_OK) { - br = new BufferedReader(new InputStreamReader(con.getInputStream())); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - - // Parse JSON & return pod list - JSONObject json = new JSONObject(sb.toString()); - return new DiasporaPodList().fromJson(json); - } else { - AppLog.e(this, "Failed to download list of pods"); - } - } catch (IOException | JSONException e) { - e.printStackTrace(); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ignored) { - } - } - } - - // Could not fetch list of pods :( - return new DiasporaPodList(); - } - - @Override - protected void onPostExecute(DiasporaPodList pods) { - if (pods == null) { - pods = new DiasporaPodList(); - } - Intent broadcastIntent = new Intent(MESSAGE_PODS_RECEIVED); - broadcastIntent.putExtra(EXTRA_PODLIST, pods); - LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent); - stopSelf(); - } - }; - getPodsAsync.execute(); - } - @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } - } + +class GetPodsTask extends AsyncTask { + private static final String PODDY_PODLIST_URL = "https://raw.githubusercontent.com/Diaspora-for-Android/dandelion/master/app/src/main/res/raw/podlist.json"; + + private final Service service; + + GetPodsTask(Service service) { + this.service = service; + } + + @Override + protected DiasporaPodList doInBackground(Void... params) { + StringBuilder sb = new StringBuilder(); + BufferedReader br = null; + try { + HttpsURLConnection con = NetCipher.getHttpsURLConnection(PODDY_PODLIST_URL); + if (con.getResponseCode() == HttpsURLConnection.HTTP_OK) { + br = new BufferedReader(new InputStreamReader(con.getInputStream())); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + + // Parse JSON & return pod list + JSONObject json = new JSONObject(sb.toString()); + return new DiasporaPodList().fromJson(json); + } else { + AppLog.e(this, "Failed to download list of pods"); + } + } catch (IOException | JSONException e) { + e.printStackTrace(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ignored) { + } + } + } + + // Could not fetch list of pods :( + return new DiasporaPodList(); + } + + @Override + protected void onPostExecute(DiasporaPodList pods) { + if (pods == null) { + pods = new DiasporaPodList(); + } + Intent broadcastIntent = new Intent(FetchPodsService.MESSAGE_PODS_RECEIVED); + broadcastIntent.putExtra(FetchPodsService.EXTRA_PODLIST, pods); + LocalBroadcastManager.getInstance(service.getApplicationContext()).sendBroadcast(broadcastIntent); + service.stopSelf(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java b/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java index 2541ec32..7cb580f2 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java @@ -27,6 +27,7 @@ import android.webkit.WebViewClient; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.activity.MainActivity; import com.github.dfa.diaspora_android.data.DiasporaPodList; +import com.github.dfa.diaspora_android.util.AppSettings; public class CustomWebViewClient extends WebViewClient { private final App app; @@ -38,11 +39,15 @@ public class CustomWebViewClient extends WebViewClient { //Open non-diaspora links in customtab/external browser public boolean shouldOverrideUrlLoading(WebView view, String url) { - String host = app.getSettings().getPod().getPodUrl().getHost(); + DiasporaPodList.DiasporaPod pod = AppSettings.get().getPod(); + String host = null; + if (pod != null && pod.getPodUrl() != null && pod.getPodUrl().getHost() != null) { + host = pod.getPodUrl().getHost(); + } - if (url.startsWith("https://" + host) - || url.startsWith("http://" + host) - || url.startsWith("https://dia.so/")) { + if (url.startsWith("https://dia.so/") + || (host != null && (url.startsWith("https://" + host) + || url.startsWith("http://" + host)))) { return false; } else { Intent i = new Intent(MainActivity.ACTION_OPEN_EXTERNAL_URL); @@ -57,7 +62,7 @@ public class CustomWebViewClient extends WebViewClient { final CookieManager cookieManager = app.getCookieManager(); String cookies = cookieManager.getCookie(url); - DiasporaPodList.DiasporaPod pod = app.getSettings().getPod(); + DiasporaPodList.DiasporaPod pod = AppSettings.get().getPod(); if (cookies != null) { cookieManager.setCookie(url, cookies);