1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 package com.example.con_server;
22 import org.iotivity.base.ModeType;
23 import org.iotivity.base.OcPlatform;
24 import org.iotivity.base.PlatformConfig;
25 import org.iotivity.base.QualityOfService;
26 import org.iotivity.base.ServiceType;
28 import android.app.Activity;
29 import android.app.AlertDialog;
30 import android.app.ProgressDialog;
31 import android.content.DialogInterface;
32 import android.net.ConnectivityManager;
33 import android.net.NetworkInfo;
34 import android.os.Bundle;
35 import android.os.Handler;
36 import android.os.Message;
37 import android.util.Log;
38 import android.view.View;
39 import android.view.View.OnClickListener;
40 import android.widget.Button;
41 import android.widget.EditText;
44 * Starting Activity of the application responsible for
45 * configuring the OcPlatform and for handling user's selection on UI.
47 public class MainActivity extends Activity {
49 private final String LOG_TAG = "[CON-SERVER]" + this.getClass().getSimpleName();
50 private Handler mHandler;
51 private static MainActivity mainActivityObj;
52 private ConfigurationServer conServerObj;
53 private static String message;
54 private EditText editText;
55 public static ProgressDialog dialog;
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setContentView(R.layout.activity_main);
62 mainActivityObj = this;
63 Button doBootStrap = (Button) findViewById(R.id.button1);
64 final Button createConfig = (Button) findViewById(R.id.button2);
65 editText = (EditText) findViewById(R.id.EditText);
66 conServerObj = new ConfigurationServer();
68 // handler for updating the UI i.e. MessageLog (TextBox)
69 mHandler = new Handler() {
71 public void handleMessage(Message msg) {
74 editText.setText(message);
75 Log.i(LOG_TAG, message);
79 setmHandler(mHandler);
81 // listener for doBootStrap Button
82 doBootStrap.setOnClickListener(new OnClickListener() {
84 public void onClick(View v) {
85 conServerObj.DoBootStrap();
86 createConfig.setEnabled(true);
90 // listener for createConfiguration Resource Button
91 createConfig.setOnClickListener(new OnClickListener() {
93 public void onClick(View v) {
94 conServerObj.CreateConfigurationResource();
95 createConfig.setEnabled(false);
99 // calling the method to check the Wi-fi connectivity and configuring
104 private void configurePlatform() {
106 // Check the wi-fi connectivity
107 ConnectivityManager connmanager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
108 NetworkInfo wifi = connmanager
109 .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
110 if (false == wifi.isConnected()) {
111 // WiFi is not connected close the application
112 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
113 dialogBuilder.setTitle("Error");
115 .setMessage("WiFi is not enabled/connected! Please connect the WiFi and start application again...");
116 dialogBuilder.setCancelable(false);
117 dialogBuilder.setPositiveButton("OK",
118 new DialogInterface.OnClickListener() {
120 public void onClick(DialogInterface dialog, int which) {
121 // Closing the application
122 mainActivityObj.finish();
126 AlertDialog dialog = dialogBuilder.create();
128 Log.i(LOG_TAG, "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
132 // If wifi is connected calling the configure method for configuring the
134 PlatformConfig cfg = new PlatformConfig(getApplicationContext(),ServiceType.IN_PROC,
135 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
136 OcPlatform.Configure(cfg);
137 Log.i(LOG_TAG, "Configuration done Successfully");
141 public void onBackPressed() {
142 super.onBackPressed();
144 // deleting all the resources that we have created.
145 if (null != conServerObj)
146 conServerObj.deleteResources();
149 @SuppressWarnings("unused")
150 private Handler handler = new Handler() {
152 public void handleMessage(Message msg) {
157 // Function called when receive a reboot Request
158 public static void reboot() throws InterruptedException {
160 dialog = new ProgressDialog(mainActivityObj);
161 dialog.setMessage("Rebooting..");
162 dialog.setTitle("Please wait ...");
163 dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
164 dialog.setProgress(0);
167 Log.i(LOG_TAG, "Rebooting.. Please wait ...");
168 Thread thread = new Thread() {
175 } catch (InterruptedException e) {
183 // Functions required for updating the UI
185 public Handler getmHandler() {
189 public void setmHandler(Handler mHandler) {
190 this.mHandler = mHandler;
193 public static MainActivity getMainActivityObject() {
194 return mainActivityObj;
197 public static void setmessage(String msg) {