• 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 #include <map>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "utils/flatbuffers.h"
27 #include "utils/intents/intent-generator.h"
28 #include "utils/java/jni-base.h"
29 #include "utils/java/jni-cache.h"
30 #include "utils/optional.h"
31 #include "utils/variant.h"
32 
33 #ifndef TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME
34 #define TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME RemoteActionTemplate
35 #endif
36 
37 #define TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME_STR \
38   TC3_ADD_QUOTES(TC3_REMOTE_ACTION_TEMPLATE_CLASS_NAME)
39 
40 #ifndef TC3_NAMED_VARIANT_CLASS_NAME
41 #define TC3_NAMED_VARIANT_CLASS_NAME NamedVariant
42 #endif
43 
44 #define TC3_NAMED_VARIANT_CLASS_NAME_STR \
45   TC3_ADD_QUOTES(TC3_NAMED_VARIANT_CLASS_NAME)
46 
47 namespace libtextclassifier3 {
48 
49 // A helper class to create RemoteActionTemplate object from model results.
50 class RemoteActionTemplatesHandler {
51  public:
52   static std::unique_ptr<RemoteActionTemplatesHandler> Create(
53       const std::shared_ptr<JniCache>& jni_cache);
54 
55   jstring AsUTF8String(const Optional<std::string>& optional) const;
56   jobject AsInteger(const Optional<int>& optional) const;
57   jobjectArray AsStringArray(const std::vector<std::string>& values) const;
58   jobject AsNamedVariant(const std::string& name, const Variant& value) const;
59   jobjectArray AsNamedVariantArray(
60       const std::map<std::string, Variant>& values) const;
61 
62   jobjectArray RemoteActionTemplatesToJObjectArray(
63       const std::vector<RemoteActionTemplate>& remote_actions) const;
64 
65   jobject EntityDataAsNamedVariantArray(
66       const reflection::Schema* entity_data_schema,
67       const std::string& serialized_entity_data) const;
68 
69  private:
RemoteActionTemplatesHandler(const std::shared_ptr<JniCache> & jni_cache)70   explicit RemoteActionTemplatesHandler(
71       const std::shared_ptr<JniCache>& jni_cache)
72       : jni_cache_(jni_cache),
73         integer_class_(nullptr, jni_cache->jvm),
74         remote_action_template_class_(nullptr, jni_cache->jvm),
75         named_variant_class_(nullptr, jni_cache->jvm) {}
76 
77   std::shared_ptr<JniCache> jni_cache_;
78 
79   // java.lang.Integer
80   ScopedGlobalRef<jclass> integer_class_;
81   jmethodID integer_init_ = nullptr;
82 
83   // RemoteActionTemplate
84   ScopedGlobalRef<jclass> remote_action_template_class_;
85   jmethodID remote_action_template_init_ = nullptr;
86 
87   // NamedVariant
88   ScopedGlobalRef<jclass> named_variant_class_;
89   jmethodID named_variant_from_int_ = nullptr;
90   jmethodID named_variant_from_long_ = nullptr;
91   jmethodID named_variant_from_float_ = nullptr;
92   jmethodID named_variant_from_double_ = nullptr;
93   jmethodID named_variant_from_bool_ = nullptr;
94   jmethodID named_variant_from_string_ = nullptr;
95 };
96 
97 }  // namespace libtextclassifier3
98 
99 #endif  // LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_H_
100