PageRenderTime 5150ms CodeModel.GetById 30ms RepoModel.GetById 3ms app.codeStats 0ms

/app/src/main/java/com/uprint/android_pack/cloudprint4androidmanager/activity/CPLoginActivity.java

https://gitlab.com/zhangxiaang/CloudPrint4AndroidManager
Java | 196 lines | 173 code | 14 blank | 9 comment | 20 complexity | 665663fc1aaf05115c48c0a2483edb34 MD5 | raw file
  1. package com.uprint.android_pack.cloudprint4androidmanager.activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.widget.Toolbar;
  5. import android.telephony.PhoneNumberFormattingTextWatcher;
  6. import android.view.KeyEvent;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.avos.avoscloud.AVException;
  12. import com.avos.avoscloud.AVInstallation;
  13. import com.avos.avoscloud.SaveCallback;
  14. import com.dd.processbutton.FlatButton;
  15. import com.rengwuxian.materialedittext.MaterialEditText;
  16. import com.uprint.android_pack.cloudprint4androidmanager.CPBaseActivity;
  17. import com.uprint.android_pack.cloudprint4androidmanager.R;
  18. import com.uprint.android_pack.cloudprint4androidmanager.network.ICallBack;
  19. import com.uprint.android_pack.cloudprint4androidmanager.network.NetValue;
  20. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonPostBiz;
  21. import com.uprint.android_pack.cloudprint4androidmanager.utils.ActivityUtils;
  22. import com.uprint.android_pack.cloudprint4androidmanager.utils.EmptyViewUtil;
  23. import com.uprint.android_pack.cloudprint4androidmanager.utils.MD5;
  24. import com.uprint.android_pack.cloudprint4androidmanager.utils.SharedPreferenceUtil;
  25. import java.lang.ref.WeakReference;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.concurrent.Executors;
  29. import butterknife.Bind;
  30. import butterknife.ButterKnife;
  31. /**
  32. * Created by zhangxiaang on 15/10/7.
  33. */
  34. public class CPLoginActivity extends CPBaseActivity {
  35. @Bind(R.id.accountText)
  36. MaterialEditText accountTextView;
  37. @Bind(R.id.passwordText)
  38. MaterialEditText passwordTextView;
  39. @Bind(R.id.logInBtn)
  40. FlatButton logInBtn;
  41. @Bind(R.id.forgetPassword)
  42. TextView forgetPaswd;
  43. private TextView title;
  44. private Map<String, String> params_map;
  45. Intent intent;
  46. String installationId;
  47. @Override
  48. public void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.loggedlayout);
  51. ButterKnife.bind(this);
  52. Executors.newCachedThreadPool();
  53. init();
  54. }
  55. private void init() {
  56. params_map = new HashMap<>();
  57. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  58. title = (TextView) mToolbar.findViewById(R.id.toolbar_title);
  59. title.setText("登陆");
  60. setSupportActionBar(mToolbar);
  61. if (getSupportActionBar() != null) {
  62. getSupportActionBar().setHomeButtonEnabled(false);
  63. getSupportActionBar().setDisplayShowHomeEnabled(false);
  64. getSupportActionBar().setDisplayShowTitleEnabled(false);
  65. }
  66. accountTextView.setOnKeyListener(new View.OnKeyListener() {
  67. @Override
  68. public boolean onKey(View v, int keyCode, KeyEvent event) {
  69. if (keyCode == 66) {
  70. passwordTextView.requestFocus();
  71. return true;
  72. }
  73. return false;
  74. }
  75. });
  76. accountTextView.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
  77. passwordTextView.setOnKeyListener(new View.OnKeyListener() {
  78. @Override
  79. public boolean onKey(View v, int keyCode, KeyEvent event) {
  80. if (keyCode == 66) {
  81. logInBtn.requestFocus();
  82. return true;
  83. }
  84. return false;
  85. }
  86. });
  87. intent = new Intent(CPLoginActivity.this, CPRegisteActivity.class);
  88. forgetPaswd.setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91. intent.putExtra("flag", true);
  92. startActivity(intent);
  93. }
  94. });
  95. }
  96. public void LoginRoute(View view) {
  97. int resId = view.getId();
  98. switch (resId) {
  99. case R.id.logInBtn:
  100. String account = accountTextView.getText().toString().replace(" ", "").replace("-", "");
  101. String paswd = passwordTextView.getText().toString();
  102. if (account == null || paswd == null || account.trim().length() == 0 || paswd.trim().length() == 0) {
  103. Toast.makeText(getApplicationContext(), "账号 密码为空", Toast.LENGTH_SHORT).show();
  104. return;
  105. }
  106. params_map.put("username", account);
  107. params_map.put("password", MD5.getMD5(paswd));
  108. //todo 提醒后台判断user 机型
  109. AVInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
  110. @Override
  111. public void done(AVException e) {
  112. if (e == null) {
  113. // String result = AVInstallation.getCurrentInstallation().get("deviceType").toString()
  114. // + AVInstallation.getCurrentInstallation().get("timeZone").toString();
  115. AVInstallation.getCurrentInstallation().saveInBackground();
  116. installationId = AVInstallation.getCurrentInstallation().getInstallationId();
  117. }
  118. }
  119. });
  120. //后台根据这个来进行组装uri推送数据 (新的订单以及推送审核通过的state)
  121. //{"data":{"alert": "Hello From LeanCloud~~~~"},"where":{"installationId":"cb219ee8-c745-48ca-870f-ae5f1e9f3e56"}}
  122. params_map.put("device_token", installationId);
  123. CommonPostBiz biz = new CommonPostBiz(this);
  124. biz.request(new ICallBack() {
  125. @Override
  126. public void displayResult(int status, Object... params) {
  127. WeakReference<CPLoginActivity> ref = new WeakReference<>(CPLoginActivity.this);
  128. EmptyViewUtil.closeLoadingDialog();
  129. JSONObject result = (JSONObject) params[0];
  130. if (result.getInteger("error") != 0) {
  131. Toast.makeText(ref.get(), result.getString("msg"), Toast.LENGTH_SHORT).show();
  132. return;
  133. }
  134. JSONObject info = result.getJSONArray("results").getJSONObject(0);
  135. SharedPreferenceUtil.storage_Token(ref.get(),
  136. info.getString("token"));
  137. int statusState = info.getInteger("state");
  138. SharedPreferenceUtil.storageUserState(ref.get(), statusState);
  139. checkoutUserState(statusState);
  140. }
  141. }, NetValue.LOGIN(), params_map);
  142. EmptyViewUtil.showLoadingDialog(this, "", false);
  143. break;
  144. case R.id.regisBtn:
  145. intent.putExtra("flag", false);
  146. startActivity(intent);
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. public void checkoutUserState(int state) {
  153. switch (state) {
  154. case -1:
  155. Toast.makeText(CPLoginActivity.this, "您审核未通过", Toast.LENGTH_SHORT).show();
  156. break;
  157. case 0:
  158. mIntent = new Intent(CPLoginActivity.this, ImproveInfoActivity.class);
  159. ActivityUtils.startActivity(CPLoginActivity.this, mIntent);
  160. break;
  161. case 1:
  162. Toast.makeText(CPLoginActivity.this, "您正处于审核期,通过后我们会及时通知您", Toast.LENGTH_SHORT).show();
  163. break;
  164. case 2:
  165. mIntent = new Intent(CPLoginActivity.this, MainActivity.class);
  166. ActivityUtils.startActivity(CPLoginActivity.this, mIntent);
  167. break;
  168. case 3:
  169. Toast.makeText(CPLoginActivity.this, "您已辞职", Toast.LENGTH_SHORT).show();
  170. break;
  171. default:
  172. //还有什么情况?
  173. break;
  174. }
  175. }
  176. @Override
  177. protected void onDestroy() {
  178. EmptyViewUtil.closeLoadingDialog();
  179. ButterKnife.unbind(this);
  180. super.onDestroy();
  181. }
  182. }