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 
17 package android.content;
18 
19 import android.net.Uri;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import com.android.internal.util.Preconditions;
24 
25 /**
26  * Contains the result of the application of a {@link ContentProviderOperation}. It is guaranteed
27  * to have exactly one of {@link #uri} or {@link #count} set.
28  */
29 public class ContentProviderResult implements Parcelable {
30     public final Uri uri;
31     public final Integer count;
32     /** {@hide} */
33     public final String failure;
34 
ContentProviderResult(Uri uri)35     public ContentProviderResult(Uri uri) {
36         this(Preconditions.checkNotNull(uri), null, null);
37     }
38 
ContentProviderResult(int count)39     public ContentProviderResult(int count) {
40         this(null, count, null);
41     }
42 
43     /** {@hide} */
ContentProviderResult(String failure)44     public ContentProviderResult(String failure) {
45         this(null, null, failure);
46     }
47 
48     /** {@hide} */
ContentProviderResult(Uri uri, Integer count, String failure)49     public ContentProviderResult(Uri uri, Integer count, String failure) {
50         this.uri = uri;
51         this.count = count;
52         this.failure = failure;
53     }
54 
ContentProviderResult(Parcel source)55     public ContentProviderResult(Parcel source) {
56         if (source.readInt() != 0) {
57             uri = Uri.CREATOR.createFromParcel(source);
58         } else {
59             uri = null;
60         }
61         if (source.readInt() != 0) {
62             count = source.readInt();
63         } else {
64             count = null;
65         }
66         if (source.readInt() != 0) {
67             failure = source.readString();
68         } else {
69             failure = null;
70         }
71     }
72 
73     /** @hide */
ContentProviderResult(ContentProviderResult cpr, int userId)74     public ContentProviderResult(ContentProviderResult cpr, int userId) {
75         uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
76         count = cpr.count;
77         failure = cpr.failure;
78     }
79 
80     @Override
writeToParcel(Parcel dest, int flags)81     public void writeToParcel(Parcel dest, int flags) {
82         if (uri != null) {
83             dest.writeInt(1);
84             uri.writeToParcel(dest, flags);
85         } else {
86             dest.writeInt(0);
87         }
88         if (count != null) {
89             dest.writeInt(1);
90             dest.writeInt(count);
91         } else {
92             dest.writeInt(0);
93         }
94         if (failure != null) {
95             dest.writeInt(1);
96             dest.writeString(failure);
97         } else {
98             dest.writeInt(0);
99         }
100     }
101 
102     @Override
describeContents()103     public int describeContents() {
104         return 0;
105     }
106 
107     public static final @android.annotation.NonNull Creator<ContentProviderResult> CREATOR =
108             new Creator<ContentProviderResult>() {
109         @Override
110         public ContentProviderResult createFromParcel(Parcel source) {
111             return new ContentProviderResult(source);
112         }
113 
114         @Override
115         public ContentProviderResult[] newArray(int size) {
116             return new ContentProviderResult[size];
117         }
118     };
119 
120     @Override
toString()121     public String toString() {
122         final StringBuilder sb = new StringBuilder("ContentProviderResult(");
123         if (uri != null) {
124             sb.append("uri=" + uri + " ");
125         }
126         if (count != null) {
127             sb.append("count=" + count + " ");
128         }
129         if (uri != null) {
130             sb.append("failure=" + failure + " ");
131         }
132         sb.deleteCharAt(sb.length() - 1);
133         sb.append(")");
134         return sb.toString();
135     }
136 }
137