From 79f10053a6a340146139b76d5f8d8b30e178d6d1 Mon Sep 17 00:00:00 2001 From: Sunil Kumar K R Date: Fri, 23 Sep 2016 15:45:24 +0530 Subject: [PATCH] added cloud provisioning android application Change-Id: I4f544c2b665543c4a8f3bec422598f5e7cb8e958 Signed-off-by: Sunil Kumar K R Reviewed-on: https://gerrit.iotivity.org/gerrit/12175 Tested-by: jenkins-iotivity Reviewed-by: Randeep Singh (cherry picked from commit 2a2c834829502b08668269b353338ea4a92c47d8) Reviewed-on: https://gerrit.iotivity.org/gerrit/12811 --- .../examples/cloudprovisioningclient/build.gradle | 37 ++ .../cloudprovisioningclient.iml | 96 +++ .../cloudprovisioningclient/proguard-rules.pro | 17 + .../src/main/AndroidManifest.xml | 22 + .../src/main/assets/oic_svr_db_client.dat | Bin 0 -> 1046 bytes .../src/main/assets/oic_svr_db_client.json | 82 +++ .../CloudProvisioningClient.java | 666 +++++++++++++++++++++ .../cloudprovisioningclient/LoginActivity.java | 86 +++ .../cloudprovisioningclient/StringConstants.java | 35 ++ .../src/main/res/drawable/dash_nil_border.xml | 30 + .../src/main/res/drawable/round_button.xml | 8 + .../src/main/res/layout/activity_login.xml | 16 + .../src/main/res/layout/main_activity.xml | 207 +++++++ .../src/main/res/layout/setting_layout.xml | 69 +++ .../src/main/res/menu/menu_cloud_provision.xml | 8 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../src/main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../src/main/res/values-v21/styles.xml | 5 + .../src/main/res/values-w820dp/dimens.xml | 6 + .../src/main/res/values/dimens.xml | 5 + .../src/main/res/values/strings.xml | 6 + .../src/main/res/values/styles.xml | 8 + android/examples/settings.gradle | 5 +- 25 files changed, 1413 insertions(+), 1 deletion(-) create mode 100755 android/examples/cloudprovisioningclient/build.gradle create mode 100755 android/examples/cloudprovisioningclient/cloudprovisioningclient.iml create mode 100755 android/examples/cloudprovisioningclient/proguard-rules.pro create mode 100755 android/examples/cloudprovisioningclient/src/main/AndroidManifest.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/assets/oic_svr_db_client.dat create mode 100755 android/examples/cloudprovisioningclient/src/main/assets/oic_svr_db_client.json create mode 100755 android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/CloudProvisioningClient.java create mode 100755 android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/LoginActivity.java create mode 100755 android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/StringConstants.java create mode 100755 android/examples/cloudprovisioningclient/src/main/res/drawable/dash_nil_border.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/drawable/round_button.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/layout/activity_login.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/layout/main_activity.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/layout/setting_layout.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/menu/menu_cloud_provision.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100755 android/examples/cloudprovisioningclient/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100755 android/examples/cloudprovisioningclient/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100755 android/examples/cloudprovisioningclient/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100755 android/examples/cloudprovisioningclient/src/main/res/values-v21/styles.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/values-w820dp/dimens.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/values/dimens.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/values/strings.xml create mode 100755 android/examples/cloudprovisioningclient/src/main/res/values/styles.xml diff --git a/android/examples/cloudprovisioningclient/build.gradle b/android/examples/cloudprovisioningclient/build.gradle new file mode 100755 index 0000000..4e2bb4b --- /dev/null +++ b/android/examples/cloudprovisioningclient/build.gradle @@ -0,0 +1,37 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 21 + buildToolsVersion "20.0.0" + + defaultConfig { + applicationId "org.iotivity.base.examples.cloudprovisioningclient" + minSdkVersion 21 + targetSdkVersion 21 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + lintOptions { + abortOnError false + } +} +repositories { + flatDir { + dirs "../../android_api/base/build/outputs/aar/" + } +} + +try { + dependencies { + compile ":iotivity-base-${TARGET_ARCH}-${RELEASE}@aar" + } +} catch (all) { + print "${ERROR_MSG}" + assert all +} diff --git a/android/examples/cloudprovisioningclient/cloudprovisioningclient.iml b/android/examples/cloudprovisioningclient/cloudprovisioningclient.iml new file mode 100755 index 0000000..5d408e7 --- /dev/null +++ b/android/examples/cloudprovisioningclient/cloudprovisioningclient.iml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/examples/cloudprovisioningclient/proguard-rules.pro b/android/examples/cloudprovisioningclient/proguard-rules.pro new file mode 100755 index 0000000..4fd6c93 --- /dev/null +++ b/android/examples/cloudprovisioningclient/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/sandeep/iot/android-sdk/android-sdk-linux/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 *; +#} diff --git a/android/examples/cloudprovisioningclient/src/main/AndroidManifest.xml b/android/examples/cloudprovisioningclient/src/main/AndroidManifest.xml new file mode 100755 index 0000000..5de8a6a --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + diff --git a/android/examples/cloudprovisioningclient/src/main/assets/oic_svr_db_client.dat b/android/examples/cloudprovisioningclient/src/main/assets/oic_svr_db_client.dat new file mode 100755 index 0000000000000000000000000000000000000000..4b5b91881ba480a4828e70405952a02ec9c50892 GIT binary patch literal 1046 zcmbVLOHRWu5H0OhSRvH~HFedSlpTpZX(vtMU_1RRTh74((G6UJ>u>;AgU6|&s3@w! zA{vi9&-32+kGe zr0qU&YqnW2N-^Fer5Lj`6&liq3J`mMr1IsdT)N_eIu%EuffRG_hYqa&V2dWUxQtB- z&>0$oD(|<-IaKU2&(44tNwv*tO`7@CX*#FbV3*g}!r0IfctxT#tMWE<7k-T%LgjI@ zhtJ!3RO%jUhO!7zW4@pZ@`_}=)pkddXQI_62Ti8fUh_L1aLH#w4nZ*FlPMqa86UgV zWHOz&WiY;_M&1vaIe(Em&L;_eO7rJ8iAELQiG(!@=+HrAy~w&xWrN6MgMU%H^k9;F zF#mj~VD`P30;D}*guMb9iR?~1bxJ! list, + OcRepresentation ocRepresentation) { + logMessage("signUp was successful"); + try { + String mUserUuid = ocRepresentation.getValue("uid"); + String mAccesstoken = ocRepresentation.getValue("accesstoken"); + + String mRefreshtoken = ocRepresentation.getValue("refreshtoken"); + String tokenType = ocRepresentation.getValue("tokentype"); + + SharedPreferences.Editor editor = settingPreference.edit(); + editor.putString("useruuid", mUserUuid); + editor.putString("accesstoken", mAccesstoken); + editor.commit(); + + logMessage("\tuserID: " + mUserUuid); + logMessage("\taccessToken: " + mAccesstoken); + logMessage("\trefreshToken: " + mRefreshtoken); + logMessage("\ttokenType: " + tokenType); + + if (ocRepresentation.hasAttribute("expiresin")) { + int expiresIn = ocRepresentation.getValue("expiresin"); + logMessage("\texpiresIn: " + expiresIn); + } + + runOnUiThread(new Runnable() { + @Override + public void run() { + signupLyt.setVisibility(View.GONE); + signinLyt.setVisibility(View.VISIBLE); + } + }); + + + } catch (OcException e) { + Log.e(TAG, e.toString()); + } + } + + + @Override + public synchronized void onPostFailed(Throwable throwable) { + logMessage("Failed to signUp"); + if (throwable instanceof OcException) { + OcException ocEx = (OcException) throwable; + Log.e(TAG, ocEx.toString()); + ErrorCode errCode = ocEx.getErrorCode(); + logMessage("Error code: " + errCode); + } + } + }; + OcAccountManager.OnPostListener onSignIn = new OcAccountManager.OnPostListener() { + @Override + public synchronized void onPostCompleted(List list, + OcRepresentation ocRepresentation) { + logMessage("signIn was successful"); + runOnUiThread(new Runnable() { + @Override + public void run() { + signinLyt.setVisibility(View.GONE); + userid.setText(settingPreference.getString("useruuid", "")); + lyt1.setVisibility(View.VISIBLE); + lyt2.setVisibility(View.VISIBLE); + } + }); + + + } + + @Override + public synchronized void onPostFailed(Throwable throwable) { + logMessage("Failed to signIn"); + if (throwable instanceof OcException) { + OcException ocEx = (OcException) throwable; + Log.e(TAG, ocEx.toString()); + ErrorCode errCode = ocEx.getErrorCode(); + logMessage("Error code: " + errCode); + } + } + }; + OcCloudProvisioning.GetIndividualAclInfoListener getIndividualAclInfoListener = + new OcCloudProvisioning.GetIndividualAclInfoListener() { + @Override + public void getIndividualAclInfoListener(boolean result) { + Log.d(TAG, "Inside getIndividualAclInfoListener "); + if (!result) { + logMessage("Individual ACL Info Successfull !!"); + } else { + logMessage("Error: Individual ACL Info failed !!"); + } + } + }; + OcCloudProvisioning.RequestCertificateListener requestCertificateListener = + new OcCloudProvisioning.RequestCertificateListener() { + @Override + public void requestCertificateListener(boolean result) { + Log.d(TAG, "Inside requestCertificateListener "); + if (!result) { + logMessage("Request certificate Successfull !!"); + } else { + logMessage("Error: Request certificate failed !!"); + } + } + }; + OcCloudProvisioning.GetCRLListener getCRLListener = + new OcCloudProvisioning.GetCRLListener() { + @Override + public void getCRLListener(boolean result) { + Log.d(TAG, "Inside getCRLListener "); + if (!result) { + logMessage("Get CRL Successfull !!"); + } else { + logMessage("Error: Get CRL failed !!"); + } + } + }; + OcCloudProvisioning.PostCRLListener postCRLListener = + new OcCloudProvisioning.PostCRLListener() { + @Override + public void postCRLListener(boolean result) { + Log.d(TAG, "Inside postCRLListener "); + if (!result) { + logMessage("Post CRL Successfull !!"); + } else { + logMessage("Error: Post CRL failed !!"); + } + } + }; + private static final int BUFFER_SIZE = 1024; + private final int REQUEST_LOGIN = 1; + Button signUp, signIn, signOut, getAclId, getAclInfo, requestCert, postCrl, getCrl; + TextView userid; + LinearLayout lyt1, lyt2, signupLyt, signinLyt; + // private TextView eventView; + SharedPreferences settingPreference; + OcCloudProvisioning ocCloudProvisioning; + String acl_Id; + OcCloudProvisioning.GetAclIdByDeviceListener getAclIdByDeviceListener = + new OcCloudProvisioning.GetAclIdByDeviceListener() { + @Override + public void getAclIdByDeviceListener(boolean result, String aclId) { + Log.d(TAG, "Inside getAclIdByDeviceListener "); + if (!result) { + acl_Id = aclId; + logMessage("Acl Id by device !!" + acl_Id); + } else { + logMessage("Error: Acl Id by device failed !!"); + } + } + }; + private OcAccountManager mAccountManager; + private String filePath = ""; + private TextView mEventsTextView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main_activity); + + signUp = (Button) findViewById(R.id.signup); + signIn = (Button) findViewById(R.id.signin); + signOut = (Button) findViewById(R.id.signout); + getAclId = (Button) findViewById(R.id.getAclId); + getAclInfo = (Button) findViewById(R.id.getAclInfo); + requestCert = (Button) findViewById(R.id.request); + postCrl = (Button) findViewById(R.id.postCRL); + getCrl = (Button) findViewById(R.id.getCRL); + + lyt1 = (LinearLayout) findViewById(R.id.lyt1); + lyt2 = (LinearLayout) findViewById(R.id.lyt2); + signupLyt = (LinearLayout) findViewById(R.id.signupLyt); + signinLyt = (LinearLayout) findViewById(R.id.signinLyt); + + + userid = (TextView) findViewById(R.id.userid); + + mEventsTextView = (TextView) findViewById(R.id.eventView); + + filePath = getFilesDir().getPath() + "/"; // data/data//files/ + //copy CBOR file when application runs first time + settingPreference = PreferenceManager.getDefaultSharedPreferences(this); + boolean isFirstRun = settingPreference.getBoolean("FIRSTRUN", true); + if (isFirstRun) { + copyCborFromAsset(); + SharedPreferences.Editor editor = settingPreference.edit(); + editor.putBoolean("FIRSTRUN", false); + editor.putString("IP", StringConstants.DEFAULT_COAP_DERVER_IP); + editor.putString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT); + editor.putString("DEVICEID", StringConstants.DEFAULT_DEVICE_ID); + editor.commit(); + } + if (settingPreference.getString("useruuid", "").equals("")) { + + lyt1.setVisibility(View.GONE); + lyt2.setVisibility(View.GONE); + signupLyt.setVisibility(View.VISIBLE); + signinLyt.setVisibility(View.GONE); + + + } else { + userid.setText(settingPreference.getString("useruuid", "")); + lyt1.setVisibility(View.VISIBLE); + lyt2.setVisibility(View.VISIBLE); + signupLyt.setVisibility(View.GONE); + signinLyt.setVisibility(View.VISIBLE); + } + + initOICStack(); + ocCloudProvisioning = new OcCloudProvisioning(settingPreference.getString("IP", ""), + Integer.valueOf(settingPreference.getString("PORT", ""))); + + signUp.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + signUp(); + } + }); + signIn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + signIn(); + } + }); + signOut.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + signOut(); + } + }); + getAclId.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getAclId(); + } + }); + getAclInfo.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getAclInfo(); + } + }); + requestCert.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + requestCert(); + } + }); + postCrl.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + postCrl(); + } + }); + getCrl.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getCrl(); + } + }); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_cloud_provision, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_settings: + setDefualtSettings(); + return (true); + + + } + return (super.onOptionsItemSelected(item)); + } + + private void signIn() { + try { + logMessage("signIn"); + if(mAccountManager==null) + { + mAccountManager = OcPlatform.constructAccountManagerObject( + StringConstants.COAP_TCP + settingPreference.getString("IP", + StringConstants.DEFAULT_COAP_DERVER_IP) + ":" + + settingPreference.getString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT), + EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP)); + } + + mAccountManager.signIn(settingPreference.getString("useruuid", ""), + settingPreference.getString("accesstoken", ""), onSignIn); + } catch (OcException e) { + e.printStackTrace(); + } + } + + private void signOut() { + try { + logMessage("signOut"); + if(mAccountManager==null) + { + mAccountManager = OcPlatform.constructAccountManagerObject( + StringConstants.COAP_TCP + settingPreference.getString("IP", + StringConstants.DEFAULT_COAP_DERVER_IP) + ":" + + settingPreference.getString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT), + EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP)); + } + + mAccountManager.signOut(CloudProvisioningClient.this); + lyt1.setVisibility(View.GONE); + lyt2.setVisibility(View.GONE); + signinLyt.setVisibility(View.VISIBLE); + } catch (OcException e) { + e.printStackTrace(); + } + } + + + private void getAclId() { + try { + logMessage("getAclId"); + logMessage("\tdeviceId= " + settingPreference.getString("DEVICEID", "")); + ocCloudProvisioning.getAclIdByDevice(settingPreference.getString("DEVICEID", ""), getAclIdByDeviceListener); + } catch (OcException e) { + e.printStackTrace(); + } + } + + private void getAclInfo() { + try { + logMessage("getAclInfo"); + logMessage("\taclid="+acl_Id); + ocCloudProvisioning.getIndividualAclInfo(acl_Id, getIndividualAclInfoListener); + + } catch (OcException e) { + e.printStackTrace(); + } + } + + private void requestCert() { + try { + logMessage("requestCert"); + ocCloudProvisioning = new OcCloudProvisioning(settingPreference.getString("IP", ""), + Integer.valueOf(settingPreference.getString("PORT", ""))); + ocCloudProvisioning.requestCertificate(requestCertificateListener); + + } catch (OcException e) { + e.printStackTrace(); + } + } + + private void postCrl() { + try { + logMessage("postCrl"); + ArrayList arrayList = new ArrayList<>(); + arrayList.add("1234"); + + ocCloudProvisioning.postCRL("20160727000000", "20161027000000", null, arrayList, postCRLListener); + + } catch (OcException e) { + e.printStackTrace(); + } + } + + private void getCrl() { + try { + logMessage("getCrl"); + ocCloudProvisioning.getCRL(getCRLListener); + + } catch (OcException e) { + e.printStackTrace(); + } + } + + private void signUp() { + try { + mAccountManager = OcPlatform.constructAccountManagerObject( + StringConstants.COAP_TCP + settingPreference.getString("IP", + StringConstants.DEFAULT_COAP_DERVER_IP) + settingPreference.getString("PORT", + StringConstants.DEFAULT_COAP_DERVER_PORT), + EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP)); + logMessage(StringConstants.COAP_TCP + settingPreference.getString("IP", + StringConstants.DEFAULT_COAP_DERVER_IP) + settingPreference.getString("PORT", + StringConstants.DEFAULT_COAP_DERVER_PORT)); + } catch (OcException e) { + e.printStackTrace(); + } + + Intent intentLogin = new Intent(this, LoginActivity.class); + startActivityForResult(intentLogin, REQUEST_LOGIN); + } + + + /** + * configure OIC platform and call findResource + */ + private void initOICStack() { + //create platform config + PlatformConfig cfg = new PlatformConfig( + this, + ServiceType.IN_PROC, + ModeType.CLIENT_SERVER, + "0.0.0.0", // bind to all available interfaces + 0, + QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE); + OcPlatform.Configure(cfg); + try { + /* + * Initialize DataBase + */ + String sqlDbPath = getFilesDir().getAbsolutePath().replace("files", "databases") + + File.separator; + File file = new File(sqlDbPath); + //check files directory exists + if (!(file.isDirectory())) { + file.mkdirs(); + Log.d(TAG, "Sql db directory created at " + sqlDbPath); + } + Log.d(TAG, "Sql db directory exists at " + sqlDbPath); + OcProvisioning.provisionInit(sqlDbPath + StringConstants.OIC_SQL_DB_FILE); + } catch (OcException e) { + logMessage(TAG + "provisionInit error: " + e.getMessage()); + Log.e(TAG, e.getMessage()); + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_LOGIN) { + String mAuthCode = data.getStringExtra("authCode"); + + + logMessage("\tauthCode: " + mAuthCode); + + try { + logMessage("Sign Up"); + mAccountManager = OcPlatform.constructAccountManagerObject( + StringConstants.COAP_TCP + settingPreference.getString("IP", + StringConstants.DEFAULT_COAP_DERVER_IP) + ":" + + settingPreference.getString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT), + EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP)); + + mAccountManager.signUp("github", mAuthCode, onSignUp); + } catch (OcException e) { + e.printStackTrace(); + } + } + } + + + /** + * Copy svr db CBOR dat file from assets folder to app data files dir + */ + private void copyCborFromAsset() { + InputStream inputStream = null; + OutputStream outputStream = null; + int length; + byte[] buffer = new byte[BUFFER_SIZE]; + try { + inputStream = getAssets().open(StringConstants.OIC_CLIENT_CBOR_DB_FILE); + File file = new File(filePath); + //check files directory exists + if (!(file.exists() && file.isDirectory())) { + file.mkdirs(); + } + outputStream = new FileOutputStream(filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE); + while ((length = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, length); + } + } catch (NullPointerException e) { + logMessage(TAG + "Null pointer exception " + e.getMessage()); + Log.e(TAG, e.getMessage()); + } catch (FileNotFoundException e) { + logMessage(TAG + "CBOR svr db file not found " + e.getMessage()); + Log.e(TAG, e.getMessage()); + } catch (IOException e) { + logMessage(TAG + StringConstants.OIC_CLIENT_CBOR_DB_FILE + " file copy failed"); + Log.e(TAG, e.getMessage()); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + Log.e(TAG, e.getMessage()); + } + } + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + Log.e(TAG, e.getMessage()); + } + } + } + } + + public void logMessage(String text) { + logMsg(text); + } + + public void logMsg(final String text) { + runOnUiThread(new Runnable() { + public void run() { + Message msg = new Message(); + msg.obj = text; + mEventsTextView.append(text); + mEventsTextView.append("\n"); + } + }); + Log.i(TAG, text); + Intent intent = new Intent(getPackageName()); + intent.putExtra(StringConstants.MESSAGE, text); + sendBroadcast(intent); + } + + + private void setDefualtSettings() { + View setingLayout = getLayoutInflater().inflate(R.layout.setting_layout, null); + + final EditText ip = (EditText) setingLayout.findViewById(R.id.ip); + final EditText port = (EditText) setingLayout.findViewById(R.id.port); + final EditText deviceId = (EditText) setingLayout.findViewById(R.id.deviceId); + + ip.setText(settingPreference.getString("IP", "")); + port.setText(settingPreference.getString("PORT", "")); + deviceId.setText(settingPreference.getString("DEVICEID", "")); + + + final AlertDialog.Builder builder = new AlertDialog.Builder(CloudProvisioningClient.this); + builder.setView(setingLayout); + builder.setPositiveButton("Submit", null).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Log.d(TAG, "AlertDialog onClick"); + + } + }); + final AlertDialog alertDialog = builder.create(); + alertDialog.setMessage("Settings"); + alertDialog.show(); + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + String ip_address = ip.getText().toString(); + String port_num = port.getText().toString(); + String deviceId_val = deviceId.getText().toString(); + + SharedPreferences.Editor editor = settingPreference.edit(); + + editor.putString("IP", ip_address); + editor.putString("PORT", port_num); + editor.putString("DEVICEID", deviceId_val); + editor.commit(); + + alertDialog.cancel(); + } + }); + alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + alertDialog.cancel(); + } + }); + + } + + @Override + public void onPostCompleted(List ocHeaderOptions, OcRepresentation ocRepresentation) { + + } + + @Override + public void onPostFailed(Throwable throwable) { + + } + + + /** + * to display on Server Message on Client screen + */ + public class MessageReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + final String message = intent.getStringExtra(StringConstants.MESSAGE); + logMessage(message); + } + } +} diff --git a/android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/LoginActivity.java b/android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/LoginActivity.java new file mode 100755 index 0000000..09f8087 --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/LoginActivity.java @@ -0,0 +1,86 @@ +/* + * ****************************************************************** + * + * Copyright 2016 Samsung Electronics All Rights Reserved. + * + * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base.examples.cloudprovisioningclient; + +import android.app.Activity; +import android.content.Intent; +import android.net.UrlQuerySanitizer; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.webkit.WebView; +import android.webkit.WebViewClient; + +import org.iotivity.base.examples.cloudprovisioningclient.R; + +/** + * This class is for login to the provider. + * Can be get auth code via web page. + */ +public class LoginActivity extends Activity { + private static final String TAG = "OIC_SIMPLE_LOGIN"; + + private WebView mWebView = null; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_login); + + mWebView = (WebView) findViewById(R.id.webView); + mWebView.setInitialScale(200); + mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.getSettings().setBuiltInZoomControls(true); + mWebView.setWebViewClient(new WebViewClientClass()); + + mWebView.loadUrl("https://github.com/login?return_to=%2Flogin%2Foauth" + + "%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri" + + "%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F"); + } + + private class WebViewClientClass extends WebViewClient { + + @Override + public void onPageFinished(WebView view, String url) { + Log.i(TAG, "called url=" + url); + + if (url.contains("http://www.example.com/oauth_callback")) { + + mWebView.setVisibility(View.INVISIBLE); + + // parsing url + UrlQuerySanitizer sanitizer = new UrlQuerySanitizer(); + sanitizer.setAllowUnregisteredParamaters(true); + sanitizer.parseUrl(url); + + String mAuthCode = sanitizer.getValue("code"); + + Intent intent = getIntent(); + intent.putExtra("authCode", mAuthCode); + setResult(RESULT_OK, intent); + + finish(); + } + } + } +} diff --git a/android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/StringConstants.java b/android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/StringConstants.java new file mode 100755 index 0000000..18c4974 --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/java/org/iotivity/base/examples/cloudprovisioningclient/StringConstants.java @@ -0,0 +1,35 @@ +/* + * ****************************************************************** + * + * Copyright 2016 Samsung Electronics All Rights Reserved. + * + * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +package org.iotivity.base.examples.cloudprovisioningclient; + +public interface StringConstants { + + public static final String DEFAULT_DEVICE_ID = "9cfbeb8e-5a1e-4d1c-9d01-2ae6fdb"; + public static final String COAP_TCP = "coap+tcp://"; + public static final String DEFAULT_COAP_DERVER_IP = "107.109.229.111"; + public static final String DEFAULT_COAP_DERVER_PORT = "5684"; + + public static final String OIC_CLIENT_CBOR_DB_FILE = "oic_svr_db_client.dat"; + public static final String MESSAGE = "message"; + public static final String OIC_SQL_DB_FILE = "Pdm.db"; + +} diff --git a/android/examples/cloudprovisioningclient/src/main/res/drawable/dash_nil_border.xml b/android/examples/cloudprovisioningclient/src/main/res/drawable/dash_nil_border.xml new file mode 100755 index 0000000..be27758 --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/res/drawable/dash_nil_border.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/examples/cloudprovisioningclient/src/main/res/drawable/round_button.xml b/android/examples/cloudprovisioningclient/src/main/res/drawable/round_button.xml new file mode 100755 index 0000000..f4ccab0 --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/res/drawable/round_button.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/android/examples/cloudprovisioningclient/src/main/res/layout/activity_login.xml b/android/examples/cloudprovisioningclient/src/main/res/layout/activity_login.xml new file mode 100755 index 0000000..dfd8e3c --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/res/layout/activity_login.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/android/examples/cloudprovisioningclient/src/main/res/layout/main_activity.xml b/android/examples/cloudprovisioningclient/src/main/res/layout/main_activity.xml new file mode 100755 index 0000000..3ce36d2 --- /dev/null +++ b/android/examples/cloudprovisioningclient/src/main/res/layout/main_activity.xml @@ -0,0 +1,207 @@ + + + + + + + + + + +