Fixed Java prevent issue
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / android / EasySetup / app / src / main / java / org / iotivity / service / easysetup / BLEActivity.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
21 package org.iotivity.service.easysetup;
22
23 import java.io.IOException;
24
25 import org.iotivity.service.easysetup.core.BleConnection;
26 import org.iotivity.service.easysetup.core.EasySetupService;
27 import org.iotivity.service.easysetup.core.EasySetupStatus;
28 import org.iotivity.service.easysetup.core.EnrolleeDevice;
29 import org.iotivity.service.easysetup.core.EnrolleeState;
30 import org.iotivity.service.easysetup.impl.BLEOnBoardingConfig;
31 import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory;
32 import org.iotivity.service.easysetup.impl.WiFiProvConfig;
33
34 import android.app.Activity;
35 import android.bluetooth.BluetoothAdapter;
36 import android.bluetooth.BluetoothManager;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.os.Bundle;
40 import android.os.Handler;
41 import android.os.Message;
42 import android.util.Log;
43 import android.view.View;
44 import android.view.View.OnClickListener;
45 import android.widget.Button;
46 import android.widget.EditText;
47 import android.widget.ProgressBar;
48 import android.widget.TextView;
49 import android.widget.Toast;
50
51 public class BLEActivity extends Activity {
52
53
54     /*Status to update the UI */
55     public static final int SUCCESS = 0;
56     public static final int FAILED = 1;
57     public static final int STATE_CHANGED = 2;
58
59     EditText mEnrolleeSsidText;
60     EditText mmEnrolleePasswordPassText;
61
62
63     TextView mDeviceNameTextView;
64     TextView mDeviceMacTextView;
65     TextView mDeviceUuidTextView;
66
67     TextView mResultTextView;
68     ProgressBar mProgressbar;
69     Button mStartButton;
70     Button mStopButton;
71     Handler mHandler = new ThreadHandler();
72
73     /**
74      * Objects to be instantiated by the programmer
75      */
76     WiFiProvConfig mWiFiProvConfig;
77     BLEOnBoardingConfig mBleOnBoardingConfig;
78     EasySetupService mEasySetupService;
79     EnrolleeDeviceFactory mDeviceFactory;
80     EnrolleeDevice mDevice;
81
82     private final int REQUEST_ENABLE_BT = 1;
83
84     @Override
85     protected void onCreate(Bundle savedInstanceState) {
86         super.onCreate(savedInstanceState);
87         setContentView(R.layout.activity_ble);
88
89         /* Initialize widgets to get user input for target network's SSID & password*/
90         mEnrolleeSsidText = (EditText) findViewById(R.id.enrolleeSsid);
91         mmEnrolleePasswordPassText = (EditText) findViewById(R.id.enrolleePass);
92         mDeviceNameTextView = (TextView) findViewById(R.id.devicename);
93         mDeviceMacTextView = (TextView) findViewById(R.id.hardAddr);
94         mDeviceUuidTextView = (TextView) findViewById(R.id.uuid);
95         mResultTextView = (TextView) findViewById(R.id.status);
96         mProgressbar = (ProgressBar) findViewById(R.id.progressBar);
97         mStartButton = (Button) findViewById(R.id.startSetup);
98         mStopButton = (Button) findViewById(R.id.stopSetup);
99
100         //default SSID and password
101         mEnrolleeSsidText.setText("hub2.4G");
102         mmEnrolleePasswordPassText.setText("11112222");
103
104        /* Create Easy Setup Service instance*/
105         mEasySetupService = EasySetupService.getInstance(getApplicationContext(),
106                 new EasySetupStatus() {
107
108                     @Override
109                     public void onFinished(final EnrolleeDevice enrolledevice) {
110                         Log.i("BleActivity", "onFinished() is received " + enrolledevice
111                                 .isSetupSuccessful());
112                         if (enrolledevice.isSetupSuccessful()) {
113                             mHandler.sendEmptyMessage(SUCCESS);
114                         } else {
115                             mHandler.sendEmptyMessage(FAILED);
116                         }
117                     }
118
119                     @Override
120                     public void onProgress(EnrolleeState state) {
121                         Log.i("MainActivity", "onProgress() is received ");
122                         mHandler.sendEmptyMessage(STATE_CHANGED);
123                     }
124
125                 });
126
127         /* Create EnrolleeDevice Factory instance*/
128         mDeviceFactory = EnrolleeDeviceFactory.newInstance(getApplicationContext());
129
130         addListnerforStartES();
131         addListenerForStopES();
132
133
134     }
135
136     public void addListnerforStartES() {
137
138         mStartButton.setOnClickListener(new OnClickListener() {
139             @Override
140             public void onClick(View v) {
141                 mProgressbar.setVisibility(View.VISIBLE);
142                 mProgressbar.setIndeterminate(true);
143                 mStartButton.setEnabled(false);
144                 mResultTextView.setText(R.string.running);
145                 mStopButton.setEnabled(true);
146                 start();
147             }
148         });
149     }
150
151     public void addListenerForStopES() {
152         mStopButton = (Button) findViewById(R.id.stopSetup);
153
154         mStopButton.setOnClickListener(new OnClickListener() {
155             @Override
156             public void onClick(View arg0) {
157                 mStartButton.setEnabled(true);
158                 mStopButton.setEnabled(false);
159                 mResultTextView.setText(R.string.stopped);
160                 mProgressbar.setIndeterminate(false);
161                 mProgressbar.setVisibility(View.INVISIBLE);
162                 mEasySetupService.stopSetup(mDevice);
163             }
164         });
165     }
166
167
168     public WiFiProvConfig getEnrollerWifiConfig() {
169         /* Provide the credentials for the Mediator Soft AP to be connected by Enrollee*/
170         mWiFiProvConfig = new WiFiProvConfig(mEnrolleeSsidText.getText().toString(),
171                 mmEnrolleePasswordPassText.getText().toString());
172         return mWiFiProvConfig;
173     }
174
175     public BLEOnBoardingConfig getOnBoardingWifiConfig() {
176         // Set the uuid of the OIC device here
177         mBleOnBoardingConfig = new BLEOnBoardingConfig();
178         /*
179         set the uuid of the OIC-device, so that the Easysetup API can find the device
180          */
181         mBleOnBoardingConfig.setUuid("ade3d529-c784-4f63-a987-eb69f70ee816");
182
183         return mBleOnBoardingConfig;
184     }
185
186
187     public void onDestroy() {
188         super.onDestroy();
189         /*Reset the Easy setup process*/
190         if (mEasySetupService != null) {
191             mEasySetupService.finish();
192         }
193     }
194
195     public void start() {
196         //This function starts the bluetooth adpater so that the easysetup can start scanning for BLE devices
197         final BluetoothManager bluetoothManager =
198                 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
199         BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
200
201         if (!mBluetoothAdapter.isEnabled()) {
202             //Bluetooth is disabled, enable it
203             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
204             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
205         } else try {
206             //IF bluetooth is directly enabled it will directly start the setup of enrollee devices
207
208                     /* Create a device using Factory instance*/
209             mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(),
210                     getEnrollerWifiConfig());
211             mEasySetupService.startSetup(mDevice);
212         } catch (IOException e) {
213             e.printStackTrace();
214         }
215     }
216
217     @Override
218     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
219         // User chose not to enable Bluetooth.
220         if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
221             Log.e("error bluetooth", "Bluetooth not enabled..Try again");
222             Toast.makeText(BLEActivity.this, "Bluetooth not enabled..Try again", Toast.LENGTH_SHORT).show();
223             return;
224         } else try {
225             //bluetooth is  enabled, now start the setup of enrollee devices
226                     /* Create a device using Factory instance*/
227             mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(),
228                     getEnrollerWifiConfig());
229             mEasySetupService.startSetup(mDevice);
230         } catch (IOException e) {
231             e.printStackTrace();
232         }
233         super.onActivityResult(requestCode, resultCode, data);
234     }
235
236
237     class ThreadHandler extends Handler {
238         @Override
239         public void handleMessage(Message msg) {
240
241             switch (msg.what) {
242                 case SUCCESS: {
243
244                     mProgressbar.setIndeterminate(false);
245                     mStopButton.setEnabled(false);
246                     mStartButton.setEnabled(true);
247                     mProgressbar.setVisibility(View.INVISIBLE);
248                     String resultMsg = "Device configured successfully";
249                     mResultTextView.setText(R.string.success);
250
251                     /* Update device information on the Ui */
252                     BleConnection connection = (BleConnection) mDevice
253                             .getConnection();
254                     mDeviceNameTextView.setText(connection.getmDeviceName());
255                     mDeviceMacTextView.setText(connection.getMacaddress());
256                     mDeviceUuidTextView.setText(connection.getmServiceUUID());
257                     Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show();
258                     break;
259                 }
260                 case FAILED: {
261
262                     mProgressbar.setIndeterminate(false);
263                     mStopButton.setEnabled(false);
264                     mStartButton.setEnabled(true);
265                     mProgressbar.setVisibility(View.INVISIBLE);
266                     String resultMsg = "Device configuration failed";
267                     mResultTextView.setText(R.string.failed);
268                     Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show();
269                     break;
270                 }
271
272                 case STATE_CHANGED: {
273                     String resultMsg = "Device state changed";
274                     Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show();
275                     break;
276                 }
277
278             }
279
280
281         }
282     }
283
284 }