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
16 changes: 16 additions & 0 deletions Android/src/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- This is for AlarmManager#setExactAndAllowWhileIdle from Android S for setting a exact alarm to handle some background tasks. -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<!-- Same as SCHEDULE_EXACT_ALARM, but for target sdk 33 or above -->
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.apps.aicore.service.BIND_SERVICE" />
Expand Down Expand Up @@ -140,6 +144,18 @@
</intent-filter>
</service>

<receiver
android:name="com.google.ai.edge.gallery.notifications.NotificationReceiver"
android:exported="false" />

<receiver
android:name="com.google.ai.edge.gallery.notifications.BootReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="gallery_high_priority_push_channel" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
name: schedule-notification
description: Schedule a notification for a specific date or repeating daily.
---

# Schedule Notification

## Instructions

To schedule a notification, you must follow these exact steps:
1. First, if the notification doesn't need to repeat daily, call the `run_intent` tool with `intent` as `get_current_date_and_time` and `parameters` as `{}` to get the user's local date and time. Then explicitly calculate the scheduling date and time in your response. Write out:
- Today's exact date.
- The target day or relative time requested by the user (e.g., "tomorrow", "this Friday").
- The exact number of days you need to add to today's date.
- The final calculated dates, ensuring you correctly roll over to the next month or year if the added days exceed the days in the current month.
2. Call the `run_intent` tool with the following exact parameters:
- intent: schedule_notification
- parameters: A JSON string with the following fields:
- title: the title of the notification. String.
- message: the message content of the notification. String.
- hour: the hour of the day (0-23) for the notification. Integer.
- minute: the minute of the hour (0-59) for the notification. Integer.
- year: (optional) the year for the notification. Integer.
- month: (optional) the month (1-12) for the notification. Integer.
- day: (optional) the day of the month (1-31) for the notification. Integer.
- repeat_daily: (optional) true if the notification should repeat daily at this time. Boolean. - deeplink: (optional) the deeplink URI to open when the notification is tapped. String.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.ai.edge.gallery

import android.app.Application
import com.google.ai.edge.gallery.data.DataStoreRepository
import com.google.ai.edge.gallery.notifications.NotificationScheduleManager
import com.google.ai.edge.gallery.ui.theme.ThemeSettings
import com.google.firebase.FirebaseApp
import dagger.hilt.android.HiltAndroidApp
Expand All @@ -27,9 +28,13 @@ import javax.inject.Inject
class GalleryApplication : Application() {

@Inject lateinit var dataStoreRepository: DataStoreRepository
@Inject lateinit var notificationScheduleManager: NotificationScheduleManager

override fun onCreate() {
super.onCreate()
// Initialize the notification schedule manager to load the scheduled notifications from the
// disk.
notificationScheduleManager.initialize()

// Load saved theme.
ThemeSettings.themeOverride.value = dataStoreRepository.readTheme()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.net.toUri
import com.google.ai.edge.gallery.notifications.NotificationScheduleManagerEntryPoint
import com.google.ai.edge.gallery.proto.ScheduledNotification
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import dagger.hilt.android.EntryPointAccessors
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.Date
Expand Down Expand Up @@ -48,13 +51,27 @@ enum class IntentAction(val action: String) {
SEND_EMAIL("send_email"),
SEND_SMS("send_sms"),
CREATE_CALENDAR_EVENT("create_calendar_event"),
GET_CURRENT_DATE_AND_TIME("get_current_date_and_time");
GET_CURRENT_DATE_AND_TIME("get_current_date_and_time"),
SCHEDULE_NOTIFICATION("schedule_notification");

companion object {
fun from(action: String): IntentAction? = entries.find { it.action == action }
}
}

@JsonClass(generateAdapter = true)
data class ScheduleNotificationParams(
val title: String,
val message: String,
val hour: Int,
val minute: Int,
val year: Int? = null,
val month: Int? = null,
val day: Int? = null,
val repeat_daily: Boolean? = null,
val deeplink: String? = null,
)

object IntentHandler {
private const val TAG = "IntentHandler"

Expand Down Expand Up @@ -142,7 +159,64 @@ object IntentHandler {
)
currentDateAndTime
}
IntentAction.SCHEDULE_NOTIFICATION -> {
scheduleNotification(context, parameters)
}
null -> "failed"
}
}

