Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / con-server / src / com / example / con_server / MainActivity.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20 package com.example.con_server;
21
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;
27
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;
42
43 /*
44  * Starting Activity of the application responsible for
45  * configuring the OcPlatform and for handling user's selection on UI.
46  */
47 public class MainActivity extends Activity {
48
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;
56
57     @Override
58     protected void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         setContentView(R.layout.activity_main);
61
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();
67
68         // handler for updating the UI i.e. MessageLog (TextBox)
69         mHandler = new Handler() {
70             @Override
71             public void handleMessage(Message msg) {
72                 switch (msg.what) {
73                     case 0:
74                         editText.setText(message);
75                         Log.i(LOG_TAG, message);
76                 }
77             }
78         };
79         setmHandler(mHandler);
80
81         // listener for doBootStrap Button
82         doBootStrap.setOnClickListener(new OnClickListener() {
83             @Override
84             public void onClick(View v) {
85                 conServerObj.DoBootStrap();
86                 createConfig.setEnabled(true);
87             }
88         });
89
90         // listener for createConfiguration Resource Button
91         createConfig.setOnClickListener(new OnClickListener() {
92             @Override
93             public void onClick(View v) {
94                 conServerObj.CreateConfigurationResource();
95                 createConfig.setEnabled(false);
96             }
97         });
98
99         // calling the method to check the Wi-fi connectivity and configuring
100         // the OcPlatform
101         configurePlatform();
102     }
103
104     private void configurePlatform() {
105
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");
114             dialogBuilder
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() {
119                         @Override
120                         public void onClick(DialogInterface dialog, int which) {
121                             // Closing the application
122                             mainActivityObj.finish();
123                         }
124                     });
125
126             AlertDialog dialog = dialogBuilder.create();
127             dialog.show();
128             Log.i(LOG_TAG, "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
129             return;
130         }
131
132         // If wifi is connected calling the configure method for configuring the
133         // ocPlatform
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");
138     }
139
140     @Override
141     public void onBackPressed() {
142         super.onBackPressed();
143
144         // deleting all the resources that we have created.
145         if (null != conServerObj)
146             conServerObj.deleteResources();
147     }
148
149     @SuppressWarnings("unused")
150     private Handler handler = new Handler() {
151                                 @Override
152                                 public void handleMessage(Message msg) {
153                                     dialog.dismiss();
154                                 }
155                             };
156
157     // Function called when receive a reboot Request
158     public static void reboot() throws InterruptedException {
159
160         dialog = new ProgressDialog(mainActivityObj);
161         dialog.setMessage("Rebooting..");
162         dialog.setTitle("Please wait ...");
163         dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
164         dialog.setProgress(0);
165         dialog.setMax(100);
166         dialog.show();
167         Log.i(LOG_TAG, "Rebooting.. Please wait ...");
168         Thread thread = new Thread() {
169             @Override
170             public void run() {
171                 try {
172                     sleep(5000);
173                     dialog.dismiss();
174
175                 } catch (InterruptedException e) {
176                     e.printStackTrace();
177                 }
178             }
179         };
180         thread.start();
181     }
182
183     // Functions required for updating the UI
184
185     public Handler getmHandler() {
186         return mHandler;
187     }
188
189     public void setmHandler(Handler mHandler) {
190         this.mHandler = mHandler;
191     }
192
193     public static MainActivity getMainActivityObject() {
194         return mainActivityObj;
195     }
196
197     public static void setmessage(String msg) {
198         message = msg;
199     }
200 }