[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / examples-android / fridgegroupserver / src / main / java / org / iotivity / base / examples / FridgeGroupServer.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;
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  * FridgeGroupServer
47  * <p/>
48  * This is the main fridgeGroupServer class. This instantiates Refrigerator object
49  * which has different resources such as LightResource, DoorResource, etc.
50  */
51 public class FridgeGroupServer extends Activity {
52     private Refrigerator refrigerator;
53
54     /**
55      * configure OIC platform and call findResource
56      */
57     private void startFridgeServer() {
58         logMessage("Configuring  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         OcPlatform.Configure(cfg);
67
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     private TextView mConsoleTextView;
78     private ScrollView mScrollView;
79     private BroadcastReceiver mMessageReceiver = new MessageReceiver();
80
81     @Override
82     protected void onCreate(Bundle savedInstanceState) {
83         super.onCreate(savedInstanceState);
84         setContentView(R.layout.activity_fridge_server);
85         registerReceiver(mMessageReceiver, new IntentFilter(Resource.INTENT));
86
87         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
88         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
89         mScrollView = (ScrollView) findViewById(R.id.scrollView);
90         mScrollView.fullScroll(View.FOCUS_DOWN);
91         final Button button = (Button) findViewById(R.id.button);
92
93         if (null == savedInstanceState) {
94             button.setOnClickListener(new View.OnClickListener() {
95                 @Override
96                 public void onClick(View v) {
97                     button.setEnabled(false);
98                     new Thread(new Runnable() {
99                         public void run() {
100                             startFridgeServer();
101                         }
102                     }).start();
103                 }
104             });
105         } else {
106             String consoleOutput = savedInstanceState.getString("consoleOutputString");
107             mConsoleTextView.setText(consoleOutput);
108         }
109     }
110
111     @Override
112     protected void onSaveInstanceState(Bundle outState) {
113         super.onSaveInstanceState(outState);
114         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
115     }
116
117     @Override
118     protected void onRestoreInstanceState(Bundle savedInstanceState) {
119         super.onRestoreInstanceState(savedInstanceState);
120         String consoleOutput = savedInstanceState.getString("consoleOutputString");
121         mConsoleTextView.setText(consoleOutput);
122     }
123
124     public class MessageReceiver extends BroadcastReceiver {
125         @Override
126         public void onReceive(Context context, Intent intent) {
127             final String message = intent.getStringExtra(Resource.MESSAGE);
128             logMessage(message);
129         }
130     }
131
132     private void logMessage(final String text) {
133         runOnUiThread(new Runnable() {
134             public void run() {
135                 final Message msg = new Message();
136                 msg.obj = text;
137                 mConsoleTextView.append("\n");
138                 mConsoleTextView.append(text);
139             }
140         });
141         Log.i(TAG, text);
142     }
143 }