1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-22 04:12:08 +01:00

Some small improvements

This commit is contained in:
Gregor Santner 2017-05-26 12:13:28 +02:00
parent e443a8ed6f
commit 87f160f4c4
No known key found for this signature in database
GPG key ID: 7E83A7834AECB009
2 changed files with 67 additions and 60 deletions

View file

@ -39,73 +39,75 @@ import javax.net.ssl.HttpsURLConnection;
import info.guardianproject.netcipher.NetCipher; import info.guardianproject.netcipher.NetCipher;
public class FetchPodsService extends Service { 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 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() { public FetchPodsService() {
} }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
getPods(); new GetPodsTask(this).execute();
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
} }
private void getPods() {
AsyncTask<Void, Void, DiasporaPodList> getPodsAsync = new AsyncTask<Void, Void, DiasporaPodList>() {
@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 @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service. // TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented"); throw new UnsupportedOperationException("Not yet implemented");
} }
} }
class GetPodsTask extends AsyncTask<Void, Void, DiasporaPodList> {
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();
}
}

View file

@ -27,6 +27,7 @@ import android.webkit.WebViewClient;
import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.activity.MainActivity; import com.github.dfa.diaspora_android.activity.MainActivity;
import com.github.dfa.diaspora_android.data.DiasporaPodList; import com.github.dfa.diaspora_android.data.DiasporaPodList;
import com.github.dfa.diaspora_android.util.AppSettings;
public class CustomWebViewClient extends WebViewClient { public class CustomWebViewClient extends WebViewClient {
private final App app; private final App app;
@ -38,11 +39,15 @@ public class CustomWebViewClient extends WebViewClient {
//Open non-diaspora links in customtab/external browser //Open non-diaspora links in customtab/external browser
public boolean shouldOverrideUrlLoading(WebView view, String url) { 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) if (url.startsWith("https://dia.so/")
|| url.startsWith("http://" + host) || (host != null && (url.startsWith("https://" + host)
|| url.startsWith("https://dia.so/")) { || url.startsWith("http://" + host)))) {
return false; return false;
} else { } else {
Intent i = new Intent(MainActivity.ACTION_OPEN_EXTERNAL_URL); Intent i = new Intent(MainActivity.ACTION_OPEN_EXTERNAL_URL);
@ -57,7 +62,7 @@ public class CustomWebViewClient extends WebViewClient {
final CookieManager cookieManager = app.getCookieManager(); final CookieManager cookieManager = app.getCookieManager();
String cookies = cookieManager.getCookie(url); String cookies = cookieManager.getCookie(url);
DiasporaPodList.DiasporaPod pod = app.getSettings().getPod(); DiasporaPodList.DiasporaPod pod = AppSettings.get().getPod();
if (cookies != null) { if (cookies != null) {
cookieManager.setCookie(url, cookies); cookieManager.setCookie(url, cookies);