123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- package com.cj.autojs.dtok;
- import android.content.Intent;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import android.text.method.HideReturnsTransformationMethod;
- import android.text.method.PasswordTransformationMethod;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.constraintlayout.widget.ConstraintLayout;
- import com.cj.autojs.dtok.entity.User;
- import com.cj.autojs.dtok.service.ForegroundService;
- import com.cj.autojs.dtok.util.SharedPreferencesUtils;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.Response;
- import okhttp3.ResponseBody;
- public class LoginActivity extends BaseActivity {
- EditText usernameEdit;
- EditText passwordEdit;
- Button loginBtn;
- ToggleButton clearUser;
- ToggleButton togglePwd;
- ConstraintLayout loginLayout;
- TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.login_activity);
- textView = findViewById(R.id.app_version);
- textView.setText(getVersionName());
- ForegroundService.stop(LoginActivity.this);
- Intent intent = getIntent();
- if (intent != null) {
- Bundle bundle = intent.getExtras();
- if (bundle != null) {
- String oldData = bundle.getString("loginError");
- Toast.makeText(this, oldData, Toast.LENGTH_SHORT).show();
- // 使用oldData进行操作
- }
- }
- initUi();
- initEvent();
- }
- public void initUi() {
- // Log.d("GET_DEVICE_ID", DeviceUtil.getDeviceIMEI(this));
- usernameEdit = findViewById(R.id.login_user);
- passwordEdit = findViewById(R.id.login_passwd);
- loginBtn = findViewById(R.id.Login_btn_Login);
- togglePwd = findViewById(R.id.togglePwd);
- clearUser = findViewById(R.id.clearUser);
- loginLayout = findViewById(R.id.loginLayout);
- Drawable drawable = getResources().getDrawable(R.drawable.ic_username, null);
- drawable.setBounds(0, 0, 70, 70);
- usernameEdit.setCompoundDrawables(drawable, null, null, null);
- Drawable drawable2 = getResources().getDrawable(R.drawable.ic_password, null);
- drawable2.setBounds(0, 0, 70, 70);
- passwordEdit.setCompoundDrawables(drawable2, null, null, null);
- usernameEdit.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- clearUser.setChecked(!TextUtils.isEmpty(charSequence));
- }
- @Override
- public void afterTextChanged(Editable editable) {
- }
- });
- clearUser.setOnCheckedChangeListener((compoundButton, b) -> {
- if (b) {
- return;
- }
- passwordEdit.setText(null);
- usernameEdit.setText(null);
- usernameEdit.setFocusable(true);
- usernameEdit.setFocusableInTouchMode(true);
- usernameEdit.requestFocus();
- usernameEdit.requestFocusFromTouch();
- });
- togglePwd.setOnCheckedChangeListener((compoundButton, b) -> {
- if (b) {
- passwordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
- } else {
- passwordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
- }
- });
- }
- public void initEvent() {
- // ProgressDialog progress = new ProgressDialog(this);
- loginBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (TextUtils.isEmpty(usernameEdit.getText())) {
- Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(passwordEdit.getText())) {
- Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
- return;
- }
- new Thread(() -> {
- Api.INSTANCE.login(usernameEdit.getText().toString(), passwordEdit.getText().toString(), new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- LoginActivity.this.runOnUiThread(() -> clearUser.toggle());
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- ResponseBody responseBody = response.body();
- if (responseBody == null) {
- LoginActivity.this.runOnUiThread(() -> {
- clearUser.toggle();
- Toast.makeText(LoginActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
- });
- } else {
- String jsonStr = responseBody.string();
- try {
- JSONObject logonObj = new JSONObject(jsonStr);
- if (!logonObj.getBoolean("success")) {
- LoginActivity.this.runOnUiThread(() -> {
- try {
- Toast.makeText(LoginActivity.this, logonObj.getString("message"), Toast.LENGTH_SHORT).show();
- } catch (JSONException e) {
- throw new RuntimeException(e);
- }
- });
- } else {
- JSONObject dataObj = logonObj.getJSONObject("data");
- Api.INSTANCE.setToken(dataObj.getString("token"));
- JSONObject userObj = dataObj.getJSONObject("user");
- User user = new User();
- user.setId(userObj.getInt("id"));
- user.setName(userObj.getString("name"));
- user.setExpireAt(userObj.getString("expireAt"));
- user.setExpireTime(userObj.getLong("expireTime"));
- user.setUsername(userObj.getString("username"));
- user.setStatus(userObj.getBoolean("status"));
- user.setMaxDevices(userObj.getInt("maxDevices"));
- user.setStatus(userObj.getBoolean("status"));
- SharedPreferencesUtils.saveUserInfo(LoginActivity.this, user);
- SharedPreferencesUtils.saveString(LoginActivity.this, "token", dataObj.getString("token"));
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- } catch (JSONException e) {
- LoginActivity.this.runOnUiThread(() -> {
- Toast.makeText(LoginActivity.this, "系统错误,请稍后再试", Toast.LENGTH_SHORT).show();
- });
- }
- }
- }
- });
- }).start();
- }
- });
- }
- }
|