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