|
| 1 | +package info.hannes.countly |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.app.Activity |
| 5 | +import android.content.Context |
| 6 | +import android.content.pm.PackageManager |
| 7 | +import android.provider.Settings |
| 8 | +import info.hannes.logcat.BuildConfig |
| 9 | +import ly.count.android.sdk.Countly |
| 10 | +import ly.count.android.sdk.CountlyConfig |
| 11 | +import ly.count.android.sdk.DeviceId |
| 12 | + |
| 13 | + |
| 14 | +@Suppress("PrivatePropertyName") |
| 15 | +class Analytics : IAnalytics { |
| 16 | + |
| 17 | + var countlyInstance: Countly = Countly.sharedInstance() |
| 18 | + private set |
| 19 | + |
| 20 | + override fun isInitialized(): Boolean { |
| 21 | + return countlyInstance.isInitialized |
| 22 | + } |
| 23 | + |
| 24 | + override fun recordEvent(event: String) { |
| 25 | + if (isInitialized()) { |
| 26 | + countlyInstance.recordEvent(event, segmentation, 1) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + override fun recordError(message: String) { |
| 31 | + if (isInitialized()) { |
| 32 | + countlyInstance.recordHandledException(RuntimeException(message)) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + override fun recordError(throwable: Throwable) { |
| 37 | + if (isInitialized()) { |
| 38 | + countlyInstance.recordHandledException(throwable) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + override fun recordWarning(message: String) { |
| 43 | + if (isInitialized()) { |
| 44 | + countlyInstance.recordHandledException(RuntimeException(message)) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + override fun onStart(activity: Activity?) { |
| 49 | + if (isInitialized()) { |
| 50 | + countlyInstance.onStart(activity) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + override fun onStop() { |
| 55 | + if (isInitialized()) { |
| 56 | + countlyInstance.onStop() |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + companion object { |
| 61 | + private const val COUNTLY_HOST = "https://countly.apinext.bmw.com" |
| 62 | + |
| 63 | + val segmentation = HashMap<String, String>() |
| 64 | + |
| 65 | + @SuppressLint("HardwareIds") |
| 66 | + fun initAnalytics(context: Context, loggingEnabled: Boolean = false, countlyKey: String) { |
| 67 | + var version = "" |
| 68 | + try { |
| 69 | + val pInfo = context.packageManager.getPackageInfo(context.packageName, 0) |
| 70 | + version = pInfo.versionName |
| 71 | + } catch (ignore: PackageManager.NameNotFoundException) { |
| 72 | + } |
| 73 | + segmentation["app_version"] = version |
| 74 | + segmentation["logging_version"] = BuildConfig.VERSION_NAME |
| 75 | + |
| 76 | + val config = CountlyConfig() |
| 77 | + .setAppKey(countlyKey) |
| 78 | + .setContext(context) |
| 79 | + .setDeviceId(Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)) |
| 80 | + .setIdMode(DeviceId.Type.DEVELOPER_SUPPLIED) |
| 81 | + .setServerURL(COUNTLY_HOST) |
| 82 | + .setLoggingEnabled(loggingEnabled) |
| 83 | + .setViewTracking(true) |
| 84 | + .setHttpPostForced(true) |
| 85 | + .enableCrashReporting() |
| 86 | + Countly.sharedInstance().init(config) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments