2 * //******************************************************************
\r
4 * // Copyright 2015 Intel Corporation.
\r
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
8 * // Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * // you may not use this file except in compliance with the License.
\r
10 * // You may obtain a copy of the License at
\r
12 * // http://www.apache.org/licenses/LICENSE-2.0
\r
14 * // Unless required by applicable law or agreed to in writing, software
\r
15 * // distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * // See the License for the specific language governing permissions and
\r
18 * // limitations under the License.
\r
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
23 package org.iotivity.base.examples.simpleserver;
\r
25 import android.app.Activity;
\r
26 import android.content.BroadcastReceiver;
\r
27 import android.content.Context;
\r
28 import android.content.Intent;
\r
29 import android.content.IntentFilter;
\r
30 import android.os.Bundle;
\r
31 import android.os.Message;
\r
32 import android.support.v4.content.LocalBroadcastManager;
\r
33 import android.text.method.ScrollingMovementMethod;
\r
34 import android.util.Log;
\r
35 import android.view.Menu;
\r
36 import android.view.MenuItem;
\r
37 import android.widget.LinearLayout;
\r
38 import android.widget.TextView;
\r
40 import org.iotivity.base.ModeType;
\r
41 import org.iotivity.base.OcPlatform;
\r
42 import org.iotivity.base.OcRepresentation;
\r
43 import org.iotivity.base.PlatformConfig;
\r
44 import org.iotivity.base.QualityOfService;
\r
45 import org.iotivity.base.ServiceType;
\r
47 import base.iotivity.org.examples.message.IMessageLogger;
\r
52 * SimpleServer instantiates a TextView and creates and configures OICPlatform.
\r
53 * It also creates a LightResource and waits for the incoming client requests to handle specific scenarios.
\r
54 * This implements IMessageLogger to display messages on the screen
\r
57 public class SimpleServer extends Activity implements IMessageLogger {
\r
58 private final static String TAG = "SimpleServer: ";
\r
59 private TextView mEventsTextView;
\r
60 private MessageReceiver mMessageReceiver = new MessageReceiver();
\r
63 protected void onCreate(Bundle savedInstanceState) {
\r
64 super.onCreate(savedInstanceState);
\r
65 setContentView(R.layout.activity_main);
\r
66 registerReceiver(mMessageReceiver, new IntentFilter("org.iotivity.base.examples.simpleserver"));
\r
68 mEventsTextView = new TextView(this);
\r
69 mEventsTextView.setMovementMethod(new ScrollingMovementMethod());
\r
70 LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
\r
71 layout.addView(mEventsTextView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f));
\r
73 OcRepresentation rep = new OcRepresentation();
\r
74 rep.setValueBool("test", false);
\r
75 boolean result = rep.getValueBool("test");
\r
81 * configure OIC platform and call findResource
\r
83 private void initOICStack() {
\r
84 //create platform config
\r
85 PlatformConfig cfg = new PlatformConfig(
\r
87 ServiceType.IN_PROC,
\r
89 "0.0.0.0", // bind to all available interfaces
\r
91 QualityOfService.LOW);
\r
92 OcPlatform.Configure(cfg);
\r
93 // Create instance of lightResource
\r
94 LightResource myLight = new LightResource(this);
\r
95 // create and register a resource
\r
96 myLight.createResource0();
\r
99 public class MessageReceiver extends BroadcastReceiver {
\r
101 public void onReceive(Context context, Intent intent) {
\r
102 final String message = intent.getStringExtra(StringConstants.MESSAGE);
\r
103 logMessage(message);
\r
107 public void logMessage(final String text) {
\r
108 runOnUiThread(new Runnable() {
\r
109 public void run() {
\r
110 final Message msg = new Message();
\r
112 mEventsTextView.append("\n");
\r
113 mEventsTextView.append(text);
\r
120 public boolean onCreateOptionsMenu(Menu menu) {
\r
121 getMenuInflater().inflate(R.menu.menu_main, menu);
\r
126 public boolean onOptionsItemSelected(MenuItem item) {
\r
127 int id = item.getItemId();
\r
128 if (id == R.id.action_settings) {
\r
131 return super.onOptionsItemSelected(item);
\r
135 public void onDestroy() {
\r
141 protected void onStop() {
\r
142 LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
\r