Commit 9951b4cf authored by Administrator's avatar Administrator

登录页面和逻辑

parent 38a4fb19
...@@ -59,4 +59,5 @@ dependencies { ...@@ -59,4 +59,5 @@ dependencies {
implementation 'com.android.support:multidex:1.0.3' implementation 'com.android.support:multidex:1.0.3'
implementation 'androidx.preference:preference:1.1.0' implementation 'androidx.preference:preference:1.1.0'
annotationProcessor 'org.projectlombok:lombok:1.18.2' annotationProcessor 'org.projectlombok:lombok:1.18.2'
compileOnly 'org.projectlombok:lombok:1.18.2'
} }
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<activity <activity
android:name=".ui.SettingsActivity" android:name=".ui.SettingsActivity"
android:label="@string/title_activity_settings" /> android:label="@string/title_activity_settings" />
<activity android:name=".MainActivity" /> <activity android:name=".ui.MainActivity" />
<activity <activity
android:name=".ui.LoginActivity" android:name=".ui.LoginActivity"
android:configChanges="orientation|screenSize" android:configChanges="orientation|screenSize"
......
...@@ -14,6 +14,7 @@ import android.os.IBinder; ...@@ -14,6 +14,7 @@ import android.os.IBinder;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.virjar.echo.adr.repo.EchoConfig; 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.client.EchoClient;
import com.virjar.echo.nat.log.EchoLogger; 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; ...@@ -9,15 +9,14 @@ import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
public class APIServices { public class APIServices {
private static Retrofit retrofit = new Retrofit.Builder() private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EchoConfig.getApiURL()) .baseUrl(EchoConfig.getApiURL())
.callFactory(createInterceptedOkHttp()) .callFactory(createInterceptedOkHttp())
// .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build(); .build();
public static UserApi userApi = retrofit.create(UserApi.class); public static UserApi userApi = retrofit.create(UserApi.class);
......
...@@ -6,6 +6,7 @@ import android.text.TextUtils; ...@@ -6,6 +6,7 @@ import android.text.TextUtils;
import com.virjar.echo.adr.R; import com.virjar.echo.adr.R;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class EchoConfig { public class EchoConfig {
...@@ -36,7 +37,7 @@ public class EchoConfig { ...@@ -36,7 +37,7 @@ public class EchoConfig {
} }
public static int getEchoServerPort() { 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() { public static String getClientId() {
......
package com.virjar.echo.adr.repo; 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.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.POST;
public interface UserApi { public interface UserApi {
@GET("api/user/userInfo") @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; package com.virjar.echo.adr.ui;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.JsonObject;
import com.virjar.echo.adr.R; 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.APIServices;
import com.virjar.echo.adr.repo.EchoConfig; import com.virjar.echo.adr.repo.EchoConfig;
import com.virjar.echo.nat.log.EchoLogger;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
...@@ -26,6 +29,7 @@ public class LoginActivity extends AppCompatActivity { ...@@ -26,6 +29,7 @@ public class LoginActivity extends AppCompatActivity {
ImageView imageView; ImageView imageView;
TextView textView; TextView textView;
int count = 0; int count = 0;
private Button buttonLogin;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -43,10 +47,34 @@ public class LoginActivity extends AppCompatActivity { ...@@ -43,10 +47,34 @@ public class LoginActivity extends AppCompatActivity {
btnSetting.setOnClickListener(new View.OnClickListener() { btnSetting.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); SettingsActivity.go(LoginActivity.this);
startActivity(new Intent(LoginActivity.this, SettingsActivity.class));
} }
}); });
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() { private void autoLoginWithToken() {
...@@ -55,28 +83,77 @@ public class LoginActivity extends AppCompatActivity { ...@@ -55,28 +83,77 @@ public class LoginActivity extends AppCompatActivity {
autoLoginWithUserName(); autoLoginWithUserName();
return; return;
} }
Call<JsonObject> loginResponse = APIServices.userApi.loginWithToken(); Call<Result<UserInfo>> loginResponse = APIServices.userApi.loginWithToken();
loginResponse.enqueue(new Callback<JsonObject>() { loginResponse.enqueue(new Callback<Result<UserInfo>>() {
@Override @Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
JsonObject body = response.body(); Result<UserInfo> body = response.body();
if (!body.get("ok").getAsBoolean()) { if (!body.isOk()) {
EchoLogger.getLogger().info("auto login with token failed: " + body.getErrorMessage());
autoLoginWithUserName(); autoLoginWithUserName();
} else {
EchoLogger.getLogger().info("auto login with token success");
onLoginSuccess(body.getData());
} }
} }
@Override @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(); autoLoginWithUserName();
} }
}); });
} }
private void 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() { private void setupAnimation() {
imageView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) { imageView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
public void onSwipeTop() { public void onSwipeTop() {
......
package com.virjar.echo.adr; package com.virjar.echo.adr.ui;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.virjar.echo.adr.HttpProxyService;
import com.virjar.echo.adr.R;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
public static void go(Context context) {
context.startActivity(new Intent(context, MainActivity.class));
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
......
package com.virjar.echo.adr.ui; package com.virjar.echo.adr.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.MenuItem; import android.view.MenuItem;
...@@ -11,6 +13,9 @@ import com.virjar.echo.adr.R; ...@@ -11,6 +13,9 @@ import com.virjar.echo.adr.R;
import com.virjar.echo.adr.repo.EchoConfig; import com.virjar.echo.adr.repo.EchoConfig;
public class SettingsActivity extends AppCompatActivity { public class SettingsActivity extends AppCompatActivity {
public static void go(Context context) {
context.startActivity(new Intent(context, SettingsActivity.class));
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
......
...@@ -97,6 +97,7 @@ ...@@ -97,6 +97,7 @@
<EditText <EditText
android:id="@+id/edt_username"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:drawablePadding="16dp" android:drawablePadding="16dp"
...@@ -110,6 +111,7 @@ ...@@ -110,6 +111,7 @@
<EditText <EditText
android:id="@+id/edt_password"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:drawablePadding="16dp" android:drawablePadding="16dp"
...@@ -131,6 +133,7 @@ ...@@ -131,6 +133,7 @@
android:orientation="horizontal"> android:orientation="horizontal">
<Button <Button
android:id="@+id/btn_sign_in"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".ui.MainActivity">
<TextView <TextView
android:layout_width="wrap_content" 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