Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cordova-plugin-local-notifications
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Nabil Sadki
cordova-plugin-local-notifications
Commits
fb993c31
Commit
fb993c31
authored
Jan 11, 2018
by
Sebastián Katzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored internal builder cache
parent
6137bc0b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
53 deletions
+55
-53
Builder.java
src/android/notification/Builder.java
+2
-24
Manager.java
src/android/notification/Manager.java
+1
-11
Notification.java
src/android/notification/Notification.java
+47
-10
Options.java
src/android/notification/Options.java
+5
-8
No files found.
src/android/notification/Builder.java
View file @
fb993c31
...
...
@@ -31,7 +31,6 @@ import android.support.v4.app.NotificationCompat;
import
android.support.v4.app.NotificationCompat.MessagingStyle.Message
;
import
android.support.v4.media.app.NotificationCompat.MediaStyle
;
import
android.support.v4.media.session.MediaSessionCompat
;
import
android.util.SparseArray
;
import
java.util.List
;
import
java.util.Random
;
...
...
@@ -49,9 +48,6 @@ import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE;
@SuppressWarnings
(
"Convert2Diamond"
)
public
final
class
Builder
{
// Cache for the builder instances
private
static
SparseArray
<
NotificationCompat
.
Builder
>
cache
=
null
;
// Application context passed by constructor
private
final
Context
context
;
...
...
@@ -153,7 +149,6 @@ public final class Builder {
}
if
(
options
.
isWithProgressBar
())
{
cacheBuilder
(
builder
);
builder
.
setProgress
(
options
.
getProgressMaxValue
(),
options
.
getProgressValue
(),
...
...
@@ -416,31 +411,14 @@ public final class Builder {
* Returns a cached builder instance or creates a new one.
*/
private
NotificationCompat
.
Builder
findOrCreateBuilder
()
{
NotificationCompat
.
Builder
builder
=
null
;
int
key
=
options
.
getId
();
if
(
cache
!=
null
)
{
builder
=
cache
.
get
(
key
);
}
NotificationCompat
.
Builder
builder
=
Notification
.
getCachedBuilder
(
key
);
if
(
builder
==
null
)
{
builder
=
new
NotificationCompat
.
Builder
(
context
,
Manager
.
CHANNEL_ID
);
builder
=
new
NotificationCompat
.
Builder
(
context
,
options
.
getChannel
()
);
}
return
builder
;
}
/**
* Caches the builder instance so it can be used later.
*
* @param builder The instance to cache.
*/
private
void
cacheBuilder
(
NotificationCompat
.
Builder
builder
)
{
if
(
cache
==
null
)
{
cache
=
new
SparseArray
<
NotificationCompat
.
Builder
>();
}
cache
.
put
(
options
.
getId
(),
builder
);
}
}
src/android/notification/Manager.java
View file @
fb993c31
...
...
@@ -50,6 +50,7 @@ import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERE
* state like triggered or scheduled. Offers shortcut ways to schedule,
* cancel or clear local notifications.
*/
@SuppressWarnings
(
"Convert2Diamond"
)
public
final
class
Manager
{
// TODO: temporary
...
...
@@ -285,17 +286,6 @@ public final class Manager {
}
/**
* If a notification with an ID exists.
*
* @param id Notification ID
*
* @return true if found.
*/
public
boolean
exist
(
int
id
)
{
return
get
(
id
)
!=
null
;
}
/**
* List of properties from all local notifications.
*/
public
List
<
JSONObject
>
getOptions
()
{
...
...
src/android/notification/Notification.java
View file @
fb993c31
...
...
@@ -33,6 +33,7 @@ import android.service.notification.StatusBarNotification;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.util.ArraySet
;
import
android.support.v4.util.Pair
;
import
android.util.SparseArray
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
...
...
@@ -55,6 +56,7 @@ import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN;
* Wrapper class around OS notification class. Handles basic operations
* like show, delete, cancel for a single local notification instance.
*/
@SuppressWarnings
(
"Convert2Diamond"
)
public
final
class
Notification
{
// Used to differ notifications by their life cycle state
...
...
@@ -74,6 +76,9 @@ public final class Notification {
// Key for private preferences
private
static
final
String
PREF_KEY_PID
=
"NOTIFICATION_PID"
;
// Cache for the builder instances
static
SparseArray
<
NotificationCompat
.
Builder
>
cache
=
null
;
// Application context passed by constructor
private
final
Context
context
;
...
...
@@ -233,15 +238,15 @@ public final class Notification {
*
* @param intent The intent to broadcast.
* @param cls The broadcast class.
*
* @return false if the receiver could not be invoked.
*/
private
boolean
trigger
(
Intent
intent
,
Class
<?>
cls
)
{
BroadcastReceiver
receiver
;
try
{
receiver
=
(
BroadcastReceiver
)
cls
.
newInstance
();
}
catch
(
InstantiationException
e
)
{
return
false
;
}
catch
(
IllegalAccessException
e
)
{
}
catch
(
InstantiationException
|
IllegalAccessException
e
)
{
return
false
;
}
...
...
@@ -254,10 +259,7 @@ public final class Notification {
*/
public
void
clear
()
{
getNotMgr
().
cancel
(
getId
());
if
(
isRepeating
())
return
;
if
(
isRepeating
())
return
;
unpersist
();
}
...
...
@@ -267,7 +269,8 @@ public final class Notification {
public
void
cancel
()
{
cancelScheduledAlarms
();
unpersist
();
getNotMgr
().
cancel
(
options
.
getId
());
getNotMgr
().
cancel
(
getId
());
clearCache
();
}
/**
...
...
@@ -302,9 +305,11 @@ public final class Notification {
* Present the local notification to user.
*/
public
void
show
()
{
if
(
builder
==
null
)
return
;
if
(
builder
==
null
)
return
;
if
(
options
.
isWithProgressBar
())
{
cacheBuilder
();
}
grantPermissionToPlaySoundFromExternal
();
getNotMgr
().
notify
(
getId
(),
builder
.
build
());
...
...
@@ -419,6 +424,38 @@ public final class Notification {
}
/**
* Caches the builder instance so it can be used later.
*/
private
void
cacheBuilder
()
{
if
(
cache
==
null
)
{
cache
=
new
SparseArray
<
NotificationCompat
.
Builder
>();
}
cache
.
put
(
getId
(),
builder
);
}
/**
* Find the cached builder instance.
*
* @param key The key under where to look for the builder.
*
* @return null if no builder instance could be found.
*/
static
NotificationCompat
.
Builder
getCachedBuilder
(
int
key
)
{
return
(
cache
!=
null
)
?
cache
.
get
(
key
)
:
null
;
}
/**
* Caches the builder instance so it can be used later.
*/
private
void
clearCache
()
{
if
(
cache
!=
null
)
{
cache
.
delete
(
getId
());
}
}
/**
* Shared private preferences for the application.
*/
private
SharedPreferences
getPrefs
(
String
key
)
{
...
...
src/android/notification/Options.java
View file @
fb993c31
...
...
@@ -54,6 +54,7 @@ import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
* possible option values. Class provides simple readers and more advanced
* methods to convert independent values into platform specific values.
*/
@SuppressWarnings
(
"Convert2Diamond"
)
public
final
class
Options
{
// Key name for bundled sound extra
...
...
@@ -91,7 +92,7 @@ public final class Options {
* @param context The application context.
* @param options The options dict map.
*/
public
Options
(
Context
context
,
JSONObject
options
)
{
Options
(
Context
context
,
JSONObject
options
)
{
this
.
context
=
context
;
this
.
options
=
options
;
this
.
assets
=
AssetUtil
.
getInstance
(
context
);
...
...
@@ -132,7 +133,7 @@ public final class Options {
*
* @return The notification ID as the string
*/
public
String
getIdentifier
()
{
String
getIdentifier
()
{
return
getId
().
toString
();
}
...
...
@@ -321,11 +322,7 @@ public final class Options {
int
aRGB
=
Integer
.
parseInt
(
hex
,
16
);
return
aRGB
+
0xFF000000
;
}
catch
(
NumberFormatException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchFieldException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalAccessException
e
)
{
}
catch
(
NumberFormatException
|
NoSuchFieldException
|
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
...
...
@@ -335,7 +332,7 @@ public final class Options {
/**
* Sound file path for the local notification.
*/
public
Uri
getSound
()
{
Uri
getSound
()
{
return
assets
.
parse
(
options
.
optString
(
"sound"
,
null
));
}
...
...
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