Skip to content

Commit 75f7293

Browse files
fix/lint#2
1 parent 3e0c3fc commit 75f7293

File tree

1 file changed

+59
-119
lines changed

1 file changed

+59
-119
lines changed

src/local-notifications.android.ts

Lines changed: 59 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
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()
14-
? global.androidx.core.app
15-
: android.support.v4.app;
13+
const NotificationManagerCompatPackageName = useAndroidX() ? global.androidx.core.app : android.support.v4.app;
1614

17-
function useAndroidX() {
15+
function useAndroidX () {
1816
return global.androidx && global.androidx.appcompat;
1917
}
2018

2119
(() => {
2220
const registerLifecycleEvents = () => {
23-
com.telerik.localnotifications.LifecycleCallbacks.registerCallbacks(
24-
app.android.nativeApp
25-
);
21+
com.telerik.localnotifications.LifecycleCallbacks.registerCallbacks(app.android.nativeApp);
2622
};
2723

2824
// Hook on the application events
@@ -33,75 +29,47 @@ function useAndroidX() {
3329
}
3430
})();
3531

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

4136
private static getInterval(interval: ScheduleInterval): number {
42-
if (interval === 'second') {
37+
if (interval === "second") {
4338
return 1000; // it's in ms
44-
} else if (interval === 'minute') {
39+
} else if (interval === "minute") {
4540
return android.app.AlarmManager.INTERVAL_FIFTEEN_MINUTES / 15;
46-
} else if (interval === 'hour') {
41+
} else if (interval === "hour") {
4742
return android.app.AlarmManager.INTERVAL_HOUR;
48-
} else if (interval === 'day') {
43+
} else if (interval === "day") {
4944
return android.app.AlarmManager.INTERVAL_DAY;
50-
} else if (interval === 'week') {
45+
} else if (interval === "week") {
5146
return android.app.AlarmManager.INTERVAL_DAY * 7;
52-
} else if (interval === 'month') {
47+
} else if (interval === "month") {
5348
return android.app.AlarmManager.INTERVAL_DAY * 31; // well that's almost accurate
54-
} else if (interval === 'year') {
49+
} else if (interval === "year") {
5550
return android.app.AlarmManager.INTERVAL_DAY * 365; // same here
5651
} else {
5752
return undefined;
5853
}
5954
}
6055

61-
private static getIcon(
62-
context: any /* android.content.Context */,
63-
resources: any,
64-
iconLocation?: string
65-
): string {
56+
private static getIcon(context: any /* android.content.Context */, resources: any, iconLocation?: string): string {
6657
const packageName: string = context.getApplicationInfo().packageName;
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-
);
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;
8464
}
8565

8666
private static cancelById(id: number): void {
8767
const context = utils.ad.getApplicationContext();
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-
);
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);
10171
alarmManager.cancel(pendingIntent);
102-
const notificationManager = context.getSystemService(
103-
android.content.Context.NOTIFICATION_SERVICE
104-
);
72+
const notificationManager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
10573
notificationManager.cancel(id);
10674

10775
com.telerik.localnotifications.Store.remove(context, id);
@@ -112,12 +80,10 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
11280
try {
11381
// nothing to do on this platform
11482
const context = utils.ad.getApplicationContext();
115-
const hasPermission = NotificationManagerCompatPackageName.NotificationManagerCompat.from(
116-
context
117-
).areNotificationsEnabled();
83+
const hasPermission=NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).areNotificationsEnabled();
11884
resolve(hasPermission);
11985
} catch (ex) {
120-
console.log('Error in LocalNotifications.hasPermission: ' + ex);
86+
console.log("Error in LocalNotifications.hasPermission: " + ex);
12187
reject(ex);
12288
}
12389
});
@@ -129,53 +95,45 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
12995
// nothing to do on this platform
13096
resolve(true);
13197
} catch (ex) {
132-
console.log('Error in LocalNotifications.requestPermission: ' + ex);
98+
console.log("Error in LocalNotifications.requestPermission: " + ex);
13399
reject(ex);
134100
}
135101
});
136102
}
137103

