1 /* 2 * Copyright (c) 2021, 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 PACKAGES_SCRIPTEXECUTOR_SRC_BUNDLEWRAPPER_H_ 18 #define PACKAGES_SCRIPTEXECUTOR_SRC_BUNDLEWRAPPER_H_ 19 20 #include "jni.h" 21 22 #include <android-base/result.h> 23 24 #include <string> 25 #include <vector> 26 27 namespace com { 28 namespace android { 29 namespace car { 30 namespace scriptexecutor { 31 32 // Used to create a java bundle object and populate its fields one at a time. 33 class BundleWrapper { 34 public: 35 explicit BundleWrapper(JNIEnv* env); 36 // BundleWrapper is not copyable. 37 BundleWrapper(const BundleWrapper&) = delete; 38 BundleWrapper& operator=(const BundleWrapper&) = delete; 39 40 virtual ~BundleWrapper(); 41 42 // Family of methods that puts the provided 'value' into the PersistableBundle 43 // under provided 'key'. 44 ::android::base::Result<void> putBoolean(const char* key, bool value); 45 ::android::base::Result<void> putLong(const char* key, int64_t value); 46 ::android::base::Result<void> putDouble(const char* key, double value); 47 ::android::base::Result<void> putString(const char* key, const char* value); 48 ::android::base::Result<void> putBooleanArray(const char* key, 49 const std::vector<unsigned char>& value); 50 ::android::base::Result<void> putDoubleArray(const char* key, const std::vector<double>& value); 51 ::android::base::Result<void> putLongArray(const char* key, const std::vector<int64_t>& value); 52 ::android::base::Result<void> putStringArray(const char* key, 53 const std::vector<std::string>& value); 54 ::android::base::Result<void> putPersistableBundle(const char* key, const BundleWrapper& value); 55 56 jobject getBundle() const; 57 58 private: 59 // The class asks Java to create PersistableBundle object and stores the reference. 60 // When the instance of this class is destroyed the actual Java PersistableBundle object behind 61 // this reference stays on and is managed by Java. 62 jobject mBundle; 63 64 // Reference to java PersistableBundle class cached for performance reasons. 65 jclass mBundleClass; 66 67 // Stores a JNIEnv* pointer. 68 JNIEnv* mJNIEnv; // not owned 69 }; 70 71 } // namespace scriptexecutor 72 } // namespace car 73 } // namespace android 74 } // namespace com 75 76 #endif // PACKAGES_SCRIPTEXECUTOR_SRC_BUNDLEWRAPPER_H_ 77