Skip to content

Commit

Permalink
feat: reduce io operations for logger + separate logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Jul 3, 2024
1 parent a4f452b commit 4ac4877
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.Context
import android.content.Intent
import android.view.accessibility.AccessibilityEvent
import android.view.inputmethod.InputMethodManager
import com.viscouspot.gitsync.util.Helper.log
import com.viscouspot.gitsync.util.Logger.log
import com.viscouspot.gitsync.util.SettingsManager


Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/viscouspot/gitsync/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import android.os.Bundle
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.Settings
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
Expand All @@ -42,7 +41,8 @@ import com.viscouspot.gitsync.ui.adapter.RecentCommitsAdapter
import com.viscouspot.gitsync.ui.fragment.CloneRepoFragment
import com.viscouspot.gitsync.util.GitManager
import com.viscouspot.gitsync.util.Helper
import com.viscouspot.gitsync.util.Helper.log
import com.viscouspot.gitsync.util.Logger
import com.viscouspot.gitsync.util.Logger.log
import com.viscouspot.gitsync.util.SettingsManager
import com.viscouspot.gitsync.util.rightDrawable
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -341,7 +341,7 @@ class MainActivity : AppCompatActivity() {
clipboardManager.setPrimaryClip(clip)

Toast.makeText(applicationContext, "Logs have been copied to clipboard!", Toast.LENGTH_SHORT).show()
Helper.deleteLogs(this)
Logger.deleteLogs(this)
}
}

Expand Down
98 changes: 0 additions & 98 deletions app/src/main/java/com/viscouspot/gitsync/util/Helper.kt
Original file line number Diff line number Diff line change
@@ -1,122 +1,24 @@
package com.viscouspot.gitsync.util

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.util.Log
import android.widget.EditText
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import com.viscouspot.gitsync.R
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.io.PrintWriter
import java.io.StringWriter
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.util.regex.Matcher
import java.util.regex.Pattern
import kotlin.random.Random


object Helper {
fun log(context: Context, type: String, e: Exception) {
val sw = StringWriter()
val pw = PrintWriter(sw)
e.printStackTrace(pw)
log(context, type, "Error: $sw")

sendBugReportNotification(context)
}

fun log(context: Context, type: String, message: String) {
Log.d("///Git Sync//$type", message)

val file = File(context.filesDir, "logs.txt")
if (!file.exists()) {
file.createNewFile()
}

FileWriter(file, true).use { writer ->
writer.appendLine("${DateTimeFormatter.ISO_INSTANT.format(Instant.now())} $type\n - $message")
}
}

fun deleteLogs(context: Context) {
val file = File(context.filesDir, "logs.txt")
if (file.exists()) {
file.delete()
}
}

private fun sendBugReportNotification(context: Context) {
val channelId = "git_sync_bug_channel"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
"Git Sync Bug",
NotificationManager.IMPORTANCE_MIN
)
val manager = context.getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(channel)
}

val emailIntent = createBugReportEmailIntent(context)
val buttonPendingIntent = PendingIntent.getActivity(context, Random.nextInt(0, 100), emailIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.bug)
.setContentTitle(context.getString(R.string.report_bug))
.setContentText(context.getString(R.string.send_bug_report))
.setContentIntent(buttonPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

with(NotificationManagerCompat.from(context)) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "${context.getString(R.string.report_bug)} ${context.getString(R.string.enable_notifications)}", Toast.LENGTH_SHORT).show()
return
}

notify(2, builder.build())
}
}

private fun createBugReportEmailIntent(context: Context): Intent {
val recipient = "bugs.viscouspotential@gmail.com"

val file = File(context.filesDir, "logs.txt")
val fileUri = FileProvider.getUriForFile(context, "${context.packageName}.provider", file)

val emailIntent = Intent(Intent.ACTION_SEND).apply {
data = Uri.parse("mailto:$recipient")
type = "message/rfc822"
putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
putExtra(Intent.EXTRA_SUBJECT, "Bug Report: Git Sync - [${DateTimeFormatter.ISO_INSTANT.format(Instant.now())}]")
putExtra(Intent.EXTRA_TEXT, "App logs are attached! \n\n")
putExtra(Intent.EXTRA_STREAM, fileUri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

return Intent.createChooser(emailIntent, context.getString(R.string.select_email_client))
}

fun getPathFromUri(context: Context, uri: Uri): String {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri) -> {
Expand Down
119 changes: 119 additions & 0 deletions app/src/main/java/com/viscouspot/gitsync/util/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.viscouspot.gitsync.util

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.FileProvider
import com.viscouspot.gitsync.R
import java.io.File
import java.io.FileWriter
import java.io.PrintWriter
import java.io.StringWriter
import java.time.Instant
import java.time.format.DateTimeFormatter
import kotlin.random.Random

object Logger {
private const val LOG_FILE_NAME = "logs.txt"
private val logBuffer = StringBuilder()

fun log(context: Context, type: String, e: Exception) {
val sw = StringWriter()
val pw = PrintWriter(sw)
e.printStackTrace(pw)
log(context, type, "Error: $sw")

sendBugReportNotification(context)
}

fun log(context: Context, type: String, message: String) {
Log.d("///Git Sync//$type", message)

val logEntry = "${DateTimeFormatter.ISO_INSTANT.format(Instant.now())} $type\n - $message\n"
synchronized(logBuffer) {
logBuffer.append(logEntry)
}
}

fun flushLogs(context: Context) {
val file = File(context.filesDir, LOG_FILE_NAME)
synchronized(logBuffer) {
if (logBuffer.isNotEmpty()) {
FileWriter(file, true).use { writer ->
writer.append(logBuffer.toString())
}
logBuffer.clear()
}
}
}

fun deleteLogs(context: Context) {
val file = File(context.filesDir, LOG_FILE_NAME)
if (file.exists()) {
file.delete()
}
}

private fun sendBugReportNotification(context: Context) {
val channelId = "git_sync_bug_channel"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
"Git Sync Bug",
NotificationManager.IMPORTANCE_MIN
)
val manager = context.getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(channel)
}

val emailIntent = createBugReportEmailIntent(context)
val buttonPendingIntent = PendingIntent.getActivity(context, Random.nextInt(0, 100), emailIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.bug)
.setContentTitle(context.getString(R.string.report_bug))
.setContentText(context.getString(R.string.send_bug_report))
.setContentIntent(buttonPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

with(NotificationManagerCompat.from(context)) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "${context.getString(R.string.report_bug)} ${context.getString(
R.string.enable_notifications)}", Toast.LENGTH_SHORT).show()
return
}

notify(2, builder.build())
}
}

private fun createBugReportEmailIntent(context: Context): Intent {
val recipient = "bugs.viscouspotential@gmail.com"

val file = File(context.filesDir, "logs.txt")
val fileUri = FileProvider.getUriForFile(context, "${context.packageName}.provider", file)

val emailIntent = Intent(Intent.ACTION_SEND).apply {
data = Uri.parse("mailto:$recipient")
type = "message/rfc822"
putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
putExtra(Intent.EXTRA_SUBJECT, "Bug Report: Git Sync - [${DateTimeFormatter.ISO_INSTANT.format(Instant.now())}]")
putExtra(Intent.EXTRA_TEXT, "App logs are attached! \n\n")
putExtra(Intent.EXTRA_STREAM, fileUri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

return Intent.createChooser(emailIntent, context.getString(R.string.select_email_client))
}
}

0 comments on commit 4ac4877

Please sign in to comment.