31b5c6aef8b7b58cbff22f345d85894dbb30e557
[platform/upstream/iotivity.git] / android / examples / simpleserver / src / main / java / org / iotivity / base / examples / simpleserver / SimpleServer.java
1 /*\r
2  * //******************************************************************\r
3  * //\r
4  * // Copyright 2015 Intel Corporation.\r
5  * //\r
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
7  * //\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
11  * //\r
12  * //      http://www.apache.org/licenses/LICENSE-2.0\r
13  * //\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
19  * //\r
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
21  */\r
22 \r
23 package org.iotivity.base.examples.simpleserver;\r
24 \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
39 \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
46 \r
47 import base.iotivity.org.examples.message.IMessageLogger;\r
48 \r
49 /**\r
50  * SimpleServer\r
51  *\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
55  */\r
56 \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
61 \r
62     @Override\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
67 \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
72 \r
73         OcRepresentation rep = new OcRepresentation();\r
74         rep.setValueBool("test", false);\r
75         boolean result = rep.getValueBool("test");\r
76 \r
77         initOICStack();\r
78     }\r
79 \r
80     /**\r
81      * configure OIC platform and call findResource\r
82      */\r
83     private void initOICStack() {\r
84         //create platform config\r
85         PlatformConfig cfg = new PlatformConfig(\r
86                 this,\r
87                 ServiceType.IN_PROC,\r
88                 ModeType.SERVER,\r
89                 "0.0.0.0", // bind to all available interfaces\r
90                 0,\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
97     }\r
98 \r
99     public class MessageReceiver extends BroadcastReceiver {\r
100         @Override\r
101         public void onReceive(Context context, Intent intent) {\r
102             final String message = intent.getStringExtra(StringConstants.MESSAGE);\r
103             logMessage(message);\r
104         }\r
105     }\r
106 \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
111                 msg.obj = text;\r
112                 mEventsTextView.append("\n");\r
113                 mEventsTextView.append(text);\r
114             }\r
115         });\r
116         Log.i(TAG, text);\r
117     }\r
118 \r
119     @Override\r
120     public boolean onCreateOptionsMenu(Menu menu) {\r
121         getMenuInflater().inflate(R.menu.menu_main, menu);\r
122         return true;\r
123     }\r
124 \r
125     @Override\r
126     public boolean onOptionsItemSelected(MenuItem item) {\r
127         int id = item.getItemId();\r
128         if (id == R.id.action_settings) {\r
129             return true;\r
130         }\r
131         return super.onOptionsItemSelected(item);\r
132     }\r
133 \r
134     @Override\r
135     public void onDestroy() {\r
136         super.onDestroy();\r
137         onStop();\r
138     }\r
139 \r
140     @Override\r
141     protected void onStop() {\r
142         LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);\r
143         super.onStop();\r
144     }\r
145 }