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
500b0e78
Commit
500b0e78
authored
Dec 13, 2016
by
Yusuke Iwaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add NotificationItem model.
parent
8e0983e2
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
266 additions
and
71 deletions
+266
-71
RoomSubscription.java
.../java/chat/rocket/android/model/ddp/RoomSubscription.java
+30
-0
NotificationItem.java
.../chat/rocket/android/model/internal/NotificationItem.java
+73
-0
RocketChatWebSocketThread.java
...hat/rocket/android/service/RocketChatWebSocketThread.java
+2
-2
StreamNotifyUserSubscriptionsChanged.java
...vice/ddp/stream/StreamNotifyUserSubscriptionsChanged.java
+6
-0
CurrentUserObserver.java
.../rocket/android/service/observer/CurrentUserObserver.java
+2
-1
NotificationItemObserver.java
...et/android/service/observer/NotificationItemObserver.java
+121
-0
ReactiveNotificationCancelManager.java
...d/service/observer/ReactiveNotificationCancelManager.java
+0
-38
ReactiveNotificationManager.java
...android/service/observer/ReactiveNotificationManager.java
+32
-30
No files found.
app/src/main/java/chat/rocket/android/model/ddp/RoomSubscription.java
View file @
500b0e78
...
...
@@ -23,6 +23,8 @@ public class RoomSubscription extends RealmObject {
private
boolean
open
;
private
boolean
alert
;
private
int
unread
;
private
long
_updatedAt
;
private
long
ls
;
//last seen.
public
String
get_id
()
{
return
_id
;
...
...
@@ -80,7 +82,35 @@ public class RoomSubscription extends RealmObject {
this
.
unread
=
unread
;
}
public
long
get_updatedAt
()
{
return
_updatedAt
;
}
public
void
set_updatedAt
(
long
_updatedAt
)
{
this
.
_updatedAt
=
_updatedAt
;
}
public
long
getLs
()
{
return
ls
;
}
public
void
setLs
(
long
ls
)
{
this
.
ls
=
ls
;
}
public
static
JSONObject
customizeJson
(
JSONObject
roomSubscriptionJson
)
throws
JSONException
{
if
(!
roomSubscriptionJson
.
isNull
(
"ls"
))
{
long
ls
=
roomSubscriptionJson
.
getJSONObject
(
"ls"
).
getLong
(
"$date"
);
roomSubscriptionJson
.
remove
(
"ls"
);
roomSubscriptionJson
.
put
(
"ls"
,
ls
);
}
if
(!
roomSubscriptionJson
.
isNull
(
"_updatedAt"
))
{
long
updatedAt
=
roomSubscriptionJson
.
getJSONObject
(
"_updatedAt"
).
getLong
(
"$date"
);
roomSubscriptionJson
.
remove
(
"_updatedAt"
);
roomSubscriptionJson
.
put
(
"_updatedAt"
,
updatedAt
);
}
return
roomSubscriptionJson
;
}
}
app/src/main/java/chat/rocket/android/model/internal/NotificationItem.java
0 → 100644
View file @
500b0e78
package
chat
.
rocket
.
android
.
model
.
internal
;
import
io.realm.RealmObject
;
import
io.realm.annotations.PrimaryKey
;
/**
* View model for notification.
*/
public
class
NotificationItem
extends
RealmObject
{
@PrimaryKey
private
String
roomId
;
private
String
title
;
private
String
description
;
private
int
unreadCount
;
private
String
senderName
;
private
long
contentUpdatedAt
;
//subscription._updatedAt
private
long
lastSeenAt
;
//max(notification dismissed, subscription.ls)
public
String
getRoomId
()
{
return
roomId
;
}
public
void
setRoomId
(
String
roomId
)
{
this
.
roomId
=
roomId
;
}
public
String
getTitle
()
{
return
title
;
}
public
void
setTitle
(
String
title
)
{
this
.
title
=
title
;
}
public
String
getDescription
()
{
return
description
;
}
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
public
int
getUnreadCount
()
{
return
unreadCount
;
}
public
void
setUnreadCount
(
int
unreadCount
)
{
this
.
unreadCount
=
unreadCount
;
}
public
String
getSenderName
()
{
return
senderName
;
}
public
void
setSenderName
(
String
senderName
)
{
this
.
senderName
=
senderName
;
}
public
long
getContentUpdatedAt
()
{
return
contentUpdatedAt
;
}
public
void
setContentUpdatedAt
(
long
contentUpdatedAt
)
{
this
.
contentUpdatedAt
=
contentUpdatedAt
;
}
public
long
getLastSeenAt
()
{
return
lastSeenAt
;
}
public
void
setLastSeenAt
(
long
lastSeenAt
)
{
this
.
lastSeenAt
=
lastSeenAt
;
}
}
app/src/main/java/chat/rocket/android/service/RocketChatWebSocketThread.java
View file @
500b0e78
...
...
@@ -22,7 +22,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.
ReactiveNotificationCancelManag
er
;
import
chat.rocket.android.service.observer.
NotificationItemObserv
er
;
import
chat.rocket.android.service.observer.ReactiveNotificationManager
;
import
chat.rocket.android.service.observer.SessionObserver
;
import
chat.rocket.android.service.observer.TokenLoginObserver
;
...
...
@@ -49,7 +49,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
NewMessageObserver
.
class
,
CurrentUserObserver
.
class
,
ReactiveNotificationManager
.
class
,
ReactiveNotificationCancelManag
er
.
class
NotificationItemObserv
er
.
class
};
private
final
Context
appContext
;
private
final
String
serverConfigId
;
...
...
app/src/main/java/chat/rocket/android/service/ddp/stream/StreamNotifyUserSubscriptionsChanged.java
View file @
500b0e78
...
...
@@ -5,6 +5,8 @@ import chat.rocket.android.api.DDPClientWraper;
import
chat.rocket.android.model.ddp.RoomSubscription
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
io.realm.RealmObject
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
public
class
StreamNotifyUserSubscriptionsChanged
extends
AbstractStreamNotifyUserEventSubscriber
{
public
StreamNotifyUserSubscriptionsChanged
(
Context
context
,
String
hostname
,
...
...
@@ -20,6 +22,10 @@ public class StreamNotifyUserSubscriptionsChanged extends AbstractStreamNotifyUs
return
RoomSubscription
.
class
;
}
@Override
protected
JSONObject
customizeFieldJson
(
JSONObject
json
)
throws
JSONException
{
return
RoomSubscription
.
customizeJson
(
super
.
customizeFieldJson
(
json
));
}
@Override
protected
String
getPrimaryKeyForModel
()
{
return
"rid"
;
}
...
...
app/src/main/java/chat/rocket/android/service/observer/CurrentUserObserver.java
View file @
500b0e78
...
...
@@ -3,6 +3,7 @@ package chat.rocket.android.service.observer;
import
android.content.Context
;
import
chat.rocket.android.api.DDPClientWraper
;
import
chat.rocket.android.api.MethodCallHelper
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.model.ddp.User
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.service.Registerable
;
...
...
@@ -65,7 +66,7 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
listeners
.
add
(
listener
);
}
return
null
;
});
})
.
continueWith
(
new
LogcatIfError
())
;
}
@DebugLog
...
...
app/src/main/java/chat/rocket/android/service/observer/NotificationItemObserver.java
0 → 100644
View file @
500b0e78
package
chat
.
rocket
.
android
.
service
.
observer
;
import
android.app.Notification
;
import
android.app.PendingIntent
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.graphics.Bitmap
;
import
android.support.annotation.NonNull
;
import
android.support.annotation.Nullable
;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.support.v4.content.ContextCompat
;
import
bolts.Task
;
import
chat.rocket.android.R
;
import
chat.rocket.android.activity.MainActivity
;
import
chat.rocket.android.api.DDPClientWraper
;
import
chat.rocket.android.helper.Avatar
;
import
chat.rocket.android.helper.TextUtils
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.model.internal.NotificationItem
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmStore
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
java.util.List
;
/**
* observes NotificationItem and notify/cancel notification.
*/
public
class
NotificationItemObserver
extends
AbstractModelObserver
<
NotificationItem
>
{
public
NotificationItemObserver
(
Context
context
,
String
hostname
,
RealmHelper
realmHelper
,
DDPClientWraper
ddpClient
)
{
super
(
context
,
hostname
,
realmHelper
,
ddpClient
);
}
@Override
public
RealmResults
<
NotificationItem
>
queryItems
(
Realm
realm
)
{
return
realm
.
where
(
NotificationItem
.
class
).
findAll
();
}
@Override
public
void
onUpdateResults
(
List
<
NotificationItem
>
results
)
{
if
(
results
.
isEmpty
())
{
return
;
}
for
(
NotificationItem
item
:
results
)
{
final
String
notificationId
=
item
.
getRoomId
();
if
(
item
.
getUnreadCount
()
>
0
&&
item
.
getContentUpdatedAt
()
>
item
.
getLastSeenAt
())
{
generateNotificationFor
(
item
)
.
onSuccess
(
task
->
{
Notification
notification
=
task
.
getResult
();
if
(
notification
!=
null
)
{
NotificationManagerCompat
.
from
(
context
)
.
notify
(
notificationId
.
hashCode
(),
notification
);
}
return
null
;
});
}
else
{
NotificationManagerCompat
.
from
(
context
).
cancel
(
notificationId
.
hashCode
());
}
}
}
private
Task
<
Notification
>
generateNotificationFor
(
NotificationItem
item
)
{
final
String
username
=
item
.
getSenderName
();
final
String
roomId
=
item
.
getRoomId
();
final
String
title
=
item
.
getTitle
();
final
String
description
=
TextUtils
.
or
(
item
.
getDescription
(),
""
).
toString
();
final
int
unreadCount
=
item
.
getUnreadCount
();
if
(
TextUtils
.
isEmpty
(
username
))
{
return
Task
.
forResult
(
generateNotification
(
roomId
,
title
,
description
,
unreadCount
,
null
));
}
int
size
=
context
.
getResources
().
getDimensionPixelSize
(
R
.
dimen
.
notification_avatar_size
);
return
new
Avatar
(
hostname
,
username
).
getBitmap
(
context
,
size
)
.
continueWithTask
(
task
->
{
Bitmap
icon
=
task
.
isFaulted
()
?
null
:
task
.
getResult
();
final
Notification
notification
=
generateNotification
(
roomId
,
title
,
description
,
unreadCount
,
icon
);
return
Task
.
forResult
(
notification
);
});
}
private
Notification
generateNotification
(
String
roomId
,
String
title
,
@NonNull
String
description
,
int
unreadCount
,
@Nullable
Bitmap
icon
)
{
Intent
intent
=
new
Intent
(
context
,
MainActivity
.
class
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_REORDER_TO_FRONT
|
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
);
ServerConfig
config
=
RealmStore
.
getDefault
().
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
"hostname"
,
hostname
).
findFirst
());
if
(
config
!=
null
)
{
intent
.
putExtra
(
"serverConfigId"
,
config
.
getServerConfigId
());
intent
.
putExtra
(
"roomId"
,
roomId
);
}
PendingIntent
pendingIntent
=
PendingIntent
.
getActivity
(
context
.
getApplicationContext
(),
(
int
)
(
System
.
currentTimeMillis
()
%
Integer
.
MAX_VALUE
),
intent
,
PendingIntent
.
FLAG_ONE_SHOT
);
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
context
)
.
setContentTitle
(
title
)
.
setContentText
(
description
)
.
setNumber
(
unreadCount
)
.
setColor
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
colorPrimary
))
.
setSmallIcon
(
R
.
drawable
.
rocket_chat_notification_24dp
)
.
setContentIntent
(
pendingIntent
);
if
(
icon
!=
null
)
{
builder
.
setLargeIcon
(
icon
);
}
if
(
description
.
length
()
>
20
)
{
return
new
NotificationCompat
.
BigTextStyle
(
builder
)
.
bigText
(
description
)
.
build
();
}
else
{
return
builder
.
build
();
}
}
}
app/src/main/java/chat/rocket/android/service/observer/ReactiveNotificationCancelManager.java
deleted
100644 → 0
View file @
8e0983e2
package
chat
.
rocket
.
android
.
service
.
observer
;
import
android.content.Context
;
import
android.support.v4.app.NotificationManagerCompat
;
import
chat.rocket.android.api.DDPClientWraper
;
import
chat.rocket.android.model.ddp.RoomSubscription
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
hugo.weaving.DebugLog
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
java.util.List
;
/**
* observing room subscriptions with unread>0.
*/
public
class
ReactiveNotificationCancelManager
extends
AbstractModelObserver
<
RoomSubscription
>
{
public
ReactiveNotificationCancelManager
(
Context
context
,
String
hostname
,
RealmHelper
realmHelper
,
DDPClientWraper
ddpClient
)
{
super
(
context
,
hostname
,
realmHelper
,
ddpClient
);
}
@Override
public
RealmResults
<
RoomSubscription
>
queryItems
(
Realm
realm
)
{
return
realm
.
where
(
RoomSubscription
.
class
)
.
equalTo
(
"open"
,
true
)
.
equalTo
(
"unread"
,
0
)
.
findAll
();
}
@DebugLog
@Override
public
void
onUpdateResults
(
List
<
RoomSubscription
>
roomSubscriptions
)
{
// TODO implement!
for
(
RoomSubscription
roomSubscription
:
roomSubscriptions
)
{
final
String
roomId
=
roomSubscription
.
getRid
();
NotificationManagerCompat
.
from
(
context
).
cancel
(
roomId
.
hashCode
());
}
}
}
app/src/main/java/chat/rocket/android/service/observer/ReactiveNotificationManager.java
View file @
500b0e78
package
chat
.
rocket
.
android
.
service
.
observer
;
import
android.app.Notification
;
import
android.app.PendingIntent
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.support.v4.content.ContextCompat
;
import
chat.rocket.android.R
;
import
chat.rocket.android.activity.MainActivity
;
import
chat.rocket.android.api.DDPClientWraper
;
import
chat.rocket.android.model.ServerConfig
;
import
chat.rocket.android.helper.LogcatIfError
;
import
chat.rocket.android.log.RCLog
;
import
chat.rocket.android.model.ddp.RoomSubscription
;
import
chat.rocket.android.model.internal.NotificationItem
;
import
chat.rocket.android.realm_helper.RealmHelper
;
import
chat.rocket.android.realm_helper.RealmStore
;
import
hugo.weaving.DebugLog
;
import
io.realm.Realm
;
import
io.realm.RealmResults
;
import
java.util.List
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
/**
* observing room subscriptions with unread>0.
...
...
@@ -31,7 +27,6 @@ public class ReactiveNotificationManager extends AbstractModelObserver<RoomSubsc
@Override
public
RealmResults
<
RoomSubscription
>
queryItems
(
Realm
realm
)
{
return
realm
.
where
(
RoomSubscription
.
class
)
.
equalTo
(
"open"
,
true
)
.
greaterThan
(
"unread"
,
0
)
.
findAll
();
}
...
...
@@ -39,30 +34,37 @@ public class ReactiveNotificationManager extends AbstractModelObserver<RoomSubsc
@Override
public
void
onUpdateResults
(
List
<
RoomSubscription
>
roomSubscriptions
)
{
// TODO implement!
JSONArray
notifications
=
new
JSONArray
();
for
(
RoomSubscription
roomSubscription
:
roomSubscriptions
)
{
final
String
roomId
=
roomSubscription
.
getRid
();
NotificationItem
item
=
realmHelper
.
executeTransactionForRead
(
realm
->
realm
.
where
(
NotificationItem
.
class
).
equalTo
(
"roomId"
,
roomId
).
findFirst
());
Intent
intent
=
new
Intent
(
context
,
MainActivity
.
class
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_REORDER_TO_FRONT
|
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
);
ServerConfig
config
=
RealmStore
.
getDefault
().
executeTransactionForRead
(
realm
->
realm
.
where
(
ServerConfig
.
class
).
equalTo
(
"hostname"
,
hostname
).
findFirst
());
if
(
config
!=
null
)
{
intent
.
putExtra
(
"serverConfigId"
,
config
.
getServerConfigId
());
intent
.
putExtra
(
"roomId"
,
roomId
);
}
PendingIntent
pendingIntent
=
PendingIntent
.
getActivity
(
context
.
getApplicationContext
(),
(
int
)
(
System
.
currentTimeMillis
()
%
Integer
.
MAX_VALUE
),
intent
,
PendingIntent
.
FLAG_ONE_SHOT
);
long
lastSeenAt
=
Math
.
max
(
item
!=
null
?
item
.
getLastSeenAt
()
:
0
,
roomSubscription
.
getLs
());
try
{
JSONObject
notification
=
new
JSONObject
()
.
put
(
"roomId"
,
roomSubscription
.
getRid
())
.
put
(
"title"
,
roomSubscription
.
getName
())
.
put
(
"description"
,
"new message"
)
.
put
(
"unreadCount"
,
roomSubscription
.
getUnread
())
.
put
(
"contentUpdatedAt"
,
roomSubscription
.
get_updatedAt
())
.
put
(
"lastSeenAt"
,
lastSeenAt
);
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
context
)
.
setContentTitle
(
roomSubscription
.
getName
())
.
setColor
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
colorPrimary
))
.
setSmallIcon
(
R
.
drawable
.
rocket_chat_notification_24dp
)
.
setContentIntent
(
pendingIntent
);
if
(
RoomSubscription
.
TYPE_DIRECT_MESSAGE
.
equals
(
roomSubscription
.
getT
()))
{
notification
.
put
(
"senderName"
,
roomSubscription
.
getName
());
}
else
{
notification
.
put
(
"senderName"
,
JSONObject
.
NULL
);
}
Notification
notification
=
builder
.
build
();
NotificationManagerCompat
.
from
(
context
).
notify
(
roomId
.
hashCode
(),
notification
);
notifications
.
put
(
notification
);
}
catch
(
JSONException
exception
)
{
RCLog
.
w
(
exception
);
}
}
realmHelper
.
executeTransaction
(
realm
->
{
realm
.
createOrUpdateAllFromJson
(
NotificationItem
.
class
,
notifications
);
return
null
;
}).
continueWith
(
new
LogcatIfError
());
}
}
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