Commit 9951b4cf authored by Administrator's avatar Administrator

登录页面和逻辑

parent 38a4fb19
......@@ -59,4 +59,5 @@ dependencies {
implementation 'com.android.support:multidex:1.0.3'
implementation 'androidx.preference:preference:1.1.0'
annotationProcessor 'org.projectlombok:lombok:1.18.2'
compileOnly 'org.projectlombok:lombok:1.18.2'
}
......@@ -20,7 +20,7 @@
<activity
android:name=".ui.SettingsActivity"
android:label="@string/title_activity_settings" />
<activity android:name=".MainActivity" />
<activity android:name=".ui.MainActivity" />
<activity
android:name=".ui.LoginActivity"
android:configChanges="orientation|screenSize"
......
......@@ -14,6 +14,7 @@ import android.os.IBinder;
import androidx.annotation.Nullable;
import com.virjar.echo.adr.repo.EchoConfig;
import com.virjar.echo.adr.ui.MainActivity;
import com.virjar.echo.nat.client.EchoClient;
import com.virjar.echo.nat.log.EchoLogger;
......
package com.virjar.echo.adr.bean;
import lombok.Data;
@Data
public class Result<O> {
public static final int OK = 200;
public static final int SERVER_ERROR = 500;
public static final int CLIENT_ERROR = 400;
public static final int NOT_FOUND = 404;
public static final int AUTH_FAIL = 403;
private O data;
private int code;
private String errorMessage;
public boolean isOk() {
return code == OK;
}
}
package com.virjar.echo.adr.bean;
import java.util.Date;
import lombok.Data;
@Data
public class UserInfo {
private int id;
private String account;
private String password;
private Integer userLevel;
private Long balance;
private String loginToken;
private Date lastActive;
private Boolean isAdmin;
}
......@@ -9,15 +9,14 @@ import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class APIServices {
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EchoConfig.getApiURL())
.callFactory(createInterceptedOkHttp())
// .addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
// .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
public static UserApi userApi = retrofit.create(UserApi.class);
......
......@@ -6,6 +6,7 @@ import android.text.TextUtils;
import com.virjar.echo.adr.R;
import java.util.Objects;
import java.util.UUID;
public class EchoConfig {
......@@ -36,7 +37,7 @@ public class EchoConfig {
}
public static int getEchoServerPort() {
return sharedPreferences.getInt("server_port", defaultServerPort);
return Integer.parseInt(Objects.requireNonNull(sharedPreferences.getString("server_port", String.valueOf(defaultServerPort))));
}
public static String getClientId() {
......
package com.virjar.echo.adr.repo;
import com.google.gson.JsonObject;
import com.virjar.echo.adr.bean.Result;
import com.virjar.echo.adr.bean.UserInfo;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
public interface UserApi {
@GET("api/user/userInfo")
Call<JsonObject> loginWithToken();
Call<Result<UserInfo>> loginWithToken();
@FormUrlEncoded
@POST("api/user/login")
Call<Result<UserInfo>> loginWithUsername(@Field("userName") String userName,
@Field("password") String password);
}
package com.virjar.echo.adr.ui;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.JsonObject;
import com.virjar.echo.adr.R;
import com.virjar.echo.adr.bean.Result;
import com.virjar.echo.adr.bean.UserInfo;
import com.virjar.echo.adr.repo.APIServices;
import com.virjar.echo.adr.repo.EchoConfig;
import com.virjar.echo.nat.log.EchoLogger;
import retrofit2.Call;
import retrofit2.Callback;
......@@ -26,6 +29,7 @@ public class LoginActivity extends AppCompatActivity {
ImageView imageView;
TextView textView;
int count = 0;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -43,10 +47,34 @@ public class LoginActivity extends AppCompatActivity {
btnSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
startActivity(new Intent(LoginActivity.this, SettingsActivity.class));
SettingsActivity.go(LoginActivity.this);
}
});
autoLoginWithToken();
// bind login button event
final EditText editTextUsername = findViewById(R.id.edt_username);
final EditText editTextPassword = findViewById(R.id.edt_password);
buttonLogin = findViewById(R.id.btn_sign_in);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = editTextUsername.getText().toString();
if (userName.isEmpty()) {
Toast.makeText(LoginActivity.this, "please input username", Toast.LENGTH_SHORT).show();
return;
}
String password = editTextPassword.getText().toString();
if (password.isEmpty()) {
Toast.makeText(LoginActivity.this, "please input password", Toast.LENGTH_SHORT).show();
return;
}
loginWithUserName(userName, password);
}
});
}
private void autoLoginWithToken() {
......@@ -55,27 +83,76 @@ public class LoginActivity extends AppCompatActivity {
autoLoginWithUserName();
return;
}
Call<JsonObject> loginResponse = APIServices.userApi.loginWithToken();
loginResponse.enqueue(new Callback<JsonObject>() {
Call<Result<UserInfo>> loginResponse = APIServices.userApi.loginWithToken();
loginResponse.enqueue(new Callback<Result<UserInfo>>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
JsonObject body = response.body();
if (!body.get("ok").getAsBoolean()) {
public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
Result<UserInfo> body = response.body();
if (!body.isOk()) {
EchoLogger.getLogger().info("auto login with token failed: " + body.getErrorMessage());
autoLoginWithUserName();
} else {
EchoLogger.getLogger().info("auto login with token success");
onLoginSuccess(body.getData());
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
public void onFailure(Call<Result<UserInfo>> call, Throwable t) {
EchoLogger.getLogger().error("auto login with token failed", t);
autoLoginWithUserName();
}
});
}
private void autoLoginWithUserName() {
String username = EchoConfig.getUsername();
String password = EchoConfig.getPassword();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
EchoLogger.getLogger().info("no user name cached,skip auto login with username");
return;
}
loginWithUserName(username, password);
}
private void loginWithUserName(String username, String password) {
buttonLogin.setClickable(false);
APIServices.userApi
.loginWithUsername(username, password)
.enqueue(new Callback<Result<UserInfo>>() {
@Override
public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
buttonLogin.setClickable(true);
final Result<UserInfo> result = response.body();
if (!result.isOk()) {
EchoLogger.getLogger().error("login failed:" + result.getErrorMessage());
LoginActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, result.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
});
return;
}
onLoginSuccess(result.getData());
}
@Override
public void onFailure(Call<Result<UserInfo>> call, Throwable t) {
buttonLogin.setClickable(true);
EchoLogger.getLogger().error("auto login with username failed", t);
}
});
}
private void onLoginSuccess(UserInfo userInfo) {
EchoConfig.updateLoginToken(userInfo.getLoginToken());
EchoConfig.saveUsernameAndPassword(userInfo.getAccount(), userInfo.getPassword());
MainActivity.go(this);
finish();
}
private void setupAnimation() {
imageView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
......
package com.virjar.echo.adr;
package com.virjar.echo.adr.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.virjar.echo.adr.HttpProxyService;
import com.virjar.echo.adr.R;
public class MainActivity extends AppCompatActivity {
public static void go(Context context) {
context.startActivity(new Intent(context, MainActivity.class));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
package com.virjar.echo.adr.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
......@@ -11,6 +13,9 @@ import com.virjar.echo.adr.R;
import com.virjar.echo.adr.repo.EchoConfig;
public class SettingsActivity extends AppCompatActivity {
public static void go(Context context) {
context.startActivity(new Intent(context, SettingsActivity.class));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
......
......@@ -97,6 +97,7 @@
<EditText
android:id="@+id/edt_username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawablePadding="16dp"
......@@ -110,6 +111,7 @@
<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawablePadding="16dp"
......@@ -131,6 +133,7 @@
android:orientation="horizontal">
<Button
android:id="@+id/btn_sign_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
......
......@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".ui.MainActivity">
<TextView
android:layout_width="wrap_content"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment