Skip to content

Commit 8b0bfbc

Browse files
Merge pull request commons-app#225 from misaochan/webview-activity
Account creation from within app (web view)
2 parents 22b6ce5 + 4726e8b commit 8b0bfbc

File tree

8 files changed

+124
-11
lines changed

8 files changed

+124
-11
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@
7676
android:name=".SettingsActivity"
7777
android:label="@string/title_activity_settings"
7878
/>
79-
<activity android:name=".AboutActivity" android:label="@string/title_activity_about"/>
79+
<activity
80+
android:name=".AboutActivity"
81+
android:label="@string/title_activity_about"/>
82+
<activity
83+
android:name=".auth.SignupActivity"
84+
android:label="@string/title_activity_signup"/>
8085

8186
<service android:name=".upload.UploadService" >
8287
</service>

app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,22 @@ public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent
181181
}
182182
});
183183

184-
signupButton.setOnClickListener(new View.OnClickListener() {
185-
public void onClick(View v) {
186-
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://commons.wikimedia.org/wiki/Special:UserLogin/signup")));
187-
}
188-
});
189-
190184
loginButton.setOnClickListener(new View.OnClickListener() {
191185
public void onClick(View v) {
192186
that.performLogin();
193187
}
194188
});
195189

196190
if (savedInstanceState == null) {
197-
Intent welcomeIntent = new Intent(this, WelcomeActivity.class);
198-
startActivity(welcomeIntent);
191+
Bundle extras = getIntent().getExtras();
192+
// Only load welcome screen if we weren't redirected from SignupActivity
193+
if (extras == null || !extras.getBoolean("Redirected")) {
194+
if (extras != null) {
195+
Log.d("SignupActivity", "Redirected? " + Boolean.toString(extras.getBoolean("Redirected")));
196+
}
197+
Intent welcomeIntent = new Intent(this, WelcomeActivity.class);
198+
startActivity(welcomeIntent);
199+
}
199200
}
200201
}
201202

@@ -227,4 +228,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
227228
return super.onOptionsItemSelected(item);
228229
}
229230

231+
//Called when Sign Up button is clicked
232+
public void signUp(View view) {
233+
Intent intent = new Intent(this, SignupActivity.class);
234+
startActivity(intent);
235+
}
230236
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

app/src/main/res/layout/activity_login.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
android:layout_width="fill_parent"
6969
android:layout_height="wrap_content"
7070
android:text="@string/signup"
71-
android:layout_gravity="center_horizontal"/>
71+
android:layout_gravity="center_horizontal"
72+
android:onClick="signUp" />
7273
</LinearLayout>
7374

7475

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/webview"
4+
android:layout_width="fill_parent"
5+
android:layout_height="fill_parent"
6+
/>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>

app/src/main/res/values/dimens.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<string name="upload_failed">File not found, please try another file</string>
1414
<string name="authentication_failed">Authentication failed!</string>
1515
<string name="uploading_started">Upload started!</string>
16-
16+
1717
<string name="upload_completed_notification_title">%1$s uploaded!</string>
1818
<string name="upload_completed_notification_text">Tap to view your upload</string>
1919
<string name="upload_progress_notification_title_start">Starting %1$s upload</string>
@@ -154,4 +154,5 @@
154154
<string name="location_permission_rationale">Optional permission: Get current location for category suggestions</string>
155155
<string name="ok">OK</string>
156156
<string name="back">Back</string>
157+
<string name="title_activity_signup">SignupActivity</string>
157158
</resources>

0 commit comments

Comments
 (0)