Skip to content

Commit 3e0c3fc

Browse files
fix/lint
1 parent 98a6172 commit 3e0c3fc

File tree

1 file changed

+118
-58
lines changed

1 file changed

+118
-58
lines changed

src/local-notifications.android.ts

Lines changed: 118 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
import * as app from "tns-core-modules/application";
2-
import * as utils from "tns-core-modules/utils/utils";
1+
import * as app from 'tns-core-modules/application';
2+
import * as utils from 'tns-core-modules/utils/utils';
33
import {
44
LocalNotificationsApi,
55
LocalNotificationsCommon,
66
ReceivedNotification,
77
ScheduleInterval,
88
ScheduleOptions
9-
} from "./local-notifications-common";
9+
} from './local-notifications-common';
1010

1111
declare const android, com, global: any;
1212

13-
const NotificationManagerCompatPackageName = useAndroidX() ? global.androidx.core.app : android.support.v4.app;
13+
const NotificationManagerCompatPackageName = useAndroidX()
14+
? global.androidx.core.app
15+
: android.support.v4.app;
1416

15-
function useAndroidX () {
17+
function useAndroidX() {
1618
return global.androidx && global.androidx.appcompat;
1719
}
1820

1921
(() => {
2022
const registerLifecycleEvents = () => {
21-
com.telerik.localnotifications.LifecycleCallbacks.registerCallbacks(app.android.nativeApp);
23+
com.telerik.localnotifications.LifecycleCallbacks.registerCallbacks(
24+
app.android.nativeApp
25+
);
2226
};
2327

2428
// Hook on the application events
@@ -29,47 +33,75 @@ function useAndroidX () {
2933
}
3034
})();
3135

32-
export class LocalNotificationsImpl extends LocalNotificationsCommon implements LocalNotificationsApi {
33-
34-
private static IS_GTE_LOLLIPOP: boolean = android.os.Build.VERSION.SDK_INT >= 21;
36+
export class LocalNotificationsImpl extends LocalNotificationsCommon
37+
implements LocalNotificationsApi {
38+
private static IS_GTE_LOLLIPOP: boolean =
39+
android.os.Build.VERSION.SDK_INT >= 21;
3540

3641
private static getInterval(interval: ScheduleInterval): number {
37-
if (interval === "second") {
42+
if (interval === 'second') {
3843
return 1000; // it's in ms
39-
} else if (interval === "minute") {
44+
} else if (interval === 'minute') {
4045
return android.app.AlarmManager.INTERVAL_FIFTEEN_MINUTES / 15;
41-
} else if (interval === "hour") {
46+
} else if (interval === 'hour') {
4247
return android.app.AlarmManager.INTERVAL_HOUR;
43-
} else if (interval === "day") {
48+
} else if (interval === 'day') {
4449
return android.app.AlarmManager.INTERVAL_DAY;
45-
} else if (interval === "week") {
50+
} else if (interval === 'week') {
4651
return android.app.AlarmManager.INTERVAL_DAY * 7;
47-
} else if (interval === "month") {
52+
} else if (interval === 'month') {
4853
return android.app.AlarmManager.INTERVAL_DAY * 31; // well that's almost accurate
49-
} else if (interval === "year") {
54+
} else if (interval === 'year') {
5055
return android.app.AlarmManager.INTERVAL_DAY * 365; // same here
5156
} else {
5257
return undefined;
5358
}
5459
}
5560

56-
private static getIcon(context: any /* android.content.Context */, resources: any, iconLocation?: string): string {
61+
private static getIcon(
62+
context: any /* android.content.Context */,
63+
resources: any,
64+
iconLocation?: string
65+
): string {
5766
const packageName: string = context.getApplicationInfo().packageName;
58-
return iconLocation
59-
&& iconLocation.indexOf(utils.RESOURCE_PREFIX) === 0
60-
&& resources.getIdentifier(iconLocation.substr(utils.RESOURCE_PREFIX.length), "drawable", packageName)
61-
|| (LocalNotificationsImpl.IS_GTE_LOLLIPOP && resources.getIdentifier("ic_stat_notify_silhouette", "drawable", packageName))
62-
|| resources.getIdentifier("ic_stat_notify", "drawable", packageName)
63-
|| context.getApplicationInfo().icon;
67+
return (
68+
(iconLocation &&
69+
iconLocation.indexOf(utils.RESOURCE_PREFIX) === 0 &&
70+
resources.getIdentifier(
71+
iconLocation.substr(utils.RESOURCE_PREFIX.length),
72+
'drawable',
73+
packageName
74+
)) ||
75+
(LocalNotificationsImpl.IS_GTE_LOLLIPOP &&
76+
resources.getIdentifier(
77+
'ic_stat_notify_silhouette',
78+
'drawable',
79+
packageName
80+
)) ||
81+
resources.getIdentifier('ic_stat_notify', 'drawable', packageName) ||
82+
context.getApplicationInfo().icon
83+
);
6484
}
6585

6686
private static cancelById(id: number): void {
6787
const context = utils.ad.getApplicationContext();
68-
const notificationIntent = new android.content.Intent(context, com.telerik.localnotifications.NotificationAlarmReceiver.class).setAction("" + id);
69-
const pendingIntent = android.app.PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
70-
const alarmManager = context.getSystemService(android.content.Context.ALARM_SERVICE);
88+
const notificationIntent = new android.content.Intent(
89+
context,
90+
com.telerik.localnotifications.NotificationAlarmReceiver.class
91+
).setAction('' + id);
92+
const pendingIntent = android.app.PendingIntent.getBroadcast(
93+
context,
94+
0,
95+
notificationIntent,
96+
0
97+
);
98+
const alarmManager = context.getSystemService(
99+
android.content.Context.ALARM_SERVICE
100+
);
71101
alarmManager.cancel(pendingIntent);
72-
const notificationManager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
102+
const notificationManager = context.getSystemService(
103+
android.content.Context.NOTIFICATION_SERVICE
104+
);
73105
notificationManager.cancel(id);
74106

75107
com.telerik.localnotifications.Store.remove(context, id);
@@ -80,10 +112,12 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
80112
try {
81113
// nothing to do on this platform
82114
const context = utils.ad.getApplicationContext();
83-
const hasPermission=NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).areNotificationsEnabled();
115+
const hasPermission = NotificationManagerCompatPackageName.NotificationManagerCompat.from(
116+
context
117+
).areNotificationsEnabled();
84118
resolve(hasPermission);
85119
} catch (ex) {
86-
console.log("Error in LocalNotifications.hasPermission: " + ex);
120+
console.log('Error in LocalNotifications.hasPermission: ' + ex);
87121
reject(ex);
88122
}
89123
});
@@ -95,45 +129,53 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
95129
// nothing to do on this platform
96130
resolve(true);
97131
} catch (ex) {
98-
console.log("Error in LocalNotifications.requestPermission: " + ex);
132+
console.log('Error in LocalNotifications.requestPermission: ' + ex);
99133
reject(ex);
100134
}
101135
});
102136
}
103137

