| Samuel Tan | 0a31202 | 2015-11-23 18:22:12 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 ANDROID_PERSISTABLE_BUNDLE_H | 
|  | 18 | #define ANDROID_PERSISTABLE_BUNDLE_H | 
|  | 19 |  | 
|  | 20 | #include <map> | 
| Samuel Tan | 52545f2 | 2016-02-12 13:56:17 -0800 | [diff] [blame] | 21 | #include <set> | 
| Samuel Tan | 0a31202 | 2015-11-23 18:22:12 -0800 | [diff] [blame] | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <binder/Parcelable.h> | 
|  | 25 | #include <utils/String16.h> | 
|  | 26 | #include <utils/StrongPointer.h> | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | namespace os { | 
|  | 31 |  | 
|  | 32 | /* | 
|  | 33 | * C++ implementation of PersistableBundle, a mapping from String values to | 
|  | 34 | * various types that can be saved to persistent and later restored. | 
|  | 35 | */ | 
|  | 36 | class PersistableBundle : public Parcelable { | 
|  | 37 | public: | 
|  | 38 | PersistableBundle() = default; | 
|  | 39 | virtual ~PersistableBundle() = default; | 
|  | 40 | PersistableBundle(const PersistableBundle& bundle) = default; | 
|  | 41 |  | 
|  | 42 | status_t writeToParcel(Parcel* parcel) const override; | 
|  | 43 | status_t readFromParcel(const Parcel* parcel) override; | 
|  | 44 |  | 
|  | 45 | bool empty() const; | 
|  | 46 | size_t size() const; | 
|  | 47 | size_t erase(const String16& key); | 
|  | 48 |  | 
|  | 49 | /* | 
|  | 50 | * Setters for PersistableBundle. Adds a a key-value pair instantiated with | 
|  | 51 | * |key| and |value| into the member map appropriate for the type of |value|. | 
|  | 52 | * If there is already an existing value for |key|, |value| will replace it. | 
|  | 53 | */ | 
|  | 54 | void putBoolean(const String16& key, bool value); | 
|  | 55 | void putInt(const String16& key, int32_t value); | 
|  | 56 | void putLong(const String16& key, int64_t value); | 
|  | 57 | void putDouble(const String16& key, double value); | 
|  | 58 | void putString(const String16& key, const String16& value); | 
|  | 59 | void putBooleanVector(const String16& key, const std::vector<bool>& value); | 
|  | 60 | void putIntVector(const String16& key, const std::vector<int32_t>& value); | 
|  | 61 | void putLongVector(const String16& key, const std::vector<int64_t>& value); | 
|  | 62 | void putDoubleVector(const String16& key, const std::vector<double>& value); | 
|  | 63 | void putStringVector(const String16& key, const std::vector<String16>& value); | 
|  | 64 | void putPersistableBundle(const String16& key, const PersistableBundle& value); | 
|  | 65 |  | 
|  | 66 | /* | 
|  | 67 | * Getters for PersistableBundle. If |key| exists, these methods write the | 
|  | 68 | * value associated with |key| into |out|, and return true. Otherwise, these | 
|  | 69 | * methods return false. | 
|  | 70 | */ | 
|  | 71 | bool getBoolean(const String16& key, bool* out) const; | 
|  | 72 | bool getInt(const String16& key, int32_t* out) const; | 
|  | 73 | bool getLong(const String16& key, int64_t* out) const; | 
|  | 74 | bool getDouble(const String16& key, double* out) const; | 
|  | 75 | bool getString(const String16& key, String16* out) const; | 
|  | 76 | bool getBooleanVector(const String16& key, std::vector<bool>* out) const; | 
|  | 77 | bool getIntVector(const String16& key, std::vector<int32_t>* out) const; | 
|  | 78 | bool getLongVector(const String16& key, std::vector<int64_t>* out) const; | 
|  | 79 | bool getDoubleVector(const String16& key, std::vector<double>* out) const; | 
|  | 80 | bool getStringVector(const String16& key, std::vector<String16>* out) const; | 
|  | 81 | bool getPersistableBundle(const String16& key, PersistableBundle* out) const; | 
|  | 82 |  | 
| Samuel Tan | 52545f2 | 2016-02-12 13:56:17 -0800 | [diff] [blame] | 83 | /* Getters for all keys for each value type */ | 
|  | 84 | std::set<String16> getBooleanKeys() const; | 
|  | 85 | std::set<String16> getIntKeys() const; | 
|  | 86 | std::set<String16> getLongKeys() const; | 
|  | 87 | std::set<String16> getDoubleKeys() const; | 
|  | 88 | std::set<String16> getStringKeys() const; | 
|  | 89 | std::set<String16> getBooleanVectorKeys() const; | 
|  | 90 | std::set<String16> getIntVectorKeys() const; | 
|  | 91 | std::set<String16> getLongVectorKeys() const; | 
|  | 92 | std::set<String16> getDoubleVectorKeys() const; | 
|  | 93 | std::set<String16> getStringVectorKeys() const; | 
|  | 94 | std::set<String16> getPersistableBundleKeys() const; | 
|  | 95 |  | 
| Samuel Tan | 0a31202 | 2015-11-23 18:22:12 -0800 | [diff] [blame] | 96 | friend bool operator==(const PersistableBundle& lhs, const PersistableBundle& rhs) { | 
|  | 97 | return (lhs.mBoolMap == rhs.mBoolMap && lhs.mIntMap == rhs.mIntMap && | 
|  | 98 | lhs.mLongMap == rhs.mLongMap && lhs.mDoubleMap == rhs.mDoubleMap && | 
|  | 99 | lhs.mStringMap == rhs.mStringMap && lhs.mBoolVectorMap == rhs.mBoolVectorMap && | 
|  | 100 | lhs.mIntVectorMap == rhs.mIntVectorMap && | 
|  | 101 | lhs.mLongVectorMap == rhs.mLongVectorMap && | 
|  | 102 | lhs.mDoubleVectorMap == rhs.mDoubleVectorMap && | 
|  | 103 | lhs.mStringVectorMap == rhs.mStringVectorMap && | 
|  | 104 | lhs.mPersistableBundleMap == rhs.mPersistableBundleMap); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | friend bool operator!=(const PersistableBundle& lhs, const PersistableBundle& rhs) { | 
|  | 108 | return !(lhs == rhs); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | private: | 
|  | 112 | status_t writeToParcelInner(Parcel* parcel) const; | 
|  | 113 | status_t readFromParcelInner(const Parcel* parcel, size_t length); | 
|  | 114 |  | 
|  | 115 | std::map<String16, bool> mBoolMap; | 
|  | 116 | std::map<String16, int32_t> mIntMap; | 
|  | 117 | std::map<String16, int64_t> mLongMap; | 
|  | 118 | std::map<String16, double> mDoubleMap; | 
|  | 119 | std::map<String16, String16> mStringMap; | 
|  | 120 | std::map<String16, std::vector<bool>> mBoolVectorMap; | 
|  | 121 | std::map<String16, std::vector<int32_t>> mIntVectorMap; | 
|  | 122 | std::map<String16, std::vector<int64_t>> mLongVectorMap; | 
|  | 123 | std::map<String16, std::vector<double>> mDoubleVectorMap; | 
|  | 124 | std::map<String16, std::vector<String16>> mStringVectorMap; | 
|  | 125 | std::map<String16, PersistableBundle> mPersistableBundleMap; | 
|  | 126 | }; | 
|  | 127 |  | 
|  | 128 | }  // namespace os | 
|  | 129 |  | 
|  | 130 | }  // namespace android | 
|  | 131 |  | 
|  | 132 | #endif  // ANDROID_PERSISTABLE_BUNDLE_H |