mirror of
https://github.com/gsantner/dandelion
synced 2024-11-21 20:02:07 +01:00
Some small improvements
This commit is contained in:
parent
e443a8ed6f
commit
87f160f4c4
2 changed files with 67 additions and 60 deletions
|
@ -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<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
|
||||
public IBinder onBind(Intent intent) {
|
||||
// TODO: Return the communication channel to the service.
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue