Integrated IP adapter Singlethread and Multithread files
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / android / ResourceHostingSampleApp / src / com / example / resourcehostingsampleapp / ResourceHosting.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package com.example.resourcehostingsampleapp;
21
22 import java.net.Inet4Address;
23 import java.net.InetAddress;
24 import java.net.NetworkInterface;
25 import java.net.SocketException;
26 import java.util.Enumeration;
27
28 import org.iotivity.base.ModeType;
29 import org.iotivity.base.OcPlatform;
30 import org.iotivity.base.OcResourceHandle;
31 import org.iotivity.base.PlatformConfig;
32 import org.iotivity.base.QualityOfService;
33 import org.iotivity.base.ServiceType;
34
35 import android.app.Activity;
36 import android.os.Bundle;
37 import android.util.Log;
38 import android.view.View;
39 import android.view.View.OnClickListener;
40 import android.widget.TextView;
41 import android.widget.Toast;
42
43 /**
44  * To execute resource hosting function for android sample application .
45  * @author Copyright 2015 Samsung Electronics All Rights Reserved.
46  * @see className class :   ResourceHosting</br>
47  *
48  */
49
50 public class ResourceHosting extends Activity implements OnClickListener
51 {
52         private final int OCSTACK_OK = 0;
53         private final int OCSTACK_ERROR = 255;
54         private final int RESOURCEHOSTING_DO_NOT_THREADRUNNING = -2;
55
56         private final String TAG = "NMResourceHosting : " + this.getClass().getSimpleName();
57         private TextView mLogTextView;
58         private String mLog = "";
59         /**
60          * To initialize UI Function Setting.
61          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
62          * @see Method  method : onCreate</br>
63          */
64         protected void onCreate(Bundle savedInstanceState)
65         {
66             super.onCreate(savedInstanceState);
67             setContentView(R.layout.activity_main);
68             mLogTextView = (TextView) findViewById(R.id.txtLog);
69             findViewById(R.id.btnStartHosting).setOnClickListener(this);
70             findViewById(R.id.btnStopHosting).setOnClickListener(this);
71             findViewById(R.id.btLogClear).setOnClickListener(this);
72
73             PlatformConfig platformConfigObj;
74
75             platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
76                     ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
77
78             Log.i(TAG, "Before Calling Configure of ocPlatform");
79             OcPlatform.Configure(platformConfigObj);
80             Log.i(TAG, "Configuration done Successfully");
81         }
82
83         /**
84          * To execute initOICStack for running Resource hosting.
85          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
86          * @see Method  method : onStart</br>
87          */
88         @Override
89         protected void onStart()
90         {
91             super.onStart();
92         }
93
94         /**
95          * To terminate Resource hosting.
96          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
97          * @see Method  method : onStop</br>
98          */
99         @Override
100         protected void onStop()
101         {
102             super.onStop();
103         }
104
105         protected void onResume()
106         {
107             super.onResume();
108         }
109
110         /**
111          * To execute initOICStack for running Resource hosting.
112          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
113          * @see Method  method : onRestart</br>
114          */
115         @Override
116         protected void onRestart()
117         {
118             super.onRestart();
119         }
120
121         /**
122          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
123          * @see Method  method : onDestroy</br>
124          */
125         protected void onDestroy()
126         {
127             super.onDestroy();
128             int result;
129             result = OICCoordinatorStop();
130             Log.d(TAG, "OICCoordinatorStop() : "+ result);
131         }
132
133         /**
134          * @see Class  class :   com_example_resourcehostingsampleapp_ResourceHosting</br>
135          * @see Method method :   onClick</br>
136          * @param v view to choice
137          */
138         public void onClick(View v)
139         {
140             int getId = v.getId();
141
142             switch (getId)
143             {
144                 case R.id.btnStartHosting:
145                     try
146                     {
147                         int result;
148                         result = OICCoordinatorStart();
149                         Log.d(TAG, "OICCoordinatorStart : " + result);
150                     }
151                     catch (Exception e)
152                     {
153                         Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
154                         Log.d(TAG, e.getMessage() + result);
155                     }
156                     break;
157                 case R.id.btnStopHosting:
158                     int result;
159                     result = OICCoordinatorStop();
160                     Log.d(TAG, "OICCoordinatorStop : "+ result);
161                     break;
162                 case R.id.btLogClear:
163                     clearLog();
164                 default:
165                     break;
166             }
167         }
168
169         /**
170          * all clear log view
171          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
172          * @see Method  method :  clearLog</br>
173          */
174         private void clearLog()
175         {
176             mLog = "";
177             mLogTextView.setText(mLog);
178             Log.i(TAG, "Log textbox is cleared");
179         }
180
181         /**
182          * recieve the callback log message.
183          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
184          * @see Method  method :  cbMessage</br>
185          * @param msg callback log message
186          */
187         public void cbMessage(String msg)
188         {
189             mLog += msg + "\n";
190             mLogTextView.setText(mLog);
191             Log.i(TAG, msg);
192         }
193
194         /**
195          * jni function - OicCorrdinatorstart() method.
196          * @see Class   class :    com_example_resourcehostingsampleapp_ResourceHosting</br>
197          * @see Method  method :  OICCoordinatorStart</br>
198          * @see Signature signature : ()V</br>
199          */
200         public native int OICCoordinatorStart();
201
202         /**
203          * jni function - OICCoordinatorStop() method.
204          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
205          * @see Method  method :  OICCoordinatorStop</br>
206          * @see signature  signature : ()V</br>
207          */
208         public native int OICCoordinatorStop();
209
210         /**
211          * jni function - ResourceHostingInit() method in order to execute OICCoordinatorStart() method.
212          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
213          * @see Method  method :  ResourceHostingInit</br>
214          * @param addr ipAddress
215          * @see signature signature : (Ljava/lang/String;)V</br>
216          */
217         public native int ResourceHostingInit(String addr);
218
219         /**
220          * jni function - ResourceHostingTerminate() method in order to terminate resource hosting
221          * @see Class   class  : com_example_resourcehostingsampleapp_ResourceHosting</br>
222          * @see Method  method : ResourceHostingTerminate</br>
223          * @see signature signature : ()V</br>
224          */
225         public native int ResourceHostingTerminate();
226
227     static
228     {
229         System.loadLibrary("gnustl_shared");
230         System.loadLibrary("oc_logger");
231         System.loadLibrary("connectivity_abstraction");
232         System.loadLibrary("ca-interface");
233         System.loadLibrary("octbstack");
234         System.loadLibrary("oc");
235         System.loadLibrary("ocstack-jni");
236         System.loadLibrary("NotificationManager");
237     }
238 }