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.fridgeserver;
\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.PlatformConfig;
\r
43 import org.iotivity.base.QualityOfService;
\r
44 import org.iotivity.base.ServiceType;
\r
46 import base.iotivity.org.examples.message.IMessageLogger;
\r
51 * This is the main fridgeServer class. This instantiates Refrigerator object
\r
52 * which has different resources (DeviceResource, LightResource, DoorResource).
\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
62 * configure OIC platform and call findResource
\r
64 private void initOICStack() {
\r
65 //create platform config
\r
66 PlatformConfig cfg = new PlatformConfig(
\r
68 ServiceType.IN_PROC,
\r
70 "0.0.0.0", // bind to all available interfaces
\r
72 QualityOfService.LOW);
\r
73 OcPlatform.Configure(cfg);
\r
74 logMessage(TAG + "Creating refrigerator resources");
\r
76 refrigerator = new Refrigerator(mContext);
\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
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
95 public class MessageReceiver extends BroadcastReceiver {
\r
97 public void onReceive(Context context, Intent intent) {
\r
98 final String message = intent.getStringExtra(StringConstants.MESSAGE);
\r
99 logMessage(message);
\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
110 mEventsTextView.append("\n");
\r
111 mEventsTextView.append(text);
\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
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