138-
addOnMessageReceivedCallback(
139-
onReceived: (data: ReceivedNotification) => void
140-
): Promise<any> {
104+
addOnMessageReceivedCallback(onReceived: (data: ReceivedNotification) => void): Promise<any> {
141105
return new Promise((resolve, reject) => {
142106
try {
143107
// note that this is ONLY triggered when the user clicked the notification in the statusbar
144108
com.telerik.localnotifications.LocalNotificationsPlugin.setOnMessageReceivedCallback(
145-
new com.telerik.localnotifications.LocalNotificationsPluginListener({
146-
success: notification => {
147-
onReceived(JSON.parse(notification));
148-
}
149-
})
109+
new com.telerik.localnotifications.LocalNotificationsPluginListener({
110+
success: notification => {
111+
onReceived(JSON.parse(notification));
112+
}
113+
})
150114
);
151115
resolve();
152116
} catch (ex) {
153-
console.log(
154-
'Error in LocalNotifications.addOnMessageReceivedCallback: ' + ex
155-
);
117+
console.log("Error in LocalNotifications.addOnMessageReceivedCallback: " + ex);
156118
reject(ex);
157119
}
158120
});
159121
}
160122

161-
addOnMessageClearedCallback(
162-
onReceived: (data: ReceivedNotification) => void
163-
): Promise<any> {
123+
addOnMessageClearedCallback(onReceived: (data: ReceivedNotification) => void): Promise<any> {
164124
return new Promise((resolve, reject) => {
165125
try {
166126
// note that this is ONLY triggered when the user clicked the notification in the statusbar
167127
com.telerik.localnotifications.LocalNotificationsPlugin.setOnMessageClearedCallback(
168-
new com.telerik.localnotifications.LocalNotificationsPluginListener({
169-
success: notification => {
170-
onReceived(JSON.parse(notification));
171-
}
172-
})
128+
new com.telerik.localnotifications.LocalNotificationsPluginListener({
129+
success: notification => {
130+
onReceived(JSON.parse(notification));
131+
}
132+
})
173133
);
174134
resolve();
175135
} catch (ex) {
176-
console.log(
177-
'Error in LocalNotifications.addOnMessageClearedCallback: ' + ex
178-
);
136+
console.log("Error in LocalNotifications.addOnMessageClearedCallback: " + ex);
179137
reject(ex);
180138
}
181139
});
@@ -187,7 +145,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
187145
LocalNotificationsImpl.cancelById(id);
188146
resolve(true);
189147
} catch (ex) {
190-
console.log('Error in LocalNotifications.cancel: ' + ex);
148+
console.log("Error in LocalNotifications.cancel: " + ex);
191149
reject(ex);
192150
}
193151
});
@@ -205,22 +163,16 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
205163
// console.log(">> < 26, StatusBarNotification[0]: " + new android.service.notification.StatusBarNotification[0]);
206164
// }
207165

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

214168
for (let i = 0; i < keys.length; i++) {
215169
LocalNotificationsImpl.cancelById(parseInt(keys[i]));
216170
}
217171

218-
NotificationManagerCompatPackageName.NotificationManagerCompat.from(
219-
context
220-
).cancelAll();
172+
NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).cancelAll();
221173
resolve();
222174
} catch (ex) {
223-
console.log('Error in LocalNotifications.cancelAll: ' + ex);
175+
console.log("Error in LocalNotifications.cancelAll: " + ex);
224176
reject(ex);
225177
}
226178
});
@@ -229,11 +181,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
229181
getScheduledIds(): Promise<number[]> {
230182
return new Promise((resolve, reject) => {
231183
try {
232-
const keys: Array<
233-
string
234-
> = com.telerik.localnotifications.Store.getKeys(
235-
utils.ad.getApplicationContext()
236-
);
184+
const keys: Array<string> = com.telerik.localnotifications.Store.getKeys(utils.ad.getApplicationContext());
237185

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

243191
resolve(ids);
244192
} catch (ex) {
245-
console.log('Error in LocalNotifications.getScheduledIds: ' + ex);
193+
console.log("Error in LocalNotifications.getScheduledIds: " + ex);
246194
reject(ex);
247195
}
248196
});
@@ -259,25 +207,18 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
259207
// the persisted options are exactly like the original ones.
260208

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

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

275218
options.atTime = options.at ? options.at.getTime() : 0;
276219

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

282223
if (options.color) {
283224
options.color = options.color.android;
@@ -290,20 +231,19 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon
290231
LocalNotificationsImpl.ensureID(options);
291232

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

297237
scheduledIds.push(options.id);
298238
}
299239

300240
resolve(scheduledIds);
301241
} catch (ex) {
302-
console.log('Error in LocalNotifications.schedule: ' + ex);
242+
console.log("Error in LocalNotifications.schedule: " + ex);
303243
reject(ex);
304244
}
305245
});
306246
}
307247
}
308248

309-
export const LocalNotifications = new LocalNotificationsImpl();
249+
export const LocalNotifications = new LocalNotificationsImpl();

0 commit comments

Comments
 (0)