APIs For Third-Party Widget Developers
This page explains the protocol that allows third-party widget developers to include their widget configurations in Smart Launcher backups and restores.
Introduction
Smart Launcher backups usually include the configuration of the launcher itself and its widgets. By using the protocol described here, your widget configuration will also be saved and restored when users create or restore a backup. This improves the user experience and ensures that widget data is retained after device resets or migrations.
How it works
The protocol relies on a custom ContentProvider
that you implement in your app. This provider gives Smart Launcher a way to export and import your widget configuration data. Smart Launcher will:
Call your provider to export widget configuration data whenever a backup is created.
Call your provider to import widget configuration data whenever a user restores a backup.
What you need to do
Implement the BackupContentProvider Create a class in your project extending
BackupContentProvider
(see code below) and implement the abstract methodsexportWidgetConfig
andimportWidgetConfig
. These methods handle the actual reading and writing of widget configuration data.Register the provider in your Manifest Add your
BackupContentProvider
to your AndroidManifest.xml. Make sure to use an authority that follows this pattern:<your_package_name>.widgetConfigProvider
For example:Implement the exportWidgetConfig method In
exportWidgetConfig(appWidgetId: Int)
, read the current configuration of the specified widget and write it to a file (for example a temporary file in your app's files directory). You can use JSON, ZIP, or any format you like. Consider including a version number in the exported file so future updates can keep compatibility. If your configuration includes sensitive data, encrypt it before returning the file.Implement the importWidgetConfig method In
importWidgetConfig(file: File, appWidgetId: Int)
, read back the configuration from the file created by the export step. Decrypt if necessary. Then apply the configuration to the widget identified byappWidgetId
. This ensures that after the user restores their backup, the widget returns to the previous configuration.Update your code when configuration format changes Over time your widget configuration format might evolve. Keep the versioning of the exported data in mind and ensure your import method can still handle older versions. This provides a smoother experience for users restoring backups that were created with older versions of your app.
The BackupContentProvider class
Below is the BackupContentProvider
base class. Add it to your project and extend it. You only need to override exportWidgetConfig
and importWidgetConfig
to handle your widget's data.
Testing your implementation
Install your app with the
BackupContentProvider
.Configure a widget and verify that it works properly.
Launch a Smart Launcher backup and restore process.
Confirm that after restoring the backup, the widget configuration is the same as before.
If everything is correct, users will never lose their widget configuration when they switch devices, reinstall the launcher, or reset their device.
Last updated
Was this helpful?