added CA interface to monitoring network status for android ble.
[platform/upstream/iotivity.git] / android / android_api / base / 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 android.content.Context;
26 import android.app.Activity;
27 import org.iotivity.base.OcException;
28 import org.iotivity.base.OcConnectivityType;
29
30 public class CaInterface {
31     static {
32         System.loadLibrary("connectivity_abstraction");
33         System.loadLibrary("ca-interface");
34     }
35     private static volatile boolean isConnectionManagerInitialized = false;
36
37     public static native void initialize(Activity activity, Context context);
38
39     /**
40      *  Method start connection manager service.
41      *  this method has to be called before other API call.
42      *  @param context                                application context
43      *  @param onConnectionManagerStateListener       connection state callback listener
44      */
45     public synchronized static void startManagerService(Context context,
46             OnConnectionManagerStateListener onConnectionManagerStateListener) {
47         if (!isConnectionManagerInitialized) {
48             CaInterface.caManagerInitialize(context, onConnectionManagerStateListener);
49             isConnectionManagerInitialized = true;
50         }
51     }
52
53     /**
54      *  Method stop connection manager service.
55      *  this method must be called, when Application is destroied.
56      */
57     public synchronized static void stopManagerService() {
58         if (isConnectionManagerInitialized) {
59             CaInterface.caManagerTerminate();
60             isConnectionManagerInitialized = false;
61         }
62     }
63
64     /**
65      *  Method set device information for Auto-Connection.
66      *  this method has to be called before FindResource is called.
67      *  @param address                      LE address of scanned bluetooth device.
68      */
69     public synchronized static void setAutoConnectionDevice(String address)
70             throws OcException {
71         CaInterface.initCheckForConnectionManager();
72         CaInterface.caManagerSetAutoConnectionDeviceInfo(address);
73     }
74
75     /**
76      *  Method unset device information for Auto-Connection.
77      *  @param address                      LE address of scanned bluetooth device.
78      */
79     public synchronized static void unsetAutoConnectionDevice(String address)
80             throws OcException {
81         CaInterface.initCheckForConnectionManager();
82         CaInterface.caManagerUnsetAutoConnectionDeviceInfo(address);
83     }
84
85     /**
86      *  Interface for connection manager state listener.
87      *  Event listeners are notified asynchronously.
88      */
89     public interface OnConnectionManagerStateListener {
90         public void onAdapterStateChanged(OcConnectivityType type, boolean enabled);
91         public void onConnectionStateChanged(OcConnectivityType type, String address,
92                 boolean connected);
93     }
94
95     private static void initCheckForConnectionManager() {
96         if (!isConnectionManagerInitialized) {
97             throw new IllegalStateException("ConnectionManager must be started by making "
98                     + "a call to CaInterface.startManagerService before any other API "
99                     + "calls are permitted");
100         }
101     }
102
103     private static native void caManagerInitialize(Context context,
104             OnConnectionManagerStateListener onConnectionManagerStateListener);
105     private static native void caManagerTerminate();
106     private static native void caManagerSetAutoConnectionDeviceInfo(String address);
107     private static native void caManagerUnsetAutoConnectionDeviceInfo(String address);
108 }