Entity.java revision 5bba632d877c2878384ff21566c8eb6a1a22f37b
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 * @hide
30 */
31public final class Entity implements Parcelable {
32    final private ContentValues mValues;
33    final private ArrayList<NamedContentValues> mSubValues;
34
35    public Entity(ContentValues values) {
36        mValues = values;
37        mSubValues = new ArrayList<NamedContentValues>();
38    }
39
40    public ContentValues getEntityValues() {
41        return mValues;
42    }
43
44    public ArrayList<NamedContentValues> getSubValues() {
45        return mSubValues;
46    }
47
48    public void addSubValue(Uri uri, ContentValues values) {
49        mSubValues.add(new Entity.NamedContentValues(uri, values));
50    }
51
52    public int describeContents() {
53        return 0;
54    }
55
56    public void writeToParcel(Parcel dest, int flags) {
57        mValues.writeToParcel(dest, 0);
58        dest.writeInt(mSubValues.size());
59        for (NamedContentValues value : mSubValues) {
60            value.uri.writeToParcel(dest, 0);
61            value.values.writeToParcel(dest, 0);
62        }
63    }
64
65    private Entity(Parcel source) {
66        mValues = ContentValues.CREATOR.createFromParcel(source);
67        final int numValues = source.readInt();
68        mSubValues = new ArrayList<NamedContentValues>(numValues);
69        for (int i = 0; i < numValues; i++) {
70            final Uri uri = Uri.CREATOR.createFromParcel(source);
71            final ContentValues values = ContentValues.CREATOR.createFromParcel(source);
72            mSubValues.add(new NamedContentValues(uri, values));
73        }
74    }
75
76    public static final Creator<Entity> CREATOR = new Creator<Entity>() {
77        public Entity createFromParcel(Parcel source) {
78            return new Entity(source);
79        }
80
81        public Entity[] newArray(int size) {
82            return new Entity[size];
83        }
84    };
85
86    public static class NamedContentValues {
87        public final Uri uri;
88        public final ContentValues values;
89
90        public NamedContentValues(Uri uri, ContentValues values) {
91            this.uri = uri;
92            this.values = values;
93        }
94    }
95
96    public String toString() {
97        final StringBuilder sb = new StringBuilder();
98        sb.append("Entity: ").append(getEntityValues());
99        for (Entity.NamedContentValues namedValue : getSubValues()) {
100            sb.append("\n  ").append(namedValue.uri);
101            sb.append("\n  -> ").append(namedValue.values);
102        }
103        return sb.toString();
104    }
105}
106