|
| 1 | +// capture-sdk - bitdrift's client SDK |
| 2 | +// Copyright Bitdrift, Inc. All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a source available license that can be found in the |
| 5 | +// LICENSE file or at: |
| 6 | +// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt |
| 7 | + |
| 8 | +package io.bitdrift.gradletestapp |
| 9 | + |
| 10 | +import android.content.Intent |
| 11 | +import android.content.pm.PackageManager |
| 12 | +import android.os.Bundle |
| 13 | +import androidx.preference.EditTextPreference |
| 14 | +import androidx.preference.Preference |
| 15 | +import androidx.preference.PreferenceCategory |
| 16 | +import androidx.preference.PreferenceFragmentCompat |
| 17 | +import kotlin.system.exitProcess |
| 18 | + |
| 19 | +class ConfigurationSettingsFragment : PreferenceFragmentCompat() { |
| 20 | + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { |
| 21 | + val context = preferenceManager.context |
| 22 | + val screen = preferenceManager.createPreferenceScreen(context) |
| 23 | + |
| 24 | + val backendCategory = PreferenceCategory(context) |
| 25 | + backendCategory.key = "control_plane_category" |
| 26 | + backendCategory.title = "Control Plane Configuration" |
| 27 | + |
| 28 | + screen.addPreference(backendCategory) |
| 29 | + |
| 30 | + val apiUrlPref = EditTextPreference(context) |
| 31 | + apiUrlPref.key = "apiUrl" |
| 32 | + apiUrlPref.title = "API URL" |
| 33 | + apiUrlPref.summary = "App needs to be restarted for changes to take effect" |
| 34 | + |
| 35 | + backendCategory.addPreference(apiUrlPref) |
| 36 | + |
| 37 | + val apiKeyPref = EditTextPreference(context) |
| 38 | + apiKeyPref.key = "apiKey" |
| 39 | + apiKeyPref.title = "API Key" |
| 40 | + apiKeyPref.summary = "App needs to be restarted for changes to take effect" |
| 41 | + |
| 42 | + backendCategory.addPreference(apiKeyPref) |
| 43 | + |
| 44 | + val restartPreference = Preference(context) |
| 45 | + restartPreference.key = "restart" |
| 46 | + restartPreference.title = "Restart the App" |
| 47 | + restartPreference.setOnPreferenceClickListener { |
| 48 | + val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName) |
| 49 | + val restartIntent = Intent.makeRestartActivityTask(launchIntent!!.component) |
| 50 | + // Required for API 34 and later |
| 51 | + // Ref: https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents |
| 52 | + restartIntent.setPackage(context.packageName); |
| 53 | + context.startActivity(restartIntent) |
| 54 | + exitProcess(0) |
| 55 | + } |
| 56 | + |
| 57 | + screen.addPreference(restartPreference) |
| 58 | + |
| 59 | + preferenceScreen = screen |
| 60 | + } |
| 61 | +} |
0 commit comments