1/* 2 * Copyright (C) 2016 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 */ 16package com.android.contacts.test.mocks; 17 18import android.content.ContentProviderClient; 19import android.content.ContentProviderOperation; 20import android.content.ContentProviderResult; 21import android.content.ContentValues; 22import android.content.OperationApplicationException; 23import android.content.res.AssetFileDescriptor; 24import android.database.Cursor; 25import android.net.Uri; 26import android.os.Bundle; 27import android.os.CancellationSignal; 28import android.os.ParcelFileDescriptor; 29import android.os.RemoteException; 30import android.support.annotation.Nullable; 31 32import java.io.FileNotFoundException; 33import java.util.ArrayList; 34 35/** 36 * Forwards calls to a {@link ContentProviderClient} 37 * 38 * <p>This allows mixing use of the system content providers in a 39 * {@link android.test.mock.MockContentResolver} 40 * </p> 41 */ 42public class ForwardingContentProvider extends android.test.mock.MockContentProvider { 43 44 private final ContentProviderClient mClient; 45 46 public ForwardingContentProvider(ContentProviderClient client) { 47 mClient = client; 48 } 49 50 @Override 51 public synchronized Cursor query(Uri url, String[] projection, String selection, 52 String[] selectionArgs, String sortOrder) { 53 try { 54 return mClient.query(url, projection, selection, selectionArgs, sortOrder); 55 } catch (RemoteException e) { 56 throw new RuntimeException(e); 57 } 58 } 59 60 @Nullable 61 @Override 62 public synchronized Cursor query(Uri url, String[] projection, String selection, 63 String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal) { 64 try { 65 return mClient.query(url, projection, selection, selectionArgs, sortOrder, 66 cancellationSignal); 67 } catch (RemoteException e) { 68 throw new RuntimeException(e); 69 } 70 } 71 72 @Override 73 public synchronized String getType(Uri url) { 74 try { 75 return mClient.getType(url); 76 } catch (RemoteException e) { 77 throw new RuntimeException(e); 78 } 79 } 80 81 @Override 82 public synchronized String[] getStreamTypes(Uri url, String mimeTypeFilter) { 83 try { 84 return mClient.getStreamTypes(url, mimeTypeFilter); 85 } catch (RemoteException e) { 86 throw new RuntimeException(e); 87 } 88 } 89 90 @Override 91 public synchronized Uri insert(Uri url, ContentValues initialValues) { 92 try { 93 return mClient.insert(url, initialValues); 94 } catch (RemoteException e) { 95 throw new RuntimeException(e); 96 } 97 } 98 99 @Override 100 public synchronized int bulkInsert(Uri url, ContentValues[] initialValues) { 101 try { 102 return mClient.bulkInsert(url, initialValues); 103 } catch (RemoteException e) { 104 throw new RuntimeException(e); 105 } 106 } 107 108 @Override 109 public synchronized int delete(Uri url, String selection, String[] selectionArgs) { 110 try { 111 return mClient.delete(url, selection, selectionArgs); 112 } catch (RemoteException e) { 113 throw new RuntimeException(e); 114 } 115 } 116 117 @Override 118 public synchronized int update(Uri url, ContentValues values, 119 String selection, String[] selectionArgs) { 120 try { 121 return mClient.update(url, values, selection, selectionArgs); 122 } catch (RemoteException e) { 123 throw new RuntimeException(e); 124 } 125 } 126 127 @Nullable 128 @Override 129 public synchronized ParcelFileDescriptor openFile(Uri url, String mode) { 130 try { 131 return mClient.openFile(url, mode); 132 } catch (RemoteException|FileNotFoundException e) { 133 throw new RuntimeException(e); 134 } 135 } 136 137 @Nullable 138 @Override 139 public synchronized ParcelFileDescriptor openFile(Uri url, String mode, 140 CancellationSignal signal) { 141 try { 142 return mClient.openFile(url, mode, signal); 143 } catch (RemoteException|FileNotFoundException e) { 144 throw new RuntimeException(e); 145 } 146 } 147 148 @Nullable 149 @Override 150 public synchronized AssetFileDescriptor openAssetFile(Uri url, String mode) { 151 try { 152 return mClient.openAssetFile(url, mode); 153 } catch (RemoteException|FileNotFoundException e) { 154 throw new RuntimeException(e); 155 } 156 } 157 158 @Nullable 159 @Override 160 public synchronized AssetFileDescriptor openAssetFile(Uri url, String mode, 161 CancellationSignal signal) { 162 try { 163 return mClient.openAssetFile(url, mode, signal); 164 } catch (RemoteException|FileNotFoundException e) { 165 throw new RuntimeException(e); 166 } 167 } 168 169 public synchronized AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri, String mimeType, 170 Bundle opts) { 171 try { 172 return mClient.openTypedAssetFileDescriptor(uri, mimeType, opts); 173 } catch (RemoteException|FileNotFoundException e) { 174 throw new RuntimeException(e); 175 } 176 } 177 178 public synchronized AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri, String mimeType, 179 Bundle opts, CancellationSignal signal) { 180 try { 181 return mClient.openTypedAssetFileDescriptor(uri, mimeType, opts, signal); 182 } catch (RemoteException|FileNotFoundException e) { 183 throw new RuntimeException(e); 184 } 185 } 186 187 @Override 188 public synchronized ContentProviderResult[] applyBatch( 189 ArrayList<ContentProviderOperation> operations) { 190 try { 191 return mClient.applyBatch(operations); 192 } catch (RemoteException|OperationApplicationException e) { 193 throw new RuntimeException(e); 194 } 195 } 196 197 @Nullable 198 @Override 199 public synchronized Bundle call(String method, String arg, Bundle extras) { 200 try { 201 return mClient.call(method, arg, extras); 202 } catch (RemoteException e) { 203 throw new RuntimeException(e); 204 } 205 } 206} 207