/
index.android.ts
109 lines (106 loc) 路 3.06 KB
/
index.android.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { FirebaseApp, FirebaseError, firebase } from '@nativescript/firebase-core';
import { ICrashlytics } from './common';
import lazy from '@nativescript/core/utils/lazy';
import StackTrace from 'stacktrace-js';
const NSCrashlyticsReference = lazy(() => org.nativescript.firebase.crashlytics.FirebaseCrashlytics);
let defaultCrashlytics: Crashlytics;
const fb = firebase();
Object.defineProperty(fb, 'crashlytics', {
value: () => {
if (!defaultCrashlytics) {
defaultCrashlytics = new Crashlytics();
}
return defaultCrashlytics;
},
writable: false,
});
export class Crashlytics implements ICrashlytics {
#native: com.google.firebase.crashlytics.FirebaseCrashlytics;
constructor() {
if (defaultCrashlytics) {
return defaultCrashlytics;
}
defaultCrashlytics = this;
this.#native = com.google.firebase.crashlytics.FirebaseCrashlytics.getInstance();
}
get native() {
return this.#native;
}
get android() {
return this.native;
}
#app: FirebaseApp;
get app(): FirebaseApp {
if (!this.#app) {
// @ts-ignore
this.#app = FirebaseApp.fromNative(this.native.app);
}
return this.#app;
}
checkForUnsentReports(): Promise<boolean> {
return new Promise((resolve, reject) => {
NSCrashlyticsReference().checkForUnsentReports(
this.native,
new org.nativescript.firebase.crashlytics.FirebaseCrashlytics.Callback({
onSuccess(param0) {
resolve(param0 as any);
},
onError(param0) {
const err = FirebaseError.fromNative(param0);
reject(err);
},
})
);
});
}
crash(): void {
NSCrashlyticsReference().crash();
}
deleteUnsentReports() {
this.native.deleteUnsentReports();
}
didCrashOnPreviousExecution(): boolean {
return this.native.didCrashOnPreviousExecution();
}
log(message: string): void {
this.native.log(message);
}
recordError(error: any): void {
if (error instanceof Error) {
StackTrace.fromError(error).then((stack) => {
const traceElements = Array.create('java.lang.StackTraceElement', stack.length);
stack.forEach((item, i) => {
traceElements[i] = new java.lang.StackTraceElement('', item.functionName, item.fileName, -1);
});
const t = new java.lang.Throwable(error.message);
t.setStackTrace(traceElements);
this.native.recordException(t);
});
} else {
this.native.recordException(error);
}
}
sendUnsentReports(): void {
this.native.sendUnsentReports();
}
setAttribute(name: string, value: string | number | boolean) {
if (typeof value === 'string') {
this.native.setCustomKey(name, value);
} else if (typeof value === 'number') {
this.native.setCustomKey(name, value);
} else if (typeof value === 'boolean') {
this.native.setCustomKey(name, value);
}
}
setAttributes(attributes: { [key: string]: string | number | boolean }) {
try {
NSCrashlyticsReference().setAttributes(this.native, JSON.stringify(attributes));
} catch (e) {}
}
setCrashlyticsCollectionEnabled(enabled: boolean) {
this.native.setCrashlyticsCollectionEnabled(java.lang.Boolean.valueOf(enabled));
}
setUserId(userId: string) {
this.native.setUserId(userId);
}
}