fun scheduleNotification(context: Context, parameters: String): String {
try {
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(ScheduleNotificationParams::class.java)
val params = jsonAdapter.fromJson(parameters)
if (params != null) {
val notificationProtoBuilder =
ScheduledNotification.newBuilder()
.setId(java.util.UUID.randomUUID().toString())
.setTitle(params.title)
.setMessage(params.message)
.setHour(params.hour)
.setMinute(params.minute)
.setChannelId("agent_skill_tasks_channel")
.setChannelName("Agent Skill Task")
if (params.year != null) {
notificationProtoBuilder.setYear(params.year)
}
if (params.month != null) {
notificationProtoBuilder.setMonth(params.month)
}
if (params.day != null) {
notificationProtoBuilder.setDay(params.day)
}
if (params.deeplink != null) {
notificationProtoBuilder.setDeeplink(params.deeplink)
}
if (params.repeat_daily != null) {
notificationProtoBuilder.setRepeatDaily(params.repeat_daily)
}

val entryPoint =
EntryPointAccessors.fromApplication(
context.applicationContext,
NotificationScheduleManagerEntryPoint::class.java,
)
val success =
entryPoint
.notificationScheduleManager()
.scheduleNotification(notificationProtoBuilder.build())
if (!success) {
return "failed"
}
return "succeeded"
} else {
Log.e(TAG, "Failed to parse schedule_notification parameters: $parameters")
return "failed"
}
} catch (e: Exception) {
Log.e(TAG, "Failed to parse schedule_notification parameters: $parameters", e)
return "failed"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ constructor(
try {
val skillAssetDirs = context.assets.list("skills") ?: emptyArray()
for (dirName in skillAssetDirs) {
// Temporarily disable this skill in built-in skills.
if (dirName == "create-calendar-event") {
// TODO: Temporarily disable some built-in skills. Enable them when ready.
if (dirName == "create-calendar-event" || dirName == "schedule-notification") {
continue
}
val skillMdPath = "skills/$dirName/SKILL.md"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.ai.edge.gallery.notifications

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import dagger.hilt.android.EntryPointAccessors

/**
* Reschedules all notifications after the device boots up.
*
* This receiver is triggered by the ACTION_BOOT_COMPLETED broadcast.
*/
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
Log.d(TAG, "Boot completed received, rescheduling notifications")
try {
val entryPoint =
EntryPointAccessors.fromApplication(
context.applicationContext,
NotificationScheduleManagerEntryPoint::class.java,
)
entryPoint.notificationScheduleManager().rescheduleAllNotifications()
} catch (e: Exception) {
Log.e(TAG, "Failed to reschedule notifications on boot", e)
}
}
}

companion object {
private const val TAG = "BootReceiver"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.ai.edge.gallery.notifications

import android.app.PendingIntent
import android.content.Context
import android.content.Intent

// Helper class for building a PendingIntent for a notification.
object NotificationPendingIntentHelper {
const val EXTRA_ID = "id"
const val EXTRA_TITLE = "title"
const val EXTRA_MESSAGE = "message"
const val EXTRA_DEEPLINK = "deeplink"
const val EXTRA_REPEAT_DAILY = "repeat_daily"
const val EXTRA_HOUR = "hour"
const val EXTRA_MINUTE = "minute"
const val EXTRA_CHANNEL_ID = "channel_id"
const val EXTRA_CHANNEL_NAME = "channel_name"

fun buildNotificationPendingIntent(
context: Context,
id: String,
title: String,
message: String,
deeplink: String,
repeatDaily: Boolean,
hour: Int,
minute: Int,
channelId: String,
channelName: String,
): PendingIntent {
val receiverClass =
Class.forName("com.google.ai.edge.gallery.notifications.NotificationReceiver")
val intent =
Intent(context, receiverClass).apply {
putExtra(EXTRA_ID, id)
putExtra(EXTRA_TITLE, title)
putExtra(EXTRA_MESSAGE, message)
putExtra(EXTRA_DEEPLINK, deeplink)
putExtra(EXTRA_REPEAT_DAILY, repeatDaily)
putExtra(EXTRA_HOUR, hour)
putExtra(EXTRA_MINUTE, minute)
putExtra(EXTRA_CHANNEL_ID, channelId)
putExtra(EXTRA_CHANNEL_NAME, channelName)
}
return PendingIntent.getBroadcast(
context,
id.hashCode(),
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
}
}
Loading
Loading