Create an internal action for IoTool
When adding new actions to the IoTool application, it is encouraged to crate new extension rather than modifying the application itself. This document will help you create your own actions. This document will cover internal actions on your device.
General things
Before we start developing, we need to address that this document will only go briefly over the over the basics on how to create new extension. For more details on how to program such extension, please look at the document located in this link. In general extension only contains 2 types classes (provider and multiple receivers). Additionally it also contains JSON file. This file is use to link UI elements with parameters.
Classes
This particular example uses "Device" as a ServiceID. It contains a receiver class and a provider class.
Note: Sections marked with "//TODO" indicate that you may need to change that part of the code.
"IoToolActionProviderDeviceCall.java"
In this example, when the function is triggered, it calls a phone number set by the user.
package io.senlab.iotool.actionprovider.device.receiver;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import io.senlab.iotool.library.actions.IoToolActionReceiver;
/**
* Created by Sandi on 18.4.2016.
*/
public class IoToolActionProviderDeviceCall extends IoToolActionReceiver {
@Override
//TODO get the values from paramters in process them
protected void onActionExecute(Context context, Bundle args, Bundle extras) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
String uri = "tel:" + args.getString("phone_number");
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
requestPermissions(context, new String[] { Manifest.permission.CALL_PHONE });
}
}
@Override
//TODO fuction that returns parameter names. Parameter names are chosen here.
protected String[] getInputParamNames(String... requiredParameters) {
return new String[] { "phone_number" };
}
}
"DeviceActionsProvider.java"
Used to feed JSON file to the application
package io.senlab.iotool.actionprovider.device.provider;
import io.senlab.iotool.actionprovider.device.R;
import io.senlab.iotool.library.actions.IoToolActionProvider;
/**
* Created by Sandi on 20.4.2016.
*/
public class DeviceActionsProvider extends IoToolActionProvider {
@Override
protected int getActionsRawResourceId() {
return R.raw.actions;
}
}
"raw/actions.json"
Example on how to make JSON. This file will most likely be needed to be changed.
{
"package": "io.senlab.iotool.actionprovider.device",
"description": "Device actions",
"developer": "SenLab",
"actions": [
{
"classname": "receiver.IoToolActionProviderDeviceCall",
"description": "Call",
"params": [
{
"type": "io.senlab.iotool.actions.input.ActionInputPhoneNumber",
"description": "Phone number to call"
}
]
}
]
}