### Benchmark Service Control and Configuration (Kotlin) Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt Shows how to start, check the status of, and stop the BenchmarkService, which is a foreground service responsible for running the benchmark tests. It also lists key configuration constants for task scheduling and benchmark duration. ```kotlin BenchmarkService.start(context) if (BenchmarkService.RUNNING) { } BenchmarkService.stop(context) val ALARM_REPEAT_MS: Long = TimeUnit.MINUTES.toMillis(10) val WORK_REPEAT_MS: Long = TimeUnit.SECONDS.toMillis(10) val MAIN_REPEAT_MS: Long = TimeUnit.SECONDS.toMillis(10) val BENCHMARK_DURATION: Long = TimeUnit.HOURS.toMillis(3) ``` -------------------------------- ### Resume Interrupted Benchmark on Boot - Kotlin Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt Implements a `BroadcastReceiver` that listens for device boot, user foreground, and package replacement events. Upon receiving such an event, it checks if a benchmark was running and resumes it by starting the `BenchmarkService`. ```kotlin class RestartReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { context?.apply { val currentBenchmark = Benchmark.load(context) if (currentBenchmark != null && currentBenchmark.running) { BenchmarkService.start(this) } } } } ``` -------------------------------- ### Handle Permissions and Start Benchmark - Kotlin Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt Manages runtime permissions for exact alarms and notifications on Android. It uses the Activity Result API to request necessary permissions and then initiates a benchmark. It also sets the benchmark duration using SharedPreferences. ```kotlin fun hasExactAlarmPermission(context: Context): Boolean { val am = context.getSystemService(ALARM_SERVICE) as AlarmManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !am.canScheduleExactAlarms()) { return false } return true } fun hasNotificationPermission(): Boolean { return Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || NotificationManagerCompat.from(this).areNotificationsEnabled() } val launchServiceAfterPermission = registerForActivityResult( ActivityResultContracts.RequestMultiplePermissions() ) { if (it.all { true }) { startBenchmark() } } val permissions = mutableListOf() if (!hasExactAlarmPermission(this)) { permissions.add(Manifest.permission.SCHEDULE_EXACT_ALARM) } if (!hasNotificationPermission()) { permissions.add(Manifest.permission.POST_NOTIFICATIONS) } launchServiceAfterPermission.launch(permissions.toTypedArray()) PreferenceManager.getDefaultSharedPreferences(this) .edit() .putLong(KEY_BENCHMARK_DURATION, TimeUnit.HOURS.toMillis(2)) .apply() ``` -------------------------------- ### Benchmark Data Class Operations (Kotlin) Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt Demonstrates the creation, event tracking, result calculation, formatting, persistence, and reporting of benchmark data using the Benchmark class. It handles timing information and persistence via SharedPreferences. ```kotlin val benchmarkDuration = TimeUnit.HOURS.toMillis(3) val benchmark = Benchmark( from = System.currentTimeMillis(), to = System.currentTimeMillis() + benchmarkDuration ) benchmark.workEvents.add(System.currentTimeMillis()) benchmark.mainEvents.add(System.currentTimeMillis()) benchmark.alarmEvents.add(System.currentTimeMillis()) val totalScore = benchmark.getTotalResult() val workScore = benchmark.getWorkResult() val mainScore = benchmark.getMainResult() val alarmScore = benchmark.getAlarmResult() val formatted = Benchmark.formatResult(totalScore) Benchmark.save(context, benchmark) val loadedBenchmark = Benchmark.load(context) val report = Benchmark.generateTextReport(context, benchmark) Benchmark.finishBenchmark(context, benchmark) ``` -------------------------------- ### Visualize Benchmark Events - Kotlin Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt Demonstrates how to create and refresh a custom `BenchmarkView` to display a timeline visualization of benchmark events. The view uses different shapes and colors to represent various types of scheduled tasks (work, main thread, alarm) plotted on a time grid. ```kotlin val benchmarkView = BenchmarkView(context, null, 0) chartContainer.addView(benchmarkView) benchmarkView.refresh() ``` -------------------------------- ### Configure AndroidManifest.xml Permissions Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt This XML snippet defines the essential permissions required for the application to perform background benchmarking. It includes declarations for boot completion, foreground services, exact alarm scheduling, and wake locks. ```xml ``` -------------------------------- ### Display and Share Benchmark Results - Kotlin Source: https://context7.com/urbandroid-team/dontkillmy-app/llms.txt Loads and displays benchmark results, including total score and breakdown by category (work, alarm, main thread). It also generates sharing intents for submitting results to dontkillmyapp.com via Google Forms or sharing a text report with a screenshot via email or other applications. ```kotlin ResultActivity.start(context) val benchmark = Benchmark.load(this) benchmark?.let { totalTextView.text = Benchmark.formatResult(it.getTotalResult()) workTextView.text = Benchmark.formatResult(it.getWorkResult()) alarmTextView.text = Benchmark.formatResult(it.getAlarmResult()) mainTextView.text = Benchmark.formatResult(it.getMainResult()) } val shareUrl = "https://docs.google.com/forms/d/e/1FAIpQLScFa3YweHO33W50ifAV8nSRGOFVCPacoikyA53SzkQXeDlQPA/viewform?" + "usp=pp_url" + "&entry.394232744=${Uri.encode(Build.MODEL)}" + "&entry.184762960=${Uri.encode(Build.VERSION.SDK_INT.toString())}" + "&entry.1019994117=${(benchmark.getMainResult() * 100).roundToInt()}" + "&entry.769591257=${(benchmark.getAlarmResult() * 100).roundToInt()}" + "&entry.1490587715=${(benchmark.getWorkResult() * 100).roundToInt()}" + "&entry.2139576680=${benchmark.getDurationSeconds()}" + "&submit=Submit" startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(shareUrl))) val body = Benchmark.generateTextReport(this, benchmark) val subject = "DontKillMyApp Report" val intent = Intent(Intent.ACTION_SEND).apply { type = "text/plain" putExtra(Intent.EXTRA_SUBJECT, subject) putExtra(Intent.EXTRA_TEXT, body) putExtra(Intent.EXTRA_STREAM, getBitmapUri(getBitmapFromView(dokiContentView))) } startActivity(Intent.createChooser(intent, "Share")) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.