ProfileProvider.java revision 5d0a768b56ed4bd0dfef81b8389247ba74766659
1/* 2 * Copyright (C) 2011 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.providers.contacts; 17 18import com.android.common.content.SQLiteContentProvider; 19 20import android.accounts.Account; 21import android.content.ContentValues; 22import android.content.Context; 23import android.content.res.AssetFileDescriptor; 24import android.database.Cursor; 25import android.net.Uri; 26 27import java.io.FileNotFoundException; 28import java.util.Locale; 29 30/** 31 * Simple content provider to handle directing profile-specific calls against a separate 32 * database from the rest of contacts. 33 */ 34public class ProfileProvider extends SQLiteContentProvider { 35 36 private static final String READ_PERMISSION = "android.permission.READ_PROFILE"; 37 private static final String WRITE_PERMISSION = "android.permission.WRITE_PROFILE"; 38 39 // The Contacts provider handles most of the logic - this provider is only invoked when the 40 // URI belongs to a profile action, setting up the proper database. 41 private final ContactsProvider2 mDelegate; 42 43 public ProfileProvider(ContactsProvider2 delegate) { 44 mDelegate = delegate; 45 } 46 47 private void enforceReadPermission() { 48 mDelegate.getContext().enforceCallingOrSelfPermission(READ_PERMISSION, null); 49 } 50 51 private void enforceWritePermission() { 52 mDelegate.getContext().enforceCallingOrSelfPermission(WRITE_PERMISSION, null); 53 } 54 55 @Override 56 protected ProfileDatabaseHelper getDatabaseHelper(Context context) { 57 return ProfileDatabaseHelper.getInstance(context); 58 } 59 60 @Override 61 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 62 String sortOrder) { 63 enforceReadPermission(); 64 if (mDb == null) { 65 mDb = getDatabaseHelper().getReadableDatabase(); 66 } 67 mDelegate.substituteDb(mDb); 68 return mDelegate.queryLocal(uri, projection, selection, selectionArgs, sortOrder, -1); 69 } 70 71 @Override 72 protected Uri insertInTransaction(Uri uri, ContentValues values) { 73 enforceWritePermission(); 74 mDelegate.substituteDb(mDb); 75 return mDelegate.insertInTransaction(uri, values); 76 } 77 78 @Override 79 protected int updateInTransaction(Uri uri, ContentValues values, String selection, 80 String[] selectionArgs) { 81 enforceWritePermission(); 82 mDelegate.substituteDb(mDb); 83 return mDelegate.updateInTransaction(uri, values, selection, selectionArgs); 84 } 85 86 @Override 87 protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) { 88 enforceWritePermission(); 89 mDelegate.substituteDb(mDb); 90 return mDelegate.deleteInTransaction(uri, selection, selectionArgs); 91 } 92 93 @Override 94 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { 95 if (mode != null && mode.contains("w")) { 96 enforceWritePermission(); 97 mDelegate.substituteDb(getDatabaseHelper().getWritableDatabase()); 98 } else { 99 enforceReadPermission(); 100 mDelegate.substituteDb(getDatabaseHelper().getReadableDatabase()); 101 } 102 return mDelegate.openAssetFileLocal(uri, mode); 103 } 104 105 @Override 106 protected void notifyChange() { 107 mDelegate.notifyChange(); 108 } 109 110 protected void notifyChange(boolean syncToNetwork) { 111 mDelegate.notifyChange(syncToNetwork); 112 } 113 114 protected void scheduleBackgroundTask(int task) { 115 mDelegate.scheduleBackgroundTask(task); 116 } 117 118 protected void scheduleBackgroundTask(int task, Object arg) { 119 mDelegate.scheduleBackgroundTask(task, arg); 120 } 121 122 protected void performBackgroundTask(int task, Object arg) { 123 mDelegate.performBackgroundTask(task, arg); 124 } 125 126 protected void updateLocaleInBackground() { 127 mDelegate.updateLocaleInBackground(); 128 } 129 130 protected void updateDirectoriesInBackground(boolean rescan) { 131 mDelegate.updateDirectoriesInBackground(rescan); 132 } 133 134 protected Account getDefaultAccount() { 135 return mDelegate.getDefaultAccount(); 136 } 137 138 protected boolean isContactsAccount(Account account) { 139 return mDelegate.isContactsAccount(account); 140 } 141 142 protected Locale getLocale() { 143 return mDelegate.getLocale(); 144 } 145 146 protected boolean isWritableAccountWithDataSet(String accountTypeAndDataSet) { 147 return mDelegate.isWritableAccountWithDataSet(accountTypeAndDataSet); 148 } 149 150 protected void onBeginTransaction() { 151 mDelegate.onBeginTransaction(); 152 } 153 154 protected void beforeTransactionCommit() { 155 mDelegate.beforeTransactionCommit(); 156 } 157 158 @Override 159 public String getType(Uri uri) { 160 return mDelegate.getType(uri); 161 } 162} 163