53576c279714facb299cb499222e39e6f04abdfe
[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 String TAG = "ResourceHosting";
57         private OcResourceHandle mResourceHandle;
58         private String  mIpAddress;
59         private TextView mLogTextView;
60         private String mLog = "";
61         /**
62          * To initialize UI Function Setting.
63          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
64          * @see Method  method : onCreate</br>
65          */
66         protected void onCreate(Bundle savedInstanceState)
67         {
68             super.onCreate(savedInstanceState);
69             setContentView(R.layout.activity_main);
70             mLogTextView = (TextView) findViewById(R.id.txtLog);
71             findViewById(R.id.btnStartHosting).setOnClickListener(this);
72             findViewById(R.id.btnStopHosting).setOnClickListener(this);
73             findViewById(R.id.btLogClear).setOnClickListener(this);
74
75             PlatformConfig platformConfigObj;
76
77             platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
78                     ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
79
80             Log.i(TAG, "Before Calling Configure of ocPlatform");
81             OcPlatform.Configure(platformConfigObj);
82             Log.i(TAG, "Configuration done Successfully");
83         }
84
85         /**
86          * To execute initOICStack for running Resource hosting.
87          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
88          * @see Method  method : onStart</br>
89          */
90         @Override
91         protected void onStart()
92         {
93             super.onStart();
94             initOICStack();
95         }
96
97         /**
98          * To terminate Resource hosting.
99          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
100          * @see Method  method : onStop</br>
101          */
102         @Override
103         protected void onStop()
104         {
105             super.onStop();
106             int result;
107             result = ResourceHostingTerminate();
108             Log.d(TAG, "ResourceHostingTerminate : "+ result);
109         }
110
111         protected void onResume()
112         {
113             super.onResume();
114         }
115
116         /**
117          * To execute initOICStack for running Resource hosting.
118          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
119          * @see Method  method : onRestart</br>
120          */
121         @Override
122         protected void onRestart()
123         {
124             super.onRestart();
125             initOICStack();
126         }
127
128         /**
129          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
130          * @see Method  method : onDestroy</br>
131          */
132         protected void onDestroy()
133         {
134             super.onDestroy();
135         }
136
137         /**
138          * get IpAddress and execute resourceHostingInit() method.
139          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
140          * @see Method  method : initOICStack</br>
141          */
142         private void initOICStack()
143         {
144             try
145             {
146                 mIpAddress = getIpAddress();
147                 int result;
148                 result = ResourceHostingInit(mIpAddress);
149                 Log.d(TAG, "ResourceHostingInit : " + result);
150             }
151             catch (Exception e)
152             {
153                 e.printStackTrace();
154             }
155         }
156
157         /**
158          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
159          * @see Method  method :  getIpAddress</br>
160          */
161         private String getIpAddress()
162         {
163             try
164             {
165                 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
166                      en.hasMoreElements();)
167                 {
168                     NetworkInterface intf = (NetworkInterface) en.nextElement();
169                     for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
170                     {
171                         InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
172                         if (!inetAddress.isLoopbackAddress())
173                         {
174                             if (inetAddress instanceof Inet4Address)
175                                 return inetAddress.getHostAddress().toString();
176                         }
177                     }
178                 }
179             }
180             catch (SocketException e)
181             {
182                 e.printStackTrace();
183             }
184             return null;
185         }
186
187         /**
188          * @see Class  class :   com_example_resourcehostingsampleapp_ResourceHosting</br>
189          * @see Method method :   onClick</br>
190          * @param v view to choice
191          */
192         public void onClick(View v)
193         {
194             int getId = v.getId();
195
196             switch (getId)
197             {
198                 case R.id.btnStartHosting:
199                     try
200                     {
201                         int result;
202                         result = OICCoordinatorStart();
203                         Log.d(TAG, "OICCoordinatorStart : " + result);
204                     }
205                     catch (Exception e)
206                     {
207                         Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
208                     }
209                     break;
210                 case R.id.btnStopHosting:
211                     int result;
212                     result = OICCoordinatorStop();
213                     Log.d(TAG, "OICCoordinatorStop : "+ result);
214                     break;
215                 case R.id.btLogClear:
216                     clearLog();
217                 default:
218                     break;
219             }
220         }
221
222         /**
223          * all clear log view
224          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
225          * @see Method  method :  clearLog</br>
226          */
227         private void clearLog()
228         {
229             mLog = "";
230             mLogTextView.setText(mLog);
231         }
232
233         /**
234          * recieve the callback log message.
235          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
236          * @see Method  method :  cbMessage</br>
237          * @param msg callback log message
238          */
239         public void cbMessage(String msg)
240         {
241             mLog += msg + "\n";
242             mLogTextView.setText(mLog);
243         }
244
245         /**
246          * jni function - OicCorrdinatorstart() method.
247          * @see Class   class :    com_example_resourcehostingsampleapp_ResourceHosting</br>
248          * @see Method  method :  OICCoordinatorStart</br>
249          * @see Signature signature : ()V</br>
250          */
251         public native int OICCoordinatorStart();
252
253         /**
254          * jni function - OICCoordinatorStop() method.
255          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
256          * @see Method  method :  OICCoordinatorStop</br>
257          * @see signature  signature : ()V</br>
258          */
259         public native int OICCoordinatorStop();
260
261         /**
262          * jni function - ResourceHostingInit() method in order to execute OICCoordinatorStart() method.
263          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
264          * @see Method  method :  ResourceHostingInit</br>
265          * @param addr ipAddress
266          * @see signature signature : (Ljava/lang/String;)V</br>
267          */
268         public native int ResourceHostingInit(String addr);
269
270         /**
271          * jni function - ResourceHostingTerminate() method in order to terminate resource hosting
272          * @see Class   class  : com_example_resourcehostingsampleapp_ResourceHosting</br>
273          * @see Method  method : ResourceHostingTerminate</br>
274          * @see signature signature : ()V</br>
275          */
276         public native int ResourceHostingTerminate();
277
278     static
279     {
280         System.loadLibrary("gnustl_shared");
281         System.loadLibrary("oc_logger");
282         System.loadLibrary("connectivity_abstraction");
283         System.loadLibrary("ca-interface");
284         System.loadLibrary("octbstack");
285         System.loadLibrary("oc");
286         System.loadLibrary("ocstack-jni");
287         System.loadLibrary("NotificationManager");
288     }
289 }