1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package com.example.sample.provider;
22 import org.iotivity.base.ModeType;
23 import org.iotivity.base.OcException;
24 import org.iotivity.base.OcPlatform;
25 import org.iotivity.base.PlatformConfig;
26 import org.iotivity.base.QualityOfService;
27 import org.iotivity.base.ServiceType;
29 import android.app.Activity;
30 import android.content.BroadcastReceiver;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.os.Bundle;
35 import android.os.Handler;
36 import android.os.Message;
37 import android.support.v4.content.LocalBroadcastManager;
38 import android.util.Log;
39 import android.view.KeyEvent;
40 import android.view.View;
41 import android.view.View.OnClickListener;
42 import android.widget.ScrollView;
43 import android.widget.TextView;
47 public class SampleProvider extends Activity implements OnClickListener,
50 private final static String TAG = "NMProvider : SampleProvider";
51 private TextView mLogTextView;
52 private TextView mTempValue;
53 private TextView mHumValue;
54 private TemperatureResource mySensor;
55 private boolean isExecutePresence;
56 private ScrollView sv_sclLog;
57 private MessageReceiver mMessageReceiver = new MessageReceiver();
58 private Handler mHandler;
59 private static String message;
60 private static SampleProvider sampleProviderObj;
65 * To initialize UI Function Setting
66 * To execute initOICStack for running find resource
69 public void onCreate(Bundle savedInstanceState)
72 sampleProviderObj = this;
73 super.onCreate(savedInstanceState);
74 setContentView(R.layout.sampleprovider_layout);
75 registerReceiver(mMessageReceiver, new IntentFilter(
76 "com.example.sample.provider.SampleProvider"));
78 mLogTextView = (TextView) findViewById(R.id.txtLog);
79 mTempValue = (TextView) findViewById(R.id.temperatureValue);
80 mHumValue = (TextView) findViewById(R.id.humidityValue);
82 sv_sclLog = (ScrollView)findViewById(R.id.sclLog);
83 sv_sclLog.fullScroll(View.FOCUS_DOWN);
84 findViewById(R.id.btnTemperatureUP).setOnClickListener(this);
85 findViewById(R.id.btnTemperatureDown).setOnClickListener(this);
86 findViewById(R.id.btnHumidityUP).setOnClickListener(this);
87 findViewById(R.id.btnHumidityDown).setOnClickListener(this);
88 findViewById(R.id.btnLogClear).setOnClickListener(this);
90 isExecutePresence = false;
91 mHandler = new Handler() {
93 public void handleMessage(Message msg) {
96 String[] tempHum = message.split(":");
97 mTempValue.setText(tempHum[0]);
98 mHumValue.setText(tempHum[1]);
102 setmHandler(mHandler);
105 private void initOICStack()
107 // create platform config
108 PlatformConfig cfg = new PlatformConfig(this,ServiceType.IN_PROC,
109 ModeType.SERVER, "0.0.0.0", // bind to all available interfaces
110 0, QualityOfService.LOW);
112 OcPlatform.Configure(cfg);
116 OcPlatform.startPresence(30);
117 isExecutePresence = true;
119 catch (OcException e)
124 mySensor = new TemperatureResource(this);
125 // create and register a resource
126 mySensor.createResource();
129 * To execute initOICStack for running find resource
132 protected void onStart()
139 * To execute initOICStack for running find resource
142 protected void onRestart()
149 * To terminate presence process and unregister messageHandler(to get message from JNI Function)
152 protected void onDestroy()
158 mySensor.destroyResource();
160 if (isExecutePresence == true)
162 OcPlatform.stopPresence();
163 isExecutePresence = false;
166 catch (OcException e)
170 LocalBroadcastManager.getInstance(this).unregisterReceiver(
174 public class MessageReceiver extends BroadcastReceiver
177 public void onReceive(Context context, Intent intent)
179 final String message = intent
180 .getStringExtra(StringConstants.MESSAGE);
186 public void logMessage(final String text)
188 runOnUiThread(new Runnable()
192 final Message msg = new Message();
194 mLogTextView.append("\n");
195 mLogTextView.append(text);
201 * To terminate presence process and unregister messagehandler(to get message from JNI Function)
204 protected void onStop()
209 mySensor.destroyResource();
211 if (isExecutePresence == true)
213 OcPlatform.stopPresence();
214 isExecutePresence = false;
218 catch (OcException e)
223 LocalBroadcastManager.getInstance(this).unregisterReceiver(
228 public void onClick(View v)
230 int getId = v.getId();
234 case R.id.btnTemperatureUP:
235 logMessage(TAG + "Click temerature up btn");
237 mySensor.notifyObserver();
239 case R.id.btnTemperatureDown:
240 logMessage(TAG + "Click temerature down btn");
242 mySensor.notifyObserver();
244 case R.id.btnHumidityUP:
245 logMessage(TAG + "Click Humidity up btn");
246 mySensor.mhumidity++;
247 mySensor.notifyObserver();
249 case R.id.btnHumidityDown:
250 logMessage(TAG + "Click Humidity down btn");
251 mySensor.mhumidity--;
252 mySensor.notifyObserver();
254 case R.id.btnLogClear:
255 mLogTextView.setText("");
256 Log.i(TAG, "Log message cleared");
260 mTempValue.setText(String.valueOf(mySensor.getTemp()));
261 mHumValue.setText(String.valueOf(mySensor.getHumidity()));
265 * To handle event about back button
268 public boolean onKeyDown(int keyCode, KeyEvent event)
270 if (keyCode == KeyEvent.KEYCODE_BACK )
274 mySensor.destroyResource();
276 if (isExecutePresence == true)
278 OcPlatform.stopPresence();
279 isExecutePresence = false;
282 catch (OcException e)
287 LocalBroadcastManager.getInstance(this).unregisterReceiver(
290 return super.onKeyDown(keyCode, event);
293 public Handler getmHandler() {
297 public void setmHandler(Handler mHandler) {
298 this.mHandler = mHandler;
301 public static SampleProvider getSampleProviderObject() {
302 return sampleProviderObj;
305 public static void setmessage(String msg) {