Skip to content

Commit

Permalink
App can run without service, recordings are now saved in files and so…
Browse files Browse the repository at this point in the history
…me small improvements
  • Loading branch information
F0x1d committed Nov 5, 2022
1 parent daaf4ce commit 04945e4
Show file tree
Hide file tree
Showing 30 changed files with 295 additions and 90 deletions.
24 changes: 12 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.f0x1d.logfox"
minSdk 21
targetSdk 33
versionCode 19
versionName "1.1.8"
versionCode 20
versionName "1.1.9"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -54,34 +54,34 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.3"

implementation 'com.github.bumptech.glide:glide:4.13.0'
kapt 'com.github.bumptech.glide:compiler:4.13.0'
implementation 'com.github.bumptech.glide:glide:4.13.2'
kapt 'com.github.bumptech.glide:compiler:4.13.2'

implementation "androidx.room:room-runtime:2.4.3"
implementation "androidx.room:room-ktx:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"

implementation "com.google.dagger:hilt-android:2.41"
kapt "com.google.dagger:hilt-compiler:2.41"
implementation "com.google.dagger:hilt-android:2.43.2"
kapt "com.google.dagger:hilt-compiler:2.43.2"
implementation 'androidx.hilt:hilt-navigation-fragment:1.0.0'

implementation "androidx.core:core-ktx:1.9.0"
implementation "androidx.collection:collection-ktx:1.2.0"
implementation "androidx.fragment:fragment-ktx:1.5.3"
implementation "androidx.fragment:fragment-ktx:1.5.4"
implementation "androidx.preference:preference-ktx:1.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.0-alpha02"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.0-alpha03"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"

implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.8.0-alpha01'
implementation 'com.google.android.material:material:1.8.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation "androidx.recyclerview:recyclerview:1.2.1"

implementation "androidx.navigation:navigation-runtime-ktx:2.5.2"
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
implementation "androidx.navigation:navigation-runtime-ktx:2.5.3"
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LogFox"
tools:targetApi="31">
android:theme="@style/Theme.LogFox">

<activity android:name=".ui.activity.MainActivity" android:exported="true">
<intent-filter>
Expand All @@ -37,6 +36,18 @@
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.f0x1d.logfox.provider"
android:exported="false"
android:grantUriPermissions="true">

<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
</application>

</manifest>
8 changes: 7 additions & 1 deletion app/src/main/java/com/f0x1d/logfox/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

@Database(entities = [AppCrash::class, LogRecording::class, UserFilter::class], version = 5)
@Database(entities = [AppCrash::class, LogRecording::class, UserFilter::class], version = 6)
@TypeConverters(CrashTypeConverter::class, AllowedLevelsConverter::class)
abstract class AppDatabase: RoomDatabase() {

Expand All @@ -27,6 +27,12 @@ abstract class AppDatabase: RoomDatabase() {
database.execSQL("ALTER TABLE UserFilter ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1")
}
}
val MIGRATION_5_6 = object : Migration(5, 6) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE LogRecording")
database.execSQL("CREATE TABLE LogRecording(id INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL, date_and_time INTEGER NOT NULL, file TEXT NOT NULL)")
}
}
}

abstract fun appCrashDao(): AppCrashDao
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/f0x1d/logfox/database/LogRecording.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.f0x1d.logfox.database

import androidx.room.*
import kotlinx.coroutines.flow.Flow
import java.io.File

@Entity
data class LogRecording(@ColumnInfo(name = "date_and_time") val dateAndTime: Long,
@ColumnInfo(name = "log") val log: String,
@PrimaryKey(autoGenerate = true) val id: Long = 0)
@ColumnInfo(name = "file") val file: String,
@PrimaryKey(autoGenerate = true) val id: Long = 0) {

fun deleteFile() = File(file).delete()
}

