• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.os.ParcelableException;
26 
27 import java.util.Objects;
28 
29 /**
30  * Contains the result of the application of a {@link ContentProviderOperation}.
31  * <p>
32  * It is guaranteed to have exactly one of {@link #uri}, {@link #count},
33  * {@link #extras}, or {@link #exception} set.
34  */
35 public class ContentProviderResult implements Parcelable {
36     public final @Nullable Uri uri;
37     public final @Nullable Integer count;
38     public final @Nullable Bundle extras;
39     public final @Nullable Throwable exception;
40 
ContentProviderResult(@onNull Uri uri)41     public ContentProviderResult(@NonNull Uri uri) {
42         this(Objects.requireNonNull(uri), null, null, null);
43     }
44 
ContentProviderResult(int count)45     public ContentProviderResult(int count) {
46         this(null, count, null, null);
47     }
48 
ContentProviderResult(@onNull Bundle extras)49     public ContentProviderResult(@NonNull Bundle extras) {
50         this(null, null, Objects.requireNonNull(extras), null);
51     }
52 
ContentProviderResult(@onNull Throwable exception)53     public ContentProviderResult(@NonNull Throwable exception) {
54         this(null, null, null, exception);
55     }
56 
57     /** {@hide} */
ContentProviderResult(Uri uri, Integer count, Bundle extras, Throwable exception)58     public ContentProviderResult(Uri uri, Integer count, Bundle extras, Throwable exception) {
59         this.uri = uri;
60         this.count = count;
61         this.extras = extras;
62         this.exception = exception;
63     }
64 
ContentProviderResult(Parcel source)65     public ContentProviderResult(Parcel source) {
66         if (source.readInt() != 0) {
67             uri = Uri.CREATOR.createFromParcel(source);
68         } else {
69             uri = null;
70         }
71         if (source.readInt() != 0) {
72             count = source.readInt();
73         } else {
74             count = null;
75         }
76         if (source.readInt() != 0) {
77             extras = source.readBundle();
78         } else {
79             extras = null;
80         }
81         if (source.readInt() != 0) {
82             exception = ParcelableException.readFromParcel(source);
83         } else {
84             exception = null;
85         }
86     }
87 
88     /** @hide */
ContentProviderResult(ContentProviderResult cpr, int userId)89     public ContentProviderResult(ContentProviderResult cpr, int userId) {
90         uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
91         count = cpr.count;
92         extras = cpr.extras;
93         exception = cpr.exception;
94     }
95 
96     @Override
writeToParcel(Parcel dest, int flags)97     public void writeToParcel(Parcel dest, int flags) {
98         if (uri != null) {
99             dest.writeInt(1);
100             uri.writeToParcel(dest, flags);
101         } else {
102             dest.writeInt(0);
103         }
104         if (count != null) {
105             dest.writeInt(1);
106             dest.writeInt(count);
107         } else {
108             dest.writeInt(0);
109         }
110         if (extras != null) {
111             dest.writeInt(1);
112             dest.writeBundle(extras);
113         } else {
114             dest.writeInt(0);
115         }
116         if (exception != null) {
117             dest.writeInt(1);
118             ParcelableException.writeToParcel(dest, exception);
119         } else {
120             dest.writeInt(0);
121         }
122     }
123 
124     @Override
describeContents()125     public int describeContents() {
126         return 0;
127     }
128 
129     public static final @android.annotation.NonNull Creator<ContentProviderResult> CREATOR =
130             new Creator<ContentProviderResult>() {
131         @Override
132         public ContentProviderResult createFromParcel(Parcel source) {
133             return new ContentProviderResult(source);
134         }
135 
136         @Override
137         public ContentProviderResult[] newArray(int size) {
138             return new ContentProviderResult[size];
139         }
140     };
141 
142     @Override
toString()143     public String toString() {
144         final StringBuilder sb = new StringBuilder("ContentProviderResult(");
145         if (uri != null) {
146             sb.append("uri=").append(uri).append(' ');
147         }
148         if (count != null) {
149             sb.append("count=").append(count).append(' ');
150         }
151         if (extras != null) {
152             sb.append("extras=").append(extras).append(' ');
153         }
154         if (exception != null) {
155             sb.append("exception=").append(exception).append(' ');
156         }
157         sb.deleteCharAt(sb.length() - 1);
158         sb.append(")");
159         return sb.toString();
160     }
161 }
162