Aide Android Java Ide Download

0 views
Skip to first unread message

Janet Denzel

unread,
May 22, 2024, 9:08:15 PM5/22/24
to derszilhardsil

In the previous codelab, you used static shortcuts to implement commonly used built-in intents (BII) in a sample app. Android developers use App Actions to extend app functionality to Google Assistant.

aide android java ide download


Download 🆓 https://t.co/dWCYN3CweD



Static shortcuts are bundled with an app and can only be updated by releasing new versions of the app. Enabling voice functionality for dynamic elements in an app, like user-generated content, is achieved using dynamic shortcuts. Apps push dynamic shortcuts after users perform relevant actions, like creating a new note in a task tracking app. With App Actions, you enable these shortcuts for voice by binding them to a BII, enabling users to access their content from Assistant by saying things like, "Hey Google, open my grocery list on ExampleApp."

In this codelab, you'll enable dynamic shortcuts for voice in a sample to-do list Android app, enabling users to ask Assistant to open the task list items they create in the app. You accomplish this using using Android architecture patterns, specifically the repository, service locator and ViewModel patterns.

This codelab builds on the App Actions concepts covered in the previous codelab, particularly BIIs and static shortcuts. If you are new to App Actions, we recommend completing that codelab before continuing.

For a dynamic shortcut to be accessible from Assistant, it needs to be bound to a relevant BII. When a BII with a shortcut is triggered, Assistant matches parameters in the user request to keywords defined in the bound shortcut. For example:

After binding your shortcuts to a BII, the next step is to enable Assistant to ingest these shortcuts by adding the Google Shortcuts Integration library to your project. With this library in place, Assistant will be aware of each shortcut pushed by your app, enabling users to launch those shortcuts by using the shortcut's trigger phrase in Assistant.

This codelab uses a sample to-do list app built for Android. With this app, users can add items to lists, search for task list items by category, and filter tasks by completion status. Download and prepare the sample app by completing this section.

Updating the app's application ID uniquely identifies the app on your test device and avoids a "Duplicate package name" error if the app is uploaded to the Play Console. To update the application ID, open app/build.gradle:

Note: After updating applicationId, you will receive a "Gradle files have changed since last project sync..." warning in Android Studio. Click Sync Now to avoid issues when uploading your app.

Several classes in our sample app will call the ShortcutManagerCompat API to push and manage dynamic shortcuts. To reduce code redundancy, you will implement a repository to enable your project classes to easily manage dynamic shortcuts.

The repository design pattern provides a clean API for managing shortcuts. The advantage to a repository is the details of the underlying API are uniformly abstracted behind a minimal API. Implement the repository by following these steps:

Create a new Kotlin class named ShortcutsRepository in the com.example.android.architecture.blueprints.todoapp.data.source package. You can find this package organized in the app/src/main/java folder. You will use this class to implement an interface providing a minimal set of methods covering our codelab use case.

Note: The ShortcutManagerCompat API methods added here are synchronous (blocking) APIs and must not be called on the main thread. We're using the @WorkerThread annotation to make it clear that the caller of these methods must also not be on the main thread.

In the preceding code sample we passed appContext to the API. This is a class property holding an Application Context. It's important to use an Application Context (as opposed to an Activity Context) to avoid memory leaks, since the context might be retained longer than the host activity lifecycle.

Additionally, the API requires that we pass a ShortcutInfoCompat object for the Task object. In the preceding code sample we accomplish this by by calling the createShortcutCompat private method, which we will update to create and return a ShortcutInfoCompat object. To accomplish this, update the createShortcutCompat stub with the following code:

With the ShortcutsRepository class created, the next step is to make instantiated objects of this class available to the rest of the app. This app manages class dependencies by implementing the service locator pattern. Open the service locator class using the class browser in Android Studio by going to Navigate > Class and typing "ServiceLocator". Click the resulting Kotlin file to open it in your IDE.

With your shortcut service created, you're ready to start pushing shortcuts. Since users generate content (task items) in this app and expect to be able to return to them later, we will voice-enable access to this content by pushing a dynamic shortcut that is bound to the GET_THING BII each time a user creates a new task. This enables Assistant to launch users directly to their requested task item when they trigger the BII by asking things like, "Hey Google, open my grocery list on SampleApp."

We first need to make the ShortcutsRepository service available to AddEditTaskViewModel. To accomplish this, import the service to ViewModelFactory, the factory class the app uses to instantiate ViewModel objects, including AddEditTaskViewModel.

Finish the ViewModelFactory changes by going one layer up, and pass ShortcutsRepository to the factory's constructor. Open Android Studio's file browser by going to Navigate > File and typing "FragmentExt.kt". Click the resulting Kotlin file located in the util package to open it in your IDE.

With the ShortcutsRepository abstraction class available to the sample app's ViewModel classes, you update AddEditTaskViewModel, the ViewModel class responsible for creating notes, to push a dynamic shortcut each time a user creates a new note.

Note: In this class, the app calls shortcutsRepository.pushShortcut() method from a new coroutine. This is done to ensure the shortcut code doesn't block the main thread, as ShortcutManagerCompat is a blocking API.

In addition to pushing new dynamic shortcuts at runtime, your app can update them to reflect the current state of your user content and preferences. It's good practice to update existing shortcuts whenever a user modifies the destination item, like renaming a task in our sample app. You should also delete a corresponding shortcut whenever the destination resource is removed to avoid displaying broken shortcuts to user.

Modify AddEditTaskViewModel to update a dynamic shortcut whenever a user changes the details of a task item. First, update the body of the class with the following code to add an update function that utilizes our repository class:

Our sample app shortcuts should be removed whenever a user deletes a task. In the sample app, task deletion logic lives in the TaskDetailViewModel class. Before we update this class, we need to update ViewModelFactory again to pass shortcutsRepository into TaskDetailViewModel.

Congratulations! Thanks to you, users of our sample app can easily return to the notes they create by asking Assistant things like, "Hey Google, open my grocery list on ExampleApp". Shortcuts encourage deeper user engagement by making it easy for users to replay frequently used actions in your app.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

92d504bec8
Reply all
Reply to author
Forward
0 new messages