Slices — Forming a bond with the G Search, Assistant and beyond …

Ashwin S
AndroidPub
Published in
6 min readJun 4, 2018
❤ My Slice at the Pizza Hut ❤

An avid Android user myself, I would prefer the search to list down whatever I am looking for at the start of my day. Suppose I want to perform a simple action like marking a task complete. What if you remember what that task is, search for it and mark it as complete right from the Google search list? Well, say hello to Slices!

Slices accelerate navigation and task completion indexed by app names or general terms relative to an action in the application.

Slice(s) is/are a single action/view or group of actions/views/more Slices.

There are 3 views in Slices:

  • Shortcut: A shortcut is presented as an icon and a text label representing the main content or action associated with the slice.
  • Small: The small format has a restricted height and can present a single SliceItem or a limited collection of items.
  • Large: The large format displays multiple small templates in a list, if scrolling is not enabled (see setScrollable(boolean)) the view will show as many items as it can comfortably fit.

Current Scenario

Well, Slices are going to be supported in the Google Search and the Google Assistant sometime in the near future. It would be awesome if you develop Slices for your app beforehand and it starts appearing in the search bar and the Assistant from Day 0. Wondering how to preview your own Slices? Well there are 2 ways:

Create a dummy activity in your own application and preview your Slice using the SliceView class.

Well, this saves your time from implementing the Slice Previewer. And I have opted for this option and going to take this article forward with this method.

Components of Slices

  • Content URI

The URI used to build the Slices. By default the absolute path of the URI is

content://applicationId/desired_search_term

You can either pass the default URI or the URI path-appended by the desired term or a custom schemed URL.

  • Slice Provider

Slice Provider is responsible for 2 primary functions:

  1. Instantiates required objects.
  2. Parses the URI or URL and builds a Slice out of it.
  • Broadcast Receiver (for Dynamic Slices)

To update the data from the Slice in your application.

How it basically works?

  • For a simple navigation from the Slice to the application.
Image from Google I/O

The Slice Provider parses the URI/URL in it’s onBindSlice() method which enables the Slice Presenter(Google Search/Assistant) to call the System APIs to create a Slice with the required content and action to navigate into the application.

  • For data updation from the Slice or the application to be reflected in vice-versa.
Image from Google I/O

In addition to the above, we can call notifyChange() to update the Slice based on your action in the application and we can utilise a Broadcast receiver to do the vice-versa.

Excited yet? Well, let’s get started by building a simple task application in which you can view and change the status of the task both from the Slice and the application! 😃

Prerequisites

  • Android Studio 3.2 Canary

For the AndroidX refactoring option.

  • The project should target Android P. i.e.,
android {
compileSdkVersion 'android-P'
defaultConfig {
...
minSdkVersion 19
targetSdkVersion 28
...
}
...
}
  • Project should have the following dependancies as such,
implementation 'androidx.slice:slice-core:1.0.0-alpha1'
implementation 'androidx.slice:slice-builders:1.0.0-alpha1'

Getting your hands dirty

  1. Create a Slice Provider either by creating a class and extending SliceProvider abstract class or by new file creation dialog which does the job for you by creating a Slice Provider template.

( I went with the later. )

Upon creation, an entry is made for the provider in the Android Manifest. Also, an entry for the Receiver which is going to pass the data to your app is added when you create a Broadcast receiver for your app through the same method.

2. Override the onBindSlice() method to build a slice based on a path say “task”. We will be building our Slice with a title, a primary action for navigating to the application and an end item for marking the status of the task.

Slices work similar to a widget wherein we create a pending intent either to open some activity inside the application or just pass updated values to the application through a broadcast receiver. Create an action by passing this pending intent and an Image drawable for the Shortcut view or a boolean to instantiate the state of the toggle to the SliceAction class.

3. Update the toggle changes to the Slice from the application by calling the notifyChange() wherever necessary.

this.contentResolver.notifyChange(MySliceProvider.getUri(this,"/"),null)

Since relative content URI is going to be unique for your application, we can include a useful Util in the Provider returning the URI based on the path to be appended as,

4. Update the toggle change to the application from the Slice by overriding the onReceive() method of your Broadcast Receiver to include the notifyChange() call and the sendBroadcast() call. Here is a sample,

Now apply those changes to your activity upon open by implementing the onReceive() method of your Broadcast receiver in the activity class.

Et voila your Slice is ready!

Tips and warnings

Well, I do have to remind you that Slices are still under development and there are certain problems which you would encounter upon development.

  1. You will get an “Android Resource Linking failed” problem if you do not target Android P and refractor your dependencies to AndroidX.
  2. Do not change the dependencies to alpha2 as the SliceViewer doesn’t seem to preview the Slices when the newer version of the Slice builders is added(at the time of writing this blog).
  3. Slice Viewer might crash if you do not provide a proper content URI in the search.
  4. If you are planning to catch URLs, just add the URL entry in the manifest and override onMapIntentToUri() in the Provider class.
  5. Permissions by default are going to be handled by the Slice Provider so do not add permissions for Slices in the manifest.
  6. Don’t perform heavy tasks like API calls and CRUD operations in the onBindSlice() method.
  7. While creating your SliceProvider from the chooser, the following dependencies are added by mistake instead of the correct ones.
implementation 'androidx.slice:slices-core:1.0.0-alpha1'
implementation 'androidx.slice:slices-builders:1.0.0-alpha1'

Remove these dependancies before proceeding and only include the dependancies as stated earlier in this blog.

Well, that’s it, folks, this is how we build a simple Slice. I will throw more light on the search indexing and templates going forward in a separate article. Here is the link to the project which has the sample.

This blog is inspired by the Google I/O 2018 talk on “Android Slices!

--

--