replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / android / notification-service / src / main / java / org / iotivity / service / ns / consumer / Provider.java
1 //******************************************************************
2 //
3 // Copyright 2016 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.ns.consumer;
22
23 import android.util.Log;
24 import org.iotivity.service.ns.common.*;
25 import java.util.Vector;
26
27 /**
28  *
29  * This class provides implementation of Notification Provider object.
30  *
31  */
32 public class Provider {
33
34     private static final String LOG_TAG = "ConsumerService_Provider";
35
36     /**
37      * Enum for defining different state of provider object
38      */
39     public enum ProviderState {
40         ALLOW(1),
41         DENY(2),
42         TOPIC(3),
43         STOPPED(12);
44
45         private int state;
46
47         private ProviderState(int state) {
48             this.state = state;
49         }
50
51         public int getProviderState() {
52             return this.state;
53         }
54     };
55
56     private String mProviderId   = null;
57     private long  mNativeHandle = 0;
58
59     /**
60      * Constructor of Provider.
61      *
62      * @param providerId
63      *            unique Id of Provider
64      */
65     public Provider(String providerId) {
66         Log.i(LOG_TAG, "Provider()");
67
68         mProviderId = providerId;
69     }
70
71     @Override
72     protected void finalize() throws Throwable {
73         try {
74             nativeDispose();
75         } catch (Throwable t) {
76             throw t;
77         } finally {
78             super.finalize();
79         }
80     }
81     /**
82      * API for getting providerId
83      *
84      * @return ConsumerId as string
85      */
86     public String getProviderId() {
87         return mProviderId;
88     }
89
90     /**
91      * API for getting for getting Topic List
92      *
93      * @return TopicsList
94      */
95     public TopicsList getTopicList() throws NSException {
96         return nativeGetTopicList();
97     }
98
99     /**
100      * API for getting for getting ProviderState
101      *
102      * @return ProviderState
103      */
104     public ProviderState getProviderState() throws NSException {
105         return nativeGetProviderState();
106     }
107
108     /**
109      * API for requesting subscription of Notification service
110      * This API should be called with a valid Provider object obtained from Discovery callback.
111      * The API should not be called when the Provider is in STOPPED state.
112      *
113      * Discovery APIs to discover Providers are as below.
114      * Start/rescanProvider for D2D,
115      * enableRemoteService for D2S,
116      *
117      * @throws NSException failure to subscribe
118      */
119     public void subscribe() throws NSException {
120         nativeSubscribe();
121     }
122
123     /**
124      * API for requesting unsubscription of Notification service
125      *
126      * This API should be called with a valid Provider object obtained from Discovery callback.
127      * The API should not be called when the Provider is in STOPPED state.
128      *
129      * @throws NSException failure to subscribe
130      */
131     public void unsubscribe() throws NSException {
132         nativeUnsubscribe();
133     }
134
135     /**
136      * API for requesting subscription status from Provider of Notification
137      * service
138      */
139     public boolean isSubscribed() throws NSException {
140         return nativeIsSubscribed();
141     }
142
143     /**
144      * This method is for Sending SyncInfo of Notification service.
145      *
146      * @param messageId
147      *            unique Id of message
148      * @param syncType
149      *            SyncType of Notification service
150      */
151     public void sendSyncInfo(long messageId, SyncInfo.SyncType syncType)
152             throws NSException {
153         nativeSendSyncInfo(messageId, syncType.ordinal());
154     }
155
156     /**
157      * This method is for registering for listeners of Notification .
158      *
159      * @param onProviderStateListener
160      *            OnProviderStateListener callback Interface
161      * @param onMessageReceivedListner
162      *            OnMessageReceivedListner callback Interface
163      * @param onSyncInfoReceivedListner
164      *            OnSyncInfoReceivedListner callback Interface
165      */
166     public void setListener(OnProviderStateListener onProviderStateListener,
167             OnMessageReceivedListener onMessageReceivedListener,
168             OnSyncInfoReceivedListener onSyncInfoReceivedListener)
169             throws NSException {
170         nativeSetListener(onProviderStateListener, onMessageReceivedListener,
171                 onSyncInfoReceivedListener);
172     }
173
174     /**
175      * Update Topic list that is wanted to be subscribed from provider
176      *
177      * @param topicsList
178      *            TopicsList of interested Topics
179      *
180      * @throws NSException failure to update topic list. 
181      */
182     public void updateTopicList(TopicsList topicsList) throws NSException {
183         nativeUpdateTopicList(topicsList);
184     }
185
186     /**
187      * Interface to implement callback function to receive provider state
188      * information
189      */
190     public interface OnProviderStateListener {
191
192         /**
193          * Callback function to receive provider state information
194          *
195          * @param state
196          *            ProviderState
197          */
198         public void onProviderStateReceived(ProviderState state);
199     }
200
201     /**
202      * Interface to implement callback function to receive Notification Message
203      */
204     public interface OnMessageReceivedListener {
205
206         /**
207          * Callback function to receive Notification Message.
208          *
209          * @param message
210          *            Notification Message
211          */
212         public void onMessageReceived(Message message);
213     }
214
215     /**
216      * Interface to implement callback function to receive message read
217      * synchronization
218      */
219     public interface OnSyncInfoReceivedListener {
220
221         /**
222          * Callback function to receive message read synchronization
223          *
224          * @param sync
225          *            SyncInfo
226          */
227         public void onSyncInfoReceived(SyncInfo sync);
228     }
229
230     private native void nativeSubscribe() throws NSException;
231     private native void nativeUnsubscribe() throws NSException;
232
233     private native void nativeSendSyncInfo(long messageId, int syncType)
234             throws NSException;
235
236     private native void nativeSetListener(
237             OnProviderStateListener onProviderStateListener,
238             OnMessageReceivedListener onMessageReceivedListener,
239             OnSyncInfoReceivedListener onSyncInfoReceivedListener)
240             throws NSException;
241
242     private native TopicsList nativeGetTopicList() throws NSException;
243
244     private native void nativeUpdateTopicList(TopicsList topicsList)
245             throws NSException;
246
247     private native ProviderState nativeGetProviderState() throws NSException;
248
249     private native boolean nativeIsSubscribed() throws NSException;
250
251     private native void nativeDispose();
252
253 }