-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathApp.java
More file actions
295 lines (258 loc) · 14.7 KB
/
App.java
File metadata and controls
295 lines (258 loc) · 14.7 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package ly.count.android.demo;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import ly.count.android.sdk.Countly;
import ly.count.android.sdk.CountlyConfig;
import ly.count.android.sdk.CrashData;
import ly.count.android.sdk.GlobalCrashFilterCallback;
import ly.count.android.sdk.ModuleLog;
import ly.count.android.sdk.WebViewDisplayOption;
import ly.count.android.sdk.messaging.CountlyConfigPush;
import ly.count.android.sdk.messaging.CountlyPush;
import static ly.count.android.sdk.messaging.CountlyPush.COUNTLY_BROADCAST_PERMISSION_POSTFIX;
public class App extends Application {
/** You should use try.count.ly instead of YOUR_SERVER for the line below if you are using Countly trial service */
private final static String COUNTLY_SERVER_URL = "https://your.server.ly";
private final static String COUNTLY_APP_KEY = "YOUR_APP_KEY";
private final static String DEFAULT_URL = "https://your.server.ly";
private final static String DEFAULT_APP_KEY = "YOUR_APP_KEY";
private final static long applicationStartTimestamp = System.currentTimeMillis();
@Override
public void onCreate() {
super.onCreate();
if (DEFAULT_URL.equals(COUNTLY_SERVER_URL) || DEFAULT_APP_KEY.equals(COUNTLY_APP_KEY)) {
Log.e("CountlyDemo", "Please provide correct COUNTLY_SERVER_URL and COUNTLY_APP_KEY");
return;
}
if (false) {
//setting up strict mode for additional validation
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null) {
// Create the NotificationChannel
NotificationChannel channel = new NotificationChannel(CountlyPush.CHANNEL_ID, getString(R.string.countly_hannel_name), NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(getString(R.string.countly_channel_description));
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.notif_sample);
channel.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(channel);
}
}
//sample certificate for the countly try server
String[] certificates = {
"MIIGnjCCBYagAwIBAgIRAN73cVA7Y1nD+S8rToAqBpQwDQYJKoZIhvcNAQELBQAwgY8xCzAJ"
+ "BgNVBAYTAkdCMRswGQYDVQQIExJHcmVhdGVyIE1hbmNoZXN0ZXIxEDAOBgNVBAcTB1"
+ "NhbGZvcmQxGDAWBgNVBAoTD1NlY3RpZ28gTGltaXRlZDE3MDUGA1UEAxMuU2VjdGln"
+ "byBSU0EgRG9tYWluIFZhbGlkYXRpb24gU2VjdXJlIFNlcnZlciBDQTAeFw0yMDA2MD"
+ "EwMDAwMDBaFw0yMjA5MDMwMDAwMDBaMBUxEzARBgNVBAMMCiouY291bnQubHkwggEi"
+ "MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCl9zmATVRwrGRtRQJcmBmA+zc/ZL"
+ "io3YfkwXO2w8u9lnw60J4JpPNn9OnGcxdM+sqbXKU3jTdjY4j3yaA6NlWibq2jU2x6"
+ "HT2sS+I5gFFE/6tO53WqjoMk48i3FkyoJDittwtQrVaRGcP8RjJH0pfXaP+JLrLAgg"
+ "HuW3tCFqYzkWi3uLGVjQbSIRNiXsM3FI0UMEa/x1I3U4hLjMjH28KagZbZLWnHOvks"
+ "AvGLg3xQkS+GSQ+6ARZ2/bGh5O9q4hCCCk0/PpwAXmrOnWtwrNuwHcCDOvuB22JxLd"
+ "t8jQDYrjwtJIvq4Yut8FQPv/75SKoETWWHyxe0x5NsB34UwA/BAgMBAAGjggNsMIID"
+ "aDAfBgNVHSMEGDAWgBSNjF7EVK2K4Xfpm/mbBeG4AY1h4TAdBgNVHQ4EFgQU8uf/ND"
+ "Rt8cu+AwARVIGXPMfxGbQwDgYDVR0PAQH/BAQDAgWgMAwGA1UdEwEB/wQCMAAwH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OgIhAL"
+ "dZl9s/qISsFm2E64ruYbdE4HKR1ZJ0zbIXOZcds7XXMA0GCSqGSIb3DQEBCwUAA4IB"
+ "AQB2Ar1h2X/S4qsVlw0gEbXO//6Rj8mTB4BFW6c5r84n0vTwvA78h003eX00y0ymxO"
+ "i5hkqB8gd1IUSWP1R1ijYtBVPdFi+SsMjUsB5NKquQNlWpo0GlFjRlcXnDC6R6toN2"
+ "QixJb47VM40Vmn2g0ZuMGfy1XoQKvIyRosT92jGm1YcF+nLEHBDr+89apZ8sUpFfWo"
+ "AnCom+8sBGwje6zP10eBbprHyzM8snvdwo/QNLAzLcvVNKP+Sr4H7HKzec3g1+THI0"
+ "M72TzoguJcOZQEI6Pd+FIP5Xad53rq4jCtRGwYrsieH49a3orBnkkJvUKni+mtkxMb"
+ "PTJ7eeMmX9g/0h"
};
Map<String, String> customHeaderValues = new ConcurrentHashMap<>();
customHeaderValues.put("foo", "bar");
Map<String, Object> automaticViewSegmentation = new ConcurrentHashMap<>();
automaticViewSegmentation.put("One", 2);
automaticViewSegmentation.put("Three", 4.44d);
automaticViewSegmentation.put("Five", "Six");
Map<String, String> metricOverride = new ConcurrentHashMap<>();
metricOverride.put("SomeKey", "123");
metricOverride.put("_carrier", "BoneyK");
//add some custom segments, like dependency library versions
Map<String, Object> customCrashSegmentation = new ConcurrentHashMap<>();
customCrashSegmentation.put("EarBook", "3.5");
customCrashSegmentation.put("AdGiver", "6.5");
//properties that we want to sent at init time
Map<String, Object> customUserProperties = new ConcurrentHashMap<>();
customUserProperties.put("A", 1);
CountlyConfig config = new CountlyConfig(this, COUNTLY_APP_KEY, COUNTLY_SERVER_URL)
// .setDeviceId("a" + applicationStartTimestamp )
.setLoggingEnabled(true)
.setLogListener(new ModuleLog.LogCallback() {
@Override public void LogHappened(String logMessage, ModuleLog.LogLevel logLevel) {
//duplicated countly logs
switch (logLevel) {
case Verbose:
//Log.v("Countly Duplicate", logMessage);
break;
case Debug:
//Log.d("Countly Duplicate", logMessage);
break;
case Info:
//Log.i("Countly Duplicate", logMessage);
break;
case Warning:
//Log.w("Countly Duplicate", logMessage);
break;
case Error:
//Log.e("Countly Duplicate", logMessage);
break;
default:
break;
}
}
})
.enableAutomaticViewTracking()
//.enrollABOnRCDownload()
//.setMaxRequestQueueSize(5)
.enableAutomaticViewShortNames()
.setGlobalViewSegmentation(automaticViewSegmentation)
.setAutomaticViewTrackingExclusions(new Class[] { ActivityExampleCustomEvents.class })
.setPushIntentAddMetadata(true)
.setLocation("us", "Böston 墨尔本", "-23.8043604,-46.6718331", "10.2.33.12")
//.setDisableLocation()
//.enableManualSessionControl()
//.enableManualSessionControlHybridMode()
//.enableTemporaryDeviceIdMode()
.setRequiresConsent(true)
.giveAllConsents()
//.setConsentEnabled(new String[] {
// Countly.CountlyFeatureNames.push, Countly.CountlyFeatureNames.sessions, Countly.CountlyFeatureNames.location,
// Countly.CountlyFeatureNames.attribution, Countly.CountlyFeatureNames.crashes, Countly.CountlyFeatureNames.events,
// Countly.CountlyFeatureNames.starRating, Countly.CountlyFeatureNames.users, Countly.CountlyFeatureNames.views,
// Countly.CountlyFeatureNames.apm, Countly.CountlyFeatureNames.remoteConfig, Countly.CountlyFeatureNames.feedback
//})
.setHttpPostForced(false)
.setParameterTamperingProtectionSalt("test-salt-checksum")
.addCustomNetworkRequestHeaders(customHeaderValues)
//.enableCertificatePinning(certificates)
//.enablePublicKeyPinning(certificates)
.RemoteConfigRegisterGlobalCallback((downloadResult, error, fullValueUpdate, downloadedValues) -> {
if (error == null) {
Log.d(Countly.TAG, "Automatic remote config download has completed. " + Countly.sharedInstance().remoteConfig().getValues());
} else {
Log.d(Countly.TAG, "Automatic remote config download encountered a problem, " + error);
}
})
.setTrackOrientationChanges(true)
//.setMetricOverride(metricOverride)
.setWebviewDisplayOption(WebViewDisplayOption.IMMERSIVE)
//.enableServerConfiguration()
.setUserProperties(customUserProperties);
// crash configuration
config.crashes
.enableCrashReporting()
.enableRecordAllThreadsWithCrash()
.setCustomCrashSegmentation(customCrashSegmentation)
.setGlobalCrashFilterCallback(new GlobalCrashFilterCallback() {
@Override public boolean filterCrash(CrashData crash) {
return crash.getStackTrace().contains("secret");
}
});
// APM configuration
config.apm.enableAppStartTimeTracking()
.enableForegroundBackgroundTracking()
.setAppStartTimestampOverride(applicationStartTimestamp);
Countly.sharedInstance().init(config);
//Log.i(demoTag, "After calling init. This should return 'true', the value is:" + Countly.sharedInstance().isInitialized());
//--- PUSH NOTIFICATIONS SETUP ----//
List<String> allowedClassNames = new ArrayList<>();
allowedClassNames.add("MainActivity");
List<String> allowedPackageNames = new ArrayList<>();
allowedPackageNames.add(getPackageName());
CountlyConfigPush countlyConfigPush = new CountlyConfigPush(this)
.setProvider(Countly.CountlyMessagingProvider.FCM)
.setAllowedIntentClassNames(allowedClassNames)
.setAllowedIntentPackageNames(allowedPackageNames);
CountlyPush.useAdditionalIntentRedirectionChecks = true;
CountlyPush.init(countlyConfigPush);
CountlyPush.setNotificationAccentColor(255, 213, 89, 134);
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(Countly.TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
CountlyPush.onTokenRefresh(token);
}
});
/* Register for broadcast action if you need to be notified when Countly message clicked */
BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent sentIntent = intent.getParcelableExtra(CountlyPush.EXTRA_INTENT);
sentIntent.setExtrasClassLoader(CountlyPush.class.getClassLoader());
Bundle bun = sentIntent.getParcelableExtra(CountlyPush.EXTRA_MESSAGE);
CountlyPush.Message message;
int actionIndex = sentIntent.getIntExtra(CountlyPush.EXTRA_ACTION_INDEX, -100);
String msg = "NULL";
if (bun != null) {
message = bun.getParcelable(CountlyPush.EXTRA_MESSAGE);
if (message != null) {
msg = message.message();
}
}
Log.i("Countly", "[CountlyActivity] Got a message, :[" + msg + "], action index:[" + actionIndex + "]");
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(CountlyPush.SECURE_NOTIFICATION_BROADCAST);
ContextCompat.registerReceiver(getApplicationContext(), messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null, ContextCompat.RECEIVER_NOT_EXPORTED);
}
}