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
06326eb3
Commit
06326eb3
authored
Jan 06, 2017
by
Tiago Cunha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PR related fixes
parent
529825c9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
144 additions
and
107 deletions
+144
-107
ServerConfigActivity.java
...va/chat/rocket/android/activity/ServerConfigActivity.java
+4
-61
ServerPolicyHelper.java
...n/java/chat/rocket/android/helper/ServerPolicyHelper.java
+1
-0
RocketChatWebSocketThread.java
...hat/rocket/android/service/RocketChatWebSocketThread.java
+55
-46
PushSettingsObserver.java
...rocket/android/service/observer/PushSettingsObserver.java
+84
-0
No files found.
app/src/main/java/chat/rocket/android/activity/ServerConfigActivity.java
View file @
06326eb3
...
...
@@ -5,21 +5,14 @@ import android.os.Bundle;
import
android.support.annotation.Nullable
;
import
android.support.v4.app.Fragment
;
import
bolts.Task
;
import
chat.rocket.android.R
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.fragment.server_config.LoginFragment
;
import
chat.rocket.android.fragment.server_config.RetryConnectFragment
;
import
chat.rocket.android.fragment.server_config.RetryLoginFragment
;
import
chat.rocket.android.fragment.server_config.WaitingFragment
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.helper.TextUtils
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.model.ddp.PublicSetting
;
import
chat.rocket.android.model.ddp.PublicSettingsConstants
;
import
chat.rocket.android.model.internal.Session
;
import
chat.rocket.android.push.gcm.GcmRegistrationIntentService
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmObjectObserver
;
import
chat.rocket.android.realm_helper.RealmStore
;
import
chat.rocket.android.service.RocketChatService
;
...
...
@@ -57,12 +50,13 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
serverConfigErrorObserver
=
RealmStore
.
getDefault
()
.
createObjectObserver
(
realm
->
realm
.
where
(
ServerConfig
.
class
)
.
equalTo
(
ServerConfig
.
ID
,
serverConfigId
))
.
equalTo
(
ServerConfig
.
ID
,
serverConfigId
)
.
equalTo
(
ServerConfig
.
STATE
,
ServerConfig
.
STATE_CONNECTION_ERROR
))
.
setOnUpdateListener
(
this
::
onRenderServerConfigError
);
sessionObserver
=
RealmStore
.
get
(
serverConfigId
)
.
createObjectObserver
(
Session:
:
queryDefaultSession
)
.
setOnUpdateListener
(
this
::
continueWith
ServerConfigSession
);
.
setOnUpdateListener
(
this
::
onRender
ServerConfigSession
);
setContentView
(
R
.
layout
.
simple_screen
);
showFragment
(
new
WaitingFragment
());
...
...
@@ -83,7 +77,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
}
private
void
onRenderServerConfigError
(
ServerConfig
config
)
{
if
(
config
.
getState
()
==
ServerConfig
.
STATE_CONNECTION_ERROR
)
{
if
(
config
!=
null
)
{
sessionObserver
.
unsub
();
showFragment
(
new
RetryConnectFragment
());
}
else
{
...
...
@@ -91,19 +85,6 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
}
}
private
void
continueWithServerConfigSession
(
final
Session
session
)
{
fetchPublicSettings
()
.
continueWith
(
task
->
{
registerForPush
();
return
task
;
})
.
continueWith
(
task
->
{
onRenderServerConfigSession
(
session
);
return
task
;
})
.
continueWith
(
new
LogcatIfError
());
}
private
void
onRenderServerConfigSession
(
Session
session
)
{
if
(
session
==
null
)
{
showFragment
(
new
LoginFragment
());
...
...
@@ -151,44 +132,6 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
fragment
.
setArguments
(
args
);
}
private
Task
<
Void
>
fetchPublicSettings
()
{
return
new
MethodCallHelper
(
this
,
serverConfigId
).
getPublicSettings
();
}
private
void
registerForPush
()
{
RealmHelper
realmHelper
=
RealmStore
.
getDefault
();
final
ServerConfig
serverConfig
=
realmHelper
.
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
ServerConfig
.
ID
,
serverConfigId
)
.
findFirst
());
serverConfig
.
setSyncPushToken
(
isPushEnabled
());
realmHelper
.
executeTransaction
(
realm
->
realm
.
copyToRealmOrUpdate
(
serverConfig
))
.
continueWith
(
task
->
{
if
(
serverConfig
.
shouldSyncPushToken
())
{
Intent
intent
=
new
Intent
(
this
,
GcmRegistrationIntentService
.
class
);
startService
(
intent
);
}
return
task
;
})
.
continueWith
(
new
LogcatIfError
());
}
private
boolean
isPushEnabled
()
{
RealmHelper
realmHelper
=
RealmStore
.
getOrCreate
(
serverConfigId
);
boolean
isPushEnable
=
PublicSetting
.
getBoolean
(
realmHelper
,
PublicSettingsConstants
.
Push
.
ENABLE
,
false
);
String
senderId
=
PublicSetting
.
getString
(
realmHelper
,
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
,
""
).
trim
();
return
isPushEnable
&&
!
""
.
equals
(
senderId
);
}
@Override
protected
void
onBackPressedNotHandled
()
{
moveTaskToBack
(
true
);
...
...
app/src/main/java/chat/rocket/android/helper/ServerPolicyHelper.java
View file @
06326eb3
...
...
@@ -73,6 +73,7 @@ public class ServerPolicyHelper {
private
static
String
removeTrailingSlash
(
String
hostname
)
{
if
(
hostname
.
charAt
(
hostname
.
length
()
-
1
)
!=
'/'
)
{
// no need for a regex - just return it
return
hostname
;
}
...
...
app/src/main/java/chat/rocket/android/service/RocketChatWebSocketThread.java
View file @
06326eb3
...
...
@@ -12,6 +12,7 @@ import bolts.Continuation;
import
bolts.Task
;
import
bolts.TaskCompletionSource
;
import
chat.rocket.android.api.DDPClientWrapper
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.helper.TextUtils
;
import
chat.rocket.android.log.RCLog
;
...
...
@@ -29,6 +30,7 @@ import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
import
chat.rocket.android.service.observer.LoadMessageProcedureObserver
;
import
chat.rocket.android.service.observer.MethodCallObserver
;
import
chat.rocket.android.service.observer.NewMessageObserver
;
import
chat.rocket.android.service.observer.PushSettingsObserver
;
import
chat.rocket.android.service.observer.ReactiveNotificationManager
;
import
chat.rocket.android.service.observer.SessionObserver
;
import
chat.rocket.android.service.observer.TokenLoginObserver
;
...
...
@@ -52,7 +54,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
CurrentUserObserver
.
class
,
ReactiveNotificationManager
.
class
,
FileUploadingToS3Observer
.
class
,
FileUploadingWithUfsObserver
.
class
FileUploadingWithUfsObserver
.
class
,
PushSettingsObserver
.
class
};
private
final
Context
appContext
;
private
final
String
serverConfigId
;
...
...
@@ -165,62 +168,68 @@ public class RocketChatWebSocketThread extends HandlerThread {
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
ServerConfig
.
ID
,
serverConfigId
).
findFirst
());
prepareWebSocket
(
config
.
getHostname
());
return
ddpClient
.
connect
(
config
.
getSession
()).
onSuccessTask
(
task
->
{
final
String
session
=
task
.
getResult
().
session
;
defaultRealm
.
executeTransaction
(
realm
->
realm
.
createOrUpdateObjectFromJson
(
ServerConfig
.
class
,
new
JSONObject
()
.
put
(
"serverConfigId"
,
serverConfigId
)
.
put
(
"session"
,
session
))
).
onSuccess
(
_task
->
serverConfigRealm
.
executeTransaction
(
realm
->
{
Session
sessionObj
=
Session
.
queryDefaultSession
(
realm
).
findFirst
();
return
ddpClient
.
connect
(
config
.
getSession
())
.
onSuccessTask
(
task
->
{
final
String
session
=
task
.
getResult
().
session
;
defaultRealm
.
executeTransaction
(
realm
->
realm
.
createOrUpdateObjectFromJson
(
ServerConfig
.
class
,
new
JSONObject
()
.
put
(
"serverConfigId"
,
serverConfigId
)
.
put
(
"session"
,
session
))
).
onSuccess
(
_task
->
serverConfigRealm
.
executeTransaction
(
realm
->
{
Session
sessionObj
=
Session
.
queryDefaultSession
(
realm
).
findFirst
();
if
(
sessionObj
==
null
)
{
realm
.
createOrUpdateObjectFromJson
(
Session
.
class
,
new
JSONObject
().
put
(
"sessionId"
,
Session
.
DEFAULT_ID
));
}
return
null
;
})).
continueWith
(
new
LogcatIfError
());
return
task
;
}).
onSuccess
(
new
Continuation
<
DDPClientCallback
.
Connect
,
Void
>()
{
// TODO type detection doesn't work due to retrolambda's bug...
@Override
public
Void
then
(
Task
<
DDPClientCallback
.
Connect
>
task
)
throws
Exception
{
registerListeners
();
if
(
sessionObj
==
null
)
{
realm
.
createOrUpdateObjectFromJson
(
Session
.
class
,
new
JSONObject
().
put
(
"sessionId"
,
Session
.
DEFAULT_ID
));
}
return
null
;
})).
continueWith
(
new
LogcatIfError
());
return
task
;
})
.
onSuccess
(
new
Continuation
<
DDPClientCallback
.
Connect
,
Void
>()
{
// TODO type detection doesn't work due to retrolambda's bug...
@Override
public
Void
then
(
Task
<
DDPClientCallback
.
Connect
>
task
)
throws
Exception
{
fetchPublicSettings
();
registerListeners
();
// handling WebSocket#onClose() callback.
task
.
getResult
().
client
.
getOnCloseCallback
().
onSuccess
(
_task
->
{
quit
();
return
null
;
}).
continueWithTask
(
_task
->
{
if
(
_task
.
isFaulted
())
{
ServerConfig
.
logConnectionError
(
serverConfigId
,
_task
.
getError
());
// handling WebSocket#onClose() callback.
task
.
getResult
().
client
.
getOnCloseCallback
().
onSuccess
(
_task
->
{
quit
();
return
null
;
}).
continueWithTask
(
_task
->
{
if
(
_task
.
isFaulted
())
{
ServerConfig
.
logConnectionError
(
serverConfigId
,
_task
.
getError
());
}
return
_task
;
});
return
null
;
}
})
.
continueWithTask
(
task
->
{
if
(
task
.
isFaulted
())
{
Exception
error
=
task
.
getError
();
if
(
error
instanceof
DDPClientCallback
.
Connect
.
Timeout
)
{
ServerConfig
.
logConnectionError
(
serverConfigId
,
new
Exception
(
"Connection Timeout"
));
}
else
{
ServerConfig
.
logConnectionError
(
serverConfigId
,
task
.
getError
());
}
}
return
_
task
;
return
task
;
});
}
return
null
;
}
}).
continueWithTask
(
task
->
{
if
(
task
.
isFaulted
())
{
Exception
error
=
task
.
getError
();
if
(
error
instanceof
DDPClientCallback
.
Connect
.
Timeout
)
{
ServerConfig
.
logConnectionError
(
serverConfigId
,
new
Exception
(
"Connection Timeout"
));
}
else
{
ServerConfig
.
logConnectionError
(
serverConfigId
,
task
.
getError
());
}
}
return
task
;
});
private
Task
<
Void
>
fetchPublicSettings
()
{
return
new
MethodCallHelper
(
serverConfigRealm
,
ddpClient
).
getPublicSettings
();
}
//@DebugLog
private
void
registerListeners
()
{
if
(!
Thread
.
currentThread
().
getName
().
equals
(
"RC_thread_"
+
serverConfigId
))
{
// execute in Looper.
new
Handler
(
getLooper
()).
post
(()
->
{
registerListeners
();
});
new
Handler
(
getLooper
()).
post
(
this
::
registerListeners
);
return
;
}
...
...
app/src/main/java/chat/rocket/android/service/observer/PushSettingsObserver.java
0 → 100644
View file @
06326eb3
package
chat
.
rocket
.
android
.
service
.
observer
;
import
android.content.Context
;
import
android.content.Intent
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
java.util.List
;
import
chat.rocket.android.api.DDPClientWrapper
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.model.ddp.PublicSetting
;
import
chat.rocket.android.model.ddp.PublicSettingsConstants
;
import
chat.rocket.android.push.gcm.GcmRegistrationIntentService
;
import
chat.rocket.android.push.interactors.DefaultPushInteractor
;
import
chat.rocket.android.push.interactors.PushInteractor
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmStore
;
public
class
PushSettingsObserver
extends
AbstractModelObserver
<
PublicSetting
>
{
public
PushSettingsObserver
(
Context
context
,
String
hostname
,
RealmHelper
realmHelper
,
DDPClientWrapper
ddpClient
)
{
super
(
context
,
hostname
,
realmHelper
,
ddpClient
);
}
@Override
public
void
onUpdateResults
(
List
<
PublicSetting
>
results
)
{
RealmHelper
realmHelper
=
RealmStore
.
getDefault
();
PushInteractor
interactor
=
new
DefaultPushInteractor
();
String
serverConfigId
=
interactor
.
getServerConfigId
(
hostname
);
final
ServerConfig
serverConfig
=
realmHelper
.
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
ServerConfig
.
ID
,
serverConfigId
)
.
findFirst
());
serverConfig
.
setSyncPushToken
(
isPushEnabled
(
results
));
realmHelper
.
executeTransaction
(
realm
->
realm
.
copyToRealmOrUpdate
(
serverConfig
))
.
continueWith
(
task
->
{
if
(
serverConfig
.
shouldSyncPushToken
())
{
Intent
intent
=
new
Intent
(
context
.
getApplicationContext
(),
GcmRegistrationIntentService
.
class
);
context
.
getApplicationContext
().
startService
(
intent
);
}
return
task
;
})
.
continueWith
(
new
LogcatIfError
());
}
@Override
public
RealmResults
<
PublicSetting
>
queryItems
(
Realm
realm
)
{
return
realm
.
where
(
PublicSetting
.
class
)
.
equalTo
(
PublicSetting
.
ID
,
PublicSettingsConstants
.
Push
.
ENABLE
)
.
or
()
.
equalTo
(
PublicSetting
.
ID
,
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
)
.
findAll
();
}
private
boolean
isPushEnabled
(
List
<
PublicSetting
>
results
)
{
return
isPushEnable
(
results
)
&&
isGcmValid
(
results
);
}
private
boolean
isPushEnable
(
List
<
PublicSetting
>
results
)
{
for
(
PublicSetting
setting
:
results
)
{
if
(
PublicSettingsConstants
.
Push
.
ENABLE
.
equals
(
setting
.
getId
()))
{
return
"true"
.
equals
(
setting
.
getValue
());
}
}
return
false
;
}
private
boolean
isGcmValid
(
List
<
PublicSetting
>
results
)
{
for
(
PublicSetting
setting
:
results
)
{
if
(
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
.
equals
(
setting
.
getId
()))
{
return
setting
.
getValue
()
!=
null
&&
!
""
.
equals
(
setting
.
getValue
());
}
}
return
false
;
}
}
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