RetrievePasswordActivity.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package com.usai.apex;
  2. import com.usai.util.Network;
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.annotation.TargetApi;
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.AlertDialog.Builder;
  9. import android.content.DialogInterface;
  10. import android.content.DialogInterface.OnClickListener;
  11. import android.content.Context;
  12. import android.os.AsyncTask;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.text.TextUtils;
  16. import android.util.Log;
  17. import android.view.Gravity;
  18. import android.view.KeyEvent;
  19. import android.view.View;
  20. import android.view.inputmethod.EditorInfo;
  21. import android.view.inputmethod.InputMethodManager;
  22. import android.widget.EditText;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25. /**
  26. * Activity which displays a login screen to the user, offering registration as
  27. * well.
  28. */
  29. public class RetrievePasswordActivity extends Activity
  30. {
  31. /**
  32. * Keep track of the login task to ensure we can cancel it if requested.
  33. */
  34. private UserLoginTask mAuthTask = null;
  35. // Values for email and password at the time of the login attempt.
  36. private String m_sEmail;
  37. private String m_sUser;
  38. // UI references.
  39. private EditText mEmailView;
  40. private EditText m_userView;
  41. private View mLoginFormView;
  42. private View mLoginStatusView;
  43. private TextView mLoginStatusMessageView;
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState)
  46. {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_retrieve_password);
  49. // Set up the login form.
  50. m_userView = (EditText) findViewById(R.id.user);
  51. // m_sEmail = getIntent().getStringExtra(EXTRA_EMAIL);
  52. mEmailView = (EditText) findViewById(R.id.email);
  53. // mEmailView.setText(m_sEmail);
  54. mEmailView
  55. .setOnEditorActionListener(new TextView.OnEditorActionListener()
  56. {
  57. @Override
  58. public boolean onEditorAction(TextView textView, int id,
  59. KeyEvent keyEvent)
  60. {
  61. if (id == R.id.btn_ok
  62. || id == EditorInfo.IME_ACTION_DONE)
  63. {
  64. InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext()
  65. .getSystemService(
  66. Context.INPUT_METHOD_SERVICE);
  67. // EditText editText =
  68. // (EditText)findViewById(R.id.xxxx);
  69. inputMethodManager.hideSoftInputFromWindow(
  70. mEmailView.getWindowToken(), 0); // ����
  71. retrivev();
  72. return true;
  73. }
  74. return false;
  75. }
  76. });
  77. mLoginFormView = findViewById(R.id.login_form);
  78. mLoginStatusView = findViewById(R.id.login_status);
  79. mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);
  80. findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener()
  81. {
  82. @Override
  83. public void onClick(View view)
  84. {
  85. InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext()
  86. .getSystemService(Context.INPUT_METHOD_SERVICE);
  87. // EditText editText =
  88. // (EditText)findViewById(R.id.xxxx);
  89. inputMethodManager.hideSoftInputFromWindow(
  90. mEmailView.getWindowToken(), 0); // ����
  91. retrivev();
  92. }
  93. });
  94. findViewById(R.id.btn_close).setOnClickListener(
  95. new View.OnClickListener()
  96. {
  97. @Override
  98. public void onClick(View view)
  99. {
  100. finish();
  101. }
  102. });
  103. }
  104. //
  105. // @Override
  106. // public boolean onCreateOptionsMenu(Menu menu) {
  107. // super.onCreateOptionsMenu(menu);
  108. // getMenuInflater().inflate(R.menu.retrieve_password, menu);
  109. // return true;
  110. // }
  111. /**
  112. * Attempts to sign in or register the account specified by the login form.
  113. * If there are form errors (invalid email, missing fields, etc.), the
  114. * errors are presented and no actual login attempt is made.
  115. */
  116. public void retrivev()
  117. {
  118. if (mAuthTask != null)
  119. {
  120. return;
  121. }
  122. // Reset errors.
  123. mEmailView.setError(null);
  124. m_userView.setError(null);
  125. // Store values at the time of the login attempt.
  126. m_sEmail = mEmailView.getText().toString();
  127. m_sUser = m_userView.getText().toString();
  128. boolean cancel = false;
  129. View focusView = null;
  130. // Check for a valid password.
  131. if (TextUtils.isEmpty(m_sUser))
  132. {
  133. m_userView.setError(getString(R.string.error_field_required));
  134. focusView = m_userView;
  135. cancel = true;
  136. }
  137. // else if (m_sUser.length() < 4) {
  138. // m_userView.setError(getString(R.string.error_invalid_password));
  139. // focusView = m_userView;
  140. // cancel = true;
  141. // }
  142. // Check for a valid email address.
  143. if (TextUtils.isEmpty(m_sEmail))
  144. {
  145. mEmailView.setError(getString(R.string.error_field_required));
  146. focusView = mEmailView;
  147. cancel = true;
  148. }
  149. else if (!m_sEmail.contains("@"))
  150. {
  151. mEmailView.setError(getString(R.string.error_invalid_email));
  152. focusView = mEmailView;
  153. cancel = true;
  154. }
  155. if (cancel)
  156. {
  157. // There was an error; don't attempt login and focus the first
  158. // form field with an error.
  159. focusView.requestFocus();
  160. }
  161. else
  162. {
  163. // Show a progress spinner, and kick off a background task to
  164. // perform the user login attempt.
  165. mLoginStatusMessageView.setText(R.string.login_progress_signing_in);
  166. showProgress(true);
  167. mAuthTask = new UserLoginTask();
  168. mAuthTask.execute((Void) null);
  169. }
  170. }
  171. /**
  172. * Shows the progress UI and hides the login form.
  173. */
  174. @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  175. private void showProgress(final boolean show)
  176. {
  177. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  178. // for very easy animations. If available, use these APIs to fade-in
  179. // the progress spinner.
  180. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
  181. {
  182. int shortAnimTime = getResources().getInteger(
  183. android.R.integer.config_shortAnimTime);
  184. mLoginStatusView.setVisibility(View.VISIBLE);
  185. mLoginStatusView.animate().setDuration(shortAnimTime)
  186. .alpha(show ? 1 : 0)
  187. .setListener(new AnimatorListenerAdapter()
  188. {
  189. @Override
  190. public void onAnimationEnd(Animator animation)
  191. {
  192. mLoginStatusView.setVisibility(show ? View.VISIBLE
  193. : View.GONE);
  194. }
  195. });
  196. mLoginFormView.setVisibility(View.VISIBLE);
  197. mLoginFormView.animate().setDuration(shortAnimTime)
  198. .alpha(show ? 0 : 1)
  199. .setListener(new AnimatorListenerAdapter()
  200. {
  201. @Override
  202. public void onAnimationEnd(Animator animation)
  203. {
  204. mLoginFormView.setVisibility(show ? View.GONE
  205. : View.VISIBLE);
  206. }
  207. });
  208. }
  209. else
  210. {
  211. // The ViewPropertyAnimator APIs are not available, so simply show
  212. // and hide the relevant UI components.
  213. mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
  214. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  215. }
  216. }
  217. /**
  218. * Represents an asynchronous login/registration task used to authenticate
  219. * the user.
  220. */
  221. public class UserLoginTask extends AsyncTask<Void, Void, Boolean>
  222. {
  223. int errorcode;
  224. @Override
  225. protected Boolean doInBackground(Void... params)
  226. {
  227. errorcode = Network.retrieve_pass(m_sUser, m_sEmail);
  228. if (errorcode == Network.RESULT_TRUE)
  229. return true;
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235. @Override
  236. protected void onPostExecute(final Boolean success)
  237. {
  238. Log.i("onPostExecute", "entry");
  239. mAuthTask = null;
  240. showProgress(false);
  241. // if (netconnect == )
  242. // {
  243. //
  244. // }
  245. if (success)
  246. {
  247. AlertDialog.Builder builder = new Builder(
  248. RetrievePasswordActivity.this);
  249. builder.setMessage("Email has been sent.");
  250. builder.setTitle("Retrieve successfully!");
  251. builder.setPositiveButton("Ok", new OnClickListener()
  252. {
  253. @Override
  254. public void onClick(DialogInterface dialog, int which)
  255. {
  256. dialog.dismiss();
  257. RetrievePasswordActivity.this.finish();
  258. }
  259. });
  260. // builder.setNegativeButton("取消", new OnClickListener() {
  261. //
  262. // @Override
  263. // public void onClick(DialogInterface dialog, int which) {
  264. // dialog.dismiss();
  265. // }
  266. // });
  267. builder.create().show();
  268. }
  269. else
  270. {
  271. switch (errorcode)
  272. {
  273. case Network.RESULT_NET_NOTAVAILABLE:
  274. {
  275. Toast toast = Toast.makeText(getApplicationContext(),
  276. getText(R.string.msg_connection_none),
  277. Toast.LENGTH_LONG);
  278. toast.setGravity(Gravity.CENTER, 0, 0);
  279. toast.show();
  280. return;
  281. }
  282. case Network.RESULT_NET_ERROR:
  283. {
  284. Toast toast = Toast.makeText(getApplicationContext(),
  285. getText(R.string.msg_net_error),
  286. Toast.LENGTH_LONG);
  287. toast.setGravity(Gravity.CENTER, 0, 0);
  288. toast.show();
  289. return;
  290. }
  291. case Network.RESULT_ERROR:
  292. // case Network.RESULT_RESPONSE_NULL:
  293. {
  294. Toast toast = Toast.makeText(getApplicationContext(),
  295. getText(R.string.msg_net_resulterror),
  296. Toast.LENGTH_LONG);
  297. toast.setGravity(Gravity.CENTER, 0, 0);
  298. toast.show();
  299. return;
  300. }
  301. case Network.RESULT_FALSE:
  302. {
  303. AlertDialog.Builder builder = new Builder(
  304. RetrievePasswordActivity.this);
  305. builder.setMessage("Invalid user name or email.");
  306. builder.setTitle("Retrieve failed");
  307. builder.setPositiveButton("Ok", new OnClickListener()
  308. {
  309. @Override
  310. public void onClick(DialogInterface dialog,
  311. int which)
  312. {
  313. dialog.dismiss();
  314. // RetrievePasswordActivity.this.finish();
  315. }
  316. });
  317. builder.create().show();
  318. return;
  319. }
  320. default:
  321. {
  322. Toast toast = Toast.makeText(getApplicationContext(),
  323. getText(R.string.msg_net_resulterror) + ":"
  324. + errorcode, Toast.LENGTH_LONG);
  325. toast.setGravity(Gravity.CENTER, 0, 0);
  326. toast.show();
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. @Override
  333. protected void onCancelled()
  334. {
  335. mAuthTask = null;
  336. showProgress(false);
  337. }
  338. }
  339. }