104-
addOnMessageReceivedCallback(onReceived: (data: ReceivedNotification) => void): Promise<any> {
138+
addOnMessageReceivedCallback(
139+
onReceived: (data: ReceivedNotification) => void
140+
): Promise<any> {
105141
return new Promise((resolve, reject) => {
106142
try {
107143
// note that this is ONLY triggered when the user clicked the notification in the statusbar
108144
com.telerik.localnotifications.LocalNotificationsPlugin.setOnMessageReceivedCallback(
109-
new com.telerik.localnotifications.LocalNotificationsPluginListener({
110-
success: notification => {
111-
onReceived(JSON.parse(notification));
112-
}
113-
})
145+
new com.telerik.localnotifications.LocalNotificationsPluginListener({
146+
success: notification => {
147+
onReceived(JSON.parse(notification));
148+
}
149+
})
114150
);
115151
resolve();
116152
} catch (ex) {
117-
console.log("Error in LocalNotifications.addOnMessageReceivedCallback: " + ex);
153+
console.log(
154+
'Error in LocalNotifications.addOnMessageReceivedCallback: ' + ex
155+
);
118156
reject(ex);
119157
}
120158
});
121159
}
122160

123-
addOnMessageClearedCallback(onReceived: (data: ReceivedNotification) => void): Promise<any> {
161+
addOnMessageClearedCallback(
162+
onReceived: (data: ReceivedNotification) => void
163+
): Promise<any> {
124164
return new Promise((resolve, reject) => {
125165
try {
126166
// note that this is ONLY triggered when the user clicked the notification in the statusbar
127167
com.telerik.localnotifications.LocalNotificationsPlugin.setOnMessageClearedCallback(
128-
new com.telerik.localnotifications.LocalNotificationsPluginListener({
129-
success: notification => {
130-
onReceived(JSON.parse(notification));
131-
}
132-
})
168+
new com.telerik.localnotifications.LocalNotificationsPluginListener({
169+
success: notification => {
170+
onReceived(JSON.parse(notification));
171+
}
172+
})
133173
);
134174
resolve();
135175
} catch (ex) {
136-
console.log("Error in LocalNotifications.addOnMessageClearedCallback: " + ex);
176+
console.log(
177+
'Error in LocalNotifications.addOnMessageClearedCallback: ' + ex
178+
);
137179
reject(ex);
138180
}
139181
});
@@ -145,7 +187,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
145187
LocalNotificationsImpl.cancelById(id);
146188
resolve(true);
147189
} catch (ex) {
148-
console.log("Error in LocalNotifications.cancel: " + ex);
190+
console.log('Error in LocalNotifications.cancel: ' + ex);
149191
reject(ex);
150192
}
151193
});
@@ -163,16 +205,22 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
163205
// console.log(">> < 26, StatusBarNotification[0]: " + new android.service.notification.StatusBarNotification[0]);
164206
// }
165207

