Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
echo-adr-Deprecated
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
echo
echo-adr-Deprecated
Commits
9951b4cf
Commit
9951b4cf
authored
Jul 21, 2020
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
登录页面和逻辑
parent
38a4fb19
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
168 additions
and
19 deletions
+168
-19
build.gradle
app/build.gradle
+1
-0
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-1
HttpProxyService.java
app/src/main/java/com/virjar/echo/adr/HttpProxyService.java
+1
-0
Result.java
app/src/main/java/com/virjar/echo/adr/bean/Result.java
+21
-0
UserInfo.java
app/src/main/java/com/virjar/echo/adr/bean/UserInfo.java
+24
-0
APIServices.java
app/src/main/java/com/virjar/echo/adr/repo/APIServices.java
+2
-3
EchoConfig.java
app/src/main/java/com/virjar/echo/adr/repo/EchoConfig.java
+2
-1
UserApi.java
app/src/main/java/com/virjar/echo/adr/repo/UserApi.java
+11
-2
LoginActivity.java
app/src/main/java/com/virjar/echo/adr/ui/LoginActivity.java
+87
-10
MainActivity.java
app/src/main/java/com/virjar/echo/adr/ui/MainActivity.java
+9
-1
SettingsActivity.java
...rc/main/java/com/virjar/echo/adr/ui/SettingsActivity.java
+5
-0
activity_login.xml
app/src/main/res/layout/activity_login.xml
+3
-0
activity_main.xml
app/src/main/res/layout/activity_main.xml
+1
-1
No files found.
app/build.gradle
View file @
9951b4cf
...
...
@@ -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'
}
app/src/main/AndroidManifest.xml
View file @
9951b4cf
...
...
@@ -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"
...
...
app/src/main/java/com/virjar/echo/adr/HttpProxyService.java
View file @
9951b4cf
...
...
@@ -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
;
...
...
app/src/main/java/com/virjar/echo/adr/bean/Result.java
0 → 100644
View file @
9951b4cf
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
;
}
}
app/src/main/java/com/virjar/echo/adr/bean/UserInfo.java
0 → 100644
View file @
9951b4cf
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
;
}
app/src/main/java/com/virjar/echo/adr/repo/APIServices.java
View file @
9951b4cf
...
...
@@ -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
);
...
...
app/src/main/java/com/virjar/echo/adr/repo/EchoConfig.java
View file @
9951b4cf
...
...
@@ -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
()
{
...
...
app/src/main/java/com/virjar/echo/adr/repo/UserApi.java
View file @
9951b4cf
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
);
}
app/src/main/java/com/virjar/echo/adr/ui/LoginActivity.java
View file @
9951b4cf
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,28 +83,77 @@ 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
())
{
public
void
onSwipeTop
()
{
...
...
app/src/main/java/com/virjar/echo/adr/MainActivity.java
→
app/src/main/java/com/virjar/echo/adr/
ui/
MainActivity.java
View file @
9951b4cf
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
);
...
...
app/src/main/java/com/virjar/echo/adr/ui/SettingsActivity.java
View file @
9951b4cf
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
)
{
...
...
app/src/main/res/layout/activity_login.xml
View file @
9951b4cf
...
...
@@ -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"
...
...
app/src/main/res/layout/activity_main.xml
View file @
9951b4cf
...
...
@@ -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"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment