Clean up some SonarQube warnings (trailing whitespace, etc).
[platform/upstream/iotivity.git] / android / examples / fridgeserver / src / main / java / org / iotivity / base / examples / fridgeserver / FridgeServer.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.base.examples.fridgeserver;
24
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;
39
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;
45
46 import base.iotivity.org.examples.message.IMessageLogger;
47
48 /**
49  * FridgeServer
50  * <p/>
51  * This is the main fridgeServer class. This instantiates Refrigerator object
52  * which has different resources (DeviceResource, LightResource, DoorResource).
53  */
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;
60
61     /**
62      * configure OIC platform and call findResource
63      */
64     private void initOICStack() {
65         //create platform config
66         PlatformConfig cfg = new PlatformConfig(
67                 this,
68                 ServiceType.IN_PROC,
69                 ModeType.SERVER,
70                 "0.0.0.0", // bind to all available interfaces
71                 0,
72                 QualityOfService.LOW);
73         OcPlatform.Configure(cfg);
74         logMessage(TAG + "Creating refrigerator resources");
75
76         refrigerator = new Refrigerator(mContext);
77     }
78
79     @Override
80     protected void onCreate(Bundle savedInstanceState) {
81         super.onCreate(savedInstanceState);
82         setContentView(R.layout.activity_fridge_server);
83         registerReceiver(mMessageReceiver, new IntentFilter(StringConstants.INTENT));
84
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));
90         mContext = this;
91
92         initOICStack();
93     }
94
95     public class MessageReceiver extends BroadcastReceiver {
96         @Override
97         public void onReceive(Context context, Intent intent) {
98             final String message = intent.getStringExtra(StringConstants.MESSAGE);
99             logMessage(message);
100         }
101     }
102
103     @Override
104     public void logMessage(final String text) {
105         if (StringConstants.ENABLE_PRINTING) {
106             runOnUiThread(new Runnable() {
107                 public void run() {
108                     final Message msg = new Message();
109                     msg.obj = text;
110                     mEventsTextView.append("\n");
111                     mEventsTextView.append(text);
112                 }
113             });
114             Log.i(TAG, text);
115         }
116     }
117
118     @Override
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);
122         return true;
123     }
124
125     @Override
126     public boolean onOptionsItemSelected(MenuItem item) {
127         int id = item.getItemId();
128         if (id == R.id.action_settings) {
129             return true;
130         }
131         return super.onOptionsItemSelected(item);
132     }
133
134     @Override
135     public void onDestroy() {
136         super.onDestroy();
137         onStop();
138     }
139
140     @Override
141     protected void onStop() {
142         LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
143         super.onStop();
144     }
145 }