Home | History | Annotate | Download | only in content
      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.UserHandle;
     22 import android.os.Parcelable;
     23 import android.os.Parcel;
     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 
     33     public ContentProviderResult(Uri uri) {
     34         if (uri == null) throw new IllegalArgumentException("uri must not be null");
     35         this.uri = uri;
     36         this.count = null;
     37     }
     38 
     39     public ContentProviderResult(int count) {
     40         this.count = count;
     41         this.uri = null;
     42     }
     43 
     44     public ContentProviderResult(Parcel source) {
     45         int type = source.readInt();
     46         if (type == 1) {
     47             count = source.readInt();
     48             uri = null;
     49         } else {
     50             count = null;
     51             uri = Uri.CREATOR.createFromParcel(source);
     52         }
     53     }
     54 
     55     /** @hide */
     56     public ContentProviderResult(ContentProviderResult cpr, int userId) {
     57         uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
     58         count = cpr.count;
     59     }
     60 
     61     public void writeToParcel(Parcel dest, int flags) {
     62         if (uri == null) {
     63             dest.writeInt(1);
     64             dest.writeInt(count);
     65         } else {
     66             dest.writeInt(2);
     67             uri.writeToParcel(dest, 0);
     68         }
     69     }
     70 
     71     public int describeContents() {
     72         return 0;
     73     }
     74 
     75     public static final Creator<ContentProviderResult> CREATOR =
     76             new Creator<ContentProviderResult>() {
     77         public ContentProviderResult createFromParcel(Parcel source) {
     78             return new ContentProviderResult(source);
     79         }
     80 
     81         public ContentProviderResult[] newArray(int size) {
     82             return new ContentProviderResult[size];
     83         }
     84     };
     85 
     86     public String toString() {
     87         if (uri != null) {
     88             return "ContentProviderResult(uri=" + uri.toString() + ")";
     89         }
     90         return "ContentProviderResult(count=" + count + ")";
     91     }
     92 }
     93