LoginActivity.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.cj.autojs.dtok;
  2. import android.content.Intent;
  3. import android.graphics.drawable.Drawable;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextUtils;
  7. import android.text.TextWatcher;
  8. import android.text.method.HideReturnsTransformationMethod;
  9. import android.text.method.PasswordTransformationMethod;
  10. import android.view.View;
  11. import android.view.Window;
  12. import android.view.WindowManager;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import android.widget.ToggleButton;
  18. import androidx.appcompat.app.AppCompatActivity;
  19. import androidx.constraintlayout.widget.ConstraintLayout;
  20. import com.cj.autojs.dtok.entity.User;
  21. import com.cj.autojs.dtok.service.ForegroundService;
  22. import com.cj.autojs.dtok.util.SharedPreferencesUtils;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25. import java.io.IOException;
  26. import okhttp3.Call;
  27. import okhttp3.Callback;
  28. import okhttp3.Response;
  29. import okhttp3.ResponseBody;
  30. public class LoginActivity extends BaseActivity {
  31. EditText usernameEdit;
  32. EditText passwordEdit;
  33. Button loginBtn;
  34. ToggleButton clearUser;
  35. ToggleButton togglePwd;
  36. ConstraintLayout loginLayout;
  37. TextView textView;
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. requestWindowFeature(Window.FEATURE_NO_TITLE);
  42. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  43. setContentView(R.layout.login_activity);
  44. textView = findViewById(R.id.app_version);
  45. textView.setText(getVersionName());
  46. ForegroundService.stop(LoginActivity.this);
  47. Intent intent = getIntent();
  48. if (intent != null) {
  49. Bundle bundle = intent.getExtras();
  50. if (bundle != null) {
  51. String oldData = bundle.getString("loginError");
  52. Toast.makeText(this, oldData, Toast.LENGTH_SHORT).show();
  53. // 使用oldData进行操作
  54. }
  55. }
  56. initUi();
  57. initEvent();
  58. }
  59. public void initUi() {
  60. // Log.d("GET_DEVICE_ID", DeviceUtil.getDeviceIMEI(this));
  61. usernameEdit = findViewById(R.id.login_user);
  62. passwordEdit = findViewById(R.id.login_passwd);
  63. loginBtn = findViewById(R.id.Login_btn_Login);
  64. togglePwd = findViewById(R.id.togglePwd);
  65. clearUser = findViewById(R.id.clearUser);
  66. loginLayout = findViewById(R.id.loginLayout);
  67. Drawable drawable = getResources().getDrawable(R.drawable.ic_username, null);
  68. drawable.setBounds(0, 0, 70, 70);
  69. usernameEdit.setCompoundDrawables(drawable, null, null, null);
  70. Drawable drawable2 = getResources().getDrawable(R.drawable.ic_password, null);
  71. drawable2.setBounds(0, 0, 70, 70);
  72. passwordEdit.setCompoundDrawables(drawable2, null, null, null);
  73. usernameEdit.addTextChangedListener(new TextWatcher() {
  74. @Override
  75. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  76. }
  77. @Override
  78. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  79. clearUser.setChecked(!TextUtils.isEmpty(charSequence));
  80. }
  81. @Override
  82. public void afterTextChanged(Editable editable) {
  83. }
  84. });
  85. clearUser.setOnCheckedChangeListener((compoundButton, b) -> {
  86. if (b) {
  87. return;
  88. }
  89. passwordEdit.setText(null);
  90. usernameEdit.setText(null);
  91. usernameEdit.setFocusable(true);
  92. usernameEdit.setFocusableInTouchMode(true);
  93. usernameEdit.requestFocus();
  94. usernameEdit.requestFocusFromTouch();
  95. });
  96. togglePwd.setOnCheckedChangeListener((compoundButton, b) -> {
  97. if (b) {
  98. passwordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
  99. } else {
  100. passwordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
  101. }
  102. });
  103. }
  104. public void initEvent() {
  105. // ProgressDialog progress = new ProgressDialog(this);
  106. loginBtn.setOnClickListener(new View.OnClickListener() {
  107. @Override
  108. public void onClick(View view) {
  109. if (TextUtils.isEmpty(usernameEdit.getText())) {
  110. Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
  111. return;
  112. }
  113. if (TextUtils.isEmpty(passwordEdit.getText())) {
  114. Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
  115. return;
  116. }
  117. new Thread(() -> {
  118. Api.INSTANCE.login(usernameEdit.getText().toString(), passwordEdit.getText().toString(), new Callback() {
  119. @Override
  120. public void onFailure(Call call, IOException e) {
  121. LoginActivity.this.runOnUiThread(() -> clearUser.toggle());
  122. }
  123. @Override
  124. public void onResponse(Call call, Response response) throws IOException {
  125. ResponseBody responseBody = response.body();
  126. if (responseBody == null) {
  127. LoginActivity.this.runOnUiThread(() -> {
  128. clearUser.toggle();
  129. Toast.makeText(LoginActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
  130. });
  131. } else {
  132. String jsonStr = responseBody.string();
  133. try {
  134. JSONObject logonObj = new JSONObject(jsonStr);
  135. if (!logonObj.getBoolean("success")) {
  136. LoginActivity.this.runOnUiThread(() -> {
  137. try {
  138. Toast.makeText(LoginActivity.this, logonObj.getString("message"), Toast.LENGTH_SHORT).show();
  139. } catch (JSONException e) {
  140. throw new RuntimeException(e);
  141. }
  142. });
  143. } else {
  144. JSONObject dataObj = logonObj.getJSONObject("data");
  145. Api.INSTANCE.setToken(dataObj.getString("token"));
  146. JSONObject userObj = dataObj.getJSONObject("user");
  147. User user = new User();
  148. user.setId(userObj.getInt("id"));
  149. user.setName(userObj.getString("name"));
  150. user.setExpireAt(userObj.getString("expireAt"));
  151. user.setExpireTime(userObj.getLong("expireTime"));
  152. user.setUsername(userObj.getString("username"));
  153. user.setStatus(userObj.getBoolean("status"));
  154. user.setMaxDevices(userObj.getInt("maxDevices"));
  155. user.setStatus(userObj.getBoolean("status"));
  156. SharedPreferencesUtils.saveUserInfo(LoginActivity.this, user);
  157. SharedPreferencesUtils.saveString(LoginActivity.this, "token", dataObj.getString("token"));
  158. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  159. startActivity(intent);
  160. finish();
  161. }
  162. } catch (JSONException e) {
  163. LoginActivity.this.runOnUiThread(() -> {
  164. Toast.makeText(LoginActivity.this, "系统错误,请稍后再试", Toast.LENGTH_SHORT).show();
  165. });
  166. }
  167. }
  168. }
  169. });
  170. }).start();
  171. }
  172. });
  173. }
  174. }