LoginFragment.java 13 KB

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