package be.khleuven.nieuws;_x000D_
_x000D_
import android.app.Activity;_x000D_
import android.app.ProgressDialog;_x000D_
import android.content.Intent;_x000D_
import android.content.SharedPreferences;_x000D_
import android.graphics.Bitmap;_x000D_
import android.net.http.SslError;_x000D_
import android.os.Bundle;_x000D_
import android.view.View;_x000D_
import android.webkit.CookieSyncManager;_x000D_
import android.webkit.SslErrorHandler;_x000D_
import android.webkit.WebView;_x000D_
import android.webkit.WebViewClient;_x000D_
_x000D_
/**_x000D_
 * @author Laurent Mouha, Robin Vrebos en Bram Miermans_x000D_
 * _x000D_
 */_x000D_
public class KHLLoginActivity extends Activity {_x000D_
	/**_x000D_
	 * De webview wat gebruikt wordt._x000D_
	 */_x000D_
	WebView myWebView;_x000D_
	/**_x000D_
	 * De url die bijgehouden wordt in de preferences wordt hier in gezet._x000D_
	 */_x000D_
	String mFeedUrl = null;_x000D_
	public static final String PREFS_NAME = "MyPrefsFile";_x000D_
	/**_x000D_
	 * Hierin wordt de url opgeslagen._x000D_
	 */_x000D_
	static SharedPreferences settings;_x000D_
	/**_x000D_
	 * Editor voor aanpassen van data in SharedPreferences._x000D_
	 */_x000D_
	SharedPreferences.Editor editor;_x000D_
_x000D_
	public void onCreate(Bundle savedInstanceState) {_x000D_
		super.onCreate(savedInstanceState);_x000D_
		setContentView(R.layout.main);_x000D_
_x000D_
		settings = getSharedPreferences(PREFS_NAME, 0);_x000D_
		editor = settings.edit();_x000D_
_x000D_
		mFeedUrl = settings.getString("feedUrl", null);_x000D_
_x000D_
		if (mFeedUrl != null) {_x000D_
			openRSS(mFeedUrl);_x000D_
		} else {_x000D_
_x000D_
			/**_x000D_
			 * Deze klasse wordt gebruikt als interface tussen de java en_x000D_
			 * javascriptcode die gebruikt wordt bij de webview_x000D_
			 * _x000D_
			 * @author Laurent Mouha, Robin Vrebos en Bram Miermans_x000D_
			 * _x000D_
			 */_x000D_
			class MyJavaScriptInterface {_x000D_
_x000D_
				/**_x000D_
				 * Verwerkt de html code en opent dan de KHLNieuwsActivity._x000D_
				 * Wordt aangeroepen vanuit de javascriptcode._x000D_
				 * _x000D_
				 * @param html_x000D_
				 *            De string die de htmlcode van de pagina bevat._x000D_
				 */_x000D_
				@SuppressWarnings("unused")_x000D_
				public void processHTML(String html) {_x000D_
					String link = fetchLink(html);_x000D_
					editor.putString("feedUrl", link);_x000D_
					editor.commit();_x000D_
					openRSS(link);_x000D_
				}_x000D_
_x000D_
				/**_x000D_
				 * Deze functie doorzoekt de htmlcode naar een geldige RSS-link._x000D_
				 * _x000D_
				 * @param html_x000D_
				 *            De string die de htmlcode van de pagina bevat._x000D_
				 * @return De juiste url van de RSS-feed._x000D_
				 */_x000D_
				private String fetchLink(String html) {_x000D_
					String[] temp;_x000D_
					temp = html.split("\n");_x000D_
					String link = "";_x000D_
					boolean stop = false;_x000D_
					try {_x000D_
						for (String t : temp) {_x000D_
							if (t.contains("<a href=\"http://portaal.khleuven.be/rss.php")) {_x000D_
								link = t;_x000D_
								stop = true;_x000D_
							}_x000D_
							if (stop)_x000D_
								break;_x000D_
						}_x000D_
_x000D_
					} catch (Exception e) {_x000D_
_x000D_
					} finally {_x000D_
						if (link != null) {_x000D_
_x000D_
							String[] parts = link.split("\"");_x000D_
							link = parts[1];_x000D_
_x000D_
						}_x000D_
					}_x000D_
					return link;_x000D_
				}_x000D_
			}_x000D_
_x000D_
			// cookie bijhouden_x000D_
			CookieSyncManager.createInstance(this);_x000D_
			CookieSyncManager.getInstance().startSync();_x000D_
_x000D_
			// webview gebruiken voor login_x000D_
			myWebView = (WebView) findViewById(R.id.webview);_x000D_
			myWebView.getSettings().setJavaScriptEnabled(true);_x000D_
			myWebView.setWebViewClient(new WebViewClient() {_x000D_
				public void onReceivedSslError(WebView view,_x000D_
						SslErrorHandler handler, SslError error) {_x000D_
					handler.proceed(); // Ignore SSL certificate errors_x000D_
				}_x000D_
_x000D_
				// wat doen wanneer pagina geladen is_x000D_
				// checken of ingelogd door te kijken of we terug op de_x000D_
				// portaalsite zitten_x000D_
				public void onPageFinished(WebView view, String url) {_x000D_
					if (view.getUrl().equals("https://portaal.khleuven.be/")) {_x000D_
						view.loadUrl("javascript:window.interfaceName.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");_x000D_
					}_x000D_
				}_x000D_
_x000D_
				public void onPageStarted(WebView view, String url,_x000D_
						Bitmap favicon) {_x000D_
					if (url.equals("https://portaal.khleuven.be/")) {_x000D_
						view.setVisibility(View.INVISIBLE);_x000D_
						ProgressDialog.show(_x000D_
								KHLLoginActivity.this, "",_x000D_
								"Fetching RSS. Please wait...", true);_x000D_
					}_x000D_
				}_x000D_
			});_x000D_
_x000D_
			myWebView.getSettings().setDatabasePath("khl.news");_x000D_
			myWebView.getSettings().setDomStorageEnabled(true);_x000D_
_x000D_
			myWebView.addJavascriptInterface(new MyJavaScriptInterface(),_x000D_
					"interfaceName");_x000D_
_x000D_
			myWebView_x000D_
					.loadUrl("https://portaal.khleuven.be/Shibboleth.sso/WAYF/khleuven?target=https%3A%2F%2Fportaal.khleuven.be%2F");_x000D_
_x000D_
		}_x000D_
_x000D_
	}_x000D_
_x000D_
	private void openRSS(String link) {_x000D_
		Intent intent = new Intent(getApplicationContext(),_x000D_
				KHLNieuwsActivity.class);_x000D_
		intent.putExtra("feedUrl", link);_x000D_
		startActivity(intent);_x000D_
	}_x000D_
_x000D_
}
// checken of ingelogd door te kijken of we terug op de_x000D_
package be.khleuven.nieuws;_x000D_
_x000D_
import android.app.Activity;_x000D_
import android.app.ProgressDialog;_x000D_
import android.content.Intent;_x000D_
import android.content.SharedPreferences;_x000D_
import android.graphics.Bitmap;_x000D_
import android.net.http.SslError;_x000D_
import android.os.Bundle;_x000D_
import android.view.View;_x000D_
import android.webkit.CookieSyncManager;_x000D_
import android.webkit.SslErrorHandler;_x000D_
import android.webkit.WebView;_x000D_
import android.webkit.WebViewClient;_x000D_
_x000D_
/**_x000D_
 * @author Laurent Mouha, Robin Vrebos en Bram Miermans_x000D_
 * _x000D_
 */_x000D_
