Imported Upstream version 0.9.1
[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 = 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                 }
76             }
77         };
78         setmHandler(mHandler);
79
80         // listener for doBootStrap Button
81         doBootStrap.setOnClickListener(new OnClickListener() {
82             @Override
83             public void onClick(View v) {
84                 conServerObj.DoBootStrap();
85                 createConfig.setEnabled(true);
86             }
87         });
88
89         // listener for createConfiguration Resource Button
90         createConfig.setOnClickListener(new OnClickListener() {
91             @Override
92             public void onClick(View v) {
93                 conServerObj.CreateConfigurationResource();
94                 createConfig.setEnabled(false);
95             }
96         });
97
98         // calling the method to check the Wi-fi connectivity and configuring
99         // the OcPlatform
100         configurePlatform();
101     }
102
103     private void configurePlatform() {
104
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");
113             dialogBuilder
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() {
118                         @Override
119                         public void onClick(DialogInterface dialog, int which) {
120                             // Closing the application
121                             mainActivityObj.finish();
122                         }
123                     });
124
125             AlertDialog dialog = dialogBuilder.create();
126             dialog.show();
127             return;
128         }
129
130         // If wifi is connected calling the configure method for configuring the
131         // ocPlatform
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");
136     }
137
138     @Override
139     public void onBackPressed() {
140         super.onBackPressed();
141
142         // deleting all the resources that we have created.
143         if (null != conServerObj)
144             conServerObj.deleteResources();
145     }
146
147     @SuppressWarnings("unused")
148     private Handler handler = new Handler() {
149                                 @Override
150                                 public void handleMessage(Message msg) {
151                                     dialog.dismiss();
152                                 }
153                             };
154
155     // Function called when receive a reboot Request
156     public static void reboot() throws InterruptedException {
157
158         dialog = new ProgressDialog(mainActivityObj);
159         dialog.setMessage("Rebooting..");
160         dialog.setTitle("Please wait ...");
161         dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
162         dialog.setProgress(0);
163         dialog.setMax(100);
164         dialog.show();
165         Thread thread = new Thread() {
166             @Override
167             public void run() {
168                 try {
169                     sleep(5000);
170                     dialog.dismiss();
171
172                 } catch (InterruptedException e) {
173                     e.printStackTrace();
174                 }
175             }
176         };
177         thread.start();
178     }
179
180     // Functions required for updating the UI
181
182     public Handler getmHandler() {
183         return mHandler;
184     }
185
186     public void setmHandler(Handler mHandler) {
187         this.mHandler = mHandler;
188     }
189
190     public static MainActivity getMainActivityObject() {
191         return mainActivityObj;
192     }
193
194     public static void setmessage(String msg) {
195         message = msg;
196     }
197 }