Skip to content

Commit

Permalink
[feat]: single activity app now
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Oct 28, 2023
1 parent 7acb016 commit 4c7780b
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 61 deletions.
24 changes: 14 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@
<activity
android:name=".ui.activity.MainActivity"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleInstance"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ui.activity.OpenFileActivity"
android:documentLaunchMode="always"
android:excludeFromRecents="true"
android:theme="@style/Theme.LogFox.Translucent"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
Expand All @@ -40,16 +53,7 @@
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.log" />
<data android:host="*" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.activity.CrashDetailsActivity"
android:label="@string/crash_details" />

<service
android:name=".service.LoggingService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.os.bundleOf
import androidx.navigation.NavDeepLinkBuilder
import com.f0x1d.logfox.LogFoxApp
import com.f0x1d.logfox.R
import com.f0x1d.logfox.database.entity.AppCrash
import com.f0x1d.logfox.extensions.*
import com.f0x1d.logfox.receiver.CopyReceiver
import com.f0x1d.logfox.ui.activity.CrashDetailsActivity

@SuppressLint("MissingPermission")
fun Context.sendErrorNotification(appCrash: AppCrash) = doIfPermitted {
Expand All @@ -23,9 +23,13 @@ fun Context.sendErrorNotification(appCrash: AppCrash) = doIfPermitted {
.setSmallIcon(R.drawable.ic_android_notification)
.setStyle(NotificationCompat.BigTextStyle().bigText(appCrash.log))
.apply {
if (appCrash.id != 0L) setContentIntent(makeActivityPendingIntent(CRASH_DETAILS_INTENT_ID, CrashDetailsActivity::class.java, bundleOf(
"crash_id" to appCrash.id
)))
if (appCrash.id != 0L) setContentIntent(
NavDeepLinkBuilder(this@sendErrorNotification)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.crashDetailsFragment)
.setArguments(bundleOf("crash_id" to appCrash.id))
.createPendingIntent()
)
}
.setAutoCancel(true)
.applyPrimaryColorIfNeeded(this@sendErrorNotification)
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/f0x1d/logfox/service/LoggingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import com.f0x1d.logfox.LogFoxApp
import com.f0x1d.logfox.R
import com.f0x1d.logfox.extensions.EXIT_APP_INTENT_ID
import com.f0x1d.logfox.extensions.STOP_LOGGING_SERVICE_INTENT_ID
import com.f0x1d.logfox.extensions.activityManager
import com.f0x1d.logfox.extensions.makeOpenAppPendingIntent
import com.f0x1d.logfox.extensions.makeServicePendingIntent
import com.f0x1d.logfox.extensions.notifications.applyPrimaryColorIfNeeded
import com.f0x1d.logfox.repository.logging.LoggingRepository
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.update
import javax.inject.Inject
import kotlin.system.exitProcess

@AndroidEntryPoint
class LoggingService: Service() {
Expand Down Expand Up @@ -70,9 +70,12 @@ class LoggingService: Service() {

private fun killApp() {
loggingRepository.stopLogging()
stopService()

exitProcess(0)
activityManager.appTasks.forEach {
it.finishAndRemoveTask()
}

stopService()
}

override fun onBind(p0: Intent?) = null
Expand Down
32 changes: 27 additions & 5 deletions app/src/main/java/com/f0x1d/logfox/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ class MainActivity: BaseViewModelActivity<MainViewModel, ActivityMainBinding>(),
}
}

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
handleIntent(intent)
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
handleIntent(intent, onNewIntent = true)
}

private fun handleIntent(intent: Intent?, onNewIntent: Boolean = false) {
if (onNewIntent)
if (navController.handleDeepLink(intent))
return

if (intent == null) return

if (intent.data == null) return

when (intent.action) {
Intent.ACTION_VIEW -> navController.navigate(
NavGraphDirections.actionGlobalLogsFragment(fileUri = intent.data)
)
}
}

override fun onEvent(event: Event) {
when (event.type) {
MainViewModel.EVENT_TYPE_SETUP -> navController.navigate(NavGraphDirections.actionGlobalSetupFragment())
Expand All @@ -99,6 +125,7 @@ class MainActivity: BaseViewModelActivity<MainViewModel, ActivityMainBinding>(),
R.id.editFilterFragment -> false
R.id.chooseAppFragment -> false
R.id.appCrashesFragment -> false
R.id.crashDetailsFragment -> false

else -> true
}
Expand Down Expand Up @@ -137,11 +164,6 @@ class MainActivity: BaseViewModelActivity<MainViewModel, ActivityMainBinding>(),
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
navController.handleDeepLink(intent)
}

override fun onDestroy() {
super.onDestroy()
navController.removeOnDestinationChangedListener(this)
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/f0x1d/logfox/ui/activity/OpenFileActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.f0x1d.logfox.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class OpenFileActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

startActivity(
Intent(this, MainActivity::class.java).apply {
action = intent.action
setDataAndType(intent.data, intent.type)
replaceExtras(intent)
}
)
finish()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.f0x1d.logfox.ui.activity
package com.f0x1d.logfox.ui.fragment.crashes

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.f0x1d.logfox.R
import com.f0x1d.logfox.database.entity.AppCrash
import com.f0x1d.logfox.databinding.ActivityCrashDetailsBinding
Expand All @@ -16,15 +20,15 @@ import com.f0x1d.logfox.extensions.showAreYouSureDeleteDialog
import com.f0x1d.logfox.extensions.views.replaceAccessibilityDelegateClassNameWithButton
import com.f0x1d.logfox.extensions.views.widgets.loadIcon
import com.f0x1d.logfox.extensions.views.widgets.setClickListenerOn
import com.f0x1d.logfox.extensions.views.widgets.setupBackButton
import com.f0x1d.logfox.ui.activity.base.BaseViewModelActivity
import com.f0x1d.logfox.extensions.views.widgets.setupBackButtonForNavController
import com.f0x1d.logfox.ui.fragment.base.BaseViewModelFragment
import com.f0x1d.logfox.utils.event.Event
import com.f0x1d.logfox.viewmodel.crashes.CrashDetailsViewModel
import dagger.hilt.android.AndroidEntryPoint
import dev.chrisbanes.insetter.applyInsetter

@AndroidEntryPoint
class CrashDetailsActivity: BaseViewModelActivity<CrashDetailsViewModel, ActivityCrashDetailsBinding>() {
class CrashDetailsFragment: BaseViewModelFragment<CrashDetailsViewModel, ActivityCrashDetailsBinding>() {

override val viewModel by viewModels<CrashDetailsViewModel>()

Expand All @@ -34,10 +38,13 @@ class CrashDetailsActivity: BaseViewModelActivity<CrashDetailsViewModel, Activit
viewModel.exportCrashToZip(it ?: return@registerForActivityResult)
}

override fun inflateBinding() = ActivityCrashDetailsBinding.inflate(layoutInflater)
override fun inflateBinding(
inflater: LayoutInflater,
container: ViewGroup?
) = ActivityCrashDetailsBinding.inflate(inflater, container, false)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.scrollView.applyInsetter {
type(navigationBars = true) {
Expand All @@ -46,23 +53,21 @@ class CrashDetailsActivity: BaseViewModelActivity<CrashDetailsViewModel, Activit
}

binding.toolbar.inflateMenu(R.menu.crash_details_menu)
binding.toolbar.setupBackButton {
onBackPressedDispatcher.onBackPressed()
}
binding.toolbar.setupBackButtonForNavController()

binding.copyLayout.replaceAccessibilityDelegateClassNameWithButton()
binding.shareLayout.replaceAccessibilityDelegateClassNameWithButton()
binding.zipLayout.replaceAccessibilityDelegateClassNameWithButton()

viewModel.crash.observe(this) {
viewModel.crash.observe(viewLifecycleOwner) {
setupFor(it ?: return@observe)
}
}

override fun onEvent(event: Event) {
when (event.type) {
CrashDetailsViewModel.EVENT_TYPE_COPY_LINK -> {
copyText(event.consume() ?: return)
requireContext().copyText(event.consume() ?: return)
snackbar(R.string.text_copied)
}
}
Expand All @@ -80,7 +85,7 @@ class CrashDetailsActivity: BaseViewModelActivity<CrashDetailsViewModel, Activit
setClickListenerOn(R.id.delete_item) {
showAreYouSureDeleteDialog {
viewModel.deleteCrash(appCrash)
finish()
findNavController().popBackStack()
}
}
}
Expand All @@ -90,12 +95,12 @@ class CrashDetailsActivity: BaseViewModelActivity<CrashDetailsViewModel, Activit
binding.appPackage.text = appCrash.packageName

binding.copyLayout.setOnClickListener {
copyText(appCrash.log)
requireContext().copyText(appCrash.log)
snackbar(R.string.text_copied)
}

binding.shareLayout.setOnClickListener {
shareIntent(appCrash.log)
requireContext().shareIntent(appCrash.log)
}

binding.zipLayout.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f0x1d.logfox.ui.fragment.crashes
package com.f0x1d.logfox.ui.fragment.crashes.list

import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -25,7 +25,7 @@ class AppCrashesFragment: BaseViewModelFragment<AppCrashesViewModel, FragmentApp

private val adapter = CrashesAdapter(click = {
findNavController().navigate(
AppCrashesFragmentDirections.actionAppCrashesFragmentToCrashDetailsActivity(it.lastCrash.id)
AppCrashesFragmentDirections.actionAppCrashesFragmentToCrashDetailsFragment(it.lastCrash.id)
)
}, delete = {
showAreYouSureDeleteDialog {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f0x1d.logfox.ui.fragment.crashes
package com.f0x1d.logfox.ui.fragment.crashes.list

import android.os.Bundle
import android.view.LayoutInflater
Expand Down Expand Up @@ -28,7 +28,7 @@ class CrashesFragment: BaseViewModelFragment<CrashesViewModel, FragmentCrashesBi

private val adapter = CrashesAdapter(click = {
val direction = when (it.count) {
1 -> CrashesFragmentDirections.actionCrashesFragmentToCrashDetailsActivity(it.lastCrash.id)
1 -> CrashesFragmentDirections.actionCrashesFragmentToCrashDetailsFragment(it.lastCrash.id)
else -> CrashesFragmentDirections.actionCrashesFragmentToAppCrashesFragment(
packageName = it.lastCrash.packageName,
appName = it.lastCrash.appName
Expand Down
31 changes: 20 additions & 11 deletions app/src/main/res/navigation/crashes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

<fragment
android:id="@+id/crashesFragment"
android:name="com.f0x1d.logfox.ui.fragment.crashes.CrashesFragment"
android:name="com.f0x1d.logfox.ui.fragment.crashes.list.CrashesFragment"
android:label="CrashesFragment" >
<action
android:id="@+id/action_crashesFragment_to_crashDetailsActivity"
app:destination="@id/crashDetailsActivity" />
android:id="@+id/action_crashesFragment_to_crashDetailsFragment"
app:destination="@id/crashDetailsFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"/>
<action
android:id="@+id/action_crashesFragment_to_appCrashesFragment"
app:destination="@id/appCrashesFragment"
Expand All @@ -21,7 +25,7 @@
</fragment>
<fragment
android:id="@+id/appCrashesFragment"
android:name="com.f0x1d.logfox.ui.fragment.crashes.AppCrashesFragment"
android:name="com.f0x1d.logfox.ui.fragment.crashes.list.AppCrashesFragment"
android:label="AppCrashesFragment" >
<argument
android:name="package_name"
Expand All @@ -32,16 +36,21 @@
app:nullable="true" />

<action
android:id="@+id/action_appCrashesFragment_to_crashDetailsActivity"
app:destination="@id/crashDetailsActivity" />
android:id="@+id/action_appCrashesFragment_to_crashDetailsFragment"
app:destination="@id/crashDetailsFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"/>
</fragment>
<activity
android:id="@+id/crashDetailsActivity"
android:name="com.f0x1d.logfox.ui.activity.CrashDetailsActivity"
android:label="CrashDetailsActivity" >
<fragment
android:id="@+id/crashDetailsFragment"
android:name="com.f0x1d.logfox.ui.fragment.crashes.CrashDetailsFragment"
android:label="CrashDetailsFragment">

<argument
android:name="crash_id"
app:argType="long"
android:defaultValue="-1L" />
</activity>
</fragment>
</navigation>
5 changes: 0 additions & 5 deletions app/src/main/res/navigation/logs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
android:id="@+id/logsFragment"
android:name="com.f0x1d.logfox.ui.fragment.LogsFragment"
android:label="LogsFragment" >
<deepLink
app:uri="content"
app:mimeType="*/*"
app:action="android.intent.action.VIEW" />

<argument
android:name="file_uri"
app:argType="android.net.Uri"
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/recordingsFragment"
app:popUpToInclusive="true">
app:popExitAnim="@anim/nav_default_pop_exit_anim">
<argument
android:name="file_uri"
app:argType="android.net.Uri"
Expand Down
Loading

0 comments on commit 4c7780b

Please sign in to comment.