Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / android / SampleResourceHosting / src / com / example / resourcehostingsampleapp / ResourceHostingSampleApp.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.lang.reflect.Method;
23 import java.net.Inet4Address;
24 import java.net.InetAddress;
25 import java.net.NetworkInterface;
26 import java.net.SocketException;
27 import java.util.Enumeration;
28
29 import org.iotivity.ResourceHosting.ResourceHosting;
30 import org.iotivity.base.ModeType;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcResourceHandle;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.QualityOfService;
35 import org.iotivity.base.ServiceType;
36
37 import android.app.Activity;
38 import android.os.Bundle;
39 import android.util.Log;
40 import android.view.View;
41 import android.view.View.OnClickListener;
42 import android.widget.TextView;
43 import android.widget.Toast;
44
45 /**
46  * To execute resource hosting function for android sample application .
47  * @author Copyright 2015 Samsung Electronics All Rights Reserved.
48  * @see className class :   ResourceHosting</br>
49  *
50  */
51
52 public class ResourceHostingSampleApp extends Activity implements OnClickListener
53 {
54         private final int OCSTACK_OK = 0;
55         private final int OCSTACK_ERROR = 255;
56         private final int RESOURCEHOSTING_DO_NOT_THREADRUNNING = -2;
57
58         private String TAG = "ResourceHosting";
59         private OcResourceHandle mResourceHandle;
60         private String  mIpAddress;
61         private TextView mLogTextView;
62         private String mLog = "";
63         private ResourceHosting resourceHosting;
64         /**
65          * To initialize UI Function Setting.
66          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
67          * @see Method  method : onCreate</br>
68          */
69         protected void onCreate(Bundle savedInstanceState)
70         {
71             super.onCreate(savedInstanceState);
72             setContentView(R.layout.activity_main);
73             mLogTextView = (TextView) findViewById(R.id.txtLog);
74             findViewById(R.id.btnStartHosting).setOnClickListener(this);
75             findViewById(R.id.btnStopHosting).setOnClickListener(this);
76             findViewById(R.id.btLogClear).setOnClickListener(this);
77
78             PlatformConfig platformConfigObj;
79             resourceHosting = new ResourceHosting();
80             platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
81                     ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
82
83             Log.i(TAG, "Before Calling Configure of ocPlatform");
84             OcPlatform.Configure(platformConfigObj);
85             Log.i(TAG, "Configuration done Successfully");
86         }
87
88         /**
89          * To execute initOICStack for running Resource hosting.
90          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
91          * @see Method  method : onStart</br>
92          */
93         @Override
94         protected void onStart()
95         {
96             super.onStart();
97             initOICStack();
98         }
99
100         /**
101          * To terminate Resource hosting.
102          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
103          * @see Method  method : onStop</br>
104          */
105         @Override
106         protected void onStop()
107         {
108             super.onStop();
109             int result;
110             result = resourceHosting.ResourceHostingTerminate();
111             Log.d(TAG, "ResourceHostingTerminate : "+ result);
112         }
113
114         protected void onResume()
115         {
116             super.onResume();
117         }
118
119         /**
120          * To execute initOICStack for running Resource hosting.
121          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
122          * @see Method  method : onRestart</br>
123          */
124         @Override
125         protected void onRestart()
126         {
127             super.onRestart();
128             initOICStack();
129         }
130
131         /**
132          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
133          * @see Method  method : onDestroy</br>
134          */
135         protected void onDestroy()
136         {
137             super.onDestroy();
138         }
139
140         /**
141          * get IpAddress and execute resourceHostingInit() method.
142          * @see Class   class : com_example_resourcehostingsampleapp_ResourceHosting</br>
143          * @see Method  method : initOICStack</br>
144          */
145         private void initOICStack()
146         {
147             try
148             {
149                 mIpAddress = getIpAddress();
150                 int result = 0;
151                 result = resourceHosting.ResourceHostingInit(mIpAddress);
152                 Log.d(TAG, "ResourceHostingInit : " + result);
153             }
154             catch (Exception e)
155             {
156                 e.printStackTrace();
157             }
158         }
159
160         /**
161          * @see Class   class :  com_example_resourcehostingsampleapp_ResourceHosting</br>
162          * @see Method  method :  getIpAddress</br>
163          */
164         private String getIpAddress()
165         {
166             try
167             {
168                 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
169                      en.hasMoreElements();)
170                 {
171                     NetworkInterface intf = (NetworkInterface) en.nextElement();
172                     for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
173                     {
174                         InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
175                         if (!inetAddress.isLoopbackAddress())
176                         {
177                             if (inetAddress instanceof Inet4Address)
178                                 return inetAddress.getHostAddress().toString();
179                         }
180                     }
181                 }
182             }
183             catch (SocketException e)
184             {
185                 e.printStackTrace();
186             }
187             return null;
188         }
189
190         /**
191          * @see Class  class :   com_example_resourcehostingsampleapp_ResourceHosting</br>
192          * @see Method method :   onClick</br>
193          * @param v view to choice
194          */
195         public void onClick(View v)
196         {
197             int getId = v.getId();
198
199             switch (getId)
200             {
201                 case R.id.btnStartHosting:
202                     try
203                     {
204                         int result;
205                         result = resourceHosting.OICCoordinatorStart();
206                         Log.d(TAG, "OICCoordinatorStart : " + result);
207                     }
208                     catch (Exception e)
209                     {
210                         Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
211                     }
212                     break;
213                 case R.id.btnStopHosting:
214                     int result;
215                     result = resourceHosting.OICCoordinatorStop();
216                     Log.d(TAG, "OICCoordinatorStop : "+ result);
217                     break;
218                 case R.id.btLogClear:
219                 default:
220                     break;
221             }
222         }
223
224 }