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.content.ContentProvider;
20 import android.net.Uri;
21 import android.os.Parcelable;
22 import android.os.Parcel;
23 
24 /**
25  * Contains the result of the application of a {@link ContentProviderOperation}. It is guaranteed
26  * to have exactly one of {@link #uri} or {@link #count} set.
27  */
28 public class ContentProviderResult implements Parcelable {
29     public final Uri uri;
30     public final Integer count;
31 
ContentProviderResult(Uri uri)32     public ContentProviderResult(Uri uri) {
33         if (uri == null) throw new IllegalArgumentException("uri must not be null");
34         this.uri = uri;
35         this.count = null;
36     }
37 
ContentProviderResult(int count)38     public ContentProviderResult(int count) {
39         this.count = count;
40         this.uri = null;
41     }
42 
ContentProviderResult(Parcel source)43     public ContentProviderResult(Parcel source) {
44         int type = source.readInt();
45         if (type == 1) {
46             count = source.readInt();
47             uri = null;
48         } else {
49             count = null;
50             uri = Uri.CREATOR.createFromParcel(source);
51         }
52     }
53 
54     /** @hide */
ContentProviderResult(ContentProviderResult cpr, int userId)55     public ContentProviderResult(ContentProviderResult cpr, int userId) {
56         uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
57         count = cpr.count;
58     }
59 
writeToParcel(Parcel dest, int flags)60     public void writeToParcel(Parcel dest, int flags) {
61         if (uri == null) {
62             dest.writeInt(1);
63             dest.writeInt(count);
64         } else {
65             dest.writeInt(2);
66             uri.writeToParcel(dest, 0);
67         }
68     }
69 
describeContents()70     public int describeContents() {
71         return 0;
72     }
73 
74     public static final Creator<ContentProviderResult> CREATOR =
75             new Creator<ContentProviderResult>() {
76         public ContentProviderResult createFromParcel(Parcel source) {
77             return new ContentProviderResult(source);
78         }
79 
80         public ContentProviderResult[] newArray(int size) {
81             return new ContentProviderResult[size];
82         }
83     };
84 
toString()85     public String toString() {
86         if (uri != null) {
87             return "ContentProviderResult(uri=" + uri.toString() + ")";
88         }
89         return "ContentProviderResult(count=" + count + ")";
90     }
91 }
92