Skip to content

Commit

Permalink
feat: FileInputStream/FileOutputStream instrumentation (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn authored Dec 14, 2021
1 parent b63373a commit 7f1e3dd
Show file tree
Hide file tree
Showing 22 changed files with 867 additions and 108 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object LibsVersion {
const val JUNIT = "4.13.2"
const val ASM = "9.2"
const val SQLITE = "2.1.0"
const val SENTRY = "5.1.2"
const val SENTRY = "5.5.0"
}

object Libs {
Expand Down
4 changes: 4 additions & 0 deletions examples/android-room/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
android:name=".ui.EditActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />

<activity
android:name=".ui.LyricsActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />

<meta-data
android:name="io.sentry.dsn"
android:value="https://[email protected]/5428559" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package io.sentry.android.roomsample

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.room.Room
import io.sentry.android.roomsample.data.TracksDatabase
import io.sentry.android.roomsample.util.DEFAULT_LYRICS
import java.io.File
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class SampleApp : Application() {

companion object {
lateinit var database: TracksDatabase
private set

lateinit var analytics: SharedPreferences
private set
}

override fun onCreate() {
Expand All @@ -17,5 +28,23 @@ class SampleApp : Application() {
.createFromAsset("tracks.db")
.fallbackToDestructiveMigration()
.build()

analytics = getSharedPreferences("analytics", Context.MODE_PRIVATE)

GlobalScope.launch(Dispatchers.IO) {
database.tracksDao().all()
.collect { tracks ->
tracks.forEachIndexed { index, track ->
// add lyrics for every 2nd track
if (index % 2 == 0) {
val dir = File("$filesDir${File.separatorChar}lyrics")
dir.mkdirs()

val file = File(dir, "${track.id}.txt")
file.writeText(DEFAULT_LYRICS)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ class EditActivity : ComponentActivity() {
} else {
if (originalTrack == null) {
addNewTrack(name, composer, duration.toLong(), unitPrice.toFloat())

val createCount = SampleApp.analytics.getInt("create_count", 0) + 1
SampleApp.analytics.edit().putInt("create_count", createCount).apply()
} else {
originalTrack.update(name, composer, duration.toLong(), unitPrice.toFloat())

val editCount = SampleApp.analytics.getInt("edit_count", 0) + 1
SampleApp.analytics.edit().putInt("edit_count", editCount).apply()
}
transaction.finish(SpanStatus.OK)
finish()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.sentry.android.roomsample.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.EditText
import androidx.activity.ComponentActivity
import androidx.appcompat.widget.Toolbar
import io.sentry.Sentry
import io.sentry.SpanStatus
import io.sentry.android.roomsample.R
import io.sentry.android.roomsample.data.Track
import java.io.File

@SuppressLint("SetTextI18n")
class LyricsActivity : ComponentActivity() {
private lateinit var file: File
private lateinit var lyricsInput: EditText

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_lyrics)

val transaction = Sentry.startTransaction(
"Track Interaction",
"ui.action.lyrics",
true
)

lyricsInput = findViewById(R.id.lyrics)
val toolbar = findViewById<Toolbar>(R.id.toolbar)

val track: Track = intent.getSerializableExtra(TRACK_EXTRA_KEY) as Track
toolbar.title = "Lyrics for ${track.name}"

val dir = File("$filesDir${File.separatorChar}lyrics")
dir.mkdirs()

file = File(dir, "${track.id}.txt")
if (file.exists()) {
lyricsInput.setText(file.readText())
}
transaction.finish(SpanStatus.OK)
}

override fun onBackPressed() {
val transaction = Sentry.getSpan() ?: Sentry.startTransaction(
"Track Interaction",
"ui.action.lyrics_finish",
true
)
if (!file.exists()) {
file.createNewFile()
}
file.writeText(lyricsInput.text.toString())
transaction.finish(SpanStatus.OK)
super.onBackPressed()
}

companion object {
const val TRACK_EXTRA_KEY = "LyricsActivity.Track"
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package io.sentry.android.roomsample.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.lifecycleScope
Expand All @@ -19,9 +11,8 @@ import io.sentry.Sentry
import io.sentry.SpanStatus
import io.sentry.android.roomsample.R
import io.sentry.android.roomsample.SampleApp
import io.sentry.android.roomsample.data.Track
import io.sentry.android.roomsample.ui.list.TrackAdapter
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runBlocking

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -55,77 +46,3 @@ class MainActivity : ComponentActivity() {
}
}
}

class TrackRow(
context: Context,
attrs: AttributeSet
) : LinearLayout(context, attrs) {

val deleteButton: View get() = findViewById(R.id.delete_track)
val editButton: View get() = findViewById(R.id.edit_track)

@SuppressLint("SetTextI18n")
fun populate(track: Track) {
val mins = (track.millis / 1000) / 60
val secs = (track.millis / 1000) % 60

findViewById<TextView>(R.id.track_name).text = track.name
findViewById<TextView>(R.id.track_duration).text = "${mins}m ${secs}s"
findViewById<TextView>(R.id.band_name).text = track.composer
}
}

class TrackAdapter : RecyclerView.Adapter<TrackAdapter.ViewHolder>() {

private var data: List<Track> = listOf()

fun populate(data: List<Track>) {
this.data = data
notifyDataSetChanged()
}

override fun getItemCount() = data.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.track_row,
parent,
false
) as TrackRow
)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.row.populate(data[position])
holder.row.deleteButton.setOnClickListener {
val transaction = Sentry.startTransaction(
"Track Interaction",
"ui.action.delete",
true
)
runBlocking {
SampleApp.database.tracksDao().delete(data[holder.bindingAdapterPosition])
}
transaction.finish(SpanStatus.OK)
}
holder.row.editButton.setOnClickListener {
val context = holder.row.context
val track = data[holder.bindingAdapterPosition]
context.startActivity(
Intent(
context,
EditActivity::class.java
).putExtra(EditActivity.TRACK_EXTRA_KEY, track)
)
}
}

override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
holder.row.deleteButton.setOnClickListener(null)
holder.row.editButton.setOnClickListener(null)
}

inner class ViewHolder(val row: TrackRow) : RecyclerView.ViewHolder(row)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.sentry.android.roomsample.ui.list

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import io.sentry.Sentry
import io.sentry.SpanStatus
import io.sentry.android.roomsample.R
import io.sentry.android.roomsample.SampleApp
import io.sentry.android.roomsample.data.Track
import io.sentry.android.roomsample.ui.EditActivity
import io.sentry.android.roomsample.ui.LyricsActivity
import kotlinx.coroutines.runBlocking

class TrackAdapter : RecyclerView.Adapter<TrackAdapter.ViewHolder>() {

private var data: List<Track> = listOf()

fun populate(data: List<Track>) {
this.data = data
notifyDataSetChanged()
}

override fun getItemCount() = data.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.track_row,
parent,
false
) as TrackRow
)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.row.populate(data[position])
holder.row.deleteButton.setOnClickListener {
val transaction = Sentry.startTransaction(
"Track Interaction",
"ui.action.delete",
true
)
runBlocking {
SampleApp.database.tracksDao().delete(data[holder.bindingAdapterPosition])
val deleteCount = SampleApp.analytics.getInt("delete_count", 0) + 1
SampleApp.analytics.edit().putInt("delete_count", deleteCount).apply()
}
transaction.finish(SpanStatus.OK)
}
holder.row.editButton.setOnClickListener {
val context = holder.row.context
val track = data[holder.bindingAdapterPosition]
context.startActivity(
Intent(
context,
EditActivity::class.java
).putExtra(EditActivity.TRACK_EXTRA_KEY, track)
)
}
holder.row.infoButton.setOnClickListener {
val context = holder.row.context
val track = data[holder.bindingAdapterPosition]
context.startActivity(
Intent(
context,
LyricsActivity::class.java
).putExtra(LyricsActivity.TRACK_EXTRA_KEY, track)
)
}
}

override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
holder.row.deleteButton.setOnClickListener(null)
holder.row.editButton.setOnClickListener(null)
}

inner class ViewHolder(val row: TrackRow) : RecyclerView.ViewHolder(row)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.sentry.android.roomsample.ui.list

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import io.sentry.android.roomsample.R
import io.sentry.android.roomsample.data.Track

class TrackRow(
context: Context,
attrs: AttributeSet
) : LinearLayout(context, attrs) {

val deleteButton: View get() = findViewById(R.id.delete_track)
val editButton: View get() = findViewById(R.id.edit_track)
val infoButton: View get() = findViewById(R.id.track_info)

@SuppressLint("SetTextI18n")
fun populate(track: Track) {
val mins = (track.millis / 1000) / 60
val secs = (track.millis / 1000) % 60

findViewById<TextView>(R.id.track_name).text = track.name
findViewById<TextView>(R.id.track_duration).text = "${mins}m ${secs}s"
findViewById<TextView>(R.id.band_name).text = track.composer
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.sentry.android.roomsample.util

val DEFAULT_LYRICS =
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus elit vel commodo varius. Suspendisse eget tempus est. Pellentesque egestas mi vitae massa ultrices vulputate. Aenean tempor nec sem eu congue. Phasellus mollis tellus odio, ut tincidunt arcu ullamcorper at. Nunc quis lorem vel risus auctor ultricies. Quisque ornare congue sagittis. Donec sit amet arcu vitae mi sodales porta vitae et mi. Nullam eu viverra urna, quis elementum risus. In at accumsan justo. Nam hendrerit, lorem ac lacinia semper, diam dolor pulvinar turpis, non tincidunt dolor lacus non quam. Etiam tempus, dui ut rutrum ornare, quam eros feugiat nisi, et blandit nisi lacus nec neque. Mauris aliquam id tellus vel molestie. Quisque ultricies ante nec lacus condimentum, at aliquet diam volutpat.
Vivamus fermentum eu ante non aliquam. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum interdum semper orci, et auctor enim pharetra vel. Ut porttitor neque in blandit scelerisque. Cras fermentum urna sit amet metus tincidunt, eu porttitor nisi mattis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Mauris pharetra leo vitae turpis commodo, sit amet cursus nunc varius. Etiam bibendum interdum dolor, a scelerisque massa mattis eu. Suspendisse consequat tortor ac tincidunt vulputate.
Maecenas non lectus sit amet dui porta dapibus. Etiam vitae velit nibh. Morbi purus urna, cursus convallis feugiat vel, vulputate eget turpis. Vivamus et tincidunt lectus. Nullam id risus est. Sed ullamcorper pellentesque massa, dignissim ultricies urna maximus ac. Vivamus maximus eu ex quis ultricies. Donec facilisis diam arcu, id cursus sapien condimentum ac. Aenean suscipit, nibh ac tempus pharetra, ligula augue bibendum arcu, sed fringilla magna nisl vel felis.
Cras lacinia efficitur elit, ac sagittis nulla commodo eu. Aliquam a est at augue imperdiet eleifend. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas luctus est non vulputate porta. Phasellus eros libero, auctor id justo ac, mollis eleifend enim. Suspendisse pulvinar mi lorem, a maximus massa tincidunt ac. Duis mattis nibh a mauris laoreet laoreet. Donec malesuada quis dui feugiat ultricies. Ut at sem consectetur, tincidunt massa in, bibendum metus.
Sed fermentum eros ac odio venenatis, id varius est mollis. Fusce sollicitudin tellus risus, vel accumsan ante auctor in. Phasellus vel blandit massa. Suspendisse potenti. Donec vitae elementum enim, quis dictum mauris. Nullam consectetur enim at turpis bibendum, consequat facilisis sem ultrices. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam eget nisl id libero tempus fringilla vitae eget urna. Etiam dui neque, vestibulum a mi vulputate, placerat interdum sem. Mauris sit amet aliquam felis. Suspendisse potenti. Nullam odio purus, ultricies in tincidunt sit amet, interdum ac orci. Nulla volutpat auctor velit, sed malesuada urna auctor sed. Suspendisse non sollicitudin tortor. Vivamus vulputate lacinia nisi, pellentesque sagittis sapien.
""".trimIndent()
Loading

0 comments on commit 7f1e3dd

Please sign in to comment.