mirror of
https://github.com/gsantner/dandelion
synced 2024-11-15 17:02:10 +01:00
Extract cookies from WebView (#6)
This commit is contained in:
parent
45cb50111a
commit
e5689bc37a
5 changed files with 205 additions and 45 deletions
|
@ -1,6 +1,10 @@
|
||||||
package com.github.dfa.diaspora_android;
|
package com.github.dfa.diaspora_android;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.webkit.CookieManager;
|
||||||
|
import android.webkit.CookieSyncManager;
|
||||||
|
|
||||||
import com.github.dfa.diaspora_android.data.AppSettings;
|
import com.github.dfa.diaspora_android.data.AppSettings;
|
||||||
import com.github.dfa.diaspora_android.util.AvatarImageLoader;
|
import com.github.dfa.diaspora_android.util.AvatarImageLoader;
|
||||||
|
@ -9,15 +13,26 @@ import com.github.dfa.diaspora_android.util.AvatarImageLoader;
|
||||||
* Created by gregor on 24.03.16.
|
* Created by gregor on 24.03.16.
|
||||||
*/
|
*/
|
||||||
public class App extends Application {
|
public class App extends Application {
|
||||||
|
public static final String TAG = "DIASPORA_";
|
||||||
|
|
||||||
private AppSettings appSettings;
|
private AppSettings appSettings;
|
||||||
private AvatarImageLoader avatarImageLoader;
|
private AvatarImageLoader avatarImageLoader;
|
||||||
public static final String APP_LOG_TAG = "DIASPORA_";
|
private CookieManager cookieManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
appSettings = new AppSettings(getApplicationContext());
|
final Context c = getApplicationContext();
|
||||||
avatarImageLoader = new AvatarImageLoader(getApplicationContext());
|
appSettings = new AppSettings(c);
|
||||||
|
avatarImageLoader = new AvatarImageLoader(c);
|
||||||
|
|
||||||
|
|
||||||
|
// Get cookie manager
|
||||||
|
cookieManager = CookieManager.getInstance();
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
CookieSyncManager.createInstance(c);
|
||||||
|
}
|
||||||
|
cookieManager.setAcceptCookie(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AppSettings getSettings() {
|
public AppSettings getSettings() {
|
||||||
|
@ -27,4 +42,8 @@ public class App extends Application {
|
||||||
public AvatarImageLoader getAvatarImageLoader() {
|
public AvatarImageLoader getAvatarImageLoader() {
|
||||||
return avatarImageLoader;
|
return avatarImageLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CookieManager getCookieManager() {
|
||||||
|
return cookieManager;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,10 +51,13 @@ import android.text.Html;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.method.LinkMovementMethod;
|
import android.text.method.LinkMovementMethod;
|
||||||
import android.text.util.Linkify;
|
import android.text.util.Linkify;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.webkit.CookieManager;
|
||||||
|
import android.webkit.CookieSyncManager;
|
||||||
import android.webkit.JavascriptInterface;
|
import android.webkit.JavascriptInterface;
|
||||||
import android.webkit.ValueCallback;
|
import android.webkit.ValueCallback;
|
||||||
import android.webkit.WebChromeClient;
|
import android.webkit.WebChromeClient;
|
||||||
|
@ -75,6 +78,7 @@ import com.github.dfa.diaspora_android.data.AppSettings;
|
||||||
import com.github.dfa.diaspora_android.data.WebUserProfile;
|
import com.github.dfa.diaspora_android.data.WebUserProfile;
|
||||||
import com.github.dfa.diaspora_android.listener.SoftKeyboardStateWatcher;
|
import com.github.dfa.diaspora_android.listener.SoftKeyboardStateWatcher;
|
||||||
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
|
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
|
||||||
|
import com.github.dfa.diaspora_android.task.ProfileFetchTask;
|
||||||
import com.github.dfa.diaspora_android.util.Helpers;
|
import com.github.dfa.diaspora_android.util.Helpers;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
@ -217,7 +221,24 @@ public class MainActivity extends AppCompatActivity
|
||||||
|
|
||||||
public void onPageFinished(WebView view, String url) {
|
public void onPageFinished(WebView view, String url) {
|
||||||
swipeRefreshLayout.setRefreshing(false);
|
swipeRefreshLayout.setRefreshing(false);
|
||||||
|
|
||||||
|
|
||||||
|
final CookieManager cookieManager = app.getCookieManager();
|
||||||
|
String cookies = cookieManager.getCookie(url);
|
||||||
|
Log.d(App.TAG, "All the cookies in a string:" + cookies);
|
||||||
|
|
||||||
|
if(cookies != null) {
|
||||||
|
cookieManager.setCookie(url, cookies);
|
||||||
|
cookieManager.setCookie("https://"+appSettings.getPodDomain(),cookies);
|
||||||
|
for(String c:cookies.split(";")){
|
||||||
|
Log.d(App.TAG, "Cookie: " + c.split("=")[0]+ " Value:"+c.split("=")[1]);
|
||||||
|
}
|
||||||
|
//new ProfileFetchTask(app).execute();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import org.json.JSONObject;
|
||||||
public class WebUserProfile {
|
public class WebUserProfile {
|
||||||
private static final int MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF = 5000;
|
private static final int MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF = 5000;
|
||||||
|
|
||||||
private Handler uiHandler;
|
private Handler callbackHandler;
|
||||||
private WebUserProfileChangedListener listener;
|
private WebUserProfileChangedListener listener;
|
||||||
private App app;
|
private App app;
|
||||||
private AppSettings appSettings;
|
private AppSettings appSettings;
|
||||||
|
@ -29,9 +29,8 @@ public class WebUserProfile {
|
||||||
private int notificationCount;
|
private int notificationCount;
|
||||||
private int unreadMessagesCount;
|
private int unreadMessagesCount;
|
||||||
|
|
||||||
public WebUserProfile(App app, Handler uiHandler, WebUserProfileChangedListener listener) {
|
|
||||||
this.listener = listener;
|
public WebUserProfile(App app) {
|
||||||
this.uiHandler = uiHandler;
|
|
||||||
this.app = app;
|
this.app = app;
|
||||||
appSettings = app.getSettings();
|
appSettings = app.getSettings();
|
||||||
|
|
||||||
|
@ -40,6 +39,12 @@ public class WebUserProfile {
|
||||||
name = appSettings.getName();
|
name = appSettings.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WebUserProfile(App app, Handler callbackHandler, WebUserProfileChangedListener listener) {
|
||||||
|
this(app);
|
||||||
|
this.listener = listener;
|
||||||
|
this.callbackHandler = callbackHandler;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isRefreshNeeded() {
|
public boolean isRefreshNeeded() {
|
||||||
return (System.currentTimeMillis() - lastLoaded) >= MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF;
|
return (System.currentTimeMillis() - lastLoaded) >= MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF;
|
||||||
}
|
}
|
||||||
|
@ -50,67 +55,38 @@ public class WebUserProfile {
|
||||||
|
|
||||||
public boolean parseJson(String jsonStr) {
|
public boolean parseJson(String jsonStr) {
|
||||||
try {
|
try {
|
||||||
this.json = new JSONObject(jsonStr);
|
json = new JSONObject(jsonStr);
|
||||||
lastLoaded = System.currentTimeMillis();
|
lastLoaded = System.currentTimeMillis();
|
||||||
|
|
||||||
String str;
|
|
||||||
int integer;
|
|
||||||
|
|
||||||
// Avatar
|
// Avatar
|
||||||
if (json.has("avatar")) {
|
if (json.has("avatar")) {
|
||||||
JSONObject avatarJson = json.getJSONObject("avatar");
|
JSONObject avatarJson = json.getJSONObject("avatar");
|
||||||
if (avatarJson.has("medium") && !((str = avatarJson.getString("medium")).equals(avatarUrl))) {
|
if (avatarJson.has("medium") && setAvatarUrl(avatarJson.getString("medium"))) {
|
||||||
app.getAvatarImageLoader().clearAvatarImage();
|
app.getAvatarImageLoader().clearAvatarImage();
|
||||||
avatarUrl = str;
|
appSettings.setAvatarUrl(avatarUrl);
|
||||||
appSettings.setAvatarUrl(str);
|
|
||||||
uiHandler.post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
listener.onUserProfileAvatarChanged(avatarUrl);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GUID (User id)
|
// GUID (User id)
|
||||||
if (json.has("guid") && !((str = json.getString("guid")).equals(guid))) {
|
if (json.has("guid") && loadGuid(json.getString("guid"))) {
|
||||||
guid = str;
|
|
||||||
appSettings.setProfileId(guid);
|
appSettings.setProfileId(guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
if (json.has("name") && !((str = json.getString("name")).equals(name))) {
|
if (json.has("name") && loadName(json.getString("name"))) {
|
||||||
name = str;
|
|
||||||
appSettings.setName(name);
|
appSettings.setName(name);
|
||||||
uiHandler.post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
listener.onUserProfileNameChanged(name);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unread message count
|
// Unread message count
|
||||||
if (json.has("notifications_count") && (integer = json.getInt("notifications_count")) != notificationCount) {
|
if (json.has("notifications_count") && loadNotificationCount(json.getInt("notifications_count"))) {
|
||||||
notificationCount = integer;
|
|
||||||
uiHandler.post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
listener.onNotificationCountChanged(notificationCount);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unread message count
|
// Unread message count
|
||||||
if (json.has("unread_messages_count") && (integer = json.getInt("unread_messages_count")) != unreadMessagesCount) {
|
if (json.has("unread_messages_count") && loadUnreadMessagesCount(json.getInt("unread_messages_count"))) {
|
||||||
unreadMessagesCount = integer;
|
|
||||||
uiHandler.post(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
listener.onUnreadMessageCountChanged(unreadMessagesCount);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isWebUserProfileLoaded = true;
|
isWebUserProfileLoaded = true;
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
Log.d(App.APP_LOG_TAG, e.getMessage());
|
Log.d(App.TAG, e.getMessage());
|
||||||
isWebUserProfileLoaded = false;
|
isWebUserProfileLoaded = false;
|
||||||
}
|
}
|
||||||
lastLoaded = System.currentTimeMillis();
|
lastLoaded = System.currentTimeMillis();
|
||||||
|
@ -141,6 +117,77 @@ public class WebUserProfile {
|
||||||
return unreadMessagesCount;
|
return unreadMessagesCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Private property setters
|
||||||
|
*/
|
||||||
|
private boolean setAvatarUrl(final String avatarUrl) {
|
||||||
|
if (!this.avatarUrl.equals(avatarUrl)) {
|
||||||
|
this.avatarUrl = avatarUrl;
|
||||||
|
if (listener != null && callbackHandler != null) {
|
||||||
|
callbackHandler.post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
listener.onUserProfileAvatarChanged(avatarUrl);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean loadGuid(final String guid) {
|
||||||
|
if (!this.guid.equals(guid)) {
|
||||||
|
this.guid = guid;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean loadName(final String name) {
|
||||||
|
if (!this.name.equals(name)) {
|
||||||
|
this.name = name;
|
||||||
|
if (listener != null && callbackHandler != null) {
|
||||||
|
callbackHandler.post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
listener.onUserProfileNameChanged(name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean loadNotificationCount(final int notificationCount) {
|
||||||
|
if (this.notificationCount != notificationCount) {
|
||||||
|
this.notificationCount = notificationCount;
|
||||||
|
if (listener != null && callbackHandler != null) {
|
||||||
|
callbackHandler.post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
listener.onNotificationCountChanged(notificationCount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean loadUnreadMessagesCount(final int unreadMessagesCount) {
|
||||||
|
if (this.unreadMessagesCount != unreadMessagesCount) {
|
||||||
|
this.unreadMessagesCount = unreadMessagesCount;
|
||||||
|
if (listener != null && callbackHandler != null) {
|
||||||
|
callbackHandler.post(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
listener.onUnreadMessageCountChanged(unreadMessagesCount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Not implemented / not needed yet:
|
* Not implemented / not needed yet:
|
||||||
* string "diasporaAddress"
|
* string "diasporaAddress"
|
||||||
|
|
|
@ -46,7 +46,7 @@ public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(App.APP_LOG_TAG, e.getMessage());
|
Log.e(App.TAG, e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (out != null) {
|
if (out != null) {
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
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;
|
||||||
|
import com.github.dfa.diaspora_android.data.WebUserProfile;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Gregor Santner (de-live-gdev) on 30.03.16.
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
|
||||||
|
try {
|
||||||
|
URL url = new URL("https://" + app.getSettings().getPodDomain() + "/stream");
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setReadTimeout(10000);
|
||||||
|
conn.setConnectTimeout(15000);
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
if (cookies != null) {
|
||||||
|
conn.setRequestProperty("Cookie", cookies);
|
||||||
|
}
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (extractedProfileData != null) {
|
||||||
|
WebUserProfile profile = new WebUserProfile(app);
|
||||||
|
profile.parseJson(extractedProfileData);
|
||||||
|
Log.d(App.TAG, "Extracted new_messages (service):"+profile.getUnreadMessagesCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue