Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions platform/jvm/gradle-test-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

<application
android:name=".GradleTestApp"
Expand Down Expand Up @@ -53,6 +55,16 @@
android:name="android.webkit.WebView.Multiprocess"
android:value="true"/>

<!-- Separate process to avoid starting the application when starting this service -->
<service android:name=".ui.service.SendTelemetryService"
android:process=":crashReportingProcess"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="This foreground service allows users to report issues" />
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ sealed class NavigationAction : AppAction {
object NavigateToDialogAndModals : NavigationAction()

object NavigateToStressTest : NavigationAction()

object InvokeService : NavigationAction()
}

sealed class StressTestAction : AppAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ fun NavigationCard(onAction: (AppAction) -> Unit) {
onClick = { onAction(NavigationAction.NavigateToDialogAndModals) },
colors = ButtonDefaults.outlinedButtonColors(contentColor = BitdriftColors.TextPrimary),
) { Text(stringResource(id = R.string.navigate_to_modal_bottom_sheet), maxLines = 1, softWrap = false) }

OutlinedButton(
onClick = { onAction(NavigationAction.InvokeService) },
colors = ButtonDefaults.outlinedButtonColors(contentColor = BitdriftColors.TextPrimary),
) { Text("Invoke Service", maxLines = 1, softWrap = false) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
Expand All @@ -32,6 +33,7 @@ import io.bitdrift.gradletestapp.data.repository.NetworkTestingRepository
import io.bitdrift.gradletestapp.data.repository.SdkRepository
import io.bitdrift.gradletestapp.data.repository.StressTestRepository
import io.bitdrift.gradletestapp.ui.compose.MainScreen
import io.bitdrift.gradletestapp.ui.service.SendTelemetryService
import io.bitdrift.gradletestapp.ui.viewmodel.MainViewModel
import io.bitdrift.gradletestapp.ui.viewmodel.MainViewModelFactory

Expand Down Expand Up @@ -111,6 +113,13 @@ class FirstFragment : Fragment() {
findNavController().navigate(R.id.action_FirstFragment_to_StressTestFragment)
}

is NavigationAction.InvokeService -> {
ContextCompat.startForegroundService(
requireContext(),
SendTelemetryService.createReportIntent(requireContext()),
)
}

else -> viewModel.handleAction(action)
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// capture-sdk - bitdrift's client SDK
// Copyright Bitdrift, Inc. All rights reserved.
//
// Use of this source code is governed by a source available license that can be found in the
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt

package io.bitdrift.gradletestapp.ui.service

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.os.Process
import androidx.core.app.NotificationCompat
import timber.log.Timber

class SendTelemetryService : Service() {
override fun onBind(intent: Intent?): IBinder? {
Timber.i("SendTelemetryService.onBind() called on process ${Process.myPid()}, thread=${Thread.currentThread().name}")
// We don't provide binding, so return null
return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.i("SendTelemetryService.onStartCommand() called on process ${Process.myPid()}, thread=${Thread.currentThread().name}")

val channelId = "send_telemetry_channel"
val notificationId = 1

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, "Send Telemetry", NotificationManager.IMPORTANCE_LOW)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
}

val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("Sending Telemetry")
.setContentText("Processing...")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.build()

startForeground(notificationId, notification)

// Simulate telemetry sending work
Thread.sleep(500)
// Stopping the service after work is done
stopSelf()

return START_NOT_STICKY
}

companion object {
fun createReportIntent(context: Context) : Intent {//, crash: Crash): Intent {
val intent = Intent(context, SendTelemetryService::class.java)
Timber.i("SendTelemetryService.createReportIntent() called on process ${Process.myPid()}, thread=${Thread.currentThread().name}")
return intent
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class MainViewModel(
is NavigationAction.NavigateToXml -> {}
is NavigationAction.NavigateToDialogAndModals -> {}
is NavigationAction.NavigateToStressTest -> {}
is NavigationAction.InvokeService -> {}
}
}

Expand Down
Loading