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.providers.contacts.util.ContactsPermissions; 19 20import android.content.ContentValues; 21import android.content.Context; 22import android.content.Intent; 23import android.content.res.AssetFileDescriptor; 24import android.database.Cursor; 25import android.database.sqlite.SQLiteDatabase; 26import android.net.Uri; 27import android.os.CancellationSignal; 28import android.provider.ContactsContract.Intents; 29 30import java.io.FileDescriptor; 31import java.io.FileNotFoundException; 32import java.io.PrintWriter; 33import java.util.Locale; 34 35/** 36 * Simple content provider to handle directing profile-specific calls against a separate 37 * database from the rest of contacts. 38 */ 39public class ProfileProvider extends AbstractContactsProvider { 40 private static final String READ_CONTACTS_PERMISSION = "android.permission.READ_CONTACTS"; 41 42 // The Contacts provider handles most of the logic - this provider is only invoked when the 43 // URI belongs to a profile action, setting up the proper database. 44 private final ContactsProvider2 mDelegate; 45 46 public ProfileProvider(ContactsProvider2 delegate) { 47 mDelegate = delegate; 48 } 49 50 @Override 51 protected ProfileDatabaseHelper getDatabaseHelper(Context context) { 52 return ProfileDatabaseHelper.getInstance(context); 53 } 54 55 @Override 56 protected ThreadLocal<ContactsTransaction> getTransactionHolder() { 57 return mDelegate.getTransactionHolder(); 58 } 59 60 @Override 61 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 62 String sortOrder) { 63 return query(uri, projection, selection, selectionArgs, sortOrder, null); 64 } 65 66 @Override 67 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 68 String sortOrder, CancellationSignal cancellationSignal) { 69 incrementStats(mQueryStats); 70 return mDelegate.queryLocal(uri, projection, selection, selectionArgs, sortOrder, -1, 71 cancellationSignal); 72 } 73 74 @Override 75 protected Uri insertInTransaction(Uri uri, ContentValues values) { 76 useProfileDbForTransaction(); 77 return mDelegate.insertInTransaction(uri, values); 78 } 79 80 @Override 81 protected int updateInTransaction(Uri uri, ContentValues values, String selection, 82 String[] selectionArgs) { 83 useProfileDbForTransaction(); 84 return mDelegate.updateInTransaction(uri, values, selection, selectionArgs); 85 } 86 87 @Override 88 protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) { 89 useProfileDbForTransaction(); 90 return mDelegate.deleteInTransaction(uri, selection, selectionArgs); 91 } 92 93 @Override 94 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { 95 return mDelegate.openAssetFileLocal(uri, mode); 96 } 97 98 private void useProfileDbForTransaction() { 99 ContactsTransaction transaction = getCurrentTransaction(); 100 SQLiteDatabase db = getDatabaseHelper().getWritableDatabase(); 101 transaction.startTransactionForDb(db, ContactsProvider2.PROFILE_DB_TAG, this); 102 } 103 104 @Override 105 protected void notifyChange() { 106 mDelegate.notifyChange(); 107 } 108 109 protected void notifyChange(boolean syncToNetwork, boolean syncToMetadataNetWork) { 110 mDelegate.notifyChange(syncToNetwork, syncToMetadataNetWork); 111 } 112 113 protected Locale getLocale() { 114 return mDelegate.getLocale(); 115 } 116 117 @Override 118 public void onBegin() { 119 mDelegate.onBeginTransactionInternal(true); 120 } 121 122 @Override 123 public void onCommit() { 124 mDelegate.onCommitTransactionInternal(true); 125 sendProfileChangedBroadcast(); 126 } 127 128 @Override 129 public void onRollback() { 130 mDelegate.onRollbackTransactionInternal(true); 131 } 132 133 @Override 134 protected boolean yield(ContactsTransaction transaction) { 135 return mDelegate.yield(transaction); 136 } 137 138 @Override 139 public String getType(Uri uri) { 140 return mDelegate.getType(uri); 141 } 142 143 /** Use only for debug logging */ 144 @Override 145 public String toString() { 146 return "ProfileProvider"; 147 } 148 149 private void sendProfileChangedBroadcast() { 150 final Intent intent = new Intent(Intents.ACTION_PROFILE_CHANGED); 151 mDelegate.getContext().sendBroadcast(intent, READ_CONTACTS_PERMISSION); 152 } 153 154 @Override 155 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 156 dump(pw, "Profile"); 157 } 158} 159