1 2 #ifndef LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_ 3 #define LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_ 4 5 #include <jni.h> 6 #include <map> 7 #include <memory> 8 #include <string> 9 #include <vector> 10 11 #include "actions/types.h" 12 #include "annotator/types.h" 13 #include "utils/i18n/locale.h" 14 #include "utils/intents/intent-config_generated.h" 15 #include "utils/java/jni-cache.h" 16 #include "utils/java/scoped_local_ref.h" 17 #include "utils/optional.h" 18 #include "utils/resources.h" 19 #include "utils/resources_generated.h" 20 #include "utils/strings/stringpiece.h" 21 22 namespace libtextclassifier3 { 23 24 // A template with parameters for an Android remote action. 25 struct RemoteActionTemplate { 26 // Title shown for the action (see: RemoteAction.getTitle). 27 Optional<std::string> title_without_entity; 28 29 // Title with entity for the action. It is not guaranteed that the client 30 // will use this, so title should be always given and general enough. 31 Optional<std::string> title_with_entity; 32 33 // Description shown for the action (see: RemoteAction.getContentDescription). 34 Optional<std::string> description; 35 36 // Description shown for the action (see: RemoteAction.getContentDescription) 37 // when app name is available. Caller is expected to replace the placeholder 38 // by the name of the app that is going to handle the action. 39 Optional<std::string> description_with_app_name; 40 41 // The action to set on the Intent (see: Intent.setAction). 42 Optional<std::string> action; 43 44 // The data to set on the Intent (see: Intent.setData). 45 Optional<std::string> data; 46 47 // The type to set on the Intent (see: Intent.setType). 48 Optional<std::string> type; 49 50 // Flags for launching the Intent (see: Intent.setFlags). 51 Optional<int> flags; 52 53 // Categories to set on the Intent (see: Intent.addCategory). 54 std::vector<std::string> category; 55 56 // Explicit application package to set on the Intent (see: Intent.setPackage). 57 Optional<std::string> package_name; 58 59 // The list of all the extras to add to the Intent. 60 std::map<std::string, Variant> extra; 61 62 // Private request code ot use for the Intent. 63 Optional<int> request_code; 64 }; 65 66 // Helper class to generate Android intents for text classifier results. 67 class IntentGenerator { 68 public: 69 static std::unique_ptr<IntentGenerator> Create( 70 const IntentFactoryModel* options, const ResourcePool* resources, 71 const std::shared_ptr<JniCache>& jni_cache); 72 73 // Generates intents for a classification result. 74 // Returns true, if the intent generator snippets could be successfully run, 75 // returns false otherwise. 76 bool GenerateIntents(const jstring device_locales, 77 const ClassificationResult& classification, 78 const int64 reference_time_ms_utc, 79 const std::string& text, 80 const CodepointSpan selection_indices, 81 const jobject context, 82 const reflection::Schema* annotations_entity_data_schema, 83 std::vector<RemoteActionTemplate>* remote_actions) const; 84 85 // Generates intents for an action suggestion. 86 // Returns true, if the intent generator snippets could be successfully run, 87 // returns false otherwise. 88 bool GenerateIntents(const jstring device_locales, 89 const ActionSuggestion& action, 90 const Conversation& conversation, const jobject context, 91 const reflection::Schema* annotations_entity_data_schema, 92 const reflection::Schema* actions_entity_data_schema, 93 std::vector<RemoteActionTemplate>* remote_actions) const; 94 95 private: IntentGenerator(const IntentFactoryModel * options,const ResourcePool * resources,const std::shared_ptr<JniCache> & jni_cache)96 IntentGenerator(const IntentFactoryModel* options, 97 const ResourcePool* resources, 98 const std::shared_ptr<JniCache>& jni_cache) 99 : options_(options), 100 resources_(Resources(resources)), 101 jni_cache_(jni_cache) {} 102 103 std::vector<Locale> ParseDeviceLocales(const jstring device_locales) const; 104 105 const IntentFactoryModel* options_; 106 const Resources resources_; 107 std::shared_ptr<JniCache> jni_cache_; 108 std::map<std::string, std::string> generators_; 109 }; 110 111 } // namespace libtextclassifier3 112 113 #endif // LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_ 114