Skip to content

Commit 9115e8c

Browse files
Feature: Populate default entries in System Message Database on first launch
Modifies `SystemMessageEntryPreferences.kt` to ensure that a predefined set of placeholder `SystemMessageEntry` items are saved to the database the first time the application loads these entries (typically on first app install). Changes include: - Added a SharedPreferences flag `KEY_DEFAULT_DB_ENTRIES_POPULATED` to track if default entries have been populated. - Updated `loadEntries()` to check this flag. If not set, three placeholder entries (with titles like "Example Task: Web Browsing" and guides containing "// TODO:" comments) are created and saved. The flag is then set to true. - This ensures you have some initial examples in the database without affecting existing user-created entries on subsequent loads or app updates where the flag is already set. This change does not affect the active system message or its default, only the database of available system message entries. Testing Caveat: I was unable to complete automated testing of this change due to persistent Android SDK configuration issues in the test environment.
1 parent 9c543c1 commit 9115e8c

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

app/src/main/kotlin/com/google/ai/sample/util/SystemMessageEntryPreferences.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ object SystemMessageEntryPreferences {
1111
private const val TAG = "SystemMessageEntryPrefs"
1212
private const val PREFS_NAME = "system_message_entry_prefs"
1313
private const val KEY_SYSTEM_MESSAGE_ENTRIES = "system_message_entries"
14+
private const val KEY_DEFAULT_DB_ENTRIES_POPULATED = "default_db_entries_populated" // Added constant
1415

1516
private fun getSharedPreferences(context: Context): SharedPreferences {
1617
return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
@@ -31,7 +32,33 @@ object SystemMessageEntryPreferences {
3132

3233
fun loadEntries(context: Context): List<SystemMessageEntry> {
3334
try {
34-
val jsonString = getSharedPreferences(context).getString(KEY_SYSTEM_MESSAGE_ENTRIES, null)
35+
val prefs = getSharedPreferences(context)
36+
val defaultsPopulated = prefs.getBoolean(KEY_DEFAULT_DB_ENTRIES_POPULATED, false)
37+
38+
if (!defaultsPopulated) {
39+
Log.d(TAG, "Default entries not populated. Populating now.")
40+
val defaultEntries = listOf(
41+
SystemMessageEntry(
42+
title = "Example Task: Web Browsing",
43+
guide = "// TODO: Define a detailed guide for the AI on how to perform web browsing tasks. \n// Example: \"To search the web, first click on the search bar (element_id: 'search_bar'), then type your query using writeText('your query'), then click the search button (element_id: 'search_button').\""
44+
),
45+
SystemMessageEntry(
46+
title = "Example Task: Sending an Email",
47+
guide = "// TODO: Provide step-by-step instructions for composing and sending an email. \n// Specify UI elements to interact with (e.g., compose button, recipient field, subject field, body field, send button).\""
48+
),
49+
SystemMessageEntry(
50+
title = "General App Navigation Guide",
51+
guide = "// TODO: Describe common navigation patterns within this app or general Android OS that the AI should know. \n// Example: \"To go to settings, click on the 'Settings' icon. To return to the previous screen, use the back button.\""
52+
)
53+
)
54+
saveEntries(context, defaultEntries) // This saves them to KEY_SYSTEM_MESSAGE_ENTRIES
55+
prefs.edit().putBoolean(KEY_DEFAULT_DB_ENTRIES_POPULATED, true).apply()
56+
Log.d(TAG, "Populated and saved default database entries.")
57+
// The logic will now fall through to load these just-saved entries.
58+
}
59+
60+
// Existing logic to load entries from KEY_SYSTEM_MESSAGE_ENTRIES:
61+
val jsonString = prefs.getString(KEY_SYSTEM_MESSAGE_ENTRIES, null)
3562
if (jsonString != null) {
3663
// Log.v(TAG, "Loaded JSON: $jsonString") // Verbose
3764
val loadedEntries = Json.decodeFromString(ListSerializer(SystemMessageEntry.serializer()), jsonString)

0 commit comments

Comments
 (0)