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 = 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);
78 setmHandler(mHandler);
80 // listener for doBootStrap Button
81 doBootStrap.setOnClickListener(new OnClickListener() {
83 public void onClick(View v) {
84 conServerObj.DoBootStrap();
85 createConfig.setEnabled(true);
89 // listener for createConfiguration Resource Button
90 createConfig.setOnClickListener(new OnClickListener() {
92 public void onClick(View v) {
93 conServerObj.CreateConfigurationResource();
94 createConfig.setEnabled(false);
98 // calling the method to check the Wi-fi connectivity and configuring
103 private void configurePlatform() {
105 // Check the wi-fi connectivity
106 ConnectivityManager connmanager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
107 NetworkInfo wifi = connmanager
108 .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
109 if (false == wifi.isConnected()) {
110 // WiFi is not connected close the application
111 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
112 dialogBuilder.setTitle("Error");
114 .setMessage("WiFi is not enabled/connected! Please connect the WiFi and start application again...");
115 dialogBuilder.setCancelable(false);
116 dialogBuilder.setPositiveButton("OK",
117 new DialogInterface.OnClickListener() {
119 public void onClick(DialogInterface dialog, int which) {
120 // Closing the application
121 mainActivityObj.finish();
125 AlertDialog dialog = dialogBuilder.create();
130 // If wifi is connected calling the configure method for configuring the
132 PlatformConfig cfg = new PlatformConfig(getApplicationContext(),ServiceType.IN_PROC,
133 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
134 OcPlatform.Configure(cfg);
135 Log.i(LOG_TAG, "Configuration done Successfully");
139 public void onBackPressed() {
140 super.onBackPressed();
142 // deleting all the resources that we have created.
143 if (null != conServerObj)
144 conServerObj.deleteResources();
147 @SuppressWarnings("unused")
148 private Handler handler = new Handler() {
150 public void handleMessage(Message msg) {
155 // Function called when receive a reboot Request
156 public static void reboot() throws InterruptedException {
158 dialog = new ProgressDialog(mainActivityObj);
159 dialog.setMessage("Rebooting..");
160 dialog.setTitle("Please wait ...");
161 dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
162 dialog.setProgress(0);
165 Thread thread = new Thread() {
172 } catch (InterruptedException e) {
180 // Functions required for updating the UI
182 public Handler getmHandler() {
186 public void setmHandler(Handler mHandler) {
187 this.mHandler = mHandler;
190 public static MainActivity getMainActivityObject() {
191 return mainActivityObj;
194 public static void setmessage(String msg) {