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
b420af80
Commit
b420af80
authored
Jan 10, 2017
by
Yusuke Iwaki
Committed by
GitHub
Jan 10, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #130 from RocketChat/refactor_push_codes
Refactor push-notification related codes
parents
c020b195
a5ca821c
Changes
17
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
300 additions
and
248 deletions
+300
-248
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+0
-3
RocketChatCache.java
app/src/main/java/chat/rocket/android/RocketChatCache.java
+9
-29
MethodCallHelper.java
...c/main/java/chat/rocket/android/api/MethodCallHelper.java
+0
-7
RaixPushHelper.java
...src/main/java/chat/rocket/android/api/RaixPushHelper.java
+38
-0
GcmPushSettingHelper.java
...java/chat/rocket/android/helper/GcmPushSettingHelper.java
+44
-0
ServerConfig.java
...src/main/java/chat/rocket/android/model/ServerConfig.java
+0
-10
GcmPushRegistration.java
...at/rocket/android/model/internal/GcmPushRegistration.java
+62
-0
GCMIntentService.java
...n/java/chat/rocket/android/push/gcm/GCMIntentService.java
+2
-3
GcmInstanceIDListenerService.java
...rocket/android/push/gcm/GcmInstanceIDListenerService.java
+20
-41
GcmRegistrationIntentService.java
...rocket/android/push/gcm/GcmRegistrationIntentService.java
+0
-81
RocketChatWebSocketThread.java
...hat/rocket/android/service/RocketChatWebSocketThread.java
+3
-1
CurrentUserObserver.java
.../rocket/android/service/observer/CurrentUserObserver.java
+0
-7
GcmPushRegistrationObserver.java
...android/service/observer/GcmPushRegistrationObserver.java
+87
-0
PushSettingsObserver.java
...rocket/android/service/observer/PushSettingsObserver.java
+9
-58
SessionObserver.java
...chat/rocket/android/service/observer/SessionObserver.java
+9
-0
RealmHelper.java
...in/java/chat/rocket/android/realm_helper/RealmHelper.java
+15
-1
RealmObjectObserver.java
...chat/rocket/android/realm_helper/RealmObjectObserver.java
+2
-7
No files found.
app/src/main/AndroidManifest.xml
View file @
b420af80
...
...
@@ -66,9 +66,6 @@
<action
android:name=
"com.google.android.gms.iid.InstanceID"
/>
</intent-filter>
</service>
<service
android:name=
".push.gcm.GcmRegistrationIntentService"
android:exported=
"false"
/>
</application>
</manifest>
app/src/main/java/chat/rocket/android/RocketChatCache.java
View file @
b420af80
...
...
@@ -12,7 +12,7 @@ public class RocketChatCache {
public
static
final
String
KEY_SELECTED_SERVER_CONFIG_ID
=
"selectedServerConfigId"
;
public
static
final
String
KEY_SELECTED_ROOM_ID
=
"selectedRoomId"
;
private
static
final
String
PUSH_ID
=
"pushId"
;
private
static
final
String
KEY_
PUSH_ID
=
"pushId"
;
/**
* get SharedPreference instance for RocketChat application cache.
...
...
@@ -21,36 +21,16 @@ public class RocketChatCache {
return
context
.
getSharedPreferences
(
"cache"
,
Context
.
MODE_PRIVATE
);
}
public
static
String
getSelectedServerConfigId
(
Context
context
)
{
return
get
(
context
).
getString
(
KEY_SELECTED_SERVER_CONFIG_ID
,
""
);
}
public
static
void
setSelectedServerConfigId
(
Context
context
,
String
serverConfigId
)
{
setString
(
get
(
context
),
KEY_SELECTED_SERVER_CONFIG_ID
,
serverConfigId
);
}
public
static
String
getSelectedRoomId
(
Context
context
)
{
return
get
(
context
).
getString
(
KEY_SELECTED_ROOM_ID
,
""
);
}
public
static
void
setSelectedRoomId
(
Context
context
,
String
roomId
)
{
setString
(
get
(
context
),
KEY_SELECTED_ROOM_ID
,
roomId
);
}
public
static
String
getPushId
(
Context
context
)
{
public
static
String
getOrCreatePushId
(
Context
context
)
{
SharedPreferences
preferences
=
get
(
context
);
String
pushId
=
null
;
if
(!
preferences
.
contains
(
PUSH_ID
))
{
if
(!
preferences
.
contains
(
KEY_PUSH_ID
))
{
// generates one and save
pushId
=
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
);
setString
(
preferences
,
PUSH_ID
,
pushId
);
String
newId
=
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
);
preferences
.
edit
()
.
putString
(
KEY_PUSH_ID
,
newId
)
.
apply
();
return
newId
;
}
return
preferences
.
getString
(
PUSH_ID
,
pushId
);
}
private
static
void
setString
(
SharedPreferences
preferences
,
String
key
,
String
value
)
{
SharedPreferences
.
Editor
editor
=
preferences
.
edit
();
editor
.
putString
(
key
,
value
);
editor
.
apply
();
return
preferences
.
getString
(
KEY_PUSH_ID
,
null
);
}
}
app/src/main/java/chat/rocket/android/api/MethodCallHelper.java
View file @
b420af80
...
...
@@ -38,13 +38,6 @@ public class MethodCallHelper {
protected
final
RealmHelper
realmHelper
;
protected
final
DDPClientWrapper
ddpClient
;
/**
* initialize with ServerConfigId.
*/
public
MethodCallHelper
(
String
serverConfigId
)
{
this
(
null
,
serverConfigId
);
}
/**
* initialize with Context and ServerConfigId.
*/
...
...
app/src/main/java/chat/rocket/android/api/PushHelper.java
→
app/src/main/java/chat/rocket/android/api/
Raix
PushHelper.java
View file @
b420af80
...
...
@@ -3,66 +3,36 @@ package chat.rocket.android.api;
import
android.content.Context
;
import
android.support.annotation.NonNull
;
import
android.support.annotation.Nullable
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
bolts.Task
;
import
chat.rocket.android.realm_helper.RealmHelper
;
public
class
PushHelper
extends
MethodCallHelper
{
public
PushHelper
(
String
serverConfigId
)
{
super
(
serverConfigId
);
}
public
PushHelper
(
Context
context
,
String
serverConfigId
)
{
public
class
RaixPushHelper
extends
MethodCallHelper
{
public
RaixPushHelper
(
Context
context
,
String
serverConfigId
)
{
super
(
context
,
serverConfigId
);
}
public
PushHelper
(
RealmHelper
realmHelper
,
DDPClientWrapper
ddpClient
)
{
public
Raix
PushHelper
(
RealmHelper
realmHelper
,
DDPClientWrapper
ddpClient
)
{
super
(
realmHelper
,
ddpClient
);
}
public
Task
<
Void
>
pushUpdate
(
@NonNull
String
pushId
,
@NonNull
String
t
oken
,
public
Task
<
Void
>
pushUpdate
(
@NonNull
String
pushId
,
@NonNull
String
gcmT
oken
,
@Nullable
String
userId
)
{
return
call
(
"raix:push-update"
,
TIMEOUT_MS
,
()
->
{
JSONObject
param
=
new
PushUpdate
(
pushId
,
token
,
userId
).
toJson
();
return
new
JSONArray
().
put
(
param
);
}).
onSuccessTask
(
task
->
Task
.
forResult
(
null
));
return
call
(
"raix:push-update"
,
TIMEOUT_MS
,
()
->
new
JSONArray
().
put
(
new
JSONObject
()
.
put
(
"id"
,
pushId
)
.
put
(
"appName"
,
"main"
)
.
put
(
"userId"
,
userId
!=
null
?
userId
:
JSONObject
.
NULL
)
.
put
(
"metadata"
,
new
JSONObject
())
.
put
(
"token"
,
new
JSONObject
().
put
(
"gcm"
,
gcmToken
))))
.
onSuccessTask
(
task
->
Task
.
forResult
(
null
));
}
public
Task
<
Void
>
pushSetUser
(
String
pushId
)
{
return
call
(
"raix:push-setuser"
,
TIMEOUT_MS
,
()
->
new
JSONArray
().
put
(
pushId
))
.
onSuccessTask
(
task
->
Task
.
forResult
(
null
));
}
private
static
class
PushUpdate
{
private
String
pushId
;
private
String
gcmToken
;
private
String
userId
;
PushUpdate
(
@NonNull
String
pushId
,
@NonNull
String
gcmToken
,
@Nullable
String
userId
)
{
this
.
pushId
=
pushId
;
this
.
gcmToken
=
gcmToken
;
this
.
userId
=
userId
;
}
JSONObject
toJson
()
throws
JSONException
{
JSONObject
param
=
new
JSONObject
();
param
.
put
(
"id"
,
pushId
);
param
.
put
(
"appName"
,
"main"
);
param
.
put
(
"userId"
,
userId
!=
null
?
userId
:
JSONObject
.
NULL
);
param
.
put
(
"metadata"
,
new
JSONObject
());
JSONObject
tokenParam
=
new
JSONObject
();
tokenParam
.
put
(
"gcm"
,
gcmToken
);
param
.
put
(
"token"
,
tokenParam
);
return
param
;
}
}
}
app/src/main/java/chat/rocket/android/helper/GcmPushSettingHelper.java
0 → 100644
View file @
b420af80
package
chat
.
rocket
.
android
.
helper
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
java.util.List
;
import
chat.rocket.android.model.ddp.PublicSetting
;
import
chat.rocket.android.model.ddp.PublicSettingsConstants
;
/**
* utility class for getting value comprehensibly from public settings list.
*/
public
class
GcmPushSettingHelper
{
public
static
RealmResults
<
PublicSetting
>
queryForGcmPushEnabled
(
Realm
realm
)
{
return
realm
.
where
(
PublicSetting
.
class
)
.
equalTo
(
PublicSetting
.
ID
,
PublicSettingsConstants
.
Push
.
ENABLE
)
.
or
()
.
equalTo
(
PublicSetting
.
ID
,
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
)
.
findAll
();
}
public
static
boolean
isGcmPushEnabled
(
List
<
PublicSetting
>
results
)
{
return
isPushEnabled
(
results
)
&&
hasValidGcmConfig
(
results
);
}
private
static
boolean
isPushEnabled
(
List
<
PublicSetting
>
results
)
{
for
(
PublicSetting
setting
:
results
)
{
if
(
PublicSettingsConstants
.
Push
.
ENABLE
.
equals
(
setting
.
getId
()))
{
return
"true"
.
equals
(
setting
.
getValue
());
}
}
return
false
;
}
private
static
boolean
hasValidGcmConfig
(
List
<
PublicSetting
>
results
)
{
for
(
PublicSetting
setting
:
results
)
{
if
(
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
.
equals
(
setting
.
getId
()))
{
return
!
TextUtils
.
isEmpty
(
setting
.
getValue
());
}
}
return
false
;
}
}
app/src/main/java/chat/rocket/android/model/ServerConfig.java
View file @
b420af80
...
...
@@ -20,7 +20,6 @@ public class ServerConfig extends RealmObject {
public
static
final
String
STATE
=
"state"
;
public
static
final
String
SESSION
=
"session"
;
public
static
final
String
ERROR
=
"error"
;
public
static
final
String
SYNC_PUSH_TOKEN
=
"syncPushToken"
;
public
static
final
int
STATE_READY
=
0
;
public
static
final
int
STATE_CONNECTING
=
1
;
...
...
@@ -32,7 +31,6 @@ public class ServerConfig extends RealmObject {
private
int
state
;
private
String
session
;
private
String
error
;
private
boolean
syncPushToken
;
/**
* Log the server connection is lost due to some exception.
...
...
@@ -102,12 +100,4 @@ public class ServerConfig extends RealmObject {
public
void
setError
(
String
error
)
{
this
.
error
=
error
;
}
public
boolean
shouldSyncPushToken
()
{
return
syncPushToken
;
}
public
void
setSyncPushToken
(
boolean
syncPushToken
)
{
this
.
syncPushToken
=
syncPushToken
;
}
}
app/src/main/java/chat/rocket/android/model/internal/GcmPushRegistration.java
0 → 100644
View file @
b420af80
package
chat
.
rocket
.
android
.
model
.
internal
;
import
io.realm.Realm
;
import
io.realm.RealmObject
;
import
io.realm.RealmQuery
;
import
io.realm.annotations.PrimaryKey
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
chat.rocket.android.model.SyncState
;
/**
* just stores gcm registration status.
*/
public
class
GcmPushRegistration
extends
RealmObject
{
@SuppressWarnings
({
"PMD.ShortVariable"
})
private
static
final
String
ID
=
"dummyId"
;
public
static
final
String
SYNC_STATE
=
"syncState"
;
public
static
final
String
GCM_PUSH_ENABLED
=
"gcmPushEnabled"
;
private
static
final
int
DEFAULT_ID
=
0
;
@PrimaryKey
private
int
dummyId
;
private
int
syncState
;
private
boolean
gcmPushEnabled
;
public
boolean
isGcmPushEnabled
()
{
return
gcmPushEnabled
;
}
public
void
setGcmPushEnabled
(
boolean
gcmPushEnabled
)
{
this
.
gcmPushEnabled
=
gcmPushEnabled
;
}
public
int
getSyncState
()
{
return
syncState
;
}
public
void
setSyncState
(
int
syncState
)
{
this
.
syncState
=
syncState
;
}
public
static
GcmPushRegistration
updateGcmPushEnabled
(
Realm
realm
,
boolean
gcmPushEnabled
)
throws
JSONException
{
GcmPushRegistration
gcmPushRegistration
=
GcmPushRegistration
.
queryDefault
(
realm
).
findFirst
();
if
(
gcmPushRegistration
!=
null
&&
(
gcmPushRegistration
.
getSyncState
()
==
SyncState
.
NOT_SYNCED
||
gcmPushRegistration
.
getSyncState
()
==
SyncState
.
SYNCING
)
&&
gcmPushEnabled
==
gcmPushRegistration
.
isGcmPushEnabled
())
{
// omit duplicated request.
return
gcmPushRegistration
;
}
return
realm
.
createOrUpdateObjectFromJson
(
GcmPushRegistration
.
class
,
new
JSONObject
()
.
put
(
ID
,
DEFAULT_ID
)
.
put
(
SYNC_STATE
,
SyncState
.
NOT_SYNCED
)
.
put
(
GCM_PUSH_ENABLED
,
gcmPushEnabled
));
}
public
static
RealmQuery
<
GcmPushRegistration
>
queryDefault
(
Realm
realm
)
{
return
realm
.
where
(
GcmPushRegistration
.
class
).
equalTo
(
ID
,
DEFAULT_ID
);
}
}
app/src/main/java/chat/rocket/android/push/gcm/GCMIntentService.java
View file @
b420af80
package
chat
.
rocket
.
android
.
push
.
gcm
;
import
com.google.android.gms.gcm.GcmListenerService
;
import
android.annotation.SuppressLint
;
import
android.content.Context
;
import
android.content.res.Resources
;
import
android.os.Bundle
;
import
android.util.Log
;
import
com.google.android.gms.gcm.GcmListenerService
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
...
...
app/src/main/java/chat/rocket/android/push/gcm/GcmInstanceIDListenerService.java
View file @
b420af80
package
chat
.
rocket
.
android
.
push
.
gcm
;
import
android.content.Intent
;
import
com.google.android.gms.iid.InstanceIDListenerService
;
import
java.util.List
;
import
chat.rocket.android.helper.GcmPushSettingHelper
;
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.GcmPushRegistration
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmStore
;
...
...
@@ -15,48 +14,28 @@ public class GcmInstanceIDListenerService extends InstanceIDListenerService {
@Override
public
void
onTokenRefresh
()
{
List
<
ServerConfig
>
serverConfigs
=
getServerConfigs
();
updateSyncPushToken
(
serverConfigs
);
if
(!
shouldRefreshToken
(
serverConfigs
))
{
return
;
List
<
ServerConfig
>
serverConfigs
=
RealmStore
.
getDefault
()
.
executeTransactionForReadResults
(
realm
->
realm
.
where
(
ServerConfig
.
class
)
.
isNotNull
(
ServerConfig
.
ID
)
.
isNotNull
(
ServerConfig
.
HOSTNAME
)
.
findAll
());
for
(
ServerConfig
serverConfig
:
serverConfigs
)
{
RealmHelper
realmHelper
=
RealmStore
.
get
(
serverConfig
.
getServerConfigId
());
if
(
realmHelper
!=
null
)
{
updateGcmToken
(
realmHelper
);
}
}
Intent
intent
=
new
Intent
(
this
,
GcmRegistrationIntentService
.
class
);
startService
(
intent
);
}
private
List
<
ServerConfig
>
getServerConfigs
()
{
return
RealmStore
.
getDefault
().
executeTransactionForReadResults
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
findAll
());
}
private
void
updateSyncPushToken
(
List
<
ServerConfig
>
serverConfigs
)
{
final
RealmHelper
realmHelper
=
RealmStore
.
getDefault
();
for
(
final
ServerConfig
serverConfig
:
serverConfigs
)
{
final
RealmHelper
serverRealmHelper
=
RealmStore
.
getOrCreate
(
serverConfig
.
getServerConfigId
());
private
void
updateGcmToken
(
RealmHelper
realmHelper
)
{
final
List
<
PublicSetting
>
results
=
realmHelper
.
executeTransactionForReadResults
(
GcmPushSettingHelper:
:
queryForGcmPushEnabled
);
final
boolean
gcmPushEnabled
=
GcmPushSettingHelper
.
isGcmPushEnabled
(
results
);
boolean
isPushEnable
=
PublicSetting
.
getBoolean
(
serverRealmHelper
,
PublicSettingsConstants
.
Push
.
ENABLE
,
false
);
String
senderId
=
PublicSetting
.
getString
(
serverRealmHelper
,
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
,
""
).
trim
();
serverConfig
.
setSyncPushToken
(
isPushEnable
&&
!
""
.
equals
(
senderId
));
realmHelper
.
executeTransaction
(
realm
->
realm
.
copyToRealmOrUpdate
(
serverConfig
));
}
}
private
boolean
shouldRefreshToken
(
List
<
ServerConfig
>
serverConfigs
)
{
for
(
ServerConfig
serverConfig
:
serverConfigs
)
{
if
(
serverConfig
.
shouldSyncPushToken
())
{
return
true
;
}
if
(
gcmPushEnabled
)
{
realmHelper
.
executeTransaction
(
realm
->
GcmPushRegistration
.
updateGcmPushEnabled
(
realm
,
gcmPushEnabled
));
}
return
false
;
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/push/gcm/GcmRegistrationIntentService.java
deleted
100644 → 0
View file @
c020b195
package
chat
.
rocket
.
android
.
push
.
gcm
;
import
android.app.IntentService
;
import
android.content.Intent
;
import
com.google.android.gms.gcm.GoogleCloudMessaging
;
import
com.google.android.gms.iid.InstanceID
;
import
java.io.IOException
;
import
java.util.List
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.api.PushHelper
;
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.ddp.User
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmStore
;
public
class
GcmRegistrationIntentService
extends
IntentService
{
public
GcmRegistrationIntentService
()
{
super
(
"GcmRegistrationIntentService"
);
}
@Override
protected
void
onHandleIntent
(
Intent
intent
)
{
final
List
<
ServerConfig
>
serverConfigs
=
getServerConfigs
();
for
(
ServerConfig
serverConfig
:
serverConfigs
)
{
sendTokenTo
(
serverConfig
);
}
}
private
List
<
ServerConfig
>
getServerConfigs
()
{
return
RealmStore
.
getDefault
().
executeTransactionForReadResults
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
findAll
());
}
private
void
sendTokenTo
(
final
ServerConfig
serverConfig
)
{
if
(!
serverConfig
.
shouldSyncPushToken
())
{
return
;
}
final
RealmHelper
realmHelper
=
RealmStore
.
get
(
serverConfig
.
getServerConfigId
());
if
(
realmHelper
==
null
)
{
return
;
}
final
String
senderId
=
PublicSetting
.
getString
(
realmHelper
,
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
,
""
).
trim
();
if
(
""
.
equals
(
senderId
))
{
markRefreshAsDone
(
serverConfig
);
return
;
}
try
{
final
String
token
=
getToken
(
senderId
);
final
User
currentUser
=
realmHelper
.
executeTransactionForRead
(
realm
->
User
.
queryCurrentUser
(
realm
).
findFirst
());
new
PushHelper
(
serverConfig
.
getServerConfigId
()).
pushUpdate
(
RocketChatCache
.
getPushId
(
this
),
token
,
currentUser
!=
null
?
currentUser
.
getId
()
:
null
)
.
onSuccess
(
task
->
{
markRefreshAsDone
(
serverConfig
);
return
task
;
});
}
catch
(
Exception
e
)
{
}
}
private
String
getToken
(
String
senderId
)
throws
IOException
{
return
InstanceID
.
getInstance
(
this
)
.
getToken
(
senderId
,
GoogleCloudMessaging
.
INSTANCE_ID_SCOPE
,
null
);
}
private
void
markRefreshAsDone
(
ServerConfig
serverConfig
)
{
serverConfig
.
setSyncPushToken
(
false
);
RealmStore
.
getDefault
().
executeTransaction
(
realm
->
realm
.
copyToRealm
(
serverConfig
));
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/service/RocketChatWebSocketThread.java
View file @
b420af80
...
...
@@ -26,6 +26,7 @@ import chat.rocket.android.service.ddp.base.UserDataSubscriber;
import
chat.rocket.android.service.observer.CurrentUserObserver
;
import
chat.rocket.android.service.observer.FileUploadingToS3Observer
;
import
chat.rocket.android.service.observer.FileUploadingWithUfsObserver
;
import
chat.rocket.android.service.observer.GcmPushRegistrationObserver
;
import
chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver
;
import
chat.rocket.android.service.observer.LoadMessageProcedureObserver
;
import
chat.rocket.android.service.observer.MethodCallObserver
;
...
...
@@ -55,7 +56,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
ReactiveNotificationManager
.
class
,
FileUploadingToS3Observer
.
class
,
FileUploadingWithUfsObserver
.
class
,
PushSettingsObserver
.
class
PushSettingsObserver
.
class
,
GcmPushRegistrationObserver
.
class
};
private
final
Context
appContext
;
private
final
String
serverConfigId
;
...
...
app/src/main/java/chat/rocket/android/service/observer/CurrentUserObserver.java
View file @
b420af80
...
...
@@ -6,10 +6,8 @@ import io.realm.RealmResults;
import
java.util.ArrayList
;
import
java.util.List
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.api.DDPClientWrapper
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.api.PushHelper
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.ddp.User
;
import
chat.rocket.android.realm_helper.RealmHelper
;
...
...
@@ -22,7 +20,6 @@ import hugo.weaving.DebugLog;
*/
public
class
CurrentUserObserver
extends
AbstractModelObserver
<
User
>
{
private
final
MethodCallHelper
methodCall
;
private
final
PushHelper
pushHelper
;
private
boolean
currentUserExists
;
private
ArrayList
<
Registrable
>
listeners
;
...
...
@@ -30,7 +27,6 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
RealmHelper
realmHelper
,
DDPClientWrapper
ddpClient
)
{
super
(
context
,
hostname
,
realmHelper
,
ddpClient
);
methodCall
=
new
MethodCallHelper
(
realmHelper
,
ddpClient
);
pushHelper
=
new
PushHelper
(
realmHelper
,
ddpClient
);
currentUserExists
=
false
;
}
...
...
@@ -62,9 +58,6 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
final
String
userId
=
user
.
getId
();
// update push info
pushHelper
.
pushSetUser
(
RocketChatCache
.
getPushId
(
context
)).
continueWith
(
new
LogcatIfError
());
// get and observe Room subscriptions.
methodCall
.
getRoomSubscriptions
().
onSuccess
(
task
->
{
if
(
listeners
!=
null
)
{
...
...
app/src/main/java/chat/rocket/android/service/observer/GcmPushRegistrationObserver.java
0 → 100644
View file @
b420af80
package
chat
.
rocket
.
android
.
service
.
observer
;
import
com.google.android.gms.gcm.GoogleCloudMessaging
;
import
com.google.android.gms.iid.InstanceID
;
import
android.content.Context
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
java.io.IOException
;
import
java.util.List
;
import
bolts.Task
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.api.DDPClientWrapper
;
import
chat.rocket.android.api.RaixPushHelper
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.SyncState
;
import
chat.rocket.android.model.ddp.PublicSetting
;
import
chat.rocket.android.model.ddp.PublicSettingsConstants
;
import
chat.rocket.android.model.ddp.User
;
import
chat.rocket.android.model.internal.GcmPushRegistration
;
import
chat.rocket.android.realm_helper.RealmHelper
;
/**
* call raix:push-update if needed.
*/
public
class
GcmPushRegistrationObserver
extends
AbstractModelObserver
<
GcmPushRegistration
>
{
public
GcmPushRegistrationObserver
(
Context
context
,
String
hostname
,
RealmHelper
realmHelper
,
DDPClientWrapper
ddpClient
)
{
super
(
context
,
hostname
,
realmHelper
,
ddpClient
);
}
@Override
public
RealmResults
<
GcmPushRegistration
>
queryItems
(
Realm
realm
)
{
return
GcmPushRegistration
.
queryDefault
(
realm
)
.
equalTo
(
GcmPushRegistration
.
SYNC_STATE
,
SyncState
.
NOT_SYNCED
)
.
equalTo
(
GcmPushRegistration
.
GCM_PUSH_ENABLED
,
true
)
.
findAll
();
}
@Override
public
void
onUpdateResults
(
List
<
GcmPushRegistration
>
results
)
{
if
(
results
.
isEmpty
())
{
return
;
}
realmHelper
.
executeTransaction
(
realm
->
{
GcmPushRegistration
.
queryDefault
(
realm
).
findFirst
().
setSyncState
(
SyncState
.
SYNCING
);
return
null
;
}).
onSuccessTask
(
_task
->
registerGcmTokenForServer
()
).
onSuccessTask
(
_task
->
realmHelper
.
executeTransaction
(
realm
->
{
GcmPushRegistration
.
queryDefault
(
realm
).
findFirst
().
setSyncState
(
SyncState
.
SYNCED
);
return
null
;
})
).
continueWith
(
task
->
{
if
(
task
.
isFaulted
())
{
realmHelper
.
executeTransaction
(
realm
->
{
GcmPushRegistration
.
queryDefault
(
realm
).
findFirst
().
setSyncState
(
SyncState
.
FAILED
);
return
null
;
}).
continueWith
(
new
LogcatIfError
());
}
return
null
;
});
}
private
Task
<
Void
>
registerGcmTokenForServer
()
throws
IOException
{
final
String
senderId
=
PublicSetting
.
getString
(
realmHelper
,
PublicSettingsConstants
.
Push
.
GCM_PROJECT_NUMBER
,
""
).
trim
();
final
String
gcmToken
=
getGcmToken
(
senderId
);
final
User
currentUser
=
realmHelper
.
executeTransactionForRead
(
realm
->
User
.
queryCurrentUser
(
realm
).
findFirst
());
final
String
userId
=
currentUser
!=
null
?
currentUser
.
getId
()
:
null
;
final
String
pushId
=
RocketChatCache
.
getOrCreatePushId
(
context
);
return
new
RaixPushHelper
(
realmHelper
,
ddpClient
)
.
pushUpdate
(
pushId
,
gcmToken
,
userId
);
}
private
String
getGcmToken
(
String
senderId
)
throws
IOException
{
return
InstanceID
.
getInstance
(
context
)
.
getToken
(
senderId
,
GoogleCloudMessaging
.
INSTANCE_ID_SCOPE
,
null
);
}
}
app/src/main/java/chat/rocket/android/service/observer/PushSettingsObserver.java
View file @
b420af80
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.helper.GcmPushSettingHelper
;
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.model.internal.GcmPushRegistration
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmStore
;
public
class
PushSettingsObserver
extends
AbstractModelObserver
<
PublicSetting
>
{
...
...
@@ -24,61 +18,18 @@ public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
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
);
return
GcmPushSettingHelper
.
queryForGcmPushEnabled
(
realm
);
}
private
boolean
isPushEnable
(
List
<
PublicSetting
>
results
)
{
for
(
PublicSetting
setting
:
results
)
{
if
(
PublicSettingsConstants
.
Push
.
ENABLE
.
equals
(
setting
.
getId
()))
{
return
"true"
.
equals
(
setting
.
getValue
());
}
}
return
false
;
}
@Override
public
void
onUpdateResults
(
List
<
PublicSetting
>
results
)
{
boolean
gcmPushEnabled
=
GcmPushSettingHelper
.
isGcmPushEnabled
(
results
);
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
());
}
if
(
gcmPushEnabled
)
{
realmHelper
.
executeTransaction
(
realm
->
GcmPushRegistration
.
updateGcmPushEnabled
(
realm
,
gcmPushEnabled
));
}
return
false
;
}
}
app/src/main/java/chat/rocket/android/service/observer/SessionObserver.java
View file @
b420af80
...
...
@@ -5,7 +5,9 @@ import io.realm.Realm;
import
io.realm.RealmResults
;
import
java.util.List
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.api.DDPClientWrapper
;
import
chat.rocket.android.api.RaixPushHelper
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.internal.GetUsersOfRoomsProcedure
;
import
chat.rocket.android.model.internal.LoadMessageProcedure
;
...
...
@@ -20,6 +22,7 @@ import hugo.weaving.DebugLog;
*/
public
class
SessionObserver
extends
AbstractModelObserver
<
Session
>
{
private
final
StreamRoomMessageManager
streamNotifyMessage
;
private
final
RaixPushHelper
pushHelper
;
private
int
count
;
/**
...
...
@@ -31,6 +34,7 @@ public class SessionObserver extends AbstractModelObserver<Session> {
count
=
0
;
streamNotifyMessage
=
new
StreamRoomMessageManager
(
context
,
hostname
,
realmHelper
,
ddpClient
);
pushHelper
=
new
RaixPushHelper
(
realmHelper
,
ddpClient
);
}
@Override
...
...
@@ -65,6 +69,11 @@ public class SessionObserver extends AbstractModelObserver<Session> {
@DebugLog
private
void
onLogin
()
{
streamNotifyMessage
.
register
();
// update push info
pushHelper
.
pushSetUser
(
RocketChatCache
.
getOrCreatePushId
(
context
))
.
continueWith
(
new
LogcatIfError
());
}
@DebugLog
...
...
realm-helpers/src/main/java/chat/rocket/android/realm_helper/RealmHelper.java
View file @
b420af80
...
...
@@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView;
import
io.realm.Realm
;
import
io.realm.RealmConfiguration
;
import
io.realm.RealmObject
;
import
io.realm.RealmQuery
;
import
io.realm.RealmResults
;
import
org.json.JSONException
;
...
...
@@ -73,6 +74,19 @@ public class RealmHelper {
}
}
public
interface
Query
<
T
extends
RealmObject
>
{
RealmQuery
<
T
>
query
(
Realm
realm
);
}
public
<
T
extends
RealmObject
>
boolean
isObjectExists
(
Query
<
T
>
query
)
{
try
(
Realm
realm
=
instance
())
{
return
query
.
query
(
realm
).
count
()
>
0
;
}
catch
(
Exception
exception
)
{
RCLog
.
w
(
exception
);
return
false
;
}
}
private
boolean
shouldUseSync
()
{
// ref: realm-java:realm/realm-library/src/main/java/io/realm/AndroidNotifier.java
// #isAutoRefreshAvailable()
...
...
@@ -140,7 +154,7 @@ public class RealmHelper {
}
public
<
T
extends
RealmObject
>
RealmObjectObserver
<
T
>
createObjectObserver
(
Realm
ObjectObserv
er
.
Query
<
T
>
query
)
{
Realm
Help
er
.
Query
<
T
>
query
)
{
return
new
RealmObjectObserver
<
T
>(
this
,
query
);
}
...
...
realm-helpers/src/main/java/chat/rocket/android/realm_helper/RealmObjectObserver.java
View file @
b420af80
...
...
@@ -3,17 +3,16 @@ package chat.rocket.android.realm_helper;
import
io.realm.Realm
;
import
io.realm.RealmChangeListener
;
import
io.realm.RealmObject
;
import
io.realm.RealmQuery
;
import
io.realm.RealmResults
;
public
class
RealmObjectObserver
<
T
extends
RealmObject
>
extends
AbstractRealmResultsObserver
<
T
>
{
private
final
Query
<
T
>
query
;
private
final
RealmHelper
.
Query
<
T
>
query
;
private
OnUpdateListener
<
T
>
onUpdateListener
;
private
Impl
<
T
>
impl
;
private
String
previousResultString
;
/*package*/
RealmObjectObserver
(
RealmHelper
helper
,
Query
<
T
>
query
)
{
/*package*/
RealmObjectObserver
(
RealmHelper
helper
,
RealmHelper
.
Query
<
T
>
query
)
{
super
(
helper
);
this
.
query
=
query
;
setImpl
(
new
Impl
<
T
>());
...
...
@@ -54,10 +53,6 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
super
.
sub
();
}
public
interface
Query
<
T
extends
RealmObject
>
{
RealmQuery
<
T
>
query
(
Realm
realm
);
}
public
interface
OnUpdateListener
<
T
extends
RealmObject
>
{
void
onUpdateObject
(
T
element
);
}
...
...
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