2016-03-30 03:13:38 +02:00
|
|
|
package com.github.dfa.diaspora_android.task;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.webkit.CookieManager;
|
|
|
|
|
|
|
|
import com.github.dfa.diaspora_android.App;
|
2016-06-04 03:48:57 +02:00
|
|
|
import com.github.dfa.diaspora_android.data.PodUserProfile;
|
2016-03-30 03:13:38 +02:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
2016-06-09 01:53:47 +02:00
|
|
|
import java.io.InputStream;
|
2016-03-30 03:13:38 +02:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.URL;
|
|
|
|
|
2016-06-09 01:53:47 +02:00
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
|
|
|
import info.guardianproject.netcipher.NetCipher;
|
|
|
|
|
2016-03-30 03:13:38 +02:00
|
|
|
/**
|
2016-06-09 01:53:47 +02:00
|
|
|
* AsyncTask to fetch a users profile
|
2016-07-18 11:59:10 +02:00
|
|
|
* Created by Gregor Santner (sangre) on 30.03.16.
|
2016-03-30 03:13:38 +02:00
|
|
|
*/
|
|
|
|
public class ProfileFetchTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
// Code for getting the profile async without any UI/WebView
|
|
|
|
// TODO: This is an early version,needs to be converted to Service
|
|
|
|
|
|
|
|
final App app;
|
|
|
|
final Context context;
|
|
|
|
|
|
|
|
public ProfileFetchTask(final App app) {
|
|
|
|
this.context = app.getApplicationContext();
|
|
|
|
this.app = app;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
String extractedProfileData = null;
|
|
|
|
final CookieManager cookieManager = app.getCookieManager();
|
|
|
|
String cookies = cookieManager.getCookie("https://" + app.getSettings().getPodDomain());
|
|
|
|
Log.d(App.TAG, cookies);
|
|
|
|
|
2016-06-09 01:53:47 +02:00
|
|
|
HttpsURLConnection connection;
|
|
|
|
InputStream inStream;
|
2016-03-30 03:13:38 +02:00
|
|
|
try {
|
|
|
|
URL url = new URL("https://" + app.getSettings().getPodDomain() + "/stream");
|
2016-06-09 01:53:47 +02:00
|
|
|
connection = NetCipher.getHttpsURLConnection(url);
|
|
|
|
connection.setReadTimeout(10000);
|
|
|
|
connection.setConnectTimeout(15000);
|
|
|
|
connection.setRequestMethod("GET");
|
2016-03-30 03:13:38 +02:00
|
|
|
if (cookies != null) {
|
2016-06-09 01:53:47 +02:00
|
|
|
connection.setRequestProperty("Cookie", cookies);
|
2016-03-30 03:13:38 +02:00
|
|
|
}
|
2016-06-09 01:53:47 +02:00
|
|
|
connection.connect();
|
2016-03-30 03:13:38 +02:00
|
|
|
|
2016-06-09 01:53:47 +02:00
|
|
|
inStream = connection.getInputStream();
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
|
2016-03-30 03:13:38 +02:00
|
|
|
String line;
|
|
|
|
final String TARGET_TAG = "window.gon={};gon.user=";
|
|
|
|
while ((line = br.readLine()) != null && !line.startsWith("<body")) {
|
|
|
|
if (line.startsWith(TARGET_TAG)) {
|
|
|
|
extractedProfileData = line.substring(TARGET_TAG.length());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 01:53:47 +02:00
|
|
|
|
|
|
|
try{
|
|
|
|
br.close();
|
|
|
|
inStream.close();
|
|
|
|
} catch (IOException e){/*Nothing*/}
|
|
|
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
2016-03-30 03:13:38 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (extractedProfileData != null) {
|
2016-06-04 03:48:57 +02:00
|
|
|
PodUserProfile profile = new PodUserProfile(app);
|
2016-03-30 03:13:38 +02:00
|
|
|
profile.parseJson(extractedProfileData);
|
2016-04-09 13:53:06 +02:00
|
|
|
Log.d(App.TAG, "Extracted new_messages (service):" + profile.getUnreadMessagesCount());
|
2016-03-30 03:13:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-09 01:53:47 +02:00
|
|
|
}
|