dcff8f76f99fa09fa48660534819e6f492a96e8d
[platform/upstream/iotivity.git] /
1 /*
2  * ******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
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  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *
20  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.base.examples.cloudprovisioningclient;
23
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.SharedPreferences;
31 import android.os.Bundle;
32 import android.os.Message;
33 import android.preference.PreferenceManager;
34 import android.util.Log;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 import android.view.View;
38 import android.widget.Button;
39 import android.widget.EditText;
40 import android.widget.LinearLayout;
41 import android.widget.TextView;
42
43 import org.iotivity.base.ErrorCode;
44 import org.iotivity.base.ModeType;
45 import org.iotivity.base.OcAccountManager;
46 import org.iotivity.base.OcCloudProvisioning;
47 import org.iotivity.base.OcConnectivityType;
48 import org.iotivity.base.OcException;
49 import org.iotivity.base.OcHeaderOption;
50 import org.iotivity.base.OcPlatform;
51 import org.iotivity.base.OcProvisioning;
52 import org.iotivity.base.OcRepresentation;
53 import org.iotivity.base.PlatformConfig;
54 import org.iotivity.base.QualityOfService;
55 import org.iotivity.base.ServiceType;
56 import org.iotivity.base.examples.cloudprovisioningclient.R;
57
58 import java.io.File;
59 import java.io.FileNotFoundException;
60 import java.io.FileOutputStream;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.io.OutputStream;
64 import java.util.ArrayList;
65 import java.util.EnumSet;
66 import java.util.List;
67
68 public class CloudProvisioningClient extends Activity implements OcAccountManager.OnPostListener {
69
70     private static final String TAG = "Cloud Provisioning Client: ";
71     OcAccountManager.OnPostListener onSignUp = new OcAccountManager.OnPostListener() {
72         @Override
73             public synchronized void onPostCompleted(List<OcHeaderOption> list,
74                     OcRepresentation ocRepresentation) {
75                 logMessage("signUp was successful");
76                 try {
77                     String mUserUuid = ocRepresentation.getValue("uid");
78                     String mAccesstoken = ocRepresentation.getValue("accesstoken");
79
80                     String mRefreshtoken = ocRepresentation.getValue("refreshtoken");
81                     String tokenType = ocRepresentation.getValue("tokentype");
82
83                     SharedPreferences.Editor editor = settingPreference.edit();
84                     editor.putString("useruuid", mUserUuid);
85                     editor.putString("accesstoken", mAccesstoken);
86                     editor.commit();
87
88                     logMessage("\tuserID: " + mUserUuid);
89                     logMessage("\taccessToken: " + mAccesstoken);
90                     logMessage("\trefreshToken: " + mRefreshtoken);
91                     logMessage("\ttokenType: " + tokenType);
92
93                     if (ocRepresentation.hasAttribute("expiresin")) {
94                         int expiresIn = ocRepresentation.getValue("expiresin");
95                         logMessage("\texpiresIn: " + expiresIn);
96                     }
97
98                     runOnUiThread(new Runnable() {
99                             @Override
100                             public void run() {
101                             signupLyt.setVisibility(View.GONE);
102                             signinLyt.setVisibility(View.VISIBLE);
103                             }
104                             });
105
106
107                 } catch (OcException e) {
108                     Log.e(TAG, e.toString());
109                 }
110             }
111
112
113         @Override
114             public synchronized void onPostFailed(Throwable throwable) {
115                 logMessage("Failed to signUp");
116                 if (throwable instanceof OcException) {
117                     OcException ocEx = (OcException) throwable;
118                     Log.e(TAG, ocEx.toString());
119                     ErrorCode errCode = ocEx.getErrorCode();
120                     logMessage("Error code: " + errCode);
121                 }
122             }
123     };
124     OcAccountManager.OnPostListener onSignIn = new OcAccountManager.OnPostListener() {
125         @Override
126             public synchronized void onPostCompleted(List<OcHeaderOption> list,
127                     OcRepresentation ocRepresentation) {
128                 logMessage("signIn was successful");
129                 runOnUiThread(new Runnable() {
130                         @Override
131                         public void run() {
132                         signinLyt.setVisibility(View.GONE);
133                         userid.setText(settingPreference.getString("useruuid", ""));
134                         lyt1.setVisibility(View.VISIBLE);
135                         lyt2.setVisibility(View.VISIBLE);
136                         }
137                         });
138
139
140             }
141
142         @Override
143             public synchronized void onPostFailed(Throwable throwable) {
144                 logMessage("Failed to signIn");
145                 if (throwable instanceof OcException) {
146                     OcException ocEx = (OcException) throwable;
147                     Log.e(TAG, ocEx.toString());
148                     ErrorCode errCode = ocEx.getErrorCode();
149                     logMessage("Error code: " + errCode);
150                 }
151             }
152     };
153
154     OcAccountManager.OnPostListener onSignOut = new OcAccountManager.OnPostListener() {
155         @Override
156             public synchronized void onPostCompleted(List<OcHeaderOption> list,
157                     OcRepresentation ocRepresentation) {
158                 logMessage("signOut was successful");
159                 runOnUiThread(new Runnable() {
160                         @Override
161                         public void run() {
162                         lyt1.setVisibility(View.GONE);
163                         lyt2.setVisibility(View.GONE);
164                         signinLyt.setVisibility(View.VISIBLE);
165                         }
166                         });
167
168             }
169
170         @Override
171             public synchronized void onPostFailed(Throwable throwable) {
172                 logMessage("Failed to signOut");
173                 if (throwable instanceof OcException) {
174                     OcException ocEx = (OcException) throwable;
175                     Log.e(TAG, ocEx.toString());
176                     ErrorCode errCode = ocEx.getErrorCode();
177                     logMessage("Error code: " + errCode);
178                 }
179             }
180     };
181
182     OcCloudProvisioning.GetIndividualAclInfoListener getIndividualAclInfoListener =
183         new OcCloudProvisioning.GetIndividualAclInfoListener() {
184             @Override
185                 public void getIndividualAclInfoListener(int result) {
186                     Log.d(TAG, "Inside getIndividualAclInfoListener ");
187                     if (result == 0) {
188                         logMessage("Individual ACL Info Successfull !!");
189                     } else {
190                         logMessage("Error: Individual ACL Info failed !!");
191                     }
192                 }
193         };
194     OcCloudProvisioning.RequestCertificateListener requestCertificateListener =
195         new OcCloudProvisioning.RequestCertificateListener() {
196             @Override
197                 public void requestCertificateListener(int result) {
198                     Log.d(TAG, "Inside requestCertificateListener ");
199                     if (result == 0) {
200                         logMessage("Request certificate Successfull !!");
201                     } else {
202                         logMessage("Error: Request certificate failed !!");
203                     }
204                 }
205         };
206     OcCloudProvisioning.GetCRLListener getCRLListener =
207         new OcCloudProvisioning.GetCRLListener() {
208             @Override
209                 public void getCRLListener(int result) {
210                     Log.d(TAG, "Inside getCRLListener ");
211                     if (result == 0) {
212                         logMessage("Get CRL Successfull !!");
213                     } else {
214                         logMessage("Error: Get CRL failed !!");
215                     }
216                 }
217         };
218     OcCloudProvisioning.PostCRLListener postCRLListener =
219         new OcCloudProvisioning.PostCRLListener() {
220             @Override
221                 public void postCRLListener(int result) {
222                     Log.d(TAG, "Inside postCRLListener ");
223                     if (result == 4) {
224                         logMessage("Post CRL Successfull !!");
225                     } else {
226                         logMessage("Error: Post CRL failed !!");
227                     }
228                 }
229         };
230     private static final int BUFFER_SIZE = 1024;
231     private final int REQUEST_LOGIN = 1;
232     Button signUp, signIn, signOut, getAclId, getAclInfo, requestCert, postCrl, getCrl;
233     TextView userid;
234     LinearLayout lyt1, lyt2, signupLyt, signinLyt;
235     // private TextView eventView;
236     SharedPreferences settingPreference;
237     OcCloudProvisioning ocCloudProvisioning;
238     String acl_Id;
239     String createacl_Id = null;
240     OcCloudProvisioning.GetAclIdByDeviceListener getAclIdByDeviceListener =
241         new OcCloudProvisioning.GetAclIdByDeviceListener() {
242             @Override
243                 public void getAclIdByDeviceListener(int result, String aclId) {
244                     Log.d(TAG, "Inside getAclIdByDeviceListener ");
245                     if (result == 0) {
246                         acl_Id = aclId;
247                         logMessage("Acl Id by device !!" + acl_Id);
248                     } else {
249                         logMessage("Error: Acl Id by device failed !!");
250                     }
251                 }
252         };
253
254     OcCloudProvisioning.CreateAclIdListener createAclIdListener =
255         new OcCloudProvisioning.CreateAclIdListener() {
256             @Override
257                 public void createAclIdListener(int result, String aclId) {
258                     Log.d(TAG, "Inside createAclIdListener ");
259                     if (result == 0) {
260                         createacl_Id = aclId;
261                         logMessage("Acl Id by create aclid !!" + createacl_Id);
262                     } else {
263                         logMessage("Error: Acl Id by create aclid failed !!");
264                     }
265                 }
266         };
267     private OcAccountManager mAccountManager;
268     private String filePath = "";
269     private TextView mEventsTextView;
270
271     @Override
272         protected void onCreate(Bundle savedInstanceState) {
273             super.onCreate(savedInstanceState);
274             setContentView(R.layout.main_activity);
275
276             signUp = (Button) findViewById(R.id.signup);
277             signIn = (Button) findViewById(R.id.signin);
278             signOut = (Button) findViewById(R.id.signout);
279             getAclId = (Button) findViewById(R.id.getAclId);
280             getAclInfo = (Button) findViewById(R.id.getAclInfo);
281             requestCert = (Button) findViewById(R.id.request);
282             postCrl = (Button) findViewById(R.id.postCRL);
283             getCrl = (Button) findViewById(R.id.getCRL);
284
285             lyt1 = (LinearLayout) findViewById(R.id.lyt1);
286             lyt2 = (LinearLayout) findViewById(R.id.lyt2);
287             signupLyt = (LinearLayout) findViewById(R.id.signupLyt);
288             signinLyt = (LinearLayout) findViewById(R.id.signinLyt);
289
290
291             userid = (TextView) findViewById(R.id.userid);
292
293             mEventsTextView = (TextView) findViewById(R.id.eventView);
294
295             filePath = getFilesDir().getPath() + "/"; //  data/data/<package>/files/
296             //copy CBOR file when application runs first time
297             settingPreference = PreferenceManager.getDefaultSharedPreferences(this);
298             boolean isFirstRun = settingPreference.getBoolean("FIRSTRUN", true);
299             if (isFirstRun) {
300                 copyCborFromAsset();
301                 SharedPreferences.Editor editor = settingPreference.edit();
302                 editor.putBoolean("FIRSTRUN", false);
303                 editor.putString("IP", StringConstants.DEFAULT_COAP_DERVER_IP);
304                 editor.putString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT);
305                 editor.putString("DEVICEID", StringConstants.DEFAULT_DEVICE_ID);
306                 editor.putString("OWNERID", StringConstants.DEFAULT_OWNER_ID);
307                 editor.putString("SERIALNUMBER", StringConstants.DEFAULT_SERIAL_NUMBER);
308                 editor.commit();
309             }
310             if (settingPreference.getString("useruuid", "").equals("")) {
311
312                 lyt1.setVisibility(View.GONE);
313                 lyt2.setVisibility(View.GONE);
314                 signupLyt.setVisibility(View.VISIBLE);
315                 signinLyt.setVisibility(View.GONE);
316
317
318             } else {
319                 userid.setText(settingPreference.getString("useruuid", ""));
320                 lyt1.setVisibility(View.VISIBLE);
321                 lyt2.setVisibility(View.VISIBLE);
322                 signupLyt.setVisibility(View.GONE);
323                 signinLyt.setVisibility(View.VISIBLE);
324             }
325
326             initOICStack();
327             ocCloudProvisioning = new OcCloudProvisioning(settingPreference.getString("IP", ""),
328                     Integer.valueOf(settingPreference.getString("PORT", "")));
329
330             signUp.setOnClickListener(new View.OnClickListener() {
331                     @Override
332                     public void onClick(View v) {
333                     signUp();
334                     }
335                     });
336             signIn.setOnClickListener(new View.OnClickListener() {
337                     @Override
338                     public void onClick(View v) {
339                     signIn();
340                     }
341                     });
342             signOut.setOnClickListener(new View.OnClickListener() {
343                     @Override
344                     public void onClick(View v) {
345                     signOut();
346                     }
347                     });
348             getAclId.setOnClickListener(new View.OnClickListener() {
349                     @Override
350                     public void onClick(View v) {
351                     getAclId();
352                     }
353                     });
354             getAclInfo.setOnClickListener(new View.OnClickListener() {
355                     @Override
356                     public void onClick(View v) {
357                     getAclInfo();
358                     }
359                     });
360             requestCert.setOnClickListener(new View.OnClickListener() {
361                     @Override
362                     public void onClick(View v) {
363                     requestCert();
364                     }
365                     });
366             postCrl.setOnClickListener(new View.OnClickListener() {
367                     @Override
368                     public void onClick(View v) {
369                     postCrl();
370                     }
371                     });
372             getCrl.setOnClickListener(new View.OnClickListener() {
373                     @Override
374                     public void onClick(View v) {
375                     getCrl();
376                     }
377                     });
378         }
379
380
381     @Override
382         public boolean onCreateOptionsMenu(Menu menu) {
383             // Inflate the menu; this adds items to the action bar if it is present.
384             getMenuInflater().inflate(R.menu.menu_cloud_provision, menu);
385             return true;
386         }
387
388     @Override
389         public boolean onOptionsItemSelected(MenuItem item) {
390             switch (item.getItemId()) {
391                 case R.id.action_settings:
392                     setDefualtSettings();
393                     return (true);
394
395
396             }
397             return (super.onOptionsItemSelected(item));
398         }
399
400     private void signIn() {
401         try {
402             logMessage("signIn");
403             if(mAccountManager==null)
404             {
405                 mAccountManager = OcPlatform.constructAccountManagerObject(
406                         StringConstants.COAP_TCP + settingPreference.getString("IP",
407                             StringConstants.DEFAULT_COAP_DERVER_IP) + ":" +
408                         settingPreference.getString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT),
409                         EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));
410             }
411
412             mAccountManager.signIn(settingPreference.getString("useruuid", ""),
413                     settingPreference.getString("accesstoken", ""), onSignIn);
414         } catch (OcException e) {
415             e.printStackTrace();
416         }
417     }
418
419     private void signOut() {
420         try {
421             logMessage("signOut");
422             if(mAccountManager==null)
423             {
424                 logMessage("Please signIn first");
425                 return;
426             }
427
428             mAccountManager.signOut(settingPreference.getString("accesstoken", ""),onSignOut);
429         } catch (OcException e) {
430             e.printStackTrace();
431         }
432     }
433
434
435     private void getAclId() {
436         try {
437             if(createacl_Id == null)
438             {
439                 ocCloudProvisioning.createAclId(settingPreference.getString("OWNERID", ""),
440                              settingPreference.getString("DEVICEID", ""), createAclIdListener);
441             }
442             else{
443                 ocCloudProvisioning.getAclIdByDevice(settingPreference.getString("DEVICEID", ""),
444                         getAclIdByDeviceListener);
445             }
446         } catch (OcException e) {
447             e.printStackTrace();
448         }
449     }
450
451     private void getAclInfo() {
452         try {
453             logMessage("getAclInfo");
454             logMessage("\taclid="+acl_Id);
455             ocCloudProvisioning.getIndividualAclInfo(acl_Id, getIndividualAclInfoListener);
456
457         } catch (OcException e) {
458             e.printStackTrace();
459         }
460     }
461
462     private void requestCert() {
463         try {
464             logMessage("requestCert");
465             ocCloudProvisioning = new OcCloudProvisioning(settingPreference.getString("IP", ""),
466                     Integer.valueOf(settingPreference.getString("PORT", "")));
467             ocCloudProvisioning.requestCertificate(requestCertificateListener);
468
469         } catch (OcException e) {
470             e.printStackTrace();
471         }
472     }
473
474     private void postCrl() {
475         try {
476             logMessage("postCrl");
477             ArrayList<String> arrayList = new ArrayList<>();
478             arrayList.add(settingPreference.getString("SERIALNUMBER", "1234"));
479
480             ocCloudProvisioning.postCRL("20160727000000", "20161027000000", null, arrayList, postCRLListener);
481
482         } catch (OcException e) {
483             e.printStackTrace();
484         }
485     }
486
487     private void getCrl() {
488         try {
489             logMessage("getCrl");
490             ocCloudProvisioning.getCRL(getCRLListener);
491
492         } catch (OcException e) {
493             e.printStackTrace();
494         }
495     }
496
497     private void signUp() {
498         try {
499             mAccountManager = OcPlatform.constructAccountManagerObject(
500                     StringConstants.COAP_TCP + settingPreference.getString("IP",
501                         StringConstants.DEFAULT_COAP_DERVER_IP) + settingPreference.getString("PORT",
502                             StringConstants.DEFAULT_COAP_DERVER_PORT),
503                     EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));
504             logMessage(StringConstants.COAP_TCP + settingPreference.getString("IP",
505                         StringConstants.DEFAULT_COAP_DERVER_IP) + settingPreference.getString("PORT",
506                             StringConstants.DEFAULT_COAP_DERVER_PORT));
507         } catch (OcException e) {
508             e.printStackTrace();
509         }
510
511         Intent intentLogin = new Intent(this, LoginActivity.class);
512         startActivityForResult(intentLogin, REQUEST_LOGIN);
513     }
514
515
516     /**
517      * configure OIC platform and call findResource
518      */
519     private void initOICStack() {
520         //create platform config
521         PlatformConfig cfg = new PlatformConfig(
522                 this,
523                 ServiceType.IN_PROC,
524                 ModeType.CLIENT_SERVER,
525                 "0.0.0.0", // bind to all available interfaces
526                 0,
527                 QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE);
528         OcPlatform.Configure(cfg);
529         try {
530             /*
531              * Initialize DataBase
532              */
533             String sqlDbPath = getFilesDir().getAbsolutePath().replace("files", "databases") +
534                 File.separator;
535             File file = new File(sqlDbPath);
536             //check files directory exists
537             if (!(file.isDirectory())) {
538                 file.mkdirs();
539                 Log.d(TAG, "Sql db directory created at " + sqlDbPath);
540             }
541             Log.d(TAG, "Sql db directory exists at " + sqlDbPath);
542             OcProvisioning.provisionInit(sqlDbPath + StringConstants.OIC_SQL_DB_FILE);
543         } catch (OcException e) {
544             logMessage(TAG + "provisionInit error: " + e.getMessage());
545             Log.e(TAG, e.getMessage());
546         }
547     }
548
549     @Override
550         public void onActivityResult(int requestCode, int resultCode, Intent data) {
551             super.onActivityResult(requestCode, resultCode, data);
552             if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_LOGIN) {
553                 String mAuthCode = data.getStringExtra("authCode");
554
555
556                 logMessage("\tauthCode: " + mAuthCode);
557
558                 try {
559                     logMessage("Sign Up");
560                     mAccountManager = OcPlatform.constructAccountManagerObject(
561                             StringConstants.COAP_TCP + settingPreference.getString("IP",
562                                 StringConstants.DEFAULT_COAP_DERVER_IP) + ":" +
563                             settingPreference.getString("PORT", StringConstants.DEFAULT_COAP_DERVER_PORT),
564                             EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));
565
566                     mAccountManager.signUp("github", mAuthCode, onSignUp);
567                 } catch (OcException e) {
568                     e.printStackTrace();
569                 }
570             }
571         }
572
573
574     /**
575      * Copy svr db CBOR dat file from assets folder to app data files dir
576      */
577     private void copyCborFromAsset() {
578         InputStream inputStream = null;
579         OutputStream outputStream = null;
580         int length;
581         byte[] buffer = new byte[BUFFER_SIZE];
582         try {
583             inputStream = getAssets().open(StringConstants.OIC_CLIENT_CBOR_DB_FILE);
584             File file = new File(filePath);
585             //check files directory exists
586             if (!(file.exists() && file.isDirectory())) {
587                 file.mkdirs();
588             }
589             outputStream = new FileOutputStream(filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE);
590             while ((length = inputStream.read(buffer)) != -1) {
591                 outputStream.write(buffer, 0, length);
592             }
593         } catch (NullPointerException e) {
594             logMessage(TAG + "Null pointer exception " + e.getMessage());
595             Log.e(TAG, e.getMessage());
596         } catch (FileNotFoundException e) {
597             logMessage(TAG + "CBOR svr db file not found " + e.getMessage());
598             Log.e(TAG, e.getMessage());
599         } catch (IOException e) {
600             logMessage(TAG + StringConstants.OIC_CLIENT_CBOR_DB_FILE + " file copy failed");
601             Log.e(TAG, e.getMessage());
602         } finally {
603             if (inputStream != null) {
604                 try {
605                     inputStream.close();
606                 } catch (IOException e) {
607                     Log.e(TAG, e.getMessage());
608                 }
609             }
610             if (outputStream != null) {
611                 try {
612                     outputStream.close();
613                 } catch (IOException e) {
614                     Log.e(TAG, e.getMessage());
615                 }
616             }
617         }
618     }
619
620     public void logMessage(String text) {
621         logMsg(text);
622     }
623
624     public void logMsg(final String text) {
625         runOnUiThread(new Runnable() {
626                 public void run() {
627                 Message msg = new Message();
628                 msg.obj = text;
629                 mEventsTextView.append(text);
630                 mEventsTextView.append("\n");
631                 }
632                 });
633         Log.i(TAG, text);
634         Intent intent = new Intent(getPackageName());
635         intent.putExtra(StringConstants.MESSAGE, text);
636         sendBroadcast(intent);
637     }
638
639
640     private void setDefualtSettings() {
641         View setingLayout = getLayoutInflater().inflate(R.layout.setting_layout, null);
642
643         final EditText ip = (EditText) setingLayout.findViewById(R.id.ip);
644         final EditText port = (EditText) setingLayout.findViewById(R.id.port);
645         final EditText deviceId = (EditText) setingLayout.findViewById(R.id.deviceId);
646         final EditText serialNum = (EditText) setingLayout.findViewById(R.id.serialNum);
647
648         ip.setText(settingPreference.getString("IP", ""));
649         port.setText(settingPreference.getString("PORT", ""));
650         deviceId.setText(settingPreference.getString("DEVICEID", ""));
651         serialNum.setText(settingPreference.getString("SERIALNUMBER", "1234"));
652
653
654         final AlertDialog.Builder builder = new AlertDialog.Builder(CloudProvisioningClient.this);
655         builder.setView(setingLayout);
656         builder.setPositiveButton("Submit", null).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
657                 public void onClick(DialogInterface dialog, int id) {
658                 Log.d(TAG, "AlertDialog onClick");
659
660                 }
661                 });
662         final AlertDialog alertDialog = builder.create();
663         alertDialog.setMessage("Settings");
664         alertDialog.show();
665         alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
666                 @Override
667                 public void onClick(View v) {
668
669                 String ip_address = ip.getText().toString();
670                 String port_num = port.getText().toString();
671                 String deviceId_val = deviceId.getText().toString();
672                 String serialNum_val = serialNum.getText().toString();
673
674                 SharedPreferences.Editor editor = settingPreference.edit();
675
676                 editor.putString("IP", ip_address);
677                 editor.putString("PORT", port_num);
678                 editor.putString("DEVICEID", deviceId_val);
679                 editor.putString("SERIALNUMBER", serialNum_val);
680                 editor.commit();
681
682                 alertDialog.cancel();
683                 }
684                 });
685         alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
686                 @Override
687                 public void onClick(View v) {
688
689                 alertDialog.cancel();
690                 }
691                 });
692
693     }
694
695     @Override
696         public void onPostCompleted(List<OcHeaderOption> ocHeaderOptions, OcRepresentation ocRepresentation) {
697
698         }
699
700     @Override
701         public void onPostFailed(Throwable throwable) {
702
703         }
704
705
706     /**
707      * to display on Server Message on Client screen
708      */
709     public class MessageReceiver extends BroadcastReceiver {
710         @Override
711             public void onReceive(Context context, Intent intent) {
712                 final String message = intent.getStringExtra(StringConstants.MESSAGE);
713                 logMessage(message);
714             }
715     }
716 }