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