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>
21#include <vector>
22
23#include <binder/Parcelable.h>
24#include <utils/String16.h>
25#include <utils/StrongPointer.h>
26
27namespace android {
28
29namespace 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 */
35class PersistableBundle : public Parcelable {
36public:
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    friend bool operator==(const PersistableBundle& lhs, const PersistableBundle& rhs) {
83        return (lhs.mBoolMap == rhs.mBoolMap && lhs.mIntMap == rhs.mIntMap &&
84                lhs.mLongMap == rhs.mLongMap && lhs.mDoubleMap == rhs.mDoubleMap &&
85                lhs.mStringMap == rhs.mStringMap && lhs.mBoolVectorMap == rhs.mBoolVectorMap &&
86                lhs.mIntVectorMap == rhs.mIntVectorMap &&
87                lhs.mLongVectorMap == rhs.mLongVectorMap &&
88                lhs.mDoubleVectorMap == rhs.mDoubleVectorMap &&
89                lhs.mStringVectorMap == rhs.mStringVectorMap &&
90                lhs.mPersistableBundleMap == rhs.mPersistableBundleMap);
91    }
92
93    friend bool operator!=(const PersistableBundle& lhs, const PersistableBundle& rhs) {
94        return !(lhs == rhs);
95    }
96
97private:
98    status_t writeToParcelInner(Parcel* parcel) const;
99    status_t readFromParcelInner(const Parcel* parcel, size_t length);
100
101    std::map<String16, bool> mBoolMap;
102    std::map<String16, int32_t> mIntMap;
103    std::map<String16, int64_t> mLongMap;
104    std::map<String16, double> mDoubleMap;
105    std::map<String16, String16> mStringMap;
106    std::map<String16, std::vector<bool>> mBoolVectorMap;
107    std::map<String16, std::vector<int32_t>> mIntVectorMap;
108    std::map<String16, std::vector<int64_t>> mLongVectorMap;
109    std::map<String16, std::vector<double>> mDoubleVectorMap;
110    std::map<String16, std::vector<String16>> mStringVectorMap;
111    std::map<String16, PersistableBundle> mPersistableBundleMap;
112};
113
114}  // namespace os
115
116}  // namespace android
117
118#endif  // ANDROID_PERSISTABLE_BUNDLE_H
119