-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
EventLogger
feature (SDKCF-6879)
- Loading branch information
1 parent
3046172
commit 281ee62
Showing
32 changed files
with
2,780 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
sample/src/main/kotlin/com/rakuten/tech/mobile/sdkutils/sample/EventLoggerActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.rakuten.tech.mobile.sdkutils.sample | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.RadioButton | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.databinding.DataBindingUtil | ||
import com.google.gson.Gson | ||
import com.google.gson.JsonSyntaxException | ||
import com.google.gson.reflect.TypeToken | ||
import com.rakuten.tech.mobile.sdkutils.eventlogger.EventLogger | ||
import com.rakuten.tech.mobile.sdkutils.sample.databinding.ActivityEventLoggerBinding | ||
import kotlin.random.Random | ||
|
||
@Suppress( | ||
"UndocumentedPublicClass", | ||
"UndocumentedPublicFunction", | ||
"MagicNumber", | ||
"TooManyFunctions" | ||
) | ||
class EventLoggerActivity : AppCompatActivity() { | ||
|
||
private lateinit var binding: ActivityEventLoggerBinding | ||
private val sdkName | ||
get() = binding.sdkNameText.text.toString().ifEmpty { "sdkutils" } | ||
private val sdkVersion | ||
get() = binding.sdkVerText.text.toString().ifEmpty { com.rakuten.tech.mobile.sdkutils.BuildConfig.VERSION_NAME } | ||
private val errorCode | ||
get() = binding.errorCodeText.text.toString() | ||
private val errorMessage | ||
get() = binding.errorMsgText.text.toString() | ||
private val numTimes | ||
get() = binding.numTimesText.text.toString().toIntOrNull() ?: 1 | ||
private val eventTypeRadId | ||
get() = binding.eventTypeRadioGrp.checkedRadioButtonId | ||
private val eventType | ||
get() = findViewById<RadioButton>(eventTypeRadId).text.toString().lowercase() | ||
private val infoString | ||
get() = binding.addtnlInfoText.text.toString() | ||
private val info: Map<String, String>? | ||
get() = if (infoString.isEmpty()) null else jsonStringToMap(infoString) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
binding = DataBindingUtil.setContentView(this, R.layout.activity_event_logger) | ||
binding.activity = this | ||
setDefaultsOrHints() | ||
|
||
EventLogger.configure(this, BuildConfig.EVENT_LOGGER_API_URL, BuildConfig.EVENT_LOGGER_API_KEY) | ||
} | ||
|
||
fun onLogEventButtonClick() { | ||
logEvent() | ||
} | ||
|
||
fun onLogUniqueEventButtonClick() { | ||
logEvent(true) | ||
} | ||
|
||
fun onCustomButtonClick() { | ||
binding.errorMsgText.setText("") | ||
} | ||
|
||
fun onException1ButtonClick() { | ||
binding.errorMsgText.setText( | ||
ArithmeticException().stackTraceToString().take(1000) | ||
) | ||
} | ||
|
||
fun onException2ButtonClick() { | ||
binding.errorMsgText.setText( | ||
IllegalArgumentException("Testing").stackTraceToString().take(1000) | ||
) | ||
} | ||
|
||
fun onShowEventsCacheClick() { | ||
val intent = Intent(this, EventLoggerCacheActivity::class.java) | ||
this.startActivity(intent) | ||
} | ||
|
||
private fun setDefaultsOrHints() { | ||
binding.apply { | ||
sdkNameText.setText("sdkutils") | ||
sdkVerText.setText(com.rakuten.tech.mobile.sdkutils.BuildConfig.VERSION_NAME) | ||
numTimesText.setText("1") | ||
addtnlInfoText.hint = """{ "key": "value" }""" | ||
} | ||
} | ||
|
||
@SuppressWarnings("LongMethod") | ||
private fun logEvent(randomizeMessage: Boolean = false) { | ||
when (eventType) { | ||
"critical" -> repeat(numTimes) { | ||
EventLogger.sendCriticalEvent( | ||
sourceName = sdkName, | ||
sourceVersion = sdkVersion, | ||
errorCode = errorCode, | ||
errorMessage = if (randomizeMessage) randomizeString() else errorMessage, | ||
info = info | ||
) | ||
} | ||
"warning" -> repeat(numTimes) { | ||
EventLogger.sendWarningEvent( | ||
sourceName = sdkName, | ||
sourceVersion = sdkVersion, | ||
errorCode = errorCode, | ||
errorMessage = if (randomizeMessage) randomizeString() else errorMessage, | ||
info = info | ||
) | ||
} | ||
} | ||
|
||
Toast.makeText(this, "Processed!", Toast.LENGTH_SHORT).show() | ||
} | ||
|
||
@SuppressWarnings("SwallowedException") | ||
private fun jsonStringToMap(jsonString: String): Map<String, String>? { | ||
val type = object : TypeToken<Map<String, String>>() {}.type | ||
return try { | ||
Gson().fromJson(jsonString, type) | ||
} catch (e: JsonSyntaxException) { | ||
Toast.makeText(this, "Not a valid Json representation!", Toast.LENGTH_SHORT).show() | ||
return null | ||
} | ||
} | ||
|
||
private fun randomizeString(length: Int = 20) = (1..length) | ||
.map { Random.nextInt(33, 127).toChar() } // Ascii alphanumeric + some special characters range | ||
.joinToString("") | ||
} |
50 changes: 50 additions & 0 deletions
50
sample/src/main/kotlin/com/rakuten/tech/mobile/sdkutils/sample/EventLoggerCacheActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.rakuten.tech.mobile.sdkutils.sample | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.databinding.DataBindingUtil | ||
import com.rakuten.tech.mobile.sdkutils.sample.databinding.ActivityEventLoggerCacheBinding | ||
import org.json.JSONObject | ||
|
||
@Suppress( | ||
"UndocumentedPublicClass", | ||
"MagicNumber" | ||
) | ||
class EventLoggerCacheActivity : AppCompatActivity() { | ||
|
||
private lateinit var binding: ActivityEventLoggerCacheBinding | ||
private lateinit var eventsCache: SharedPreferences | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
binding = DataBindingUtil.setContentView(this, R.layout.activity_event_logger_cache) | ||
eventsCache = this | ||
.getSharedPreferences("com.rakuten.tech.mobile.sdkutils.eventlogger.events", Context.MODE_PRIVATE) | ||
setCacheText() | ||
} | ||
|
||
private fun setCacheText() { | ||
|
||
if (eventsCache.all.isEmpty()) { | ||
binding.numEventsLabel.text = "Count: 0" | ||
return | ||
} | ||
|
||
val textBuilder = StringBuilder(0) | ||
val allEvents = eventsCache.all | ||
binding.numEventsLabel.text = "Count: ${allEvents.size}" | ||
for (event in allEvents) { | ||
textBuilder.append(event.key) | ||
textBuilder.append("\n") | ||
textBuilder.append( | ||
JSONObject(event.value.toString()).toString(4) | ||
) | ||
textBuilder.append("\n\n\n") | ||
} | ||
|
||
binding.eventsStorageText.text = textBuilder | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.