-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathindex.android.ts
181 lines (157 loc) · 4.63 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Utils } from '@nativescript/core';
import { firebase, FirebaseApp } from '@nativescript/firebase-core';
import { ConsentStatus, ConsentType, EventParameter, IAnalytics } from './common';
export * from './common';
let defaultAnalytics: Analytics;
const fb = firebase();
Object.defineProperty(fb, 'analytics', {
value: () => {
if (!defaultAnalytics) {
defaultAnalytics = new Analytics();
}
return defaultAnalytics;
},
writable: false,
});
function numberHasDecimals(item: number) {
return !(item % 1 === 0);
}
function serialize(data) {
switch (typeof data) {
case 'string':
case 'boolean':
case 'number': {
return data;
}
case 'object': {
if (data === null) {
return null;
}
if (Array.isArray(data)) {
const store = new java.util.ArrayList();
data.forEach((item) => {
const value = serialize(item);
switch (typeof value) {
case 'object':
if (value instanceof android.os.Bundle) {
store.add(value);
}
if (value instanceof java.util.ArrayList) {
store.add(value);
}
break;
}
});
return store;
}
const store = new android.os.Bundle();
Object.keys(data).forEach((key) => {
const value = serialize(data[key]);
switch (typeof value) {
case 'boolean':
store.putBoolean(key, value);
break;
case 'number':
if (numberHasDecimals(value)) {
store.putDouble(key, value);
} else {
store.putLong(key, value);
}
break;
case 'string':
store.putString(key, value);
break;
case 'object':
if (value instanceof android.os.Bundle) {
store.putBundle(key, value);
} else if (value instanceof java.util.ArrayList) {
store.putParcelableArrayList(key, value);
} else {
store.putString(key, null);
}
break;
}
});
return store;
}
default:
return null;
}
}
export class Analytics implements IAnalytics {
_native: com.google.firebase.analytics.FirebaseAnalytics;
_app: FirebaseApp;
constructor() {
if (defaultAnalytics) {
return defaultAnalytics;
}
defaultAnalytics = this;
this._native = com.google.firebase.analytics.FirebaseAnalytics.getInstance(Utils.android.getApplicationContext());
}
handleOpenURL(url: string): void {}
handleUserActivity(userActivity: any): void {}
get app(): FirebaseApp {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(com.google.firebase.FirebaseApp.getInstance());
}
return this._app;
}
get appInstanceId(): string {
return this._native.getAppInstanceId();
}
setSessionTimeoutInterval(sessionTimeoutInterval: number): void {
this._native.setSessionTimeoutDuration(sessionTimeoutInterval);
}
setUserProperty(key: string, value: string): void {
this._native.setUserProperty(key, value);
}
setAnalyticsCollectionEnabled(analyticsCollectionEnabled: boolean): void {
this._native.setAnalyticsCollectionEnabled(analyticsCollectionEnabled);
}
setUserId(userId: string): void {
this._native.setUserId(userId);
}
logEvent(name: string, parameters: EventParameter): void {
(<any>org).nativescript.firebase.analytics.FirebaseAnalytics.logEvent(this._native, name, serialize(parameters));
}
resetAnalyticsData(): void {
this._native.resetAnalyticsData();
}
setDefaultEventParameters(parameters: EventParameter): void {
this._native.setDefaultEventParameters(serialize(parameters) as android.os.Bundle);
}
setConsent(consentSettings: Map<ConsentType, ConsentStatus>): void {
const nativeMap = new java.util.HashMap(consentSettings?.size ?? 0);
consentSettings.forEach((value, key) => {
let nativeKey;
let nativeValue;
switch (key) {
case ConsentType.Ad_Storage:
nativeKey = com.google.firebase.analytics.FirebaseAnalytics.ConsentType.AD_STORAGE;
break;
case ConsentType.Analytics_Storage:
nativeKey = com.google.firebase.analytics.FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE;
break;
case ConsentType.Ad_User_Data:
nativeKey = com.google.firebase.analytics.FirebaseAnalytics.ConsentType.AD_USER_DATA;
break;
case ConsentType.Ad_Personalization:
nativeKey = com.google.firebase.analytics.FirebaseAnalytics.ConsentType.AD_PERSONALIZATION;
break;
}
switch (value) {
case ConsentStatus.Denied:
nativeValue = com.google.firebase.analytics.FirebaseAnalytics.ConsentStatus.DENIED;
break;
case ConsentStatus.Granted:
nativeValue = com.google.firebase.analytics.FirebaseAnalytics.ConsentStatus.GRANTED;
break;
}
if (nativeKey && nativeValue) {
nativeMap.put(nativeKey, nativeValue);
}
});
this._native.setConsent(nativeMap);
}
}