Header Ads

Using both the Control & Notification APIs for customised UIs in your SmartWatch 2 extension [code]

A few of you SmartWatch developers out there have recently asked us if it’s possible to use both the Control API and Notification API together in a single extension. The good news is – yes, you can use both of these Smart Extension APIs in a single SmartWatch extension, for any device that supports the Smart Extension APIs with both control and notification, including the SmartWatch 2. Read on to get the full details from Marlin Liew, Developer Technical Services Engineer at Sony, who provides a code example showing how to use both of these APIs in a single project.
Marlin Liew
Marlin Liew, Sony Developer Technical Services Engineer.
The Control API and Notification API are parts of the Smart Extension APIs, which enable apps to access the general controls of a number of Sony Smart Accessories, such as SmartWatch 2 and the original SmartWatch. Note that apps for Sony’s smart accessories are called Smart Extensions in our terminology. The Control API, as the name implies, lets your Smart Extension take full control over the accessory’s display. The Notification API is used by simple event driven data providers, such as SMS, MMS, and missed calls.
As a developer, you may want to have notifications in, for example, your SmartWatch 2 extension, but you may also want the flexibility to customise the UI differently than the default Notification view.
Default Notification view for SmartWatch 2.
Default Notification view for SmartWatch 2.
 At the moment, customisation of the Notification UI is not supported in the Smart Extension UI. This is because the host application is in charge of creating the UI for the device that it’s connected to. However, you can still use the Notification UI as a starting point, and then open a customised UI. To do this, start your extension in Notification mode, then, once a notification is displayed from the menu, launch a Control extension, where you can design the UI any way you like.
The following diagram provides an example of how a SmartWatch 2 extension application would use both the Notification and Control APIs. On the SmartWatch 2, the user would touch on the menu button of the notification window, then press 1 to launch and view the first message. 
Control & Notification API diagram
Example of a SmartWatch 2 extension application flow using both the Notification and Control APIs.
How to integrate the Notification and Control APIs
Note: This tutorial assumes that you have a SmartWatch 2 extension already under development. For help on developing a new SmartWatch 2 extension, please refer to our How to create an app extension for SmartWatch 2 tutorial to get started.
The easiest way to integrate the Notification and Control APIs in your existing extension is to first start with the Control extension template. This template is included as part of the Sony Add-on SDK package, and can be found in the folder /samples/SmartExtensions/SampleControlExtension. Once you have the template, follow the steps below:
1. Modify the SampleRegistrationInformation.java to tell the host application to support the Notification API. This is done in the method getRequiredNotificationApiVersion().
    public int getRequiredNotificationApiVersion() {
        return 1; //update this value to 1
    } 
2. Add the following line to getExtensionRegistrationConfiguration() to tell the extension to start in Notification mode:
values.put(Registration.ExtensionColumns.LAUNCH_MODE, Registration.LaunchMode.NOTIFICATION);
Now you should be able to receive notifications in your Control extension.
Testing notifications in your Control extension
To send a sample event, we’ll copy the addData() method from the Notification sample and add it to SampleExtensionService.java:
private void addData() {
        Log.d(LOG_TAG, “addData”);
        Random rand = new Random();
        int index = rand.nextInt(5);
        String name = NAMES[index];
        String message = MESSAGE[index];
        long time = System.currentTimeMillis();
        long sourceId = NotificationUtil
                .getSourceId(this, EXTENSION_SPECIFIC_ID);
        if (sourceId == NotificationUtil.INVALID_ID) {
            Log.e(LOG_TAG, “Failed to insert data”);
            return;
        }
        ContentValues eventValues = new ContentValues();
        eventValues.put(Notification.EventColumns.EVENT_READ_STATUS, false);
        eventValues.put(Notification.EventColumns.DISPLAY_NAME, name);
        eventValues.put(Notification.EventColumns.MESSAGE, message);
        eventValues.put(Notification.EventColumns.PERSONAL, 1);
        eventValues.put(Notification.EventColumns.PUBLISHED_TIME, time);
        eventValues.put(Notification.EventColumns.SOURCE_ID, sourceId);
        try {
            getContentResolver().insert(Notification.Event.URI, eventValues);
        } catch (IllegalArgumentException e) {
            Log.e(LOG_TAG, “Failed to insert event”, e);
        } catch (SecurityException e) {
            Log.e(LOG_TAG, “Failed to insert event, is Live Ware Manager installed?”, e);
        } catch (SQLException e) {
            Log.e(LOG_TAG, “Failed to insert event”, e);
        }
    }
Now, just add a call to addData() in onCreate():
public void onCreate() {
        super.onCreate();
        addData();
        Log.d(SampleExtensionService.LOG_TAG, “SampleControlService: onCreate”);
    }  
Creating your menus
The final thing to do is create your menus and to open your Control extension from an item in the menu. To create your menu, add the following code to getSourceRegistrationConfiguration() in SampleRegistrationInformation.java:
                sourceValues.put(Notification.SourceColumns.ACTION_ICON_1,
                ExtensionUtils.getUriString(mContext, R.drawable.actions_1));
Here, R.drawable.actions_1 is the icon to be shown in the menu.
We also need to update the onViewEvent() method in SampleExtensionService.java to handle this action and send an Intent to start the Control:
if (Notification.SourceColumns.ACTION_1.equals(action)) {
            Intent intent = new Intent(Control.Intents.CONTROL_START_REQUEST_INTENT);
            intent.putExtra(Control.Intents.EXTRA_AEA_PACKAGE_NAME, “com.sony.samplecontrolnotification”);
            intent.setPackage(“com.sonymobile.smartconnect.smartwatch2″);
            sendBroadcast(intent, Registration.HOSTAPP_PERMISSION);
        }
Now, when you run your extension, it should start in Notification mode, as shown in the diagram above. The methodaddData() will be called to create a list of dummy notifications.  From here, select a notification to view its contents. Press the menu button to bring up the menu, then select “1”.  The Control part of the extension should now start and you can customize this to work theway you want.
***
We hope this has given you everything you need to add both the Control and Notification APIs into your extension. For further reference, check out this example SmartWatch 2 project file that has the Control and Notifications APIs incorporated into it. If you have any questions about these Smart Extension APIs or about Smart Accessory extension development, please drop us a line below and we’ll get back to you as soon as we can.

No comments:

Powered by Blogger.