public class KHLLoginActivity extends Activity {_x000D_
	/**_x000D_
	 * De webview wat gebruikt wordt._x000D_
	 */_x000D_
	WebView myWebView;_x000D_
	/**_x000D_
	 * De url die bijgehouden wordt in de preferences wordt hier in gezet._x000D_
	 */_x000D_
	String mFeedUrl = null;_x000D_
	public static final String PREFS_NAME = "MyPrefsFile";_x000D_
	/**_x000D_
	 * Hierin wordt de url opgeslagen._x000D_
	 */_x000D_
	static SharedPreferences settings;_x000D_
	/**_x000D_
	 * Editor voor aanpassen van data in SharedPreferences._x000D_
	 */_x000D_
	SharedPreferences.Editor editor;_x000D_
_x000D_
	public void onCreate(Bundle savedInstanceState) {_x000D_
		super.onCreate(savedInstanceState);_x000D_
		setContentView(R.layout.main);_x000D_
_x000D_
		settings = getSharedPreferences(PREFS_NAME, 0);_x000D_
		editor = settings.edit();_x000D_
_x000D_
		mFeedUrl = settings.getString("feedUrl", null);_x000D_
_x000D_
		if (mFeedUrl != null) {_x000D_
			openRSS(mFeedUrl);_x000D_
		} else {_x000D_
_x000D_
			/**_x000D_
			 * Deze klasse wordt gebruikt als interface tussen de java en_x000D_
			 * javascriptcode die gebruikt wordt bij de webview_x000D_
			 * _x000D_
			 * @author Laurent Mouha, Robin Vrebos en Bram Miermans_x000D_
			 * _x000D_
			 */_x000D_
			class MyJavaScriptInterface {_x000D_
_x000D_
				/**_x000D_
				 * Verwerkt de html code en opent dan de KHLNieuwsActivity._x000D_
				 * Wordt aangeroepen vanuit de javascriptcode._x000D_
				 * _x000D_
				 * @param html_x000D_
				 *            De string die de htmlcode van de pagina bevat._x000D_
				 */_x000D_
				@SuppressWarnings("unused")_x000D_
				public void processHTML(String html) {_x000D_
					String link = fetchLink(html);_x000D_
					editor.putString("feedUrl", link);_x000D_
					editor.commit();_x000D_
					openRSS(link);_x000D_
				}_x000D_
_x000D_
				/**_x000D_
				 * Deze functie doorzoekt de htmlcode naar een geldige RSS-link._x000D_
				 * _x000D_
				 * @param html_x000D_
				 *            De string die de htmlcode van de pagina bevat._x000D_
				 * @return De juiste url van de RSS-feed._x000D_
				 */_x000D_
				private String fetchLink(String html) {_x000D_
					String[] temp;_x000D_
					temp = html.split("\n");_x000D_
					String link = "";_x000D_
					boolean stop = false;_x000D_
					try {_x000D_
						for (String t : temp) {_x000D_
							if (t.contains("<a href=\"http://portaal.khleuven.be/rss.php")) {_x000D_
								link = t;_x000D_
								stop = true;_x000D_
							}_x000D_
							if (stop)_x000D_
								break;_x000D_
						}_x000D_
_x000D_
					} catch (Exception e) {_x000D_
_x000D_
					} finally {_x000D_
						if (link != null) {_x000D_
_x000D_
							String[] parts = link.split("\"");_x000D_
							link = parts[1];_x000D_
_x000D_
						}_x000D_
					}_x000D_
					return link;_x000D_
				}_x000D_
			}_x000D_
_x000D_
			// cookie bijhouden_x000D_
			CookieSyncManager.createInstance(this);_x000D_
			CookieSyncManager.getInstance().startSync();_x000D_
_x000D_
			// webview gebruiken voor login_x000D_
			myWebView = (WebView) findViewById(R.id.webview);_x000D_
			myWebView.getSettings().setJavaScriptEnabled(true);_x000D_
			myWebView.setWebViewClient(new WebViewClient() {_x000D_
				public void onReceivedSslError(WebView view,_x000D_
						SslErrorHandler handler, SslError error) {_x000D_
					handler.proceed(); // Ignore SSL certificate errors_x000D_
				}_x000D_
_x000D_
				// wat doen wanneer pagina geladen is_x000D_
				// checken of<SUF>
				// portaalsite zitten_x000D_
				public void onPageFinished(WebView view, String url) {_x000D_
					if (view.getUrl().equals("https://portaal.khleuven.be/")) {_x000D_
						view.loadUrl("javascript:window.interfaceName.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");_x000D_
					}_x000D_
				}_x000D_
_x000D_
				public void onPageStarted(WebView view, String url,_x000D_
						Bitmap favicon) {_x000D_
					if (url.equals("https://portaal.khleuven.be/")) {_x000D_
						view.setVisibility(View.INVISIBLE);_x000D_
						ProgressDialog.show(_x000D_
								KHLLoginActivity.this, "",_x000D_
								"Fetching RSS. Please wait...", true);_x000D_
					}_x000D_
				}_x000D_
			});_x000D_
_x000D_
			myWebView.getSettings().setDatabasePath("khl.news");_x000D_
			myWebView.getSettings().setDomStorageEnabled(true);_x000D_
_x000D_
			myWebView.addJavascriptInterface(new MyJavaScriptInterface(),_x000D_
					"interfaceName");_x000D_
_x000D_
			myWebView_x000D_
					.loadUrl("https://portaal.khleuven.be/Shibboleth.sso/WAYF/khleuven?target=https%3A%2F%2Fportaal.khleuven.be%2F");_x000D_
_x000D_
		}_x000D_
_x000D_
	}_x000D_
_x000D_
	private void openRSS(String link) {_x000D_
		Intent intent = new Intent(getApplicationContext(),_x000D_
				KHLNieuwsActivity.class);_x000D_
		intent.putExtra("feedUrl", link);_x000D_
		startActivity(intent);_x000D_
	}_x000D_
_x000D_
}