Simplify the event handling of java implementation
[platform/core/ml/beyond.git] / subprojects / libbeyond-android / src / main / java / com / samsung / android / beyond / discovery / Discovery.java
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.samsung.android.beyond.discovery;
18
19 import android.util.Log;
20 import android.content.Context;
21
22 import com.samsung.android.beyond.EventListener;
23 import com.samsung.android.beyond.EventObject;
24 import com.samsung.android.beyond.NativeInstance;
25 import com.samsung.android.beyond.ConfigType;
26
27 public class Discovery extends NativeInstance {
28     static {
29         initialize();
30     }
31
32     public static class Info {
33         public String name;
34         public String host;
35         public int port;
36         public String uuid;
37     }
38
39     public class DefaultEventObject extends EventObject {
40     }
41
42     private static final String TAG = "BEYOND_ANDROID_DISCOVERY";
43
44     public Discovery(Context context, String[] arguments) {
45         if (context == null || arguments == null || arguments.length == 0) {
46             throw new IllegalArgumentException("There is no argument");
47         }
48
49         registerNativeInstance(create(arguments), (Long instance) -> destroy(instance));
50
51         if (this.configure(ConfigType.CONTEXT_ANDROID, context) != 0) {
52             Log.e(TAG, "Failed to configure the android context");
53         }
54     }
55
56     public int activate() {
57         if (instance == 0) {
58             throw new IllegalStateException();
59         }
60
61         return activate(instance);
62     }
63
64     public int deactivate() {
65         if (instance == 0) {
66             throw new IllegalStateException();
67         }
68
69         return deactivate(instance);
70     }
71
72     public int setItem(String key, byte[] value) {
73         if (instance == 0) {
74             throw new IllegalStateException();
75         }
76
77         return setItem(instance, key, value);
78     }
79
80     public int removeItem(String key) {
81         if (instance == 0) {
82             throw new IllegalStateException();
83         }
84
85         return removeItem(instance, key);
86     }
87
88     public int configure(char type, Object obj) {
89         if (type == ConfigType.CONTEXT_ANDROID) {
90             if (obj.getClass().getName().equals("android.app.Application") == true) {
91                 return this.configure(instance, type, obj);
92             }
93
94             // TODO:
95             // This function should generate a proper exception
96             return -22;
97         }
98
99         return -22;
100     }
101
102     public void setEventListener(EventListener eventListener) {
103         if (setEventListener(instance, eventListener) < 0) {
104             // TODO: throw an exception
105         }
106     }
107
108     private static native void initialize();
109     private native long create(String[] arguments);
110     private static native void destroy(long instance);
111     private native int activate(long instance);
112     private native int deactivate(long instance);
113     private native int setItem(long instance, String key, byte[] value);
114     private native int removeItem(long instance, String key);
115     private native int configure(long instance, char type, Object obj);
116     private native int setEventListener(long instance, EventListener eventListener);
117 }