Merge pull request #225 from misaochan/webview-activity

Account creation from within app (web view)
This commit is contained in:
Nicolas Raoul 2016-08-12 12:29:01 +09:00 committed by GitHub
commit 8b0bfbc11e
8 changed files with 124 additions and 11 deletions

View file

@ -76,7 +76,12 @@
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
/>
<activity android:name=".AboutActivity" android:label="@string/title_activity_about"/>
<activity
android:name=".AboutActivity"
android:label="@string/title_activity_about"/>
<activity
android:name=".auth.SignupActivity"
android:label="@string/title_activity_signup"/>
<service android:name=".upload.UploadService" >
</service>

View file

@ -181,12 +181,6 @@ public class LoginActivity extends AccountAuthenticatorActivity {
}
});
signupButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://commons.wikimedia.org/wiki/Special:UserLogin/signup")));
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
that.performLogin();
@ -194,8 +188,15 @@ public class LoginActivity extends AccountAuthenticatorActivity {
});
if (savedInstanceState == null) {
Intent welcomeIntent = new Intent(this, WelcomeActivity.class);
startActivity(welcomeIntent);
Bundle extras = getIntent().getExtras();
// Only load welcome screen if we weren't redirected from SignupActivity
if (extras == null || !extras.getBoolean("Redirected")) {
if (extras != null) {
Log.d("SignupActivity", "Redirected? " + Boolean.toString(extras.getBoolean("Redirected")));
}
Intent welcomeIntent = new Intent(this, WelcomeActivity.class);
startActivity(welcomeIntent);
}
}
}
@ -227,4 +228,9 @@ public class LoginActivity extends AccountAuthenticatorActivity {
return super.onOptionsItemSelected(item);
}
//Called when Sign Up button is clicked
public void signUp(View view) {
Intent intent = new Intent(this, SignupActivity.class);
startActivity(intent);
}
}

View file

@ -0,0 +1,83 @@
package fr.free.nrw.commons.auth;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import fr.free.nrw.commons.R;
public class SignupActivity extends Activity {
private boolean otherPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SignupActivity", "Signup Activity started");
otherPage = false;
WebView webView = new WebView(this);
setContentView(webView);
webView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webView.getSettings();
//Needed to refresh Captcha. Might introduce XSS vulnerabilities, but we can trust Wikimedia's site... right?
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes")) {
//Signup success, so clear cookies, notify user, and load LoginActivity again
Log.d("SignupActivity", "Overriding URL" + url);
CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(false);
cookieManager.removeSessionCookie();
Toast toast = Toast.makeText(getApplicationContext(), "Account created!", Toast.LENGTH_LONG);
toast.show();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.putExtra("Redirected", true);
startActivity(intent);
return true;
} else {
//If user clicks any other links in the webview
Log.d("SignupActivity", "Not overriding URL, URL is: " + url);
otherPage = true;
return false;
}
}
}
@Override
public void onBackPressed() {
if (otherPage == true) {
//If we are in any page except the main signup page, back button should take us back to signup page
Intent intent = new Intent(this, SignupActivity.class);
startActivity(intent);
}
else {
//If we are in signup page, back button should take us back to LoginActivity
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.putExtra("Redirected", true);
startActivity(intent);
}
}
}

View file

@ -68,7 +68,8 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/signup"
android:layout_gravity="center_horizontal"/>
android:layout_gravity="center_horizontal"
android:onClick="signUp" />
</LinearLayout>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

View file

@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View file

@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

View file

@ -13,7 +13,7 @@
<string name="upload_failed">File not found, please try another file</string>
<string name="authentication_failed">Authentication failed!</string>
<string name="uploading_started">Upload started!</string>
<string name="upload_completed_notification_title">%1$s uploaded!</string>
<string name="upload_completed_notification_text">Tap to view your upload</string>
<string name="upload_progress_notification_title_start">Starting %1$s upload</string>
@ -154,4 +154,5 @@
<string name="location_permission_rationale">Optional permission: Get current location for category suggestions</string>
<string name="ok">OK</string>
<string name="back">Back</string>
<string name="title_activity_signup">SignupActivity</string>
</resources>