Commit 6d3b5a2b authored by Larry Sachs's avatar Larry Sachs Committed by Rick Bell

Android OBT using Java O-O classes

Change-Id: I913c4876092bcc8d90ab00d708ee86519de0848a
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/29429Tested-by: default avatarIoTivity Jenkins <jenkins-daemon@iotivity.org>
Reviewed-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
Reviewed-by: default avatarRick Bell <richard.s.bell@intel.com>
parent d1f8602f
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "org.iotivity.onboardingtool"
minSdkVersion 23
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ["src/main/jniLibs", "$buildDir/native-libs"]
}
}
splits {
abi {
enable true
reset()
include 'x86_64', 'armeabi'
universalApk false
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile files('libs/iotivity-lite.jar')
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/larrys/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.iotivity.onboardingtool">
<uses-feature android:name="android.hardware.nfc" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".OnBoardingActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package org.iotivity.onboardingtool;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.iotivity.OCAcePermissionsMask;
import org.iotivity.OCAceWildcard;
import org.iotivity.OCInterfaceMask;
import org.iotivity.OCObtDeviceStatusHandler;
import org.iotivity.OCUuidUtil;
import org.iotivity.oc.OcAceResource;
import org.iotivity.oc.OcObt;
import org.iotivity.oc.OcSecurityAce;
import java.util.HashSet;
import java.util.Set;
class AcePropertiesHelper {
private static final String TAG = AcePropertiesHelper.class.getSimpleName();
private OnBoardingActivity activity;
private OcObt obt;
private String uuid;
private OCObtDeviceStatusHandler provisionAce2Handler;
private int resourceNumber;
AcePropertiesHelper(OnBoardingActivity activity, OcObt obt, String uuid, OCObtDeviceStatusHandler provisionAce2Handler) {
this.activity = activity;
this.obt = obt;
this.uuid = uuid;
this.provisionAce2Handler = provisionAce2Handler;
}
void getProperties(final OcSecurityAce ace) {
++resourceNumber;
final OcAceResource aceResource = new OcAceResource(ace);
if (aceResource == null) {
Toast.makeText(activity, "Error allocating new ace resource", Toast.LENGTH_LONG).show();
return;
}
AlertDialog.Builder hrefAlertDialogBuilder = new AlertDialog.Builder(activity);
LinearLayout layout = new LinearLayout(activity);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
final EditText input = new EditText(activity);
layout.addView(input);
String title = activity.getResources().getString(R.string.hrefDialogTitle).replace("?", Integer.toString(resourceNumber));
hrefAlertDialogBuilder.setTitle(title);
hrefAlertDialogBuilder.setMessage(R.string.hrefDialogMessage);
hrefAlertDialogBuilder.setView(layout);
hrefAlertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String href = input.getText().toString().trim();
if (!href.isEmpty()) {
if (href.length() > 63) {
href = href.substring(0, 63);
}
aceResource.setHref(href);
aceResource.setWildcard(OCAceWildcard.OC_ACE_NO_WC);
getResourceTypes(ace, aceResource);
} else {
AlertDialog.Builder wildcardAlertDialogBuilder = new AlertDialog.Builder(activity);
String title = activity.getResources().getString(R.string.wildcardDialogTitle).replace("?", Integer.toString(resourceNumber));
wildcardAlertDialogBuilder.setTitle(title);
wildcardAlertDialogBuilder.setItems(R.array.wildcardOptions, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
aceResource.setWildcard(OCAceWildcard.OC_ACE_NO_WC);
break;
case 1:
aceResource.setWildcard(OCAceWildcard.OC_ACE_WC_ALL);
break;
case 2:
aceResource.setWildcard(OCAceWildcard.OC_ACE_WC_ALL_SECURED);
break;
case 3:
aceResource.setWildcard(OCAceWildcard.OC_ACE_WC_ALL_PUBLIC);
break;
default:
break;
}
getResourceTypes(ace, aceResource);
}
});
Dialog wildcardDialog = wildcardAlertDialogBuilder.create();
wildcardDialog.show();
}
}
});
Dialog hrefDialog = hrefAlertDialogBuilder.create();
hrefDialog.show();
}
private void getResourceTypes(final OcSecurityAce ace, final OcAceResource aceResource) {
AlertDialog.Builder resourceTypesAlertDialogBuilder = new AlertDialog.Builder(activity);
LinearLayout layout = new LinearLayout(activity);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
final EditText input = new EditText(activity);
layout.addView(input);
String title = activity.getResources().getString(R.string.resourceTypesDialogTitle).replace("?", Integer.toString(resourceNumber));
resourceTypesAlertDialogBuilder.setTitle(title);
resourceTypesAlertDialogBuilder.setMessage(R.string.resourceTypesDialogMessage);
resourceTypesAlertDialogBuilder.setView(layout);
resourceTypesAlertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String resourceTypes = input.getText().toString().trim();
if (!resourceTypes.isEmpty()) {
String[] types = resourceTypes.split(",");
aceResource.setNumberOfResourceTypes(types.length);
for (String resType : types) {
resType = resType.trim();
if (!resType.isEmpty()) {
if (resType.length() > 127) {
resType = resType.substring(0, 127);
}
aceResource.bindResourceType(resType);
}
}
}
getInterfaces(ace, aceResource);
}
});
Dialog resourceTypesDialog = resourceTypesAlertDialogBuilder.create();
resourceTypesDialog.show();
}
private void getInterfaces(final OcSecurityAce ace, final OcAceResource aceResource) {
final Set<Integer> interfaces = new HashSet<>();
AlertDialog.Builder interfacesAlertDialogBuilder = new AlertDialog.Builder(activity);
String title = activity.getResources().getString(R.string.interfacesDialogTitle).replace("?", Integer.toString(resourceNumber));
interfacesAlertDialogBuilder.setTitle(title);
interfacesAlertDialogBuilder.setMultiChoiceItems(R.array.interfaces, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
interfaces.add(which);
} else {
if (interfaces.contains(which)) {
interfaces.remove(which);
}
}
}
});
interfacesAlertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!interfaces.isEmpty()) {
for (int iface : interfaces) {
switch (iface) {
case 0:
aceResource.bindInterface(OCInterfaceMask.BASELINE);
break;
case 1:
aceResource.bindInterface(OCInterfaceMask.LL);
break;
case 2:
aceResource.bindInterface(OCInterfaceMask.B);
break;
case 3:
aceResource.bindInterface(OCInterfaceMask.R);
break;
case 4:
aceResource.bindInterface(OCInterfaceMask.RW);
break;
case 5:
aceResource.bindInterface(OCInterfaceMask.A);
break;
case 6:
aceResource.bindInterface(OCInterfaceMask.S);
break;
default:
break;
}
}
}
AlertDialog.Builder moreResourcesAlertDialogBuilder = new AlertDialog.Builder(activity);
moreResourcesAlertDialogBuilder.setTitle(R.string.addMoreResourceDialogMessage);
moreResourcesAlertDialogBuilder.setPositiveButton("Yes"/*android.R.string.yes*/, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getProperties(ace);
}
});
moreResourcesAlertDialogBuilder.setNegativeButton("No"/*android.R.string.no*/, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getPermissions(ace);
}
});
Dialog moreResourcesDialog = moreResourcesAlertDialogBuilder.create();
moreResourcesDialog.show();
}
});
Dialog interfacesDialog = interfacesAlertDialogBuilder.create();
interfacesDialog.show();
}
private void getPermissions(final OcSecurityAce ace) {
final Set<Integer> permissions = new HashSet<>();
AlertDialog.Builder permissionsAlertDialogBuilder = new AlertDialog.Builder(activity);
permissionsAlertDialogBuilder.setTitle(R.string.permissionsDialogTitle);
permissionsAlertDialogBuilder.setMultiChoiceItems(R.array.permissions, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
permissions.add(which);
} else {
if (permissions.contains(which)) {
permissions.remove(which);
}
}
}
});
permissionsAlertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!permissions.isEmpty()) {
for (int permission : permissions) {
switch (permission) {
case 0:
ace.addPermission(OCAcePermissionsMask.CREATE);
break;
case 1:
ace.addPermission(OCAcePermissionsMask.RETRIEVE);
break;
case 2:
ace.addPermission(OCAcePermissionsMask.UPDATE);
break;
case 3:
ace.addPermission(OCAcePermissionsMask.DELETE);
break;
case 4:
ace.addPermission(OCAcePermissionsMask.NOTIFY);
break;
default:
break;
}
}
}
new Thread(new Runnable() {
public void run() {
if (obt.provisionAce(OCUuidUtil.stringToUuid(uuid), ace, provisionAce2Handler) < 0) {
final String msg = "Failed to provision ace credentials for " + uuid;
Log.d(TAG, msg);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
});
}
}
}).start();
}
});
Dialog permissionsDialog = permissionsAlertDialogBuilder.create();
permissionsDialog.show();
}
}
package org.iotivity.onboardingtool;
import android.util.Log;
import android.widget.Toast;
import org.iotivity.OCObtDeviceStatusHandler;
import org.iotivity.OCUuid;
import org.iotivity.OCUuidUtil;
public class DeviceResetHandler implements OCObtDeviceStatusHandler {
private static final String TAG = DeviceResetHandler.class.getSimpleName();
private OnBoardingActivity activity;
public DeviceResetHandler(OnBoardingActivity activity) {
this.activity = activity;
}
@Override
public void handler(OCUuid uuid, int status) {
String deviceId = OCUuidUtil.uuidToString(uuid);
final String msg = (status >= 0) ?
"Successfully performed hard reset on device " + deviceId :
"Error performing hard reset on device " + deviceId + ", status = " + status;
Log.d(TAG, msg);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
});
}
}
package org.iotivity.onboardingtool;
import android.util.Log;
import android.widget.Toast;
import org.iotivity.OCObtDeviceStatusHandler;
import org.iotivity.OCUuid;
import org.iotivity.OCUuidUtil;
public class GenerateRandomPinHandler implements OCObtDeviceStatusHandler {
private static final String TAG = GenerateRandomPinHandler.class.getSimpleName();
private OnBoardingActivity activity;
public GenerateRandomPinHandler(OnBoardingActivity activity) {
this.activity = activity;
}
@Override
public void handler(OCUuid uuid, int status) {
String deviceId = OCUuidUtil.uuidToString(uuid);
final String msg = (status >= 0) ?
"Successfully generated Random PIN on device " + deviceId :
"Error generating Random PIN on device " + deviceId + ", status = " + status;
Log.d(TAG, msg);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
});
}
}
package org.iotivity.onboardingtool;
import android.util.Log;
import android.widget.Toast;
import org.iotivity.OCObtDeviceStatusHandler;
import org.iotivity.OCUuid;
import org.iotivity.OCUuidUtil;
public class JustWorksHandler implements OCObtDeviceStatusHandler {
private static final String TAG = JustWorksHandler.class.getSimpleName();
private OnBoardingActivity activity;
public JustWorksHandler(OnBoardingActivity activity) {
this.activity = activity;
}
@Override
public void handler(OCUuid uuid, int status) {
String deviceId = OCUuidUtil.uuidToString(uuid);
final String msg = (status >= 0) ?
"Successfully performed OTM on device " + deviceId :
"Error performing ownership transfer on device " + deviceId + ", status = " + status;
Log.d(TAG, msg);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
});
}
}
package org.iotivity.onboardingtool;
import android.util.Log;
import org.iotivity.OCMainInitHandler;
import org.iotivity.oc.OcDevice;
import org.iotivity.oc.OcPlatform;
public class ObtInitHandler implements OCMainInitHandler {
private static final String TAG = ObtInitHandler.class.getSimpleName();
private OnBoardingActivity activity;
private OcPlatform obtPlatform;
public ObtInitHandler(OnBoardingActivity activity, OcPlatform obtPlatform) {
this.activity = activity;
this.obtPlatform = obtPlatform;
}
@Override
public int initialize() {
Log.d(TAG, "inside ObtInitHandler.initialize()");
int ret = obtPlatform.platformInit("OCF");
if (ret >= 0) {
OcDevice device = new OcDevice("/oic/d", "oic.d.sensor", "OBT", "ocf.1.0.0", "ocf.res.1.0.0");
ret |= obtPlatform.addDevice(device);
}
return ret;
}