Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / android / EasySetup / app / src / main / java / org / iotivity / service / easysetup / EasysetupActivity.java
1 /**
2  * ***************************************************************
3  * <p/>
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  * <p/>
6  * <p/>
7  * <p/>
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * <p/>
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * <p/>
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * <p/>
20  * ****************************************************************
21  */
22
23 package org.iotivity.service.easysetup;
24
25 import android.app.Activity;
26 import android.content.Context;
27 import android.content.SharedPreferences;
28 import android.os.Bundle;
29 import android.preference.PreferenceManager;
30 import android.util.Log;
31 import android.view.View;
32 import android.widget.ArrayAdapter;
33 import android.widget.Button;
34 import android.widget.EditText;
35 import android.widget.LinearLayout;
36 import android.widget.RadioButton;
37 import android.widget.RadioGroup;
38 import android.widget.Spinner;
39 import android.widget.TextView;
40 import android.widget.Toast;
41 import android.widget.ToggleButton;
42
43 import org.iotivity.base.ModeType;
44 import org.iotivity.base.OcConnectivityType;
45 import org.iotivity.base.OcException;
46 import org.iotivity.base.OcPlatform;
47 import org.iotivity.base.OcProvisioning;
48 import org.iotivity.base.OcResource;
49 import org.iotivity.base.PlatformConfig;
50 import org.iotivity.base.QualityOfService;
51 import org.iotivity.base.ServiceType;
52 import org.iotivity.service.easysetup.mediator.CloudProp;
53 import org.iotivity.service.easysetup.mediator.CloudPropProvisioningCallback;
54 import org.iotivity.service.easysetup.mediator.CloudPropProvisioningStatus;
55 import org.iotivity.service.easysetup.mediator.DeviceConfig;
56 import org.iotivity.service.easysetup.mediator.DeviceProp;
57 import org.iotivity.service.easysetup.mediator.DevicePropProvisioningCallback;
58 import org.iotivity.service.easysetup.mediator.DevicePropProvisioningStatus;
59 import org.iotivity.service.easysetup.mediator.ESException;
60 import org.iotivity.service.easysetup.mediator.EasySetup;
61 import org.iotivity.service.easysetup.mediator.EnrolleeConf;
62 import org.iotivity.service.easysetup.mediator.GetConfigurationCallback;
63 import org.iotivity.service.easysetup.mediator.GetConfigurationStatus;
64 import org.iotivity.service.easysetup.mediator.RemoteEnrollee;
65 import org.iotivity.service.easysetup.mediator.SecurityProvisioningCallback;
66 import org.iotivity.service.easysetup.mediator.SecurityProvisioningStatus;
67 import org.iotivity.service.easysetup.mediator.WiFiConfig;
68 import org.iotivity.service.easysetup.mediator.enums.ESCloudProvState;
69 import org.iotivity.service.easysetup.mediator.enums.ESResult;
70 import org.iotivity.service.easysetup.mediator.enums.WIFI_AUTHTYPE;
71 import org.iotivity.service.easysetup.mediator.enums.WIFI_ENCTYPE;
72 import org.iotivity.service.easysetup.mediator.enums.WIFI_FREQ;
73 import org.iotivity.service.easysetup.mediator.enums.WIFI_MODE;
74
75 import java.io.File;
76 import java.io.FileNotFoundException;
77 import java.io.FileOutputStream;
78 import java.io.IOException;
79 import java.io.InputStream;
80 import java.io.OutputStream;
81 import java.util.ArrayList;
82 import java.util.EnumSet;
83
84
85 public class EasysetupActivity extends Activity {
86     private static final String TAG = "Easysetup Mediator: ";
87     PlatformConfig cfg;
88
89     private static final int BUFFER_SIZE = 1024;
90
91     private String filePath = "";
92     public static final String OIC_CLIENT_JSON_DB_FILE =  "oic_svr_db_client.dat";
93     public static final String OIC_SQL_DB_FILE =  "PDM.db";
94
95     private boolean isSecurityEnabled = false;
96     private boolean isFirstTime = true;
97
98     ToggleButton mSecurityMode;
99
100     RadioGroup mEasysetupProcess;
101     RadioButton mConfigureSecProcess;
102     RadioButton mGetConfigurationProcess;
103     RadioButton mProvisionDevConfProcess;
104     RadioButton mProvisionCloudConfProcess;
105
106     Button mDiscoverResource;
107     Button mStartGetConfiguration;
108     Button mStartConfigureSec;
109     Button mStartProvisionDevConf;
110     Button mStartProvisionCloudConf;
111
112     TextView mGetconfigurationStateText;
113     TextView mDevNameText;
114     TextView mLanguageText;
115     TextView mCountryText;
116     TextView mWifiModeText;
117     TextView mWifiFreqText;
118     TextView mCloudAccessableText;
119     TextView mSecStateText;
120     TextView mSecDevIDText;
121     TextView mProvisionDevConfState;
122     TextView mProvisionCloudConfState;
123
124     EditText mEnrollerSsidText;
125     EditText mEnrollerPWText;
126     EditText mInputLanguageText;
127     EditText mInputCountryText;
128     EditText mAuthCodeText;
129     EditText mAuthProviderText;
130     EditText mCIServerText;
131
132     LinearLayout mGetConfigurationInfo;
133     LinearLayout mConfigureSecInfo;
134     LinearLayout mProvisionDevConfInfo;
135     LinearLayout mProvisionCloudConfInfo;
136
137     Spinner mAuthType;
138     Spinner mEncType;
139
140     EasySetup mEasySetup;
141     RemoteEnrollee mRemoteEnrollee;
142
143     Activity mActivity;
144     Context mContext;
145
146     @Override
147     protected void onCreate(Bundle savedInstanceState) {
148         super.onCreate(savedInstanceState);
149         setContentView(R.layout.easysetup_main);
150
151         mActivity = EasysetupActivity.this;
152         mContext = mActivity.getBaseContext();
153
154         mSecurityMode = (ToggleButton) findViewById(R.id.btn_Security);
155
156         mEasysetupProcess = (RadioGroup) findViewById(R.id.rg_EasysetupProcess);
157
158         mConfigureSecProcess = (RadioButton) findViewById(R.id.btn_configurSec);
159         mGetConfigurationProcess = (RadioButton) findViewById(R.id.btn_getConfiguration);
160         mProvisionDevConfProcess = (RadioButton) findViewById(R.id.btn_provisionDevConf);
161         mProvisionCloudConfProcess =
162                 (RadioButton) findViewById(R.id.btn_provisionCloudConf);
163
164         mDiscoverResource = (Button) findViewById(R.id.btn_discoverResource);
165         mStartGetConfiguration =
166                 (Button) findViewById(R.id.btn_startGetConfiguration);
167         mStartConfigureSec = (Button) findViewById(R.id.btn_startConfigureSec);
168         mStartProvisionDevConf = (Button) findViewById(R.id.btn_startProvisionDevConf);
169         mStartProvisionCloudConf = (Button) findViewById(R.id.btn_startProvisionCloudConf);
170
171         mGetconfigurationStateText =
172                 (TextView) findViewById(R.id.txt_getConfigurationState);
173         mDevNameText = (TextView) findViewById(R.id.txt_devName);
174         mLanguageText = (TextView) findViewById(R.id.txt_language);
175         mCountryText = (TextView) findViewById(R.id.txt_country);
176         mWifiModeText = (TextView) findViewById(R.id.txt_wifiMode);
177         mWifiFreqText = (TextView) findViewById(R.id.txt_wifiFreq);
178         mCloudAccessableText = (TextView) findViewById(R.id.txt_cloudAccessable);
179         mSecStateText = (TextView) findViewById(R.id.txt_secState);
180         mSecDevIDText = (TextView) findViewById(R.id.txt_secDevID);
181         mProvisionDevConfState = (TextView) findViewById(R.id.txt_provisionDevConfState);
182         mProvisionCloudConfState =
183                 (TextView) findViewById(R.id.txt_provisionCloudConfState);
184
185         mEnrollerSsidText = (EditText) findViewById(R.id.editText_EnrollerSSID);
186         mEnrollerPWText = (EditText) findViewById(R.id.editText_EnrollerPW);
187         mInputLanguageText = (EditText) findViewById(R.id.editText_Language);
188         mInputCountryText = (EditText) findViewById(R.id.editText_Country);
189         mAuthCodeText = (EditText) findViewById(R.id.editText_authcode);
190         mAuthProviderText = (EditText) findViewById(R.id.editText_authprovider);
191         mCIServerText = (EditText) findViewById(R.id.editText_ciserver);
192
193         mGetConfigurationInfo =
194                 (LinearLayout) findViewById(R.id.layout_GetConfiguration);
195         mConfigureSecInfo = (LinearLayout) findViewById(R.id.layout_ConfigurSec);
196         mProvisionDevConfInfo = (LinearLayout) findViewById(R.id.layout_ProvisionDevConf);
197         mProvisionCloudConfInfo = (LinearLayout) findViewById(R.id.layout_ProvisionCloudConf);
198
199         mAuthType = (Spinner) findViewById(R.id.spinner_authType);
200         mEncType = (Spinner) findViewById(R.id.spinner_encType);
201
202         mEasysetupProcess.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
203             @Override
204             public void onCheckedChanged(RadioGroup group, int checkedId) {
205                 mGetConfigurationInfo.setVisibility(View.GONE);
206                 mConfigureSecInfo.setVisibility(View.GONE);
207                 mProvisionDevConfInfo.setVisibility(View.GONE);
208                 mProvisionCloudConfInfo.setVisibility(View.GONE);
209
210                 switch (checkedId) {
211                     case R.id.btn_configurSec:
212                         mConfigureSecInfo.setVisibility(View.VISIBLE);
213                         break;
214
215                     case R.id.btn_getConfiguration:
216                         mGetConfigurationInfo.setVisibility(View.VISIBLE);
217                         break;
218
219                     case R.id.btn_provisionDevConf:
220                         mProvisionDevConfInfo.setVisibility(View.VISIBLE);
221                         break;
222
223                     case R.id.btn_provisionCloudConf:
224                         mProvisionCloudConfInfo.setVisibility(View.VISIBLE);
225                         break;
226                 }
227             }
228         });
229
230         ArrayAdapter<CharSequence> adAuthType, adEnctype;
231
232         adAuthType = ArrayAdapter.createFromResource(this, R.array.auth_type,
233                 android.R.layout.simple_spinner_item);
234         adAuthType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
235
236         adEnctype = ArrayAdapter.createFromResource(this, R.array.enc_type,
237                 android.R.layout.simple_spinner_item);
238         adEnctype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
239
240         mAuthType.setAdapter(adAuthType);
241         mAuthType.setSelection(0);
242
243         mEncType.setAdapter(adEnctype);
244         mEncType.setSelection(0);
245
246         addListenerForDiscoverEnrollee();
247         addListenerForStartConfigureSec();
248         addListenerForStartGetConfiguration();
249         addListenerForStartProvisionDevProp();
250         addListenerForStartProvisionCloudProp();
251
252         mSecurityMode.setClickable(false);
253         mConfigureSecProcess.setEnabled(false);
254         mGetConfigurationProcess.setEnabled(false);
255         mProvisionDevConfProcess.setEnabled(false);
256         mProvisionCloudConfProcess.setEnabled(false);
257
258         mEasySetup = EasySetup.getInstance(getApplicationContext());
259
260         initOICStack();
261     }
262
263     private void initOICStack() {
264         filePath = getFilesDir().getPath() + "/";
265
266         SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences
267                 (getApplicationContext());
268         boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
269         if (isFirstRun) {
270             if(!copyJsonFromAsset())
271             {
272                 Log.e(TAG, "initOICStack error: " + "copyJsonFromAsset()");
273                 Toast.makeText(this,"Can't Copy DB file from asset, please retry start SampleApp.",
274                     Toast.LENGTH_LONG).show();
275                 return;
276             }
277             SharedPreferences.Editor editor = wmbPreference.edit();
278             editor.putBoolean("FIRSTRUN", false);
279             editor.commit();
280         }
281
282         cfg = new PlatformConfig(
283                 this,
284                 ServiceType.IN_PROC,
285                 ModeType.CLIENT_SERVER,
286                 "0.0.0.0", // bind to all available interfaces
287                 0,
288                 QualityOfService.LOW, filePath + OIC_CLIENT_JSON_DB_FILE);
289         try {
290             /*
291              * Initialize DataBase
292              */
293
294             OcPlatform.Configure(cfg);
295
296             String sqlDbPath = getFilesDir().getAbsolutePath().replace("files", "databases") +
297                     File.separator;
298             File file = new File(sqlDbPath);
299             //check files directory exists
300             if (!(file.isDirectory())) {
301                 file.mkdirs();
302                 Log.d(TAG, "Sql db directory created at " + sqlDbPath);
303             }
304             Log.d(TAG, "Sql db directory exists at " + sqlDbPath);
305
306             //SQLiteDatabase.openOrCreateDatabase(sqlDbPath+ OIC_SQL_DB_FILE, null);
307             OcProvisioning.provisionInit(sqlDbPath + OIC_SQL_DB_FILE);
308             mSecurityMode.setChecked(true);
309         } catch (OcException e) {
310             logMessage(TAG + "provisionInit error: " + e.getMessage());
311             Log.e(TAG, e.getMessage());
312             Toast.makeText(this,"provisionInit error: " + e.getMessage(),
313                     Toast.LENGTH_LONG).show();
314             mSecurityMode.setChecked(false);
315             return;
316         } catch (UnsatisfiedLinkError e) {
317
318             // Note : Easy setup is built with SECURED = 0, but user still selects Security feature
319             // while running the Mediator App it couldn't find "libocprovision.so".
320             // As per the programmer guide, security feature should be invoked only if build is done with SECURED = 1.
321             mSecurityMode.setChecked(false);
322             Log.e(TAG, " Easy setup is built with secured  = 0, but executed with security feature");
323             Toast.makeText(this,"Security is not enabled [Easy setup is built with SECURED = 0]",
324                     Toast.LENGTH_LONG).show();
325             return;
326         }
327     }
328
329     OcPlatform.OnResourceFoundListener listener =
330             new OcPlatform.OnResourceFoundListener() {
331                 @Override
332                 public void onFindResourceFailed(Throwable throwable, String s) {
333                     Log.e(TAG, "Failed found resource, ecode: " + s);
334                 }
335                 @Override
336                 public void onResourceFound(OcResource ocResource) {
337                     synchronized (mActivity) {
338                         if(isFirstTime){
339                             if (null == ocResource) {
340                                 Log.e(TAG, "Found resource is invalid");
341                                 return;
342                             }
343
344                             if(ocResource.getHost().contains("coap+tcp")) {
345                                 Log.d(TAG, "Recv Found resource event  from tcp port, ignoring URI : " + ocResource.getUri());
346                                 runOnUiThread(new Runnable() {
347                                     @Override
348                                     public void run() {
349                                         mDiscoverResource.setEnabled(true);
350                                     }
351                                 });
352                                 return;
353                             }
354
355                             // Get the resource URI
356                             String resourceUri = ocResource.getUri();
357                             // Get the resource host address
358                             String hostAddress = ocResource.getHost();
359                             Log.d(TAG,"URI of the resource: " + resourceUri);
360                             Log.d(TAG,"Host address of the resource: " + hostAddress);
361
362                             runOnUiThread(new Runnable() {
363                                 @Override
364                                 public void run() {
365                                     mDiscoverResource.setText("Founded");
366                                     mConfigureSecProcess.setEnabled(true);
367                                     mGetConfigurationProcess.setEnabled(true);
368                                     mProvisionDevConfProcess.setEnabled(true);
369                                     mProvisionCloudConfProcess.setEnabled(true);
370                                 }
371                             });
372                             isFirstTime = false;
373                             mRemoteEnrollee = mEasySetup.createRemoteEnrollee(ocResource);
374                         }
375                     }
376                 }
377             };
378
379     private void addListenerForDiscoverEnrollee() {
380         mDiscoverResource.setOnClickListener(new View.OnClickListener() {
381             @Override
382             public void onClick(View v) {
383                 Thread thread = new Thread() {
384                     boolean result = true;
385                     @Override
386                     public void run() {
387                         runOnUiThread(new Runnable() {
388                             @Override
389                             public void run() {
390                                 mDiscoverResource.setEnabled(false);
391                             }
392                         });
393
394                         try {
395                             String requestUri = OcPlatform.WELL_KNOWN_QUERY + "?rt=ocf.wk.prov";
396                             OcPlatform.findResource("",
397                                     requestUri,
398                                     EnumSet.of(OcConnectivityType.CT_DEFAULT),
399                                     listener
400                             );
401                         }
402                         catch (OcException e) {
403                             e.printStackTrace();
404                             result = false;
405                             runOnUiThread(new Runnable() {
406                                 @Override
407                                 public void run() {
408                                     mDiscoverResource.setEnabled(true);
409                                 }
410                             });
411                         }
412                     }
413                 };
414
415                 thread.start();
416             }
417         });
418     }
419
420     private void addListenerForStartConfigureSec() {
421         mStartConfigureSec.setOnClickListener(new View.OnClickListener() {
422             @Override
423             public void onClick(View v) {
424                 Thread thread = new Thread() {
425                     @Override
426                     public void run() {
427                         runOnUiThread(new Runnable() {
428                             @Override
429                             public void run() {
430                                 mStartConfigureSec.setEnabled(false);
431                             }
432                         });
433
434                         try {
435                             mRemoteEnrollee.provisionSecurity(new SecurityProvisioningCallback() {
436                                 @Override
437                                 public void onProgress(final SecurityProvisioningStatus securityProvisioningStatus) {
438                                     if(securityProvisioningStatus.getESResult() == ESResult.ES_OK) {
439                                         runOnUiThread(new Runnable() {
440                                             @Override
441                                             public void run() {
442                                                 mSecStateText.setText("Success");
443                                                 mSecDevIDText.setText(securityProvisioningStatus.getDevUUID());
444                                             }
445                                         });
446                                     }
447                                     else {
448                                         runOnUiThread(new Runnable() {
449                                             @Override
450                                             public void run() {
451                                                 mSecStateText.setText("Failed");
452                                                 mStartConfigureSec.setEnabled(true);
453                                             }
454                                         });
455                                     }
456                                 }
457                             });
458                         } catch (ESException e) {
459                             e.printStackTrace();
460                             runOnUiThread(new Runnable() {
461                                 @Override
462                                 public void run() {
463                                     mStartConfigureSec.setEnabled(true);
464                                 }
465                             });
466                         }
467                     }
468                 };
469
470                 thread.start();
471             }
472         });
473     }
474
475     private void addListenerForStartGetConfiguration(){
476         mStartGetConfiguration.setOnClickListener(new View.OnClickListener() {
477             @Override
478             public void onClick(View v) {
479                 Thread thread = new Thread() {
480                     @Override
481                     public void run() {
482                         runOnUiThread(new Runnable() {
483                             @Override
484                             public void run() {
485                                 mGetconfigurationStateText.setText("Process");
486                                 mStartGetConfiguration.setEnabled(false);
487                             }
488                         });
489
490                         try {
491                             mRemoteEnrollee.getConfiguration(new GetConfigurationCallback() {
492                                 @Override
493                                 public void onProgress(GetConfigurationStatus getConfigurationStatus) {
494                                     if(getConfigurationStatus.getESResult() == ESResult.ES_OK) {
495
496                                         final EnrolleeConf enrolleeConf = getConfigurationStatus.getEnrolleeConf();
497                                         runOnUiThread(new Runnable() {
498                                             @Override
499                                             public void run() {
500                                                 mGetconfigurationStateText.setText("Success");
501                                                 mDevNameText.setText(enrolleeConf.getDeviceName());
502                                                 setWifiModes(enrolleeConf.getWiFiModes());
503                                                 setWifiFreq(enrolleeConf.getWiFiFreq());
504
505                                                 if(enrolleeConf.isCloudAccessible()) {
506                                                     mCloudAccessableText.setText("TRUE");
507                                                 }
508                                                 else {
509                                                     mCloudAccessableText.setText("FALSE");
510                                                 }
511                                             }
512                                         });
513
514                                     }
515                                     else {
516                                         runOnUiThread(new Runnable() {
517                                             @Override
518                                             public void run() {
519                                                 mGetconfigurationStateText.setText("Failed");
520                                                 mStartGetConfiguration.setEnabled(true);
521                                             }
522                                         });
523                                     }
524                                 }
525                             });
526                         } catch (ESException e) {
527                             e.printStackTrace();
528                             runOnUiThread(new Runnable() {
529                                 @Override
530                                 public void run() {
531                                     mGetconfigurationStateText.setText("Failed");
532                                     mStartGetConfiguration.setEnabled(true);
533                                 }
534                             });
535                         }
536                     }
537                 };
538
539                 thread.start();
540             }
541         });
542     }
543
544     private void addListenerForStartProvisionDevProp() {
545         mStartProvisionDevConf.setOnClickListener(new View.OnClickListener() {
546             @Override
547             public void onClick(View v) {
548                 Thread thread = new Thread() {
549                     @Override
550                     public void run() {
551                         try {
552                             runOnUiThread(new Runnable() {
553                                 @Override
554                                 public void run() {
555                                     mProvisionDevConfState.setText("Progress");
556                                     mStartProvisionDevConf.setEnabled(false);
557                                 }
558                             });
559
560                             String enrollerSSID = mEnrollerSsidText.getText().toString();
561                             String enrollerPW = mEnrollerPWText.getText().toString();
562                             WIFI_AUTHTYPE authType =
563                                     WIFI_AUTHTYPE.fromInt(mAuthType.getSelectedItemPosition());
564                             WIFI_ENCTYPE encType =
565                                     WIFI_ENCTYPE.fromInt(mEncType.getSelectedItemPosition());
566                             String inputLanguage = mInputLanguageText.getText().toString();
567                             String inputCountry = mInputCountryText.getText().toString();
568
569                             DeviceProp deviceProp = new DeviceProp();
570                             deviceProp.setWiFiProp(enrollerSSID, enrollerPW, authType, encType);
571                             deviceProp.setDevConfProp(inputLanguage, inputCountry);
572
573                             mRemoteEnrollee.provisionDeviceProperties(deviceProp, new DevicePropProvisioningCallback() {
574                                 @Override
575                                 public void onProgress(DevicePropProvisioningStatus devPropProvisioningStatus) {
576                                     final ESResult result = devPropProvisioningStatus.getESResult();
577                                     runOnUiThread(new Runnable() {
578                                         @Override
579                                         public void run() {
580                                             if(result.equals(ESResult.ES_OK)) {
581                                                 mProvisionDevConfState.setText("Success");
582                                             }
583                                             else if(result.equals(ESResult.ES_ERROR)) {
584                                                 mProvisionDevConfState.setText("Failed");
585                                             }
586                                             else if(result.equals(ESResult.ES_UNAUTHORIZED)) {
587                                                 mProvisionDevConfState.setText("Failed. Need SecProv");
588                                             }
589                                             mStartProvisionDevConf.setEnabled(true);
590                                         }
591                                     });
592                                 }
593                             });
594                         } catch (ESException e) {
595                             e.printStackTrace();
596                             runOnUiThread(new Runnable() {
597                                 @Override
598                                 public void run() {
599                                     mProvisionDevConfState.setText("Failed");
600                                     mStartProvisionDevConf.setEnabled(true);
601                                 }
602                             });
603                         }
604                     }
605                 };
606
607                 thread.start();
608             }
609         });
610     }
611
612     private void addListenerForStartProvisionCloudProp() {
613         mStartProvisionCloudConf.setOnClickListener(new View.OnClickListener() {
614             @Override
615             public void onClick(View v) {
616                 Thread thread = new Thread() {
617                     @Override
618                     public void run() {
619                         runOnUiThread(new Runnable() {
620                             @Override
621                             public void run() {
622                                 mProvisionCloudConfState.setText("Progress");
623                                 mStartProvisionCloudConf.setEnabled(false);
624                             }
625                         });
626
627                         try {
628                             String authCode = mAuthCodeText.getText().toString();
629                             String authProvider = mAuthProviderText.getText().toString();
630                             String ciserver = mCIServerText.getText().toString();
631
632                             CloudProp cloudProp = new CloudProp();
633                             cloudProp.setCloudProp(authCode, authProvider, ciserver);
634
635                             mRemoteEnrollee.provisionCloudProperties(cloudProp, new CloudPropProvisioningCallback() {
636                                 @Override
637                                 public void onProgress(CloudPropProvisioningStatus cloudProvisioningStatus) {
638                                     final ESResult result = cloudProvisioningStatus.getESResult();
639                                     final ESCloudProvState state = cloudProvisioningStatus.getESCloudState();
640                                     runOnUiThread(new Runnable() {
641                                         @Override
642                                         public void run() {
643                                             if(result.equals(ESResult.ES_OK)) {
644                                                 if(state.equals(ESCloudProvState.ES_CLOUD_ENROLLEE_FOUND)) {
645                                                     mProvisionCloudConfState.setText("Found Resource");
646                                                 }
647                                                 else if(state.equals(ESCloudProvState.ES_CLOUD_PROVISIONING_SUCCESS)) {
648                                                     mProvisionCloudConfState.setText("Success");
649                                                 }
650                                             }
651                                             else {
652                                                 if(state.equals(ESCloudProvState.ES_CLOUD_ENROLLEE_NOT_FOUND)) {
653                                                     mProvisionCloudConfState.setText("Not Found Resource");
654                                                 }
655                                                 else if(state.equals(ESCloudProvState.ES_CLOUD_PROVISIONING_ERROR)) {
656                                                     mProvisionCloudConfState.setText("Failed");
657                                                 }
658                                                 mStartProvisionCloudConf.setEnabled(true);
659                                             }
660                                         }
661                                     });
662                                 }
663                             });
664                         } catch (ESException e) {
665                             e.printStackTrace();
666                             runOnUiThread(new Runnable() {
667                                 @Override
668                                 public void run() {
669                                     mProvisionCloudConfState.setText("Failed");
670                                     mStartProvisionCloudConf.setEnabled(true);
671                                 }
672                             });
673                         }
674                     }
675                 };
676
677                 thread.start();
678             }
679         });
680     }
681
682     private boolean copyJsonFromAsset() {
683         InputStream inputStream = null;
684         OutputStream outputStream = null;
685         int length;
686         byte[] buffer = new byte[BUFFER_SIZE];
687         try {
688             inputStream = getAssets().open(OIC_CLIENT_JSON_DB_FILE);
689             File file = new File(filePath);
690             //check files directory exists
691             if (!(file.exists() && file.isDirectory())) {
692                 file.mkdirs();
693             }
694             outputStream = new FileOutputStream(filePath + OIC_CLIENT_JSON_DB_FILE);
695             while ((length = inputStream.read(buffer)) != -1) {
696                 outputStream.write(buffer, 0, length);
697             }
698         } catch (NullPointerException e) {
699             logMessage(TAG + "Null pointer exception " + e.getMessage());
700             Log.e(TAG, e.getMessage());
701             return false;
702         } catch (FileNotFoundException e) {
703             logMessage(TAG + "Json svr db file not found " + e.getMessage());
704             Log.e(TAG, e.getMessage());
705             return false;
706         } catch (IOException e) {
707             logMessage(TAG + OIC_CLIENT_JSON_DB_FILE + " file copy failed");
708             Log.e(TAG, e.getMessage());
709             return false;
710         } finally {
711             if (inputStream != null) {
712                 try {
713                     inputStream.close();
714                 } catch (IOException e) {
715                     Log.e(TAG, e.getMessage());
716                     return false;
717                 }
718             }
719             if (outputStream != null) {
720                 try {
721                     outputStream.close();
722                 } catch (IOException e) {
723                     Log.e(TAG, e.getMessage());
724                     return false;
725                 }
726             }
727         }
728         return true;
729     }
730
731     public void logMessage(String text) {
732
733     }
734
735     public void setWifiModes(ArrayList<WIFI_MODE> types) {
736         String temp = "WIFI - ";
737
738         for(WIFI_MODE type : types) {
739             if(type.equals(WIFI_MODE.WIFI_11A)) {
740                 temp = temp + "11A ";
741             }
742             else if(type.equals(WIFI_MODE.WIFI_11B)) {
743                 temp = temp + "11B ";
744             }
745             else if(type.equals(WIFI_MODE.WIFI_11G)) {
746                 temp = temp + "11G ";
747             }
748             else if(type.equals(WIFI_MODE.WIFI_11N)) {
749                 temp = temp + "11N ";
750             }
751             else if(type.equals(WIFI_MODE.WIFI_11AC)) {
752                 temp = temp + "11AC ";
753             }
754         }
755         final String modeTypes = temp;
756         runOnUiThread(new Runnable() {
757             @Override
758             public void run() {
759                 mWifiModeText.setText(modeTypes);
760             }
761         });
762     }
763
764     public void setWifiFreq(final WIFI_FREQ freq) {
765         runOnUiThread(new Runnable() {
766             @Override
767             public void run() {
768                 if(freq.equals(WIFI_FREQ.WIFI_24G)) {
769                     mWifiFreqText.setText("2.4G");
770                 }
771                 else if(freq.equals(WIFI_FREQ.WIFI_5G)) {
772                     mWifiFreqText.setText("5G");
773                 }
774                 else if(freq.equals(WIFI_FREQ.WIFI_BOTH)) {
775                     mWifiFreqText.setText("2.4G & 5G");
776                 }
777             }
778         });
779     }
780
781     @Override
782     protected void onDestroy() {
783         super.onDestroy();
784     }
785 }