JavaDoc comments and code formatting
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / core / EasySetupService.java
1 package org.iotivity.service.easysetup.core;
2
3 import android.content.Context;
4 import android.util.Log;
5
6 import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory;
7
8 import java.io.IOException;
9 import java.util.ArrayList;
10
11 /**
12  * This is facade class, a single point of contact for Application.
13  * It contains set of APIs to do easy setup of the enrolling device.
14  * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
15  * PROVISION   - This is a step where the netowork's detail & credentials are given to the enrolling device.
16  */
17 public class EasySetupService {
18
19     private static EasySetupService sInstance;
20
21     private final EasySetupStatus mCallback;
22
23     private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
24
25     private final ProvisioningCallback mProvisioningCallback;
26
27     private static Context mContext;
28
29     public EnrolleeDeviceFactory mDeviceFactory;
30
31
32     private EasySetupService(EasySetupStatus callback) {
33         mCallback = callback;
34         mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
35         mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
36         mContext = null;
37         mDeviceFactory = null;
38     }
39
40     /**
41      * Gives a singleton instance of Easy setup service.
42      *
43      * @param callback Application needs to provide this callback to receive the status of of easy setup process.
44      */
45
46     public synchronized static EasySetupService getInstance(Context context, EasySetupStatus callback) {
47         if (sInstance == null) {
48             sInstance = new EasySetupService(callback);
49             mContext = context;
50         }
51         return sInstance;
52     }
53
54     /**
55      * Starts Easy setup process for the enrolling device.
56      *
57      * @param enrolledevice Device to be enrolled in network
58      * @throws IOException Throws exception in case of any connection error.
59      */
60
61     public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException {
62
63         mEnrolleeDeviceList.add(enrolledevice);
64
65         // Starts the provisioning directly if the device is already on boarded on the network.
66         if (enrolledevice.onBoarded()) {
67             enrolledevice.startProvisioning(mProvisioningCallback);
68             return;
69         }
70
71         enrolledevice.startOnBoarding(new OnBoardingCallback() {
72
73             @Override
74             public void onFinished(ConnectionInterface connection) {
75                 if (connection.isConnected()) {
76                     try {
77                         Thread.sleep(15000);//Sleep for allowing thin device to start the services
78                     } catch (InterruptedException e) {
79                         e.printStackTrace();
80                     }
81
82                     // Start provisioning here
83                     enrolledevice.setConnection(connection);
84                     enrolledevice.startProvisioning(mProvisioningCallback);
85                 } else {
86                     enrolledevice.mState = EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;
87                     mProvisioningCallback.onFinished(enrolledevice);
88                 }
89
90             }
91
92         });
93
94     }
95
96     /**
97      * Stops on-going Easy setup process for enrolling device.
98      *
99      * @param enrolledevice Device to be enrolled in network
100      */
101     public synchronized void stopSetup(EnrolleeDevice enrolledevice) {
102         enrolledevice.stopOnBoardingProcess();
103         mEnrolleeDeviceList.remove(enrolledevice);
104     }
105
106     public synchronized void getEnrolleeDevice(OnBoardingConfig connType) {
107         mDeviceFactory = EnrolleeDeviceFactory.newInstance(mContext);
108     }
109
110     class ProvisioningCallbackImpl extends ProvisioningCallback {
111
112         private final EasySetupStatus mCallback;
113
114         ProvisioningCallbackImpl(EasySetupStatus callback) {
115             mCallback = callback;
116         }
117
118         @Override
119         public void onFinished(EnrolleeDevice enrolledevice) {
120             //if(mEnrolleeDeviceList.contains(enrolledevice)) {
121             mCallback.onFinished(enrolledevice);
122             //}
123         }
124
125     }
126
127
128 }