2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base.examples.fridgeserver;
25 import android.app.Activity;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.os.Bundle;
31 import android.os.Message;
32 import android.support.v4.content.LocalBroadcastManager;
33 import android.text.method.ScrollingMovementMethod;
34 import android.util.Log;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 import android.widget.LinearLayout;
38 import android.widget.TextView;
40 import org.iotivity.base.ModeType;
41 import org.iotivity.base.OcPlatform;
42 import org.iotivity.base.PlatformConfig;
43 import org.iotivity.base.QualityOfService;
44 import org.iotivity.base.ServiceType;
46 import base.iotivity.org.examples.message.IMessageLogger;
51 * This is the main fridgeServer class. This instantiates Refrigerator object
52 * which has different resources (DeviceResource, LightResource, DoorResource).
54 public class FridgeServer extends Activity implements IMessageLogger {
55 private Context mContext;
56 private static String TAG = "FridgeServer: ";
57 private TextView mEventsTextView;
58 private MessageReceiver mMessageReceiver = new MessageReceiver();
59 private Refrigerator refrigerator;
62 * configure OIC platform and call findResource
64 private void initOICStack() {
65 //create platform config
66 PlatformConfig cfg = new PlatformConfig(
70 "0.0.0.0", // bind to all available interfaces
72 QualityOfService.LOW);
73 OcPlatform.Configure(cfg);
74 logMessage(TAG + "Creating refrigerator resources");
76 refrigerator = new Refrigerator(mContext);
80 protected void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82 setContentView(R.layout.activity_fridge_server);
83 registerReceiver(mMessageReceiver, new IntentFilter(StringConstants.INTENT));
85 mEventsTextView = new TextView(this);
86 mEventsTextView.setMovementMethod(new ScrollingMovementMethod());
87 LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
88 layout.addView(mEventsTextView, new LinearLayout.LayoutParams
89 (LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f));
95 public class MessageReceiver extends BroadcastReceiver {
97 public void onReceive(Context context, Intent intent) {
98 final String message = intent.getStringExtra(StringConstants.MESSAGE);
104 public void logMessage(final String text) {
105 if (StringConstants.ENABLE_PRINTING) {
106 runOnUiThread(new Runnable() {
108 final Message msg = new Message();
110 mEventsTextView.append("\n");
111 mEventsTextView.append(text);
119 public boolean onCreateOptionsMenu(Menu menu) {
120 // Inflate the menu; this adds items to the action bar if it is present.
121 getMenuInflater().inflate(R.menu.menu_fridge_server, menu);
126 public boolean onOptionsItemSelected(MenuItem item) {
127 int id = item.getItemId();
128 if (id == R.id.action_settings) {
131 return super.onOptionsItemSelected(item);
135 public void onDestroy() {
141 protected void onStop() {
142 LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);