Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5171a4bf
authored
Dec 03, 2019
by
bachinger
Committed by
Oliver Woodman
Dec 06, 2019
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
reduce number of notification updates
Issue: #6657 PiperOrigin-RevId: 283563218
parent
e5957912
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
33 deletions
library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java
View file @
5171a4bf
...
@@ -25,6 +25,7 @@ import android.graphics.Bitmap;
...
@@ -25,6 +25,7 @@ import android.graphics.Bitmap;
import
android.graphics.Color
;
import
android.graphics.Color
;
import
android.os.Handler
;
import
android.os.Handler
;
import
android.os.Looper
;
import
android.os.Looper
;
import
android.os.Message
;
import
android.support.v4.media.session.MediaSessionCompat
;
import
android.support.v4.media.session.MediaSessionCompat
;
import
androidx.annotation.DrawableRes
;
import
androidx.annotation.DrawableRes
;
import
androidx.annotation.IntDef
;
import
androidx.annotation.IntDef
;
...
@@ -52,7 +53,6 @@ import java.util.Collections;
...
@@ -52,7 +53,6 @@ import java.util.Collections;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
org.checkerframework.checker.nullness.qual.RequiresNonNull
;
/**
/**
* A notification manager to start, update and cancel a media style notification reflecting the
* A notification manager to start, update and cancel a media style notification reflecting the
...
@@ -269,14 +269,7 @@ public class PlayerNotificationManager {
...
@@ -269,14 +269,7 @@ public class PlayerNotificationManager {
*/
*/
public
void
onBitmap
(
final
Bitmap
bitmap
)
{
public
void
onBitmap
(
final
Bitmap
bitmap
)
{
if
(
bitmap
!=
null
)
{
if
(
bitmap
!=
null
)
{
mainHandler
.
post
(
postUpdateNotificationBitmap
(
bitmap
,
notificationTag
);
()
->
{
if
(
player
!=
null
&&
notificationTag
==
currentNotificationTag
&&
isNotificationStarted
)
{
startOrUpdateNotification
(
bitmap
);
}
});
}
}
}
}
}
}
...
@@ -303,6 +296,11 @@ public class PlayerNotificationManager {
...
@@ -303,6 +296,11 @@ public class PlayerNotificationManager {
*/
*/
private
static
final
String
ACTION_DISMISS
=
"com.google.android.exoplayer.dismiss"
;
private
static
final
String
ACTION_DISMISS
=
"com.google.android.exoplayer.dismiss"
;
// Internal messages.
private
static
final
int
MSG_START_OR_UPDATE_NOTIFICATION
=
0
;
private
static
final
int
MSG_UPDATE_NOTIFICATION_BITMAP
=
1
;
/**
/**
* Visibility of notification on the lock screen. One of {@link
* Visibility of notification on the lock screen. One of {@link
* NotificationCompat#VISIBILITY_PRIVATE}, {@link NotificationCompat#VISIBILITY_PUBLIC} or {@link
* NotificationCompat#VISIBILITY_PRIVATE}, {@link NotificationCompat#VISIBILITY_PUBLIC} or {@link
...
@@ -598,7 +596,10 @@ public class PlayerNotificationManager {
...
@@ -598,7 +596,10 @@ public class PlayerNotificationManager {
controlDispatcher
=
new
DefaultControlDispatcher
();
controlDispatcher
=
new
DefaultControlDispatcher
();
window
=
new
Timeline
.
Window
();
window
=
new
Timeline
.
Window
();
instanceId
=
instanceIdCounter
++;
instanceId
=
instanceIdCounter
++;
mainHandler
=
new
Handler
(
Looper
.
getMainLooper
());
//noinspection Convert2MethodRef
mainHandler
=
Util
.
createHandler
(
Looper
.
getMainLooper
(),
msg
->
PlayerNotificationManager
.
this
.
handleMessage
(
msg
));
notificationManager
=
NotificationManagerCompat
.
from
(
context
);
notificationManager
=
NotificationManagerCompat
.
from
(
context
);
playerListener
=
new
PlayerListener
();
playerListener
=
new
PlayerListener
();
notificationBroadcastReceiver
=
new
NotificationBroadcastReceiver
();
notificationBroadcastReceiver
=
new
NotificationBroadcastReceiver
();
...
@@ -662,7 +663,7 @@ public class PlayerNotificationManager {
...
@@ -662,7 +663,7 @@ public class PlayerNotificationManager {
this
.
player
=
player
;
this
.
player
=
player
;
if
(
player
!=
null
)
{
if
(
player
!=
null
)
{
player
.
addListener
(
playerListener
);
player
.
addListener
(
playerListener
);
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
}
}
...
@@ -945,26 +946,17 @@ public class PlayerNotificationManager {
...
@@ -945,26 +946,17 @@ public class PlayerNotificationManager {
/** Forces an update of the notification if already started. */
/** Forces an update of the notification if already started. */
public
void
invalidate
()
{
public
void
invalidate
()
{
if
(
isNotificationStarted
&&
player
!=
null
)
{
if
(
isNotificationStarted
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
}
}
@Nullable
private
void
startOrUpdateNotification
(
Player
player
,
@Nullable
Bitmap
bitmap
)
{
private
Notification
startOrUpdateNotification
()
{
Assertions
.
checkNotNull
(
this
.
player
);
return
startOrUpdateNotification
(
/* bitmap= */
null
);
}
@RequiresNonNull
(
"player"
)
@Nullable
private
Notification
startOrUpdateNotification
(
@Nullable
Bitmap
bitmap
)
{
Player
player
=
this
.
player
;
boolean
ongoing
=
getOngoing
(
player
);
boolean
ongoing
=
getOngoing
(
player
);
builder
=
createNotification
(
player
,
builder
,
ongoing
,
bitmap
);
builder
=
createNotification
(
player
,
builder
,
ongoing
,
bitmap
);
if
(
builder
==
null
)
{
if
(
builder
==
null
)
{
stopNotification
(
/* dismissedByUser= */
false
);
stopNotification
(
/* dismissedByUser= */
false
);
return
null
;
return
;
}
}
Notification
notification
=
builder
.
build
();
Notification
notification
=
builder
.
build
();
notificationManager
.
notify
(
notificationId
,
notification
);
notificationManager
.
notify
(
notificationId
,
notification
);
...
@@ -975,16 +967,16 @@ public class PlayerNotificationManager {
...
@@ -975,16 +967,16 @@ public class PlayerNotificationManager {
notificationListener
.
onNotificationStarted
(
notificationId
,
notification
);
notificationListener
.
onNotificationStarted
(
notificationId
,
notification
);
}
}
}
}
NotificationListener
listener
=
notificationListener
;
@Nullable
NotificationListener
listener
=
notificationListener
;
if
(
listener
!=
null
)
{
if
(
listener
!=
null
)
{
listener
.
onNotificationPosted
(
notificationId
,
notification
,
ongoing
);
listener
.
onNotificationPosted
(
notificationId
,
notification
,
ongoing
);
}
}
return
notification
;
}
}
private
void
stopNotification
(
boolean
dismissedByUser
)
{
private
void
stopNotification
(
boolean
dismissedByUser
)
{
if
(
isNotificationStarted
)
{
if
(
isNotificationStarted
)
{
isNotificationStarted
=
false
;
isNotificationStarted
=
false
;
mainHandler
.
removeMessages
(
MSG_START_OR_UPDATE_NOTIFICATION
);
notificationManager
.
cancel
(
notificationId
);
notificationManager
.
cancel
(
notificationId
);
context
.
unregisterReceiver
(
notificationBroadcastReceiver
);
context
.
unregisterReceiver
(
notificationBroadcastReceiver
);
if
(
notificationListener
!=
null
)
{
if
(
notificationListener
!=
null
)
{
...
@@ -1261,6 +1253,37 @@ public class PlayerNotificationManager {
...
@@ -1261,6 +1253,37 @@ public class PlayerNotificationManager {
&&
player
.
getPlayWhenReady
();
&&
player
.
getPlayWhenReady
();
}
}
private
void
postStartOrUpdateNotification
()
{
if
(!
mainHandler
.
hasMessages
(
MSG_START_OR_UPDATE_NOTIFICATION
))
{
mainHandler
.
sendEmptyMessage
(
MSG_START_OR_UPDATE_NOTIFICATION
);
}
}
private
void
postUpdateNotificationBitmap
(
Bitmap
bitmap
,
int
notificationTag
)
{
mainHandler
.
obtainMessage
(
MSG_UPDATE_NOTIFICATION_BITMAP
,
notificationTag
,
C
.
INDEX_UNSET
/* ignored */
,
bitmap
)
.
sendToTarget
();
}
private
boolean
handleMessage
(
Message
msg
)
{
switch
(
msg
.
what
)
{
case
MSG_START_OR_UPDATE_NOTIFICATION:
if
(
player
!=
null
)
{
startOrUpdateNotification
(
player
,
/* bitmap= */
null
);
}
break
;
case
MSG_UPDATE_NOTIFICATION_BITMAP:
if
(
player
!=
null
&&
isNotificationStarted
&&
currentNotificationTag
==
msg
.
arg1
)
{
startOrUpdateNotification
(
player
,
(
Bitmap
)
msg
.
obj
);
}
break
;
default
:
return
false
;
}
return
true
;
}
private
static
Map
<
String
,
NotificationCompat
.
Action
>
createPlaybackActions
(
private
static
Map
<
String
,
NotificationCompat
.
Action
>
createPlaybackActions
(
Context
context
,
int
instanceId
)
{
Context
context
,
int
instanceId
)
{
Map
<
String
,
NotificationCompat
.
Action
>
actions
=
new
HashMap
<>();
Map
<
String
,
NotificationCompat
.
Action
>
actions
=
new
HashMap
<>();
...
@@ -1326,37 +1349,37 @@ public class PlayerNotificationManager {
...
@@ -1326,37 +1349,37 @@ public class PlayerNotificationManager {
@Override
@Override
public
void
onPlayerStateChanged
(
boolean
playWhenReady
,
@Player
.
State
int
playbackState
)
{
public
void
onPlayerStateChanged
(
boolean
playWhenReady
,
@Player
.
State
int
playbackState
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
@Override
@Override
public
void
onIsPlayingChanged
(
boolean
isPlaying
)
{
public
void
onIsPlayingChanged
(
boolean
isPlaying
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
@Override
@Override
public
void
onTimelineChanged
(
Timeline
timeline
,
int
reason
)
{
public
void
onTimelineChanged
(
Timeline
timeline
,
int
reason
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
@Override
@Override
public
void
onPlaybackParametersChanged
(
PlaybackParameters
playbackParameters
)
{
public
void
onPlaybackParametersChanged
(
PlaybackParameters
playbackParameters
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
@Override
@Override
public
void
onPositionDiscontinuity
(
int
reason
)
{
public
void
onPositionDiscontinuity
(
int
reason
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
@Override
@Override
public
void
onRepeatModeChanged
(
@Player
.
RepeatMode
int
repeatMode
)
{
public
void
onRepeatModeChanged
(
@Player
.
RepeatMode
int
repeatMode
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
@Override
@Override
public
void
onShuffleModeEnabledChanged
(
boolean
shuffleModeEnabled
)
{
public
void
onShuffleModeEnabledChanged
(
boolean
shuffleModeEnabled
)
{
s
tartOrUpdateNotification
();
postS
tartOrUpdateNotification
();
}
}
}
}
...
...
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