LoginFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package com.usai.apex;
  2. //import android.app.Fragment;
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import com.usai.util.Network;
  7. import android.R.integer;
  8. import android.animation.Animator;
  9. import android.animation.AnimatorListenerAdapter;
  10. import android.app.Fragment;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.os.AsyncTask;
  14. import android.os.Build;
  15. //import android.content.SharedPreferences;
  16. import android.os.Bundle;
  17. import android.text.TextUtils;
  18. import android.util.Log;
  19. import android.view.Gravity;
  20. import android.view.KeyEvent;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.view.ViewGroup;
  24. import android.view.inputmethod.EditorInfo;
  25. import android.view.inputmethod.InputMethodManager;
  26. import android.widget.EditText;
  27. import android.widget.TextView;
  28. //import android.widget.TextView;
  29. import android.widget.Toast;
  30. /**
  31. * Activity which displays a login screen to the user, offering registration as
  32. * well.
  33. */
  34. public class LoginFragment extends Fragment/* implements OnClickListener */
  35. {
  36. private String m_sUser;
  37. private String m_sPassword;
  38. private EditText m_etName;
  39. private EditText m_etPassword;
  40. private UserLoginTask mAuthTask = null;
  41. private TextView mLoginStatusMessageView;
  42. private View mLoginFormView;
  43. private View mLoginStatusView;
  44. // SQLiteDatabase m_db;
  45. @Override
  46. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  47. Bundle savedInstanceState)
  48. {
  49. View view = inflater.inflate(R.layout.fragment_login, null);
  50. // Button btn = (Button) view.findViewById(R.id.sign_in_button);
  51. // btn.setOnClickListener(this);
  52. // Set up the login form.
  53. // mUser = getIntent().getStringExtra(EXTRA_EMAIL);
  54. m_etName = (EditText) view.findViewById(R.id.user);
  55. // mUserView.setText(mUser);
  56. m_etPassword = (EditText) view.findViewById(R.id.password);
  57. m_etPassword
  58. .setOnEditorActionListener(new TextView.OnEditorActionListener()
  59. {
  60. @Override
  61. public boolean onEditorAction(TextView textView, int id,
  62. KeyEvent keyEvent)
  63. {
  64. if (id == R.id.login
  65. || id == EditorInfo.IME_ACTION_DONE)
  66. {
  67. InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
  68. .getApplicationContext().getSystemService(
  69. Context.INPUT_METHOD_SERVICE);
  70. // EditText editText =
  71. // (EditText)findViewById(R.id.xxxx);
  72. inputMethodManager.hideSoftInputFromWindow(
  73. m_etPassword.getWindowToken(), 0); // Òþ²Ø
  74. attemptLogin();
  75. return true;
  76. }
  77. return false;
  78. }
  79. });
  80. mLoginFormView = view.findViewById(R.id.login_form);
  81. mLoginStatusView = view.findViewById(R.id.login_status);
  82. mLoginStatusMessageView = (TextView) view
  83. .findViewById(R.id.login_status_message);
  84. view.findViewById(R.id.sign_in_button).setOnClickListener(
  85. new View.OnClickListener()
  86. {
  87. @Override
  88. public void onClick(View view)
  89. {
  90. InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
  91. .getApplicationContext().getSystemService(
  92. Context.INPUT_METHOD_SERVICE);
  93. // EditText editText =
  94. // (EditText)findViewById(R.id.xxxx);
  95. inputMethodManager.hideSoftInputFromWindow(
  96. m_etPassword.getWindowToken(), 0);
  97. attemptLogin();
  98. // showProgress(true);
  99. }
  100. });
  101. return view;
  102. // return super.onCreateView(inflater, container, savedInstanceState);
  103. }
  104. // @Override
  105. // public void onClick(View v)
  106. // {
  107. // // TODO Auto-generated method stub
  108. // if (v.getId() == R.id.sign_in_button)
  109. // {
  110. // SharedPreferences sharedPrefrences = getActivity()
  111. // .getSharedPreferences("test", Context.MODE_PRIVATE);
  112. // Intent intent = new Intent();
  113. // intent.setClass(getActivity(), FunctionSelectActivity.class);
  114. // getActivity().startActivity(intent);
  115. // }
  116. //
  117. // }
  118. // /**
  119. // * A dummy authentication store containing known user names and passwords.
  120. // * TODO: remove after connecting to a real authentication system.
  121. // */
  122. // private static final String[] DUMMY_CREDENTIALS = new String[] {
  123. // "foo@example.com:hello", "bar@example.com:world" };
  124. //
  125. // /**
  126. // * The default email to populate the email field with.
  127. // */
  128. // public static final String EXTRA_EMAIL =
  129. // "com.example.android.authenticatordemo.extra.EMAIL";
  130. //
  131. // /**
  132. // * Keep track of the login task to ensure we can cancel it if requested.
  133. // */
  134. // private UserLoginTask mAuthTask = null;
  135. //
  136. // // Values for email and password at the time of the login attempt.
  137. // private String mUser;
  138. // private String mPassword;
  139. //
  140. // // UI references.
  141. // private EditText mUserView;
  142. // private EditText mPasswordView;
  143. // @Override
  144. // protected void onCreate(Bundle savedInstanceState)
  145. // {
  146. // super.onCreate(savedInstanceState);
  147. //
  148. // setContentView(R.layout.activity_login);
  149. //
  150. // // Set up the login form.
  151. // mUser = getIntent().getStringExtra(EXTRA_EMAIL);
  152. // mUserView = (EditText) findViewById(R.id.email);
  153. // mUserView.setText(mUser);
  154. //
  155. // mPasswordView = (EditText) findViewById(R.id.password);
  156. // mPasswordView
  157. // .setOnEditorActionListener(new TextView.OnEditorActionListener()
  158. // {
  159. // @Override
  160. // public boolean onEditorAction(TextView textView, int id,
  161. // KeyEvent keyEvent)
  162. // {
  163. // if (id == R.id.login || id == EditorInfo.IME_NULL)
  164. // {
  165. // attemptLogin();
  166. // return true;
  167. // }
  168. // return false;
  169. // }
  170. // });
  171. //
  172. // mLoginFormView = findViewById(R.id.login_form);
  173. // mLoginStatusView = findViewById(R.id.login_status);
  174. // mLoginStatusMessageView = (TextView)
  175. // findViewById(R.id.login_status_message);
  176. //
  177. // findViewById(R.id.sign_in_button).setOnClickListener(
  178. // new View.OnClickListener()
  179. // {
  180. // @Override
  181. // public void onClick(View view)
  182. // {
  183. // attemptLogin();
  184. // }
  185. // });
  186. // }
  187. //
  188. // @Override
  189. // public boolean onCreateOptionsMenu(Menu menu)
  190. // {
  191. // super.onCreateOptionsMenu(menu);
  192. // getMenuInflater().inflate(R.menu.login, menu);
  193. // return true;
  194. // }
  195. //
  196. // /**
  197. // * Attempts to sign in or register the account specified by the login
  198. // form.
  199. // * If there are form errors (invalid email, missing fields, etc.), the
  200. // * errors are presented and no actual login attempt is made.
  201. // */
  202. public void attemptLogin()
  203. {
  204. if (mAuthTask != null)
  205. {
  206. return;
  207. }
  208. // Reset errors.
  209. m_etName.setError(null);
  210. m_etPassword.setError(null);
  211. // Store values at the time of the login attempt.
  212. m_sUser = m_etName.getText().toString();
  213. m_sPassword = m_etPassword.getText().toString();
  214. boolean cancel = false;
  215. View focusView = null;
  216. // Check for a valid password.
  217. if (TextUtils.isEmpty(m_sPassword))
  218. {
  219. m_etPassword.setError(getString(R.string.error_field_required));
  220. focusView = m_etPassword;
  221. cancel = true;
  222. }
  223. else
  224. if (m_sPassword.length() < 4)
  225. {
  226. m_etPassword
  227. .setError(getString(R.string.error_invalid_password));
  228. focusView = m_etPassword;
  229. cancel = true;
  230. }
  231. // Check for a valid user name.
  232. if (TextUtils.isEmpty(m_sUser))
  233. {
  234. m_etName.setError(getString(R.string.error_field_required));
  235. focusView = m_etName;
  236. cancel = true;
  237. }
  238. // else if (!m_sName.contains("@")) {
  239. // m_etName.setError(getString(R.string.error_invalid_email));
  240. // focusView = m_etName;
  241. // cancel = true;
  242. // }
  243. if (cancel)
  244. {
  245. // There was an error; don't attempt login and focus the first
  246. // form field with an error.
  247. focusView.requestFocus();
  248. }
  249. else
  250. {
  251. // Show a progress spinner, and kick off a background task to
  252. // perform the user login attempt.
  253. mLoginStatusMessageView.setText(R.string.login_progress_signing_in);
  254. showProgress(true);
  255. mAuthTask = new UserLoginTask();
  256. mAuthTask.execute((Void) null);
  257. }
  258. }
  259. //
  260. // /**
  261. // * Shows the progress UI and hides the login form.
  262. // */
  263. // @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  264. private void showProgress(final boolean show)
  265. {
  266. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  267. // for very easy animations. If available, use these APIs to fade-in
  268. // the progress spinner.
  269. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
  270. {
  271. int shortAnimTime = getResources().getInteger(
  272. android.R.integer.config_shortAnimTime);
  273. mLoginStatusView.setVisibility(View.VISIBLE);
  274. mLoginStatusView.animate().setDuration(shortAnimTime)
  275. .alpha(show ? 1 : 0)
  276. .setListener(new AnimatorListenerAdapter()
  277. {
  278. @Override
  279. public void onAnimationEnd(Animator animation)
  280. {
  281. mLoginStatusView.setVisibility(show ? View.VISIBLE
  282. : View.INVISIBLE);
  283. }
  284. });
  285. mLoginFormView.setVisibility(View.VISIBLE);
  286. mLoginFormView.animate().setDuration(shortAnimTime)
  287. .alpha(show ? 0 : 1)
  288. .setListener(new AnimatorListenerAdapter()
  289. {
  290. @Override
  291. public void onAnimationEnd(Animator animation)
  292. {
  293. mLoginFormView.setVisibility(show ? View.INVISIBLE
  294. : View.VISIBLE);
  295. }
  296. });
  297. }
  298. else
  299. {
  300. // The ViewPropertyAnimator APIs are not available, so simply show
  301. // and hide the relevant UI components.
  302. mLoginStatusView
  303. .setVisibility(show ? View.VISIBLE : View.INVISIBLE);
  304. mLoginFormView.setVisibility(show ? View.INVISIBLE : View.VISIBLE);
  305. }
  306. }
  307. //
  308. // /**
  309. // * Represents an asynchronous login/registration task used to authenticate
  310. // * the user.
  311. // */
  312. public class UserLoginTask extends AsyncTask<Void, Void, Boolean>
  313. {
  314. // int err_code = ERR_CODE_NONE;
  315. int errorcode;
  316. @Override
  317. protected Boolean doInBackground(Void... params)
  318. {
  319. errorcode = Network.get_Auth(m_sUser, m_sPassword);
  320. if (errorcode == Network.RESULT_TRUE)
  321. return true;
  322. else
  323. {
  324. return false;
  325. }
  326. }
  327. @Override
  328. protected void onPostExecute(final Boolean success)
  329. {
  330. Log.i("onPostExecute", "entry");
  331. mAuthTask = null;
  332. showProgress(false);
  333. // if (netconnect == )
  334. // {
  335. //
  336. // }
  337. switch (errorcode)
  338. {
  339. case Network.RESULT_NET_NOTAVAILABLE:
  340. {
  341. Toast toast = Toast.makeText(getActivity()
  342. .getApplicationContext(),
  343. getText(R.string.msg_connection_none),
  344. Toast.LENGTH_LONG);
  345. toast.setGravity(Gravity.CENTER, 0, 0);
  346. toast.show();
  347. return;
  348. }
  349. case Network.RESULT_NET_ERROR:
  350. {
  351. Toast toast = Toast.makeText(getActivity()
  352. .getApplicationContext(),
  353. getText(R.string.msg_net_error), Toast.LENGTH_LONG);
  354. toast.setGravity(Gravity.CENTER, 0, 0);
  355. toast.show();
  356. return;
  357. }
  358. case Network.RESULT_ERROR:
  359. // case Network.RESULT_RESPONSE_NULL:
  360. {
  361. Toast toast = Toast.makeText(getActivity()
  362. .getApplicationContext(),
  363. getText(R.string.msg_net_resulterror),
  364. Toast.LENGTH_LONG);
  365. toast.setGravity(Gravity.CENTER, 0, 0);
  366. toast.show();
  367. return;
  368. }
  369. default:
  370. break;
  371. }
  372. if (success)
  373. {
  374. // SharedPreferences.Editor editor = RunOnce.edit();
  375. // editor.putBoolean("FirstRun"+globalUtil.getVerName(this),
  376. // false);
  377. // // Don't forget to commit your edits!!!
  378. // editor.commit();
  379. Intent intent = new Intent();
  380. intent.setClass(getActivity(), FunctionSelectActivity.class);
  381. intent.putExtra("user", m_sUser);
  382. intent.putExtra("password", m_sPassword);
  383. startActivity(intent);
  384. getActivity().finish();
  385. }
  386. else
  387. {
  388. m_etPassword
  389. .setError(getString(R.string.error_incorrect_password));
  390. m_etPassword.requestFocus();
  391. }
  392. }
  393. @Override
  394. protected void onCancelled()
  395. {
  396. mAuthTask = null;
  397. showProgress(false);
  398. }
  399. }
  400. }