|
| 1 | +package fr.free.nrw.commons.auth; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.net.Uri; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.app.Activity; |
| 7 | +import android.util.Log; |
| 8 | +import android.webkit.CookieManager; |
| 9 | +import android.webkit.CookieSyncManager; |
| 10 | +import android.webkit.WebResourceResponse; |
| 11 | +import android.webkit.WebSettings; |
| 12 | +import android.webkit.WebView; |
| 13 | +import android.webkit.WebViewClient; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import fr.free.nrw.commons.R; |
| 17 | + |
| 18 | +public class SignupActivity extends Activity { |
| 19 | + |
| 20 | + private boolean otherPage; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onCreate(Bundle savedInstanceState) { |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + Log.d("SignupActivity", "Signup Activity started"); |
| 26 | + otherPage = false; |
| 27 | + |
| 28 | + |
| 29 | + WebView webView = new WebView(this); |
| 30 | + setContentView(webView); |
| 31 | + |
| 32 | + webView.setWebViewClient(new MyWebViewClient()); |
| 33 | + WebSettings webSettings = webView.getSettings(); |
| 34 | + //Needed to refresh Captcha. Might introduce XSS vulnerabilities, but we can trust Wikimedia's site... right? |
| 35 | + webSettings.setJavaScriptEnabled(true); |
| 36 | + |
| 37 | + webView.loadUrl("https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes"); |
| 38 | + } |
| 39 | + |
| 40 | + private class MyWebViewClient extends WebViewClient { |
| 41 | + @Override |
| 42 | + public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 43 | + if (url.equals("https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes")) { |
| 44 | + //Signup success, so clear cookies, notify user, and load LoginActivity again |
| 45 | + Log.d("SignupActivity", "Overriding URL" + url); |
| 46 | + |
| 47 | + CookieSyncManager.createInstance(getApplicationContext()); |
| 48 | + CookieManager cookieManager = CookieManager.getInstance(); |
| 49 | + cookieManager.removeAllCookie(); |
| 50 | + cookieManager.setAcceptCookie(false); |
| 51 | + cookieManager.removeSessionCookie(); |
| 52 | + |
| 53 | + Toast toast = Toast.makeText(getApplicationContext(), "Account created!", Toast.LENGTH_LONG); |
| 54 | + toast.show(); |
| 55 | + |
| 56 | + Intent intent = new Intent(getApplicationContext(), LoginActivity.class); |
| 57 | + intent.putExtra("Redirected", true); |
| 58 | + startActivity(intent); |
| 59 | + return true; |
| 60 | + } else { |
| 61 | + //If user clicks any other links in the webview |
| 62 | + Log.d("SignupActivity", "Not overriding URL, URL is: " + url); |
| 63 | + otherPage = true; |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onBackPressed() { |
| 71 | + if (otherPage == true) { |
| 72 | + //If we are in any page except the main signup page, back button should take us back to signup page |
| 73 | + Intent intent = new Intent(this, SignupActivity.class); |
| 74 | + startActivity(intent); |
| 75 | + } |
| 76 | + else { |
| 77 | + //If we are in signup page, back button should take us back to LoginActivity |
| 78 | + Intent intent = new Intent(getApplicationContext(), LoginActivity.class); |
| 79 | + intent.putExtra("Redirected", true); |
| 80 | + startActivity(intent); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments