• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 #ifndef LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_H_
18 #define LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_H_
19 
20 #include <jni.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "utils/base/statusor.h"
28 #include "utils/flatbuffers/flatbuffers.h"
29 #include "utils/flatbuffers/mutable.h"
30 #include "utils/intents/remote-action-template.h"
31 #include "utils/java/jni-base.h"
32 #include "utils/java/jni-cache.h"
33 #include "utils/optional.h"
34 #include "utils/variant.h"
35 
36 #ifndef TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME
37 #define TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME RemoteActionTemplate
38 #endif
39 
40 #define TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME_STR \
41   TC3_ADD_QUOTES(TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME)
42 
43 #ifndef TC3_NAMED_VARIANT_CLASS_NAME
44 #define TC3_NAMED_VARIANT_CLASS_NAME NamedVariant
45 #endif
46 
47 #define TC3_NAMED_VARIANT_CLASS_NAME_STR \
48   TC3_ADD_QUOTES(TC3_NAMED_VARIANT_CLASS_NAME)
49 
50 namespace libtextclassifier3 {
51 
52 // A helper class to create RemoteActionTemplate object from model results.
53 class RemoteActionTemplatesHandler {
54  public:
55   static StatusOr<std::unique_ptr<RemoteActionTemplatesHandler>> Create(
56       const std::shared_ptr<JniCache>& jni_cache);
57 
58   StatusOr<ScopedLocalRef<jstring>> AsUTF8String(
59       const Optional<std::string>& optional) const;
60   StatusOr<ScopedLocalRef<jobject>> AsInteger(
61       const Optional<int>& optional) const;
62   StatusOr<ScopedLocalRef<jobjectArray>> AsStringArray(
63       const std::vector<std::string>& values) const;
64   StatusOr<ScopedLocalRef<jfloatArray>> AsFloatArray(
65       const std::vector<float>& values) const;
66   StatusOr<ScopedLocalRef<jintArray>> AsIntArray(
67       const std::vector<int>& values) const;
68   StatusOr<ScopedLocalRef<jobject>> AsNamedVariant(const std::string& name,
69                                                    const Variant& value) const;
70   StatusOr<ScopedLocalRef<jobjectArray>> AsNamedVariantArray(
71       const std::map<std::string, Variant>& values) const;
72 
73   StatusOr<ScopedLocalRef<jobjectArray>> RemoteActionTemplatesToJObjectArray(
74       const std::vector<RemoteActionTemplate>& remote_actions) const;
75 
76   StatusOr<ScopedLocalRef<jobjectArray>> EntityDataAsNamedVariantArray(
77       const reflection::Schema* entity_data_schema,
78       const std::string& serialized_entity_data) const;
79 
80  private:
RemoteActionTemplatesHandler(const std::shared_ptr<JniCache> & jni_cache)81   explicit RemoteActionTemplatesHandler(
82       const std::shared_ptr<JniCache>& jni_cache)
83       : jni_cache_(jni_cache),
84         integer_class_(nullptr, jni_cache->jvm),
85         remote_action_template_class_(nullptr, jni_cache->jvm),
86         named_variant_class_(nullptr, jni_cache->jvm) {}
87 
88   std::shared_ptr<JniCache> jni_cache_;
89 
90   // java.lang.Integer
91   ScopedGlobalRef<jclass> integer_class_;
92   jmethodID integer_init_ = nullptr;
93 
94   // RemoteActionTemplate
95   ScopedGlobalRef<jclass> remote_action_template_class_;
96   jmethodID remote_action_template_init_ = nullptr;
97 
98   // NamedVariant
99   ScopedGlobalRef<jclass> named_variant_class_;
100   jmethodID named_variant_from_int_ = nullptr;
101   jmethodID named_variant_from_long_ = nullptr;
102   jmethodID named_variant_from_float_ = nullptr;
103   jmethodID named_variant_from_double_ = nullptr;
104   jmethodID named_variant_from_bool_ = nullptr;
105   jmethodID named_variant_from_string_ = nullptr;
106   jmethodID named_variant_from_string_array_ = nullptr;
107   jmethodID named_variant_from_float_array_ = nullptr;
108   jmethodID named_variant_from_int_array_ = nullptr;
109   jmethodID named_variant_from_named_variant_array_ = nullptr;
110 };
111 
112 }  // namespace libtextclassifier3
113 
114 #endif  // LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_H_
115