Skip to content

Instantly share code, notes, and snippets.

@ashwin-sp
Created June 4, 2018 02:51
Show Gist options
  • Save ashwin-sp/8916332b535dbbc8312f579d9f6218e1 to your computer and use it in GitHub Desktop.
Save ashwin-sp/8916332b535dbbc8312f579d9f6218e1 to your computer and use it in GitHub Desktop.
override fun onBindSlice(sliceUri: Uri): Slice? {
return if (sliceUri.path == "/task") {
// Path recognized. Customize the Slice using the androidx.slice.builders API.
// Note: ANR and StrictMode are enforced here so don't do any heavy operations.
// Only bind data that is currently available in memory.
ListBuilder(context, sliceUri, INFINITY)
.addRow {it.setTitle("Task Completed? ")
it.setPrimaryAction(createActivityAction())
it.addEndItem(createToggleAction(!state))
}
.build()
} else {
// Error: Path not found.
ListBuilder(context, sliceUri, INFINITY)
.addRow { it.setTitle("URI not found.") }
.build()
}
}
private fun createActivityAction(): SliceAction {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(CHECKED, state).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return SliceAction(PendingIntent.getActivity(context, 0, intent, 0),
IconCompat.createWithResource(context, R.drawable.ic_launcher_foreground),
"Open MainActivity."
)
}
private fun createToggleAction(boolean: Boolean): SliceAction{
val intent = Intent()
intent.setClass(context, MyReceiver::class.java)
intent.putExtra(CHECKED, boolean)
val toggleIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
return SliceAction(toggleIntent, "Toggle", state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment