[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / iotivity-linux / src / main / java / org / iotivity / ca / CaInterface.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2015 Intel Corporation.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.ca;
24
25 import org.iotivity.base.OcException;
26 import org.iotivity.base.OcConnectivityType;
27
28 class BluetoothDevice {}
29
30 public class CaInterface {
31     static {
32     }
33     private static volatile boolean isConnectionManagerInitialized = false;
34     private static volatile boolean isBtPairingInitialized = false;
35
36     public static void initialize()  {}
37
38     /**
39      *  Method start connection manager service.
40      *  this method has to be called before other API call.
41      *  @param context                                application context
42      *  @param onConnectionManagerStateListener       connection state callback listener
43      */
44     public synchronized static void startManagerService(
45             OnConnectionManagerStateListener onConnectionManagerStateListener) {
46         if (!isConnectionManagerInitialized) {
47             CaInterface.caManagerInitialize(onConnectionManagerStateListener);
48             isConnectionManagerInitialized = true;
49         }
50     }
51
52     /**
53      *  Method stop connection manager service.
54      *  this method must be called, when Application is destroied.
55      */
56     public synchronized static void stopManagerService() {
57         if (isConnectionManagerInitialized) {
58             CaInterface.caManagerTerminate();
59             isConnectionManagerInitialized = false;
60         }
61     }
62
63     /**
64      *  Method set device information for Auto-Connection.
65      *  this method has to be called before FindResource is called.
66      *  @param address                      LE address of scanned bluetooth device.
67      */
68     public synchronized static void setAutoConnectionDevice(String address)
69             throws OcException {
70         CaInterface.initCheckForConnectionManager();
71         CaInterface.caManagerSetAutoConnectionDeviceInfo(address);
72     }
73
74     /**
75      *  Method unset device information for Auto-Connection.
76      *  @param address                      LE address of scanned bluetooth device.
77      */
78     public synchronized static void unsetAutoConnectionDevice(String address)
79             throws OcException {
80         CaInterface.initCheckForConnectionManager();
81         CaInterface.caManagerUnsetAutoConnectionDeviceInfo(address);
82     }
83
84     /**
85      *  Interface for connection manager state listener.
86      *  Event listeners are notified asynchronously.
87      */
88     public interface OnConnectionManagerStateListener {
89         public void onAdapterStateChanged(OcConnectivityType type, boolean enabled);
90         public void onConnectionStateChanged(OcConnectivityType type, String address,
91                 boolean connected);
92     }
93
94     private static void initCheckForConnectionManager() {
95         if (!isConnectionManagerInitialized) {
96             throw new IllegalStateException("ConnectionManager must be started by making "
97                     + "a call to CaInterface.startManagerService before any other API "
98                     + "calls are permitted");
99         }
100     }
101
102     private static void caManagerInitialize(
103             OnConnectionManagerStateListener onConnectionManagerStateListener) {}
104     private static void caManagerTerminate() {}
105     private static void caManagerSetAutoConnectionDeviceInfo(String address) {}
106     private static void caManagerUnsetAutoConnectionDeviceInfo(String address) {}
107
108     /**
109      *  start bluetooth pairing service.
110      *  @param context                      application context
111      */
112     public synchronized static void startBtPairingService(
113             OnBtDeviceFoundListener listener) {
114         if (!isBtPairingInitialized) {
115             /* TODO */
116             CaInterface.caBtPairingInitialize(listener);
117
118             isBtPairingInitialized = true;
119         }
120     }
121
122     /**
123      *  stop bluetooth pairing service.
124      */
125     public synchronized static void stopBtPairingService() {
126         if (isBtPairingInitialized) {
127             CaInterface.caBtPairingTerminate();
128
129             isBtPairingInitialized = false;
130         }
131     }
132
133     /**
134      *  start bluetooth device scan.
135      */
136     public synchronized static void startScan()
137             throws OcException {
138         CaInterface.initCheckForBtPairingUtil();
139         CaInterface.caBtPairingStartScan();
140     }
141
142     /**
143      *  stop bluetooth device scan.
144      */
145     public synchronized static void stopScan()
146             throws OcException {
147         CaInterface.initCheckForBtPairingUtil();
148         CaInterface.caBtPairingStopScan();
149     }
150
151     /**
152      *  create bond
153      */
154     public synchronized static void createBond(BluetoothDevice device)
155             throws OcException {
156         CaInterface.initCheckForBtPairingUtil();
157         CaInterface.caBtPairingCreateBond(device);
158     }
159
160     public interface OnBtDeviceFoundListener {
161         public void onBtDeviceFound(BluetoothDevice device) throws OcException;
162     }
163
164     private static void initCheckForBtPairingUtil() {
165         if (!isBtPairingInitialized) {
166             throw new IllegalStateException("BT pairing Util must be started by making "
167                     + "a call to CaInterface.startBtPairingService before any other API "
168                     + "calls are permitted");
169         }
170     }
171
172     private static void caBtPairingInitialize(
173             OnBtDeviceFoundListener listener) {}
174     private static void caBtPairingTerminate() {}
175     private static void caBtPairingStartScan() {}
176     private static void caBtPairingStopScan() {}
177     private static void caBtPairingCreateBond(BluetoothDevice device) {}
178 }