1 /*
2  * Copyright (C) 2012 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mail.utils;
19 
20 import android.content.ContentProvider;
21 import android.content.ContentProviderOperation;
22 import android.content.ContentProviderResult;
23 import android.content.ContentResolver;
24 import android.content.ContentValues;
25 import android.net.Uri;
26 import android.os.AsyncTask;
27 
28 import com.google.common.collect.Lists;
29 
30 import java.util.ArrayList;
31 
32 /**
33  * Simple utility class to make an asynchronous {@link ContentProvider} request expressed as
34  * a list of {@link ContentProviderOperation}s. As with all {@link AsyncTask}s, subclasses should
35  * override {@link #onPostExecute(Object)} to handle success/failure.
36  *
37  * @see InsertTask
38  * @see UpdateTask
39  * @see DeleteTask
40  *
41  */
42 public class ContentProviderTask extends AsyncTask<Void, Void, ContentProviderTask.Result> {
43 
44     private ContentResolver mResolver;
45     private String mAuthority;
46     private ArrayList<ContentProviderOperation> mOps;
47 
48     private static final String LOG_TAG = LogTag.getLogTag();
49 
50     public static class Result {
51         public final Exception exception;
52         public final ContentProviderResult[] results;
53 
54         /**
55          * Create a new result.
56          * @param exception
57          * @param results
58          */
Result(Exception exception, ContentProviderResult[] results)59         private Result(Exception exception, ContentProviderResult[] results) {
60             this.exception = exception;
61             this.results = results;
62         }
63 
64         /**
65          * Create a new success result.
66          * @param success
67          * @return
68          */
newSuccess(ContentProviderResult[] success)69         private static Result newSuccess(ContentProviderResult[] success) {
70             return new Result(null, success);
71         }
72 
73         /**
74          * Create a new failure result.
75          * @param failure
76          */
newFailure(Exception failure)77         private static Result newFailure(Exception failure) {
78             return new Result(failure, null);
79         }
80     }
81 
82     @Override
doInBackground(Void... params)83     protected Result doInBackground(Void... params) {
84         Result result;
85             try {
86                 result = Result.newSuccess(mResolver.applyBatch(mAuthority, mOps));
87             } catch (Exception e) {
88                 LogUtils.w(LOG_TAG, e, "exception executing ContentProviderOperationsTask");
89                 result = Result.newFailure(e);
90             }
91         return result;
92     }
93 
run(ContentResolver resolver, String authority, ArrayList<ContentProviderOperation> ops)94     public void run(ContentResolver resolver, String authority,
95             ArrayList<ContentProviderOperation> ops) {
96         mResolver = resolver;
97         mAuthority = authority;
98         mOps = ops;
99         executeOnExecutor(THREAD_POOL_EXECUTOR, (Void) null);
100     }
101 
102     public static class InsertTask extends ContentProviderTask {
103 
run(ContentResolver resolver, Uri uri, ContentValues values)104         public void run(ContentResolver resolver, Uri uri, ContentValues values) {
105             final ContentProviderOperation op = ContentProviderOperation
106                     .newInsert(uri)
107                     .withValues(values)
108                     .build();
109             super.run(resolver, uri.getAuthority(), Lists.newArrayList(op));
110         }
111 
112     }
113 
114     public static class UpdateTask extends ContentProviderTask {
115 
run(ContentResolver resolver, Uri uri, ContentValues values, String selection, String[] selectionArgs)116         public void run(ContentResolver resolver, Uri uri, ContentValues values,
117                 String selection, String[] selectionArgs) {
118             final ContentProviderOperation op = ContentProviderOperation
119                     .newUpdate(uri)
120                     .withValues(values)
121                     .withSelection(selection, selectionArgs)
122                     .build();
123             super.run(resolver, uri.getAuthority(), Lists.newArrayList(op));
124         }
125 
126     }
127 
128     public static class DeleteTask extends ContentProviderTask {
129 
run(ContentResolver resolver, Uri uri, String selection, String[] selectionArgs)130         public void run(ContentResolver resolver, Uri uri, String selection,
131                 String[] selectionArgs) {
132             final ContentProviderOperation op = ContentProviderOperation
133                     .newDelete(uri)
134                     .withSelection(selection, selectionArgs)
135                     .build();
136             super.run(resolver, uri.getAuthority(), Lists.newArrayList(op));
137         }
138 
139     }
140 
141 }
142