Hello! Did you get a chance to see the Apple Watch yet? I bought a Smart Watch3 last year and have been playing with it for a while. Before, it was hard for me to catch the alert from my smartphone when I had it in my pocket. Now with the direct vibration to my wrist, I don’t miss the notifications anymore. And also, I don’t have to go through the fuss of taking out the phone from my pocket every time I need to check something. I am sure that the smartwatch has various usage possibilities.
So, the Smart Watch3 works on an OS called Android Wear. There is an SDK to create an application for this OS as well. This blog will cover about an application I have made that is related to Salesforce.
What Kind of App Is It?
Application Features:
Check incomplete tasks
Add new tasks
Complete tasks
Sync Salesforce ToDo’s to Google calendar (just for fun)
Check today’s schedule (just for fun)
It is a simple app that allows you to manage your tasks on Android Wear using Salesforce ToDo service. I will go over each feature and its code.
Check Incomplete Tasks
When you start the app on Android Wear, a list of incomplete tasks will show up.
You can scroll down to check the entire list. The ToDo data is the data saved on Salesforce ToDo. However, it does not go retrieve it every time the app is launched. Smart Watch3 itself does not connect to the internet. So, a program on the smartphone side calls Salesforce RestApi to retrieve the record data from the ToDo object.
The data in the smartphone will be synced to the Smart Watch3 by Wearable Data Layer API. In future updates of Android Wear, it can connect itself to the internet via Wi-Fi. I hope it will apply to the Smart Watch3 soon. The connection method to Salesforce uses Salesforce Mobile SDK. Therefore, OAuth authorization is set already.
Maybe it is better in terms of API consumption to retrieve the data once there is an update to the data instead of setting a time interval to retrieve the data. Here is the code to call the Wearable Data Layer API for Check Task window (Activity).
@Override
protected void onStart(){
super.onStart();
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API).build();
mGoogleApiClient.connect();
PendingResult<DataItemBuffer> dataItems = Wearable.DataApi.getDataItems(mGoogleApiClient);
dataItems.setResultCallback(new ResultCallback<DataItemBuffer>() {
@Override
public void onResult(DataItemBuffer dataItems) {
for (DataItem dataItem : dataItems) {
if (dataItem.getUri().getPath().equals(“/task_checker”)) {
DataMap dataMap = DataMap.fromByteArray(dataItem.getData());
reloadViewData(dataMap.getDataMapArrayList(“todos”));
break;
}
}
}
});
}
I used GoogleApiClient to call Wearable Data Layer API asynchronously and to retrieve ToDo data that are stored in the Smartphone. The screen is refreshed when a new task is added. I have implemented this with OnStart override. OnResume might have been better to use.
Add New Task
The screen migrates to the voice recognition screen when the “Add” button is clicked.
Basically, the data input is done by voice. Here is the code after the “Add” button is clicked.
public void onButtonClicked(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, “The Contents of ToDo”);
//Open the voice input screen
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
The last line of code (7th row) calls the startActivityForeResult method. When the process is complete, on ActivityResult method written below is called and the screen will migrate to the confirmation screen.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
///Use the string that is received
Log.d(TAG, “Spoken Text:” + spokenText);
Intent intent = new Intent( this, ConfirmActivity.class );
intent.putExtra(“spokenText”, spokenText);
startActivity(intent);
}
super.onActivityResult(requestCode, resultCode, data);
}
The new task is added automatically after three seconds of waiting. You can cancel by clicking the cross button on the screen. I have used “DelayedConfirmationView.DelayedConfirmationListener” user interface because I wanted to automate the checking process after a certain time interval.
Lastly, I will go over the feature to add records to the Salesforce ToDo object. So, the Wearable Data Layer API is called to add data via the Smart Watch3. A program on the smartphone side has a class that inherits the WearableListenerService class. This will call the WearableListenerService’s onDataChanged method synchronously when a data is updated by the Wearable Data Layer API.
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
super.onDataChanged(dataEventBuffer);
for (DataEvent event : dataEventBuffer) {
DataItem dataItem = event.getDataItem();
if (dataItem.getUri().getPath().equals(“/task_checker/updated”) && event.getType() == DataEvent.TYPE_CHANGED) {
DataMap dataMap = DataMap.fromByteArray(event.getDataItem().getData()).getDataMap(“todo”);
if(dataMap != null){
upsertTask(dataMap);
deleteUpdatedTodo(dataItem.getUri());
}
break;
}
}
}
At this moment, the Salesforce RestAPI is called to add records to the ToDo object. Also, in parallel with the ToDo input processing, it is programmed to show the new task on the Smart Watch3 task list.
Task Complete
Basically, the logic is the same as adding new tasks to update the ToDo data records in Salesforce.
Sync Salesforce ToDo’s to Google Calendar (Just For Fun)
Android Wear has a standard feature to check and sync with smartphone’s Google Calendar ToDo.
Why not use this feature to see if I can sync Salesforce ToDo’s to Google Calendar?! Android Wear is provided with a feature that shows tasks, so implementation is not necessary. I created a program that runs on the smartphone settings and background.
I will omit the details but it basically sets the sync time between Salesforce tasks and Google Calendar. I used a class called AlarmManager which runs a program on the set time or interval. It retrieves data from Salesforce and updates the difference to the Google Calendar. As a result, I can check Google Calendar’s task and today’s ToDos on Salesforce simultaneously. Vice versa, salesforce tasks will show up on Google Calendar apps on the smartphone.
Summary
I have created an app that does not need configuration on the Salesforce organization. However, you can use Apex triggers if you wanted to make the notification flow as GCM–> Smartphone –>Wear. Furthermore, it might be fun to imagine the possibilities of making an app with sensor devices (lights, gyroscopes, magnets, accelerometers, and GPS) already built into the watch.
Commentaires