166-
const keys: Array<string> = com.telerik.localnotifications.Store.getKeys(utils.ad.getApplicationContext());
208+
const keys: Array<
209+
string
210+
> = com.telerik.localnotifications.Store.getKeys(
211+
utils.ad.getApplicationContext()
212+
);
167213

168214
for (let i = 0; i < keys.length; i++) {
169215
LocalNotificationsImpl.cancelById(parseInt(keys[i]));
170216
}
171217

172-
NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).cancelAll();
218+
NotificationManagerCompatPackageName.NotificationManagerCompat.from(
219+
context
220+
).cancelAll();
173221
resolve();
174222
} catch (ex) {
175-
console.log("Error in LocalNotifications.cancelAll: " + ex);
223+
console.log('Error in LocalNotifications.cancelAll: ' + ex);
176224
reject(ex);
177225
}
178226
});
@@ -181,7 +229,11 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
181229
getScheduledIds(): Promise<number[]> {
182230
return new Promise((resolve, reject) => {
183231
try {
184-
const keys: Array<string> = com.telerik.localnotifications.Store.getKeys(utils.ad.getApplicationContext());
232+
const keys: Array<
233+
string
234+
> = com.telerik.localnotifications.Store.getKeys(
235+
utils.ad.getApplicationContext()
236+
);
185237

186238
const ids: number[] = [];
187239
for (let i = 0; i < keys.length; i++) {
@@ -190,7 +242,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
190242

191243
resolve(ids);
192244
} catch (ex) {
193-
console.log("Error in LocalNotifications.getScheduledIds: " + ex);
245+
console.log('Error in LocalNotifications.getScheduledIds: ' + ex);
194246
reject(ex);
195247
}
196248
});
@@ -207,18 +259,25 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
207259
// the persisted options are exactly like the original ones.
208260

209261
for (let n in scheduleOptions) {
210-
const options = LocalNotificationsImpl.merge(scheduleOptions[n], LocalNotificationsImpl.defaults);
262+
const options = LocalNotificationsImpl.merge(
263+
scheduleOptions[n],
264+
LocalNotificationsImpl.defaults
265+
);
211266

212267
options.icon = LocalNotificationsImpl.getIcon(
213-
context,
214-
resources,
215-
LocalNotificationsImpl.IS_GTE_LOLLIPOP && options.silhouetteIcon || options.icon
268+
context,
269+
resources,
270+
(LocalNotificationsImpl.IS_GTE_LOLLIPOP &&
271+
options.silhouetteIcon) ||
272+
options.icon
216273
);
217274

218275
options.atTime = options.at ? options.at.getTime() : 0;
219276

220277
// Used when restoring the notification after a reboot:
221-
options.repeatInterval = LocalNotificationsImpl.getInterval(options.interval);
278+
options.repeatInterval = LocalNotificationsImpl.getInterval(
279+
options.interval
280+
);
222281

223282
if (options.color) {
224283
options.color = options.color.android;
@@ -231,15 +290,16 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
231290
LocalNotificationsImpl.ensureID(options);
232291

233292
com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(
234-
new org.json.JSONObject(JSON.stringify(options)),
235-
context);
293+
new org.json.JSONObject(JSON.stringify(options)),
294+
context
295+
);
236296

237297
scheduledIds.push(options.id);
238298
}
239299

240300
resolve(scheduledIds);
241301
} catch (ex) {
242-
console.log("Error in LocalNotifications.schedule: " + ex);
302+
console.log('Error in LocalNotifications.schedule: ' + ex);
243303
reject(ex);
244304
}
245305
});

0 commit comments

Comments
 (0)