summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Tan <samueltan@google.com>2016-02-12 13:56:17 -0800
committerSamuel Tan <samueltan@google.com>2016-02-12 14:34:44 -0800
commit52545f2e3c9f942f59b416ba63e1b6068700d783 (patch)
treebab815991559235632fb0818ff496fdfb254bab1
parentdda39f2a2229ea74cc4738daa1c6a1c758284d7d (diff)
downloadnative-52545f2e3c9f942f59b416ba63e1b6068700d783.tar.gz
libbinder: add getters for PersistableBundle keys
Add getters for the keys in each map stored in PersistableBundle. This will allow users of PersistableBundle to iterate over entries of a specific type in PersistableBundle. Also, add "using" statements to PersistableBundle for std containers to reduce verbosity. BUG: 27171235 Change-Id: Idd4b3bf713071ca8d8d023f1f83bdaf3ccbdd46f TEST: libbinder compiles successfully.
-rw-r--r--include/binder/PersistableBundle.h14
-rw-r--r--libs/binder/PersistableBundle.cpp79
2 files changed, 82 insertions, 11 deletions
diff --git a/include/binder/PersistableBundle.h b/include/binder/PersistableBundle.h
index fe5619fc52..322fef9e4f 100644
--- a/include/binder/PersistableBundle.h
+++ b/include/binder/PersistableBundle.h
@@ -18,6 +18,7 @@
#define ANDROID_PERSISTABLE_BUNDLE_H
#include <map>
+#include <set>
#include <vector>
#include <binder/Parcelable.h>
@@ -79,6 +80,19 @@ public:
bool getStringVector(const String16& key, std::vector<String16>* out) const;
bool getPersistableBundle(const String16& key, PersistableBundle* out) const;
+ /* Getters for all keys for each value type */
+ std::set<String16> getBooleanKeys() const;
+ std::set<String16> getIntKeys() const;
+ std::set<String16> getLongKeys() const;
+ std::set<String16> getDoubleKeys() const;
+ std::set<String16> getStringKeys() const;
+ std::set<String16> getBooleanVectorKeys() const;
+ std::set<String16> getIntVectorKeys() const;
+ std::set<String16> getLongVectorKeys() const;
+ std::set<String16> getDoubleVectorKeys() const;
+ std::set<String16> getStringVectorKeys() const;
+ std::set<String16> getPersistableBundleKeys() const;
+
friend bool operator==(const PersistableBundle& lhs, const PersistableBundle& rhs) {
return (lhs.mBoolMap == rhs.mBoolMap && lhs.mIntMap == rhs.mIntMap &&
lhs.mLongMap == rhs.mLongMap && lhs.mDoubleMap == rhs.mDoubleMap &&
diff --git a/libs/binder/PersistableBundle.cpp b/libs/binder/PersistableBundle.cpp
index aef791c37b..a20359f243 100644
--- a/libs/binder/PersistableBundle.cpp
+++ b/libs/binder/PersistableBundle.cpp
@@ -32,6 +32,9 @@ using android::Parcel;
using android::sp;
using android::status_t;
using android::UNEXPECTED_NULL;
+using std::map;
+using std::set;
+using std::vector;
enum {
// Keep in sync with BUNDLE_MAGIC in frameworks/base/core/java/android/os/BaseBundle.java.
@@ -55,12 +58,22 @@ enum {
namespace {
template <typename T>
-bool getValue(const android::String16& key, T* out, const std::map<android::String16, T>& map) {
+bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
const auto& it = map.find(key);
if (it == map.end()) return false;
*out = it->second;
return true;
}
+
+template <typename T>
+set<android::String16> getKeys(const map<android::String16, T>& map) {
+ if (map.empty()) return set<android::String16>();
+ set<android::String16> keys;
+ for (const auto& key_value_pair : map) {
+ keys.emplace(key_value_pair.first);
+ }
+ return keys;
+}
} // namespace
namespace android {
@@ -188,27 +201,27 @@ void PersistableBundle::putString(const String16& key, const String16& value) {
mStringMap[key] = value;
}
-void PersistableBundle::putBooleanVector(const String16& key, const std::vector<bool>& value) {
+void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
erase(key);
mBoolVectorMap[key] = value;
}
-void PersistableBundle::putIntVector(const String16& key, const std::vector<int32_t>& value) {
+void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
erase(key);
mIntVectorMap[key] = value;
}
-void PersistableBundle::putLongVector(const String16& key, const std::vector<int64_t>& value) {
+void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
erase(key);
mLongVectorMap[key] = value;
}
-void PersistableBundle::putDoubleVector(const String16& key, const std::vector<double>& value) {
+void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
erase(key);
mDoubleVectorMap[key] = value;
}
-void PersistableBundle::putStringVector(const String16& key, const std::vector<String16>& value) {
+void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
erase(key);
mStringVectorMap[key] = value;
}
@@ -238,23 +251,23 @@ bool PersistableBundle::getString(const String16& key, String16* out) const {
return getValue(key, out, mStringMap);
}
-bool PersistableBundle::getBooleanVector(const String16& key, std::vector<bool>* out) const {
+bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
return getValue(key, out, mBoolVectorMap);
}
-bool PersistableBundle::getIntVector(const String16& key, std::vector<int32_t>* out) const {
+bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
return getValue(key, out, mIntVectorMap);
}
-bool PersistableBundle::getLongVector(const String16& key, std::vector<int64_t>* out) const {
+bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
return getValue(key, out, mLongVectorMap);
}
-bool PersistableBundle::getDoubleVector(const String16& key, std::vector<double>* out) const {
+bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
return getValue(key, out, mDoubleVectorMap);
}
-bool PersistableBundle::getStringVector(const String16& key, std::vector<String16>* out) const {
+bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
return getValue(key, out, mStringVectorMap);
}
@@ -262,6 +275,50 @@ bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBun
return getValue(key, out, mPersistableBundleMap);
}
+set<String16> PersistableBundle::getBooleanKeys() const {
+ return getKeys(mBoolMap);
+}
+
+set<String16> PersistableBundle::getIntKeys() const {
+ return getKeys(mIntMap);
+}
+
+set<String16> PersistableBundle::getLongKeys() const {
+ return getKeys(mLongMap);
+}
+
+set<String16> PersistableBundle::getDoubleKeys() const {
+ return getKeys(mDoubleMap);
+}
+
+set<String16> PersistableBundle::getStringKeys() const {
+ return getKeys(mStringMap);
+}
+
+set<String16> PersistableBundle::getBooleanVectorKeys() const {
+ return getKeys(mBoolVectorMap);
+}
+
+set<String16> PersistableBundle::getIntVectorKeys() const {
+ return getKeys(mIntVectorMap);
+}
+
+set<String16> PersistableBundle::getLongVectorKeys() const {
+ return getKeys(mLongVectorMap);
+}
+
+set<String16> PersistableBundle::getDoubleVectorKeys() const {
+ return getKeys(mDoubleVectorMap);
+}
+
+set<String16> PersistableBundle::getStringVectorKeys() const {
+ return getKeys(mStringVectorMap);
+}
+
+set<String16> PersistableBundle::getPersistableBundleKeys() const {
+ return getKeys(mPersistableBundleMap);
+}
+
status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
/*
* To keep this implementation in sync with writeArrayMapInternal() in