Permalink
Browse files

add registering page and necessary routines

  • Loading branch information...
1 parent 13ec619 commit 9a364d957b76f92c2adc524fa8d51e5008d85c0c @nobnago committed Jun 3, 2012
View
79 res/layout/my_account_register.xml
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:paddingLeft="10dp"
+ android:paddingTop="15dp"
+ android:paddingRight="10dp"
+ android:paddingBottom="15dp"
+ android:orientation="vertical">
+
+ <LinearLayout android:id="@+id/MyAccountLayout"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content">
+
+ <TextView
+ android:textColor="#000000"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/username"/>
+ <EditText android:id="@+id/username1"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:inputType="textPersonName"/>
+ <TextView
+ android:textColor="#000000"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/username_repeat"/>
+ <EditText android:id="@+id/username2"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:inputType="textPersonName"/>
+
+ <TextView
+ android:layout_width="fill_parent"
+ android:textColor="#000000"
+ android:layout_height="wrap_content"
+ android:text="@string/password"/>
+ <EditText android:id="@+id/password1"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:inputType="textPassword"/>
+ <TextView
+ android:layout_width="fill_parent"
+ android:textColor="#000000"
+ android:layout_height="wrap_content"
+ android:text="@string/password_repeat"/>
+ <EditText android:id="@+id/password2"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:inputType="textPassword"/>
+
+ </LinearLayout>
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+
+ <Button android:id="@+id/register_button"
+ android:layout_height="fill_parent"
+ android:layout_width="0dip"
+ android:layout_gravity="center"
+ android:gravity="center"
+ android:layout_weight="1"
+ android:singleLine="true"
+ android:text="@string/sign_up"/>
+
+ <Button android:id="@+id/cancel_button"
+ android:layout_height="fill_parent"
+ android:layout_width="0dip"
+ android:text="@string/cancel"
+ android:layout_gravity="center"
+ android:singleLine="true"
+ android:layout_weight="1"
+ android:gravity="center"/>
+ </LinearLayout>
+
+</LinearLayout>
View
7 res/values/04-network.xml
@@ -73,7 +73,9 @@
<!-- MyAccount.java -->
<string name="username">Username</string>
+<string name="username_repeat">Repeat username</string>
<string name="password">Password</string>
+<string name="password_repeat">Repeat password</string>
<string name="log_in">Log in</string>
<string name="sign_up_description">Don\'t have an AnkiWeb account? It\'s free!</string>
<string name="sign_up">Sign up</string>
@@ -147,4 +149,9 @@
<string name="sync_media_no_changes">No changes to media files.</string>
<string name="sync_media_success">Media synced successfully.</string>
+<string name="register_error">An error occurred. Please try again later.</string>
+<string name="register_title">Register account</string>
+<string name="register_mismatch">The username/password repetition does not match or is empty. Please re-enter.</string>
+<string name="registering_message">Creating account.\nPlease wait...</string>
+
</resources>
View
7 src/com/ichi2/anki/Info.java
@@ -46,7 +46,6 @@
public static final int TYPE_WELCOME = 1;
public static final int TYPE_NEW_VERSION = 2;
public static final int TYPE_SHARED_DECKS = 3;
- public static final int TYPE_CREATE_ACCOUNT = 4;
private int mType;
private WebView mWebView;
@@ -160,12 +159,6 @@ public void onClick(View arg0) {
builder.show();
break;
- case TYPE_CREATE_ACCOUNT:
- mWebView.loadUrl(res.getString(R.string.ankionline_sign_up_url));
- mWebView.setWebViewClient(new CustomWebViewClient());
- continueButton.setText(res.getString(R.string.download_button_return));
- break;
-
default:
finish();
break;
View
142 src/com/ichi2/anki/MyAccount.java
@@ -44,16 +44,24 @@
private View mLoginToMyAccountView;
private View mLoggedIntoMyAccountView;
+ private View mRegisterView;
private EditText mUsername;
private EditText mPassword;
+ private EditText mUsername1;
+ private EditText mPassword1;
+ private EditText mUsername2;
+ private EditText mPassword2;
+
private TextView mUsernameLoggedIn;
private StyledProgressDialog mProgressDialog;
private StyledDialog mNoConnectionAlert;
private StyledDialog mConnectionErrorAlert;
private StyledDialog mInvalidUserPassAlert;
+ private StyledDialog mRegisterAlert;
+ private StyledDialog mErrorAlert;
@Override
@@ -124,6 +132,22 @@ private void login() {
}
+ private void register() {
+ // Hide soft keyboard
+ InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+ inputMethodManager.hideSoftInputFromWindow(mUsername.getWindowToken(), 0);
+
+ String username = mUsername1.getText().toString();
+ String password = mPassword1.getText().toString();
+
+ if (!"".equalsIgnoreCase(username) && !"".equalsIgnoreCase(password)) {
+ Connection.register(registerListener, new Connection.Payload(new Object[] { username, password }));
+ } else {
+ mInvalidUserPassAlert.show();
+ }
+ }
+
+
private void logout() {
SharedPreferences preferences = PrefSettings.getSharedPrefs(getBaseContext());
Editor editor = preferences.edit();
@@ -142,7 +166,7 @@ private void initAllContentViews() {
Themes.setTextViewStyle(mLoginToMyAccountView.findViewById(R.id.no_account_text));
mUsername = (EditText) mLoginToMyAccountView.findViewById(R.id.username);
mPassword = (EditText) mLoginToMyAccountView.findViewById(R.id.password);
-
+
Button loginButton = (Button) mLoginToMyAccountView.findViewById(R.id.login_button);
loginButton.setOnClickListener(new OnClickListener() {
@@ -158,12 +182,7 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
- Intent intent = new Intent(MyAccount.this, Info.class);
- intent.putExtra(Info.TYPE_EXTRA, Info.TYPE_CREATE_ACCOUNT);
- startActivity(intent);
- if (UIUtils.getApiLevel() > 4) {
- ActivityTransitionAnimation.slide(MyAccount.this, ActivityTransitionAnimation.RIGHT);
- }
+ setContentView(mRegisterView);
}
});
@@ -181,6 +200,38 @@ public void onClick(View v) {
}
});
+
+ mRegisterView = getLayoutInflater().inflate(R.layout.my_account_register, null);
+ Themes.setWallpaper(mRegisterView);
+ mUsername1 = (EditText) mRegisterView.findViewById(R.id.username1);
+ mPassword1 = (EditText) mRegisterView.findViewById(R.id.password1);
+ mUsername2 = (EditText) mRegisterView.findViewById(R.id.username2);
+ mPassword2 = (EditText) mRegisterView.findViewById(R.id.password2);
+
+ Button registerButton = (Button) mRegisterView.findViewById(R.id.register_button);
+ registerButton.setOnClickListener(new OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ if (mUsername1.getText().toString().length() > 0 && mPassword1.getText().toString().length() > 0 && mUsername1.getText().toString().equals(mUsername2.getText().toString()) && mPassword1.getText().toString().equals(mPassword2.getText().toString())) {
+ register();
+ } else {
+ mRegisterAlert.show();
+ }
+ }
+
+ });
+
+ Button cancelButton = (Button) mRegisterView.findViewById(R.id.cancel_button);
+ cancelButton.setOnClickListener(new OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ finishWithAnimation(ActivityTransitionAnimation.FADE);
+ }
+
+ });
+
}
@@ -198,6 +249,27 @@ private void initAllAlertDialogs() {
builder.setPositiveButton(res.getString(R.string.ok), null);
mNoConnectionAlert = builder.create();
+ builder.setTitle(res.getString(R.string.register_title));
+ builder.setIcon(android.R.drawable.ic_dialog_alert);
+ builder.setMessage(res.getString(R.string.register_error));
+ builder.setPositiveButton(res.getString(R.string.ok), null);
+ mErrorAlert = builder.create();
+
+ builder.setTitle(res.getString(R.string.register_title));
+ builder.setIcon(android.R.drawable.ic_dialog_alert);
+ builder.setMessage(res.getString(R.string.register_mismatch));
+ builder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {
+
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ mUsername1.setText("");
+ mUsername2.setText("");
+ mPassword1.setText("");
+ mPassword2.setText("");
+ }
+ });
+ mRegisterAlert = builder.create();
+
builder = new StyledDialog.Builder(this);
builder.setTitle(res.getString(R.string.log_in));
builder.setIcon(android.R.drawable.ic_dialog_alert);
@@ -283,6 +355,62 @@ public void onDisconnected() {
}
};
+
+ Connection.TaskListener registerListener = new Connection.TaskListener() {
+
+ @Override
+ public void onProgressUpdate(Object... values) {
+ // Pass
+ }
+
+
+ @Override
+ public void onPreExecute() {
+ Log.i(AnkiDroidApp.TAG, "MyAccount - onPreExcecute");
+ if (mProgressDialog == null || !mProgressDialog.isShowing()) {
+ mProgressDialog = StyledProgressDialog.show(MyAccount.this, "",
+ getResources().getString(R.string.registering_message), true);
+ }
+ }
+
+
+ @Override
+ public void onPostExecute(Payload data) {
+ Log.i(AnkiDroidApp.TAG, "MyAccount - onPostExecute, succes = " + data.success);
+ if (mProgressDialog != null) {
+ mProgressDialog.dismiss();
+ }
+
+ if (data.success) {
+ Log.i(AnkiDroidApp.TAG, "User successfully registered!");
+ saveUserInformation((String) data.data[0], (String) data.data[1]);
+
+ Intent i = MyAccount.this.getIntent();
+ if (i.hasExtra("notLoggedIn") && i.getExtras().getBoolean("notLoggedIn", false)) {
+ MyAccount.this.setResult(RESULT_OK, i);
+ finishWithAnimation(ActivityTransitionAnimation.FADE);
+ } else {
+ // Show logged view
+ mUsernameLoggedIn.setText((String) data.data[0]);
+ setContentView(mLoggedIntoMyAccountView);
+ }
+ } else {
+ mErrorAlert.show();
+ if (data.data != null) {
+ mErrorAlert.setMessage(((String[])data.data)[0]);
+ }
+ }
+ }
+
+
+ @Override
+ public void onDisconnected() {
+ if (mNoConnectionAlert != null) {
+ mNoConnectionAlert.show();
+ }
+ }
+ };
+
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
View
53 src/com/ichi2/async/Connection.java
@@ -65,6 +65,7 @@
public static final int TASK_TYPE_GET_PERSONAL_DECKS = 3;
public static final int TASK_TYPE_SEND_CRASH_REPORT = 4;
public static final int TASK_TYPE_DOWNLOAD_MEDIA = 5;
+ public static final int TASK_TYPE_REGISTER = 6;
private static Context sContext;
@@ -139,6 +140,12 @@ public static Connection login(TaskListener listener, Payload data) {
}
+ public static Connection register(TaskListener listener, Payload data) {
+ data.taskType = TASK_TYPE_REGISTER;
+ return launchConnectionTask(listener, data);
+ }
+
+
public static Connection getSharedDecks(TaskListener listener, Payload data) {
data.taskType = TASK_TYPE_GET_SHARED_DECKS;
return launchConnectionTask(listener, data);
@@ -177,8 +184,11 @@ protected Payload doInBackground(Payload... params) {
private Payload doOneInBackground(Payload data) {
switch (data.taskType) {
- case TASK_TYPE_LOGIN:
- return doInBackgroundLogin(data);
+ case TASK_TYPE_LOGIN:
+ return doInBackgroundLogin(data);
+
+ case TASK_TYPE_REGISTER:
+ return doInBackgroundRegister(data);
// case TASK_TYPE_GET_SHARED_DECKS:
// return doInBackgroundGetSharedDecks(data);
@@ -214,7 +224,8 @@ private Payload doInBackgroundLogin(Payload data) {
data.returnType = ret.getStatusLine().getStatusCode();
if (data.returnType == 200) {
try {
- hostkey = (new JSONObject(server.stream2String(ret.getEntity().getContent()))).getString("key");
+ JSONObject jo = (new JSONObject(server.stream2String(ret.getEntity().getContent())));
+ hostkey = jo.getString("key");
valid = (hostkey != null) && (hostkey.length() > 0);
} catch (JSONException e) {
valid = false;
@@ -233,6 +244,42 @@ private Payload doInBackgroundLogin(Payload data) {
return data;
}
+ private Payload doInBackgroundRegister(Payload data) {
+ String username = (String) data.data[0];
+ String password = (String) data.data[1];
+ BasicHttpSyncer server = new RemoteServer(this, null);
+ HttpResponse ret = server.register(username, password);
+ String hostkey = null;
+ boolean valid = false;
+ data.returnType = ret.getStatusLine().getStatusCode();
+ String status = null;
+ if (data.returnType == 200) {
+ try {
+ JSONObject jo = (new JSONObject(server.stream2String(ret.getEntity().getContent())));
+ status = jo.getString("status");
+ if (status.equals("ok")) {
+ hostkey = jo.getString("hkey");
+ valid = (hostkey != null) && (hostkey.length() > 0);
+ }
+ } catch (JSONException e) {
+ } catch (IllegalStateException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ if (valid) {
+ data.success = true;
+ data.data = new String[] {username, hostkey};
+ } else {
+ data.success = false;
+ if (status != null) {
+ data.data = new String[] {status};
+ }
+ }
+ return data;
+ }
+
private Payload doInBackgroundSync(Payload data) {
// for for doInBackgroundLoadDeckCounts if any
DeckTask.waitToFinish();
View
2 src/com/ichi2/libanki/Collection.java
@@ -50,7 +50,7 @@
// collection schema & syncing vars
public static final int SCHEMA_VERSION = 11;
- public static final String SYNC_URL = "https://ankiweb.net/sync/";
+ public static final String SYNC_URL = "https://ankiweb.net/";
public static final int SYNC_VER = 5;
private AnkiDb mDb;
View
19 src/com/ichi2/libanki/sync/BasicHttpSyncer.java
@@ -41,6 +41,7 @@
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray;
+import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
@@ -91,6 +92,9 @@ public HttpResponse req(String method, InputStream fobj, boolean hkey) {
return req(method, fobj, 6, hkey);
}
public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey) {
+ return req(method, fobj, comp, hkey, null);
+ }
+ public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData) {
try {
String bdry = "--" + BOUNDARY;
StringWriter buf = new StringWriter();
@@ -133,7 +137,13 @@ public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey)
}
bos.close();
// connection headers
- HttpPost httpPost = new HttpPost(Collection.SYNC_URL + method);
+ String url = Collection.SYNC_URL;
+ if (method.equals("register")) {
+ url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password=" + registerData.getString("p");
+ } else {
+ url = url + "sync/" + method;
+ }
+ HttpPost httpPost = new HttpPost(url);
HttpEntity entity = new ProgressByteEntity(bos.toByteArray());
// body
@@ -157,6 +167,8 @@ public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey)
throw new RuntimeException(e);
} catch (IOException e) {
return null;
+ } catch (JSONException e) {
+ throw new RuntimeException(e);
}
}
@@ -309,4 +321,9 @@ public static ByteArrayInputStream getInputStream(String string) {
return null;
}
}
+
+
+ public HttpResponse register(String user, String pw) {
+ return null;
+ }
}
View
17 src/com/ichi2/libanki/sync/RemoteServer.java
@@ -16,8 +16,9 @@
package com.ichi2.libanki.sync;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
@@ -47,6 +48,20 @@ public HttpResponse hostKey(String user, String pw) {
}
@Override
+ public HttpResponse register(String user, String pw) {
+ try {
+ JSONObject jo = new JSONObject();
+ jo.put("u", URLEncoder.encode(user, "UTF-8" ));
+ jo.put("p", URLEncoder.encode(pw, "UTF-8" ));
+ return super.req("register", null, 6, false, jo);
+ } catch (JSONException e) {
+ return null;
+ } catch (UnsupportedEncodingException e) {
+ return null;
+ }
+ }
+
+ @Override
public HttpResponse meta() {
try {
JSONObject jo = new JSONObject();

0 comments on commit 9a364d9

Please sign in to comment.