10a31202370581a91b3425e7b60757268d0f6b539Samuel Tan/*
20a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * Copyright (C) 2015 The Android Open Source Project
30a31202370581a91b3425e7b60757268d0f6b539Samuel Tan *
40a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * Licensed under the Apache License, Version 2.0 (the "License");
50a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * you may not use this file except in compliance with the License.
60a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * You may obtain a copy of the License at
70a31202370581a91b3425e7b60757268d0f6b539Samuel Tan *
80a31202370581a91b3425e7b60757268d0f6b539Samuel Tan *      http://www.apache.org/licenses/LICENSE-2.0
90a31202370581a91b3425e7b60757268d0f6b539Samuel Tan *
100a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * Unless required by applicable law or agreed to in writing, software
110a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * distributed under the License is distributed on an "AS IS" BASIS,
120a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * See the License for the specific language governing permissions and
140a31202370581a91b3425e7b60757268d0f6b539Samuel Tan * limitations under the License.
150a31202370581a91b3425e7b60757268d0f6b539Samuel Tan */
160a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
170a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#define LOG_TAG "PersistableBundle"
180a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
190a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#include <binder/PersistableBundle.h>
200a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
210a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#include <limits>
220a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
230a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#include <binder/IBinder.h>
240a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#include <binder/Parcel.h>
250a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#include <log/log.h>
260a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#include <utils/Errors.h>
270a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
280a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::BAD_TYPE;
290a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::BAD_VALUE;
300a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::NO_ERROR;
310a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::Parcel;
320a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::sp;
330a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::status_t;
340a31202370581a91b3425e7b60757268d0f6b539Samuel Tanusing android::UNEXPECTED_NULL;
350a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
360a31202370581a91b3425e7b60757268d0f6b539Samuel Tanenum {
370a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    // Keep in sync with BUNDLE_MAGIC in frameworks/base/core/java/android/os/BaseBundle.java.
380a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    BUNDLE_MAGIC = 0x4C444E42,
390a31202370581a91b3425e7b60757268d0f6b539Samuel Tan};
400a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
410a31202370581a91b3425e7b60757268d0f6b539Samuel Tanenum {
420a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    // Keep in sync with frameworks/base/core/java/android/os/Parcel.java.
430a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_STRING = 0,
440a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_INTEGER = 1,
450a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_LONG = 6,
460a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_DOUBLE = 8,
470a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_BOOLEAN = 9,
480a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_STRINGARRAY = 14,
490a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_INTARRAY = 18,
500a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_LONGARRAY = 19,
510a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_BOOLEANARRAY = 23,
520a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_PERSISTABLEBUNDLE = 25,
530a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    VAL_DOUBLEARRAY = 28,
540a31202370581a91b3425e7b60757268d0f6b539Samuel Tan};
550a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
560a31202370581a91b3425e7b60757268d0f6b539Samuel Tannamespace {
570a31202370581a91b3425e7b60757268d0f6b539Samuel Tantemplate <typename T>
580a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool getValue(const android::String16& key, T* out, const std::map<android::String16, T>& map) {
590a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    const auto& it = map.find(key);
600a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (it == map.end()) return false;
610a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    *out = it->second;
620a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return true;
630a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
640a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}  // namespace
650a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
660a31202370581a91b3425e7b60757268d0f6b539Samuel Tannamespace android {
670a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
680a31202370581a91b3425e7b60757268d0f6b539Samuel Tannamespace os {
690a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
700a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#define RETURN_IF_FAILED(calledOnce)                                     \
710a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    {                                                                    \
720a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        status_t returnStatus = calledOnce;                              \
730a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        if (returnStatus) {                                              \
740a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
750a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            return returnStatus;                                         \
760a31202370581a91b3425e7b60757268d0f6b539Samuel Tan         }                                                               \
770a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
780a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
790a31202370581a91b3425e7b60757268d0f6b539Samuel Tan#define RETURN_IF_ENTRY_ERASED(map, key)                                 \
800a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    {                                                                    \
810a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        size_t num_erased = map.erase(key);                              \
820a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        if (num_erased) {                                                \
830a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
840a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            return num_erased;                                           \
850a31202370581a91b3425e7b60757268d0f6b539Samuel Tan         }                                                               \
860a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
870a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
880a31202370581a91b3425e7b60757268d0f6b539Samuel Tanstatus_t PersistableBundle::writeToParcel(Parcel* parcel) const {
890a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    /*
900a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * Keep implementation in sync with writeToParcelInner() in
910a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * frameworks/base/core/java/android/os/BaseBundle.java.
920a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     */
930a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
940a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    // Special case for empty bundles.
950a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (empty()) {
960a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(0));
970a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        return NO_ERROR;
980a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
990a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1000a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    size_t length_pos = parcel->dataPosition();
1010a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(parcel->writeInt32(1));  // dummy, will hold length
1020a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC));
1030a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1040a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    size_t start_pos = parcel->dataPosition();
1050a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(writeToParcelInner(parcel));
1060a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    size_t end_pos = parcel->dataPosition();
1070a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1080a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    // Backpatch length. This length value includes the length header.
1090a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    parcel->setDataPosition(length_pos);
1100a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    size_t length = end_pos - start_pos;
1110a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (length > std::numeric_limits<int32_t>::max()) {
112715dec70e0d3b61679ddac3355e567a787b66f69Samuel Tan        ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
1130a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        return BAD_VALUE;
1140a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
1150a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
1160a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    parcel->setDataPosition(end_pos);
1170a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return NO_ERROR;
1180a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1190a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1200a31202370581a91b3425e7b60757268d0f6b539Samuel Tanstatus_t PersistableBundle::readFromParcel(const Parcel* parcel) {
1210a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    /*
1220a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * Keep implementation in sync with readFromParcelInner() in
1230a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * frameworks/base/core/java/android/os/BaseBundle.java.
1240a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     */
1250a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    int32_t length = parcel->readInt32();
1260a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (length < 0) {
1270a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        ALOGE("Bad length in parcel: %d", length);
1280a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        return UNEXPECTED_NULL;
1290a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
1300a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1310a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return readFromParcelInner(parcel, static_cast<size_t>(length));
1320a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1330a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1340a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::empty() const {
1350a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return size() == 0u;
1360a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1370a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1380a31202370581a91b3425e7b60757268d0f6b539Samuel Tansize_t PersistableBundle::size() const {
1390a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return (mBoolMap.size() +
1400a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mIntMap.size() +
1410a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mLongMap.size() +
1420a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mDoubleMap.size() +
1430a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mStringMap.size() +
1440a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mBoolVectorMap.size() +
1450a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mIntVectorMap.size() +
1460a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mLongVectorMap.size() +
1470a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mDoubleVectorMap.size() +
1480a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mStringVectorMap.size() +
1490a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            mPersistableBundleMap.size());
1500a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1510a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1520a31202370581a91b3425e7b60757268d0f6b539Samuel Tansize_t PersistableBundle::erase(const String16& key) {
1530a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mBoolMap, key);
1540a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mIntMap, key);
1550a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mLongMap, key);
1560a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
1570a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mStringMap, key);
1580a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
1590a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
1600a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
1610a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
1620a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
1630a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return mPersistableBundleMap.erase(key);
1640a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1650a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1660a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putBoolean(const String16& key, bool value) {
1670a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1680a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mBoolMap[key] = value;
1690a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1700a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1710a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putInt(const String16& key, int32_t value) {
1720a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1730a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mIntMap[key] = value;
1740a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1750a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1760a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putLong(const String16& key, int64_t value) {
1770a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1780a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mLongMap[key] = value;
1790a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1800a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1810a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putDouble(const String16& key, double value) {
1820a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1830a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mDoubleMap[key] = value;
1840a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1850a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1860a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putString(const String16& key, const String16& value) {
1870a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1880a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mStringMap[key] = value;
1890a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1900a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1910a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putBooleanVector(const String16& key, const std::vector<bool>& value) {
1920a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1930a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mBoolVectorMap[key] = value;
1940a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
1950a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
1960a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putIntVector(const String16& key, const std::vector<int32_t>& value) {
1970a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
1980a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mIntVectorMap[key] = value;
1990a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2000a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2010a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putLongVector(const String16& key, const std::vector<int64_t>& value) {
2020a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
2030a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mLongVectorMap[key] = value;
2040a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2050a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2060a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putDoubleVector(const String16& key, const std::vector<double>& value) {
2070a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
2080a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mDoubleVectorMap[key] = value;
2090a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2100a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2110a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putStringVector(const String16& key, const std::vector<String16>& value) {
2120a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
2130a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mStringVectorMap[key] = value;
2140a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2150a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2160a31202370581a91b3425e7b60757268d0f6b539Samuel Tanvoid PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
2170a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    erase(key);
2180a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    mPersistableBundleMap[key] = value;
2190a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2200a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2210a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getBoolean(const String16& key, bool* out) const {
2220a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mBoolMap);
2230a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2240a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2250a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getInt(const String16& key, int32_t* out) const {
2260a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mIntMap);
2270a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2280a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2290a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getLong(const String16& key, int64_t* out) const {
2300a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mLongMap);
2310a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2320a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2330a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getDouble(const String16& key, double* out) const {
2340a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mDoubleMap);
2350a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2360a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2370a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getString(const String16& key, String16* out) const {
2380a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mStringMap);
2390a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2400a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2410a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getBooleanVector(const String16& key, std::vector<bool>* out) const {
2420a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mBoolVectorMap);
2430a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2440a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2450a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getIntVector(const String16& key, std::vector<int32_t>* out) const {
2460a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mIntVectorMap);
2470a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2480a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2490a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getLongVector(const String16& key, std::vector<int64_t>* out) const {
2500a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mLongVectorMap);
2510a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2520a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2530a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getDoubleVector(const String16& key, std::vector<double>* out) const {
2540a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mDoubleVectorMap);
2550a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2560a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2570a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getStringVector(const String16& key, std::vector<String16>* out) const {
2580a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mStringVectorMap);
2590a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2600a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2610a31202370581a91b3425e7b60757268d0f6b539Samuel Tanbool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
2620a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return getValue(key, out, mPersistableBundleMap);
2630a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
2640a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2650a31202370581a91b3425e7b60757268d0f6b539Samuel Tanstatus_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
2660a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    /*
2670a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * To keep this implementation in sync with writeArrayMapInternal() in
2680a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * frameworks/base/core/java/android/os/Parcel.java, the number of key
2690a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * value pairs must be written into the parcel before writing the key-value
2700a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * pairs themselves.
2710a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     */
2720a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    size_t num_entries = size();
2730a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (num_entries > std::numeric_limits<int32_t>::max()) {
274715dec70e0d3b61679ddac3355e567a787b66f69Samuel Tan        ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
2750a31202370581a91b3425e7b60757268d0f6b539Samuel Tan              num_entries);
2760a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        return BAD_VALUE;
2770a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
2780a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
2790a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
2800a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mBoolMap) {
2810a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
2820a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
2830a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
2840a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
2850a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mIntMap) {
2860a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
2870a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
2880a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
2890a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
2900a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mLongMap) {
2910a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
2920a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
2930a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
2940a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
2950a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mDoubleMap) {
2960a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
2970a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
2980a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
2990a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3000a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mStringMap) {
3010a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3020a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
3030a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
3040a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3050a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mBoolVectorMap) {
3060a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3070a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
3080a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
3090a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3100a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mIntVectorMap) {
3110a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3120a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
3130a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
3140a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3150a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mLongVectorMap) {
3160a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3170a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
3180a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
3190a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3200a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mDoubleVectorMap) {
3210a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3220a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
3230a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
3240a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3250a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mStringVectorMap) {
3260a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3270a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
3280a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
3290a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3300a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (const auto& key_val_pair : mPersistableBundleMap) {
3310a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
3320a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
3330a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
3340a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3350a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return NO_ERROR;
3360a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
3370a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
3380a31202370581a91b3425e7b60757268d0f6b539Samuel Tanstatus_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
3390a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    /*
3400a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * Note: we don't actually use length for anything other than an empty PersistableBundle
3410a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
3420a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * implementation.
3430a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     */
3440a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (length == 0) {
3450a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        // Empty PersistableBundle or end of data.
3460a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        return NO_ERROR;
3470a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3480a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
3490a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    int32_t magic;
3500a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(parcel->readInt32(&magic));
3510a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    if (magic != BUNDLE_MAGIC) {
3520a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
3530a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        return BAD_VALUE;
3540a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
3550a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
3560a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    /*
3570a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * To keep this implementation in sync with unparcel() in
3580a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * frameworks/base/core/java/android/os/BaseBundle.java, the number of
3590a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * key-value pairs must be read from the parcel before reading the key-value
3600a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     * pairs themselves.
3610a31202370581a91b3425e7b60757268d0f6b539Samuel Tan     */
3620a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    int32_t num_entries;
3630a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    RETURN_IF_FAILED(parcel->readInt32(&num_entries));
3640a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
3650a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    for (; num_entries > 0; --num_entries) {
3660a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        size_t start_pos = parcel->dataPosition();
3670a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        String16 key;
3680a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        int32_t value_type;
3690a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->readString16(&key));
3700a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        RETURN_IF_FAILED(parcel->readInt32(&value_type));
3710a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
3720a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        /*
3730a31202370581a91b3425e7b60757268d0f6b539Samuel Tan         * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
3740a31202370581a91b3425e7b60757268d0f6b539Samuel Tan         * are unique.
3750a31202370581a91b3425e7b60757268d0f6b539Samuel Tan         */
3760a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        switch (value_type) {
3770a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_STRING: {
3780a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
3790a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
3800a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
3810a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_INTEGER: {
3820a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
3830a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
3840a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
3850a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_LONG: {
3860a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
3870a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
3880a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
3890a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_DOUBLE: {
3900a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
3910a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
3920a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
3930a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_BOOLEAN: {
3940a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
3950a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
3960a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
3970a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_STRINGARRAY: {
3980a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
3990a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4000a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4010a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_INTARRAY: {
4020a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
4030a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4040a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4050a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_LONGARRAY: {
4060a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
4070a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4080a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4090a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_BOOLEANARRAY: {
4100a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
4110a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4120a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4130a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_PERSISTABLEBUNDLE: {
4140a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
4150a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4160a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4170a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            case VAL_DOUBLEARRAY: {
4180a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
4190a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4200a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4210a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            default: {
4220a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                ALOGE("Unrecognized type: %d", value_type);
4230a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                return BAD_TYPE;
4240a31202370581a91b3425e7b60757268d0f6b539Samuel Tan                break;
4250a31202370581a91b3425e7b60757268d0f6b539Samuel Tan            }
4260a31202370581a91b3425e7b60757268d0f6b539Samuel Tan        }
4270a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    }
4280a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
4290a31202370581a91b3425e7b60757268d0f6b539Samuel Tan    return NO_ERROR;
4300a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}
4310a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
4320a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}  // namespace os
4330a31202370581a91b3425e7b60757268d0f6b539Samuel Tan
4340a31202370581a91b3425e7b60757268d0f6b539Samuel Tan}  // namespace android
435