Skip to content

Commit

Permalink
[ANDR][Example] Add config settings page (#88)
Browse files Browse the repository at this point in the history
* [ANDR][Example] Add config settings page

* Use api and url settings to init bitdrift logger

* fmt

* reshuffle

* add license

* Fix test
  • Loading branch information
murki authored Oct 24, 2024
1 parent d2f5dd9 commit f99fb64
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 8 deletions.
1 change: 1 addition & 0 deletions platform/jvm/gradle-test-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
implementation("androidx.preference:preference-ktx:1.2.1")
implementation("com.apollographql.apollo3:apollo-runtime:3.8.3")
implementation("com.apollographql.apollo3:apollo-runtime:3.8.3")
implementation("com.jakewharton.timber:timber:5.0.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class AndroidViewReplayTest {
assertThat(metrics.errorViewCount).isEqualTo(0)
assertThat(metrics.exceptionCausingViewCount).isEqualTo(0)
// AppCompatTextView multiline label
assertThat(screen).contains(ReplayRect(type = ReplayType.Label, x = 36, y = 235, width = 758, height = 47))
assertThat(screen).contains(ReplayRect(type = ReplayType.Label, x = 36, y = 277, width = 698, height = 38))
assertThat(screen).contains(ReplayRect(type = ReplayType.Label, x = 36, y = 421, width = 758, height = 47))
assertThat(screen).contains(ReplayRect(type = ReplayType.Label, x = 36, y = 463, width = 698, height = 38))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import okhttp3.HttpUrl;

public class BitdriftInit {
public static void initBitdriftCaptureInJava() {
public static void initBitdriftCaptureInJava(HttpUrl apiUrl, String apiKey) {
String userID = UUID.randomUUID().toString();
List<FieldProvider> fieldProviders = new ArrayList<>();
fieldProviders.add(() -> {
Expand All @@ -30,12 +30,12 @@ public static void initBitdriftCaptureInJava() {
});

Capture.Logger.start(
"<YOUR API KEY GOES HERE>",
apiKey,
new SessionStrategy.Fixed(),
new Configuration(),
fieldProviders,
null,
HttpUrl.get("https://api.bitdrift.io")
apiUrl
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import kotlin.system.exitProcess

class ConfigurationSettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = preferenceManager.context
val screen = preferenceManager.createPreferenceScreen(context)

val backendCategory = PreferenceCategory(context)
backendCategory.key = "control_plane_category"
backendCategory.title = "Control Plane Configuration"

screen.addPreference(backendCategory)

val apiUrlPref = EditTextPreference(context)
apiUrlPref.key = "apiUrl"
apiUrlPref.title = "API URL"
apiUrlPref.summary = "App needs to be restarted for changes to take effect"

backendCategory.addPreference(apiUrlPref)

val apiKeyPref = EditTextPreference(context)
apiKeyPref.key = "apiKey"
apiKeyPref.title = "API Key"
apiKeyPref.summary = "App needs to be restarted for changes to take effect"

backendCategory.addPreference(apiKeyPref)

val restartPreference = Preference(context)
restartPreference.key = "restart"
restartPreference.title = "Restart the App"
restartPreference.setOnPreferenceClickListener {
val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName)
val restartIntent = Intent.makeRestartActivityTask(launchIntent!!.component)
// Required for API 34 and later
// Ref: https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents
restartIntent.setPackage(context.packageName);
context.startActivity(restartIntent)
exitProcess(0)
}

screen.addPreference(restartPreference)

preferenceScreen = screen
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ class FirstFragment : Fragment() {
setContent {
// In Compose world
MaterialTheme {
Text("Hello from Compose!")
Text(
text = "Text in Compose",
style = MaterialTheme.typography.h6,
color = MaterialTheme.colors.secondary,
)
}
}
}
Expand All @@ -110,6 +114,9 @@ class FirstFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.btnNavigateConfiguration.setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_ConfigFragment)
}
binding.btnCopySessionUrl.setOnClickListener(this::copySessionUrl)
binding.btnStartNewSession.setOnClickListener(this::startNewSession)
binding.btnTempDeviceCode.setOnClickListener(this::getTempDeviceCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import io.bitdrift.capture.Capture
import io.bitdrift.capture.Capture.Logger.sessionUrl
import io.bitdrift.capture.LogLevel
import io.bitdrift.capture.events.span.Span
import io.bitdrift.capture.events.span.SpanResult
import io.bitdrift.capture.timber.CaptureTree
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import papa.AppLaunchType
import papa.PapaEvent
import papa.PapaEventListener
Expand Down Expand Up @@ -81,7 +83,14 @@ class GradleTestApp : Application() {
}

private fun initLogging() {
BitdriftInit.initBitdriftCaptureInJava()
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val stringApiUrl = prefs.getString("apiUrl", null)
val apiUrl = stringApiUrl?.toHttpUrlOrNull()
if (apiUrl == null) {
Log.e("GradleTestApp", "Failed to initialize bitdrift logger due to invalid API URL: $stringApiUrl")
return
}
BitdriftInit.initBitdriftCaptureInJava(apiUrl, prefs.getString("apiKey", ""))
// Timber
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
tools:context=".FirstFragment"
>

<Button
android:id="@+id/btnNavigateConfiguration"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/navigate_config"
/>

<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<action
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
<action
android:id="@+id/action_FirstFragment_to_ConfigFragment"
app:destination="@id/ConfigurationSettingsFragment" />
</fragment>
<fragment
android:id="@+id/SecondFragment"
Expand All @@ -25,4 +28,9 @@
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
</fragment>
</navigation>
<fragment
android:id="@+id/ConfigurationSettingsFragment"
android:name="io.bitdrift.gradletestapp.ConfigurationSettingsFragment"
android:label="@string/config_fragment_label">
</fragment>
</navigation>
2 changes: 2 additions & 0 deletions platform/jvm/gradle-test-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
<string name="config_fragment_label">Configuration Settings Fragment</string>
<string name="navigate_config">Navigate to Configuration</string>
<string name="okhttp">Perform OkHttp Request</string>
<string name="graphql">Perform Apollo GraphQL Request</string>
<string name="compat_multiline_txt">Text Label on AppCompatTextView\nSecond Line on same Text Label</string>
Expand Down

0 comments on commit f99fb64

Please sign in to comment.