Android Base API merge to master
[contrib/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / FridgeServer.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.fridgeserver;\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.PlatformConfig;\r
43 import org.iotivity.base.QualityOfService;\r
44 import org.iotivity.base.ServiceType;\r
45 \r
46 import base.iotivity.org.examples.message.IMessageLogger;\r
47 \r
48 /**\r
49  * FridgeServer\r
50  * <p/>\r
51  * This is the main fridgeServer class. This instantiates Refrigerator object\r
52  * which has different resources (DeviceResource, LightResource, DoorResource).\r
53  */\r
54 public class FridgeServer extends Activity implements IMessageLogger {\r
55     private Context mContext;\r
56     private static String TAG = "FridgeServer: ";\r
57     private TextView mEventsTextView;\r
58     private MessageReceiver mMessageReceiver = new MessageReceiver();\r
59     private Refrigerator refrigerator;\r
60 \r
61     /**\r
62      * configure OIC platform and call findResource\r
63      */\r
64     private void initOICStack() {\r
65         //create platform config\r
66         PlatformConfig cfg = new PlatformConfig(\r
67                 this,\r
68                 ServiceType.IN_PROC,\r
69                 ModeType.SERVER,\r
70                 "0.0.0.0", // bind to all available interfaces\r
71                 0,\r
72                 QualityOfService.LOW);\r
73         OcPlatform.Configure(cfg);\r
74         logMessage(TAG + "Creating refrigerator resources");\r
75 \r
76         refrigerator = new Refrigerator(mContext);\r
77     }\r
78 \r
79     @Override\r
80     protected void onCreate(Bundle savedInstanceState) {\r
81         super.onCreate(savedInstanceState);\r
82         setContentView(R.layout.activity_fridge_server);\r
83         registerReceiver(mMessageReceiver, new IntentFilter(StringConstants.INTENT));\r
84 \r
85         mEventsTextView = new TextView(this);\r
86         mEventsTextView.setMovementMethod(new ScrollingMovementMethod());\r
87         LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);\r
88         layout.addView(mEventsTextView, new LinearLayout.LayoutParams\r
89                 (LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f));\r
90         mContext = this;\r
91 \r
92         initOICStack();\r
93     }\r
94 \r
95     public class MessageReceiver extends BroadcastReceiver {\r
96         @Override\r
97         public void onReceive(Context context, Intent intent) {\r
98             final String message = intent.getStringExtra(StringConstants.MESSAGE);\r
99             logMessage(message);\r
100         }\r
101     }\r
102 \r
103     @Override\r
104     public void logMessage(final String text) {\r
105         if (StringConstants.ENABLE_PRINTING) {\r
106             runOnUiThread(new Runnable() {\r
107                 public void run() {\r
108                     final Message msg = new Message();\r
109                     msg.obj = text;\r
110                     mEventsTextView.append("\n");\r
111                     mEventsTextView.append(text);\r
112                 }\r
113             });\r
114             Log.i(TAG, text);\r
115         }\r
116     }\r
117 \r
118     @Override\r
119     public boolean onCreateOptionsMenu(Menu menu) {\r
120         // Inflate the menu; this adds items to the action bar if it is present.\r
121         getMenuInflater().inflate(R.menu.menu_fridge_server, 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 }\r