@Dao
interface LogRecordingDao {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/f0x1d/logfox/di/RoomModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object RoomModule {
.addMigrations(
AppDatabase.MIGRATION_2_3,
AppDatabase.MIGRATION_3_4,
AppDatabase.MIGRATION_4_5
AppDatabase.MIGRATION_4_5,
AppDatabase.MIGRATION_5_6
).build()
}
44 changes: 33 additions & 11 deletions app/src/main/java/com/f0x1d/logfox/extensions/ContextExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import android.os.Build
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import com.f0x1d.logfox.R
import com.f0x1d.logfox.repository.logging.LoggingRepository
import com.f0x1d.logfox.service.LoggingService
import com.f0x1d.logfox.utils.preferences.AppPreferences
import java.io.File
import kotlin.system.exitProcess


Expand All @@ -33,9 +36,21 @@ val Context.notificationManagerCompat get() = NotificationManagerCompat.from(thi
val Context.notificationManager get() = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val Context.activityManager get() = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

fun Context.startLoggingAndService(loggingRepository: LoggingRepository) {
fun Context.startLoggingAndService(loggingRepository: LoggingRepository, appPreferences: AppPreferences, force: Boolean = false) {
loggingRepository.startLoggingIfNot()

if (appPreferences.startOnLaunch || force) {
startLoggingService()
}
}

fun Context.startLoggingAndServiceIfCan(loggingRepository: LoggingRepository, appPreferences: AppPreferences, force: Boolean = false) {
if (hasPermissionToReadLogs()) {
startLoggingAndService(loggingRepository, appPreferences, force)
}
}

fun Context.startLoggingService() {
Intent(this, LoggingService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startForegroundService(it)
Expand All @@ -44,12 +59,6 @@ fun Context.startLoggingAndService(loggingRepository: LoggingRepository) {
}
}

fun Context.startLoggingAndServiceIfCan(loggingRepository: LoggingRepository) {
if (hasPermissionToReadLogs()) {
startLoggingAndService(loggingRepository)
}
}

fun Context.hardRestartApp() {
for (task in activityManager.appTasks) task.finishAndRemoveTask()

Expand All @@ -60,11 +69,22 @@ fun Context.hardRestartApp() {

fun Context.toast(text: Int) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()

fun Context.shareIntent(text: String) {
fun Context.shareIntent(text: String) = baseShareIntent {
it.putExtra(Intent.EXTRA_TEXT, text)
it.type = "text/plain"
}

fun Context.shareFileIntent(file: File) = baseShareIntent {
val uri = FileProvider.getUriForFile(this, "com.f0x1d.logfox.provider", file)

it.putExtra(Intent.EXTRA_STREAM, uri)
it.type = "text/plain"
}

private fun Context.baseShareIntent(block: (Intent) -> Unit) {
try {
val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, text)
intent.type = "text/plain"
block.invoke(intent)

startActivity(Intent.createChooser(intent, getString(R.string.share)))
} catch (e: Exception) {
Expand All @@ -79,7 +99,9 @@ fun Context.catchingNotNumber(block: () -> Unit) = try {
toast(R.string.this_is_not_a_number)
}

fun Context.sendKillApp() = startService(Intent(this, LoggingService::class.java).setAction(LoggingService.ACTION_KILL_SERVICE))
fun Context.sendKillApp() = sendService(LoggingService.ACTION_KILL_SERVICE)
fun Context.sendStopService() = sendService(LoggingService.ACTION_STOP_SERVICE)
private fun Context.sendService(action: String) = startService(Intent(this, LoggingService::class.java).setAction(action))

fun Context.hasNotificationsPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= 33)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BootReceiver: BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED && appPreferences.startOnBoot) {
context.startLoggingAndServiceIfCan(loggingRepository)
context.startLoggingAndServiceIfCan(loggingRepository, appPreferences, true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LoggingRepository @Inject constructor(crashesRepository: CrashesRepository
}

val logsFlow = MutableStateFlow(emptyList<LogLine>())
val serviceRunningFlow = MutableStateFlow(false)

private var loggingJob: Job? = null
private var loggingInterval = AppPreferences.LOGS_UPDATE_INTERVAL_DEFAULT
Expand Down
Loading

0 comments on commit 04945e4

Please sign in to comment.