[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / examples-java / presenceserver / src / main / java / org / iotivity / base / examples / PresenceServer.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 package org.iotivity.base.examples;
23
24 import android.app.Activity;
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.text.method.ScrollingMovementMethod;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.CompoundButton;
31 import android.widget.ScrollView;
32 import android.widget.TextView;
33 import android.widget.ToggleButton;
34
35 import org.iotivity.base.ModeType;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcPlatform;
38 import org.iotivity.base.OcResourceHandle;
39 import org.iotivity.base.PlatformConfig;
40 import org.iotivity.base.QualityOfService;
41 import org.iotivity.base.ResourceProperty;
42 import org.iotivity.base.ServiceType;
43
44 import java.util.EnumSet;
45
46 /**
47  * A server example for presence notification
48  */
49 public class PresenceServer extends Activity {
50     private OcResourceHandle mResourceHandle;
51
52     private void startPresenceServer() {
53         Context context = this;
54
55         PlatformConfig platformConfig = new PlatformConfig(
56                 context,
57                 ServiceType.IN_PROC,
58                 ModeType.SERVER,
59                 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
60                 0,         // Uses randomly available port
61                 QualityOfService.LOW
62         );
63
64         msg("Configuring platform.");
65         OcPlatform.Configure(platformConfig);
66
67         try {
68             msg("Creating resource of type \"core.light\".");
69             createResource();
70             sleep(1);
71
72             msg("Starting presence notifications.");
73             OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
74             sleep(1);
75         } catch (OcException e) {
76             Log.e(TAG, e.toString());
77             msg("Error: " + e.toString());
78         }
79         printLine();
80         enableStartStopButton();
81     }
82
83     /**
84      * This function internally calls registerResource API.
85      */
86     private void createResource() {
87         String resourceUri = "/a/light"; // URI of the resource
88         String resourceTypeName = "core.light"; // resource type name.
89         String resourceInterface = OcPlatform.DEFAULT_INTERFACE; // resource interface.
90
91         try {
92             // This will internally create and register the resource.
93             mResourceHandle = OcPlatform.registerResource(
94                     resourceUri,
95                     resourceTypeName,
96                     resourceInterface,
97                     null, //Use default entity handler
98                     EnumSet.of(ResourceProperty.DISCOVERABLE));
99         } catch (OcException e) {
100             msg("Resource creation was unsuccessful.");
101             Log.e(TAG, e.toString());
102         }
103     }
104
105     private void stopPresenceServer() {
106         try {
107             msg("Stopping presence notifications.");
108             OcPlatform.stopPresence();
109
110             msg("Unregister resource.");
111             if (null != mResourceHandle) OcPlatform.unregisterResource(mResourceHandle);
112         } catch (OcException e) {
113             Log.e(TAG, e.toString());
114             msg("Error: " + e.toString());
115         }
116
117         printLine();
118         enableStartStopButton();
119     }
120
121     //******************************************************************************
122     // End of the OIC specific code
123     //******************************************************************************
124
125     private final static String TAG = PresenceServer.class.getSimpleName();
126     private TextView mConsoleTextView;
127     private ScrollView mScrollView;
128
129     @Override
130     protected void onCreate(Bundle savedInstanceState) {
131         super.onCreate(savedInstanceState);
132         setContentView(R.layout.activity_presence_server);
133
134         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
135         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
136         mScrollView = (ScrollView) findViewById(R.id.scrollView);
137         mScrollView.fullScroll(View.FOCUS_DOWN);
138         final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
139
140         if (null == savedInstanceState) {
141             toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
142                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
143                     toggleButton.setEnabled(false);
144                     if (isChecked) {
145                         new Thread(new Runnable() {
146                             public void run() {
147                                 startPresenceServer();
148                             }
149                         }).start();
150                     } else {
151                         new Thread(new Runnable() {
152                             public void run() {
153                                 stopPresenceServer();
154                             }
155                         }).start();
156                     }
157                 }
158             });
159         } else {
160             String consoleOutput = savedInstanceState.getString("consoleOutputString");
161             mConsoleTextView.setText(consoleOutput);
162             boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
163             toggleButton.setChecked(buttonCheked);
164         }
165     }
166
167     @Override
168     protected void onSaveInstanceState(Bundle outState) {
169         super.onSaveInstanceState(outState);
170         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
171         ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
172         outState.putBoolean("toggleButtonChecked", toggleButton.isChecked());
173     }
174
175     @Override
176     protected void onRestoreInstanceState(Bundle savedInstanceState) {
177         super.onRestoreInstanceState(savedInstanceState);
178
179         String consoleOutput = savedInstanceState.getString("consoleOutputString");
180         mConsoleTextView.setText(consoleOutput);
181
182         final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
183         boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
184         toggleButton.setChecked(buttonCheked);
185     }
186
187     private void msg(final String text) {
188         runOnUiThread(new Runnable() {
189             public void run() {
190                 mConsoleTextView.append("\n");
191                 mConsoleTextView.append(text);
192                 mScrollView.fullScroll(View.FOCUS_DOWN);
193             }
194         });
195         Log.i(TAG, text);
196     }
197
198     private void printLine() {
199         msg("------------------------------------------------------------------------");
200     }
201
202     private void sleep(int seconds) {
203         try {
204             Thread.sleep(seconds * 1000);
205         } catch (InterruptedException e) {
206             e.printStackTrace();
207             Log.e(TAG, e.toString());
208         }
209     }
210
211     private void enableStartStopButton() {
212         runOnUiThread(new Runnable() {
213             public void run() {
214                 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
215                 toggleButton.setEnabled(true);
216             }
217         });
218     }
219 }