[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / examples-java / 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.text.method.ScrollingMovementMethod;
33 import android.util.Log;
34 import android.view.View;
35 import android.widget.Button;
36 import android.widget.ScrollView;
37 import android.widget.TextView;
38
39 import org.iotivity.base.ModeType;
40 import org.iotivity.base.OcPlatform;
41 import org.iotivity.base.PlatformConfig;
42 import org.iotivity.base.QualityOfService;
43 import org.iotivity.base.ServiceType;
44
45 /**
46  * FridgeServer
47  * <p/>
48  * This is the main fridgeServer class. This instantiates Refrigerator object
49  * which has different resources (DeviceResource, LightResource, DoorResources).
50  */
51 public class FridgeServer extends Activity {
52     private Refrigerator refrigerator;
53
54     /**
55      * configure OIC platform and call findResource
56      */
57     private void startFridgeServer() {
58         //create platform config
59         PlatformConfig cfg = new PlatformConfig(
60                 this, //context
61                 ServiceType.IN_PROC,
62                 ModeType.SERVER,
63                 "0.0.0.0", // bind to all available interfaces
64                 0,
65                 QualityOfService.LOW);
66         logMessage("Configuring platform");
67         OcPlatform.Configure(cfg);
68         logMessage("Creating refrigerator resources");
69         refrigerator = new Refrigerator(this);
70         logMessage("-----------------------------------------------------");
71     }
72
73     //******************************************************************************
74     // End of the OIC specific code
75     //******************************************************************************
76     private static String TAG = "FridgeServer: ";
77     public static final String MESSAGE = "message";
78     public static final String INTENT = "org.iotivity.base.examples.fridgeserver";
79     private TextView mConsoleTextView;
80     private ScrollView mScrollView;
81     private MessageReceiver mMessageReceiver = new MessageReceiver();
82
83     @Override
84     protected void onCreate(Bundle savedInstanceState) {
85         super.onCreate(savedInstanceState);
86         setContentView(R.layout.activity_fridge_server);
87         registerReceiver(mMessageReceiver, new IntentFilter(INTENT));
88
89         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
90         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
91         mScrollView = (ScrollView) findViewById(R.id.scrollView);
92         mScrollView.fullScroll(View.FOCUS_DOWN);
93         final Button button = (Button) findViewById(R.id.button);
94
95         if (null == savedInstanceState) {
96             button.setOnClickListener(new View.OnClickListener() {
97                 @Override
98                 public void onClick(View v) {
99                     button.setEnabled(false);
100                     new Thread(new Runnable() {
101                         public void run() {
102                             startFridgeServer();
103                         }
104                     }).start();
105                 }
106             });
107         } else {
108             String consoleOutput = savedInstanceState.getString("consoleOutputString");
109             mConsoleTextView.setText(consoleOutput);
110         }
111     }
112
113     public class MessageReceiver extends BroadcastReceiver {
114         @Override
115         public void onReceive(Context context, Intent intent) {
116             final String message = intent.getStringExtra(MESSAGE);
117             logMessage(message);
118         }
119     }
120
121     public void logMessage(final String text) {
122         runOnUiThread(new Runnable() {
123             public void run() {
124                 final Message msg = new Message();
125                 msg.obj = text;
126                 mConsoleTextView.append("\n");
127                 mConsoleTextView.append(text);
128             }
129         });
130         Log.i(TAG, text);
131     }
132 }