Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Administrator
AloqaIM-Android
Commits
26ebda44
Commit
26ebda44
authored
Dec 22, 2016
by
Tiago Cunha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Checks for the server version
parent
cab83399
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
13 deletions
+141
-13
InputHostnameFragment.java
...android/fragment/server_config/InputHostnameFragment.java
+36
-13
ServerHelper.java
...rc/main/java/chat/rocket/android/helper/ServerHelper.java
+103
-0
strings.xml
app/src/main/res/values/strings.xml
+2
-0
No files found.
app/src/main/java/chat/rocket/android/fragment/server_config/InputHostnameFragment.java
View file @
26ebda44
...
...
@@ -2,11 +2,14 @@ package chat.rocket.android.fragment.server_config;
import
android.support.design.widget.Snackbar
;
import
android.widget.TextView
;
import
android.widget.Toast
;
import
org.json.JSONObject
;
import
chat.rocket.android.R
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.helper.OkHttpHelper
;
import
chat.rocket.android.helper.ServerHelper
;
import
chat.rocket.android.helper.TextUtils
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.realm_helper.RealmObjectObserver
;
...
...
@@ -37,11 +40,42 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
}
private
void
handleConnect
()
{
final
String
hostname
=
ServerHelper
.
enforceHostname
(
getHostname
());
ServerHelper
.
isApiVersionValid
(
OkHttpHelper
.
getClientForUploadFile
(),
hostname
,
new
ServerHelper
.
Callback
()
{
@Override
public
void
isValid
()
{
getActivity
().
runOnUiThread
(()
->
onServerValid
(
hostname
));
}
@Override
public
void
isNotValid
()
{
getActivity
().
runOnUiThread
(()
->
Toast
.
makeText
(
getActivity
(),
R
.
string
.
input_hostname_invalid_server_message
,
Toast
.
LENGTH_SHORT
).
show
());
}
});
}
@Override
public
void
onResume
()
{
super
.
onResume
();
}
@Override
public
void
onDestroyView
()
{
serverConfigObserver
.
unsub
();
super
.
onDestroyView
();
}
private
String
getHostname
()
{
final
TextView
editor
=
(
TextView
)
rootView
.
findViewById
(
R
.
id
.
editor_hostname
);
final
String
hostname
=
TextUtils
.
or
(
TextUtils
.
or
(
editor
.
getText
(),
editor
.
getHint
()),
""
).
toString
();
return
TextUtils
.
or
(
TextUtils
.
or
(
editor
.
getText
(),
editor
.
getHint
()),
""
).
toString
();
}
private
void
onServerValid
(
String
hostname
)
{
RocketChatCache
.
get
(
getContext
()).
edit
()
.
putString
(
RocketChatCache
.
KEY_SELECTED_SERVER_CONFIG_ID
,
serverConfigId
)
.
apply
();
...
...
@@ -55,17 +89,6 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
.
put
(
"state"
,
ServerConfig
.
STATE_READY
))).
continueWith
(
new
LogcatIfError
());
}
@Override
public
void
onResume
()
{
super
.
onResume
();
}
@Override
public
void
onDestroyView
()
{
serverConfigObserver
.
unsub
();
super
.
onDestroyView
();
}
private
void
showError
(
String
errString
)
{
Snackbar
.
make
(
rootView
,
errString
,
Snackbar
.
LENGTH_LONG
).
show
();
}
...
...
app/src/main/java/chat/rocket/android/helper/ServerHelper.java
0 → 100644
View file @
26ebda44
package
chat
.
rocket
.
android
.
helper
;
import
android.support.annotation.NonNull
;
import
org.json.JSONObject
;
import
java.io.IOException
;
import
okhttp3.Call
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
okhttp3.ResponseBody
;
public
class
ServerHelper
{
private
static
final
String
DEFAULT_HOST
=
".rocket.chat"
;
private
static
final
String
API_INFO_PATH
=
"/api/info"
;
private
static
final
String
VERSION_PROPERTY
=
"version"
;
public
static
String
enforceHostname
(
String
hostname
)
{
if
(
hostname
==
null
)
{
return
"demo.rocket.chat"
;
}
return
removeProtocol
(
enforceDefaultHost
(
hostname
));
}
public
static
void
isApiVersionValid
(
@NonNull
OkHttpClient
client
,
@NonNull
String
host
,
@NonNull
Callback
callback
)
{
Request
request
;
try
{
request
=
new
Request
.
Builder
()
.
url
(
"https://"
+
host
+
API_INFO_PATH
)
.
get
()
.
build
();
}
catch
(
Exception
e
)
{
callback
.
isNotValid
();
return
;
}
client
.
newCall
(
request
).
enqueue
(
new
okhttp3
.
Callback
()
{
@Override
public
void
onFailure
(
Call
call
,
IOException
e
)
{
// some connection error
callback
.
isNotValid
();
}
@Override
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
if
(!
response
.
isSuccessful
()
||
!
isValid
(
response
.
body
()))
{
callback
.
isNotValid
();
return
;
}
callback
.
isValid
();
}
});
}
@NonNull
private
static
String
enforceDefaultHost
(
String
hostname
)
{
if
(
hostname
.
indexOf
(
'.'
)
==
-
1
)
{
hostname
=
hostname
+
DEFAULT_HOST
;
}
return
hostname
;
}
@NonNull
private
static
String
removeProtocol
(
String
hostname
)
{
// yep. cheap.
return
hostname
.
replace
(
"http://"
,
""
).
replace
(
"https://"
,
""
);
}
private
static
boolean
isValid
(
ResponseBody
body
)
{
if
(
body
==
null
||
body
.
contentLength
()
==
0
)
{
return
false
;
}
try
{
final
JSONObject
jsonObject
=
new
JSONObject
(
body
.
string
());
return
jsonObject
.
has
(
VERSION_PROPERTY
)
&&
isVersionValid
(
jsonObject
.
getString
(
VERSION_PROPERTY
));
}
catch
(
Exception
e
)
{
return
false
;
}
}
private
static
boolean
isVersionValid
(
String
version
)
{
if
(
version
==
null
||
version
.
length
()
==
0
)
{
return
false
;
}
String
[]
versionParts
=
version
.
split
(
"\\."
);
return
versionParts
.
length
>=
3
&&
Integer
.
valueOf
(
versionParts
[
1
])
>=
49
;
}
public
interface
Callback
{
void
isValid
();
void
isNotValid
();
}
}
app/src/main/res/values/strings.xml
View file @
26ebda44
...
...
@@ -39,4 +39,6 @@
<string
name=
"image_upload_message_spec_title"
>
Attach image
</string>
<string
name=
"audio_upload_message_spec_title"
>
Attach audio
</string>
<string
name=
"video_upload_message_spec_title"
>
Attach video
</string>
<string
name=
"input_hostname_invalid_server_message"
>
Invalid server version
</string>
</resources>
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