Entity.java revision 03d9490758c9318cee6d14d3cc5007556dce92d0
1/*
2 * Copyright (C) 2009 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
17package android.content;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21import android.net.Uri;
22import android.util.Log;
23
24import java.util.ArrayList;
25
26/**
27 * Objects that pass through the ContentProvider and ContentResolver's methods that deal with
28 * Entities must implement this abstract base class and thus themselves be Parcelable.
29 */
30public final class Entity implements Parcelable {
31    final private ContentValues mValues;
32    final private ArrayList<NamedContentValues> mSubValues;
33
34    public Entity(ContentValues values) {
35        mValues = values;
36        mSubValues = new ArrayList<NamedContentValues>();
37    }
38
39    public ContentValues getEntityValues() {
40        return mValues;
41    }
42
43    public ArrayList<NamedContentValues> getSubValues() {
44        return mSubValues;
45    }
46
47    public void addSubValue(Uri uri, ContentValues values) {
48        mSubValues.add(new Entity.NamedContentValues(uri, values));
49    }
50
51    public int describeContents() {
52        return 0;
53    }
54
55    public void writeToParcel(Parcel dest, int flags) {
56        mValues.writeToParcel(dest, 0);
57        dest.writeInt(mSubValues.size());
58        for (NamedContentValues value : mSubValues) {
59            value.uri.writeToParcel(dest, 0);
60            value.values.writeToParcel(dest, 0);
61        }
62    }
63
64    private Entity(Parcel source) {
65        mValues = ContentValues.CREATOR.createFromParcel(source);
66        final int numValues = source.readInt();
67        mSubValues = new ArrayList<NamedContentValues>(numValues);
68        for (int i = 0; i < numValues; i++) {
69            final Uri uri = Uri.CREATOR.createFromParcel(source);
70            final ContentValues values = ContentValues.CREATOR.createFromParcel(source);
71            mSubValues.add(new NamedContentValues(uri, values));
72        }
73    }
74
75    public static final Creator<Entity> CREATOR = new Creator<Entity>() {
76        public Entity createFromParcel(Parcel source) {
77            return new Entity(source);
78        }
79
80        public Entity[] newArray(int size) {
81            return new Entity[size];
82        }
83    };
84
85    public static class NamedContentValues {
86        public final Uri uri;
87        public final ContentValues values;
88
89        public NamedContentValues(Uri uri, ContentValues values) {
90            this.uri = uri;
91            this.values = values;
92        }
93    }
94
95    public String toString() {
96        final StringBuilder sb = new StringBuilder();
97        sb.append("Entity: ").append(getEntityValues());
98        for (Entity.NamedContentValues namedValue : getSubValues()) {
99            sb.append("\n  ").append(namedValue.uri);
100            sb.append("\n  -> ").append(namedValue.values);
101        }
102        return sb.toString();
103    }
104}
105