• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.providers.contacts;
17 
18 import com.android.providers.contacts.util.ContactsPermissions;
19 
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.res.AssetFileDescriptor;
24 import android.database.Cursor;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.net.Uri;
27 import android.os.CancellationSignal;
28 import android.provider.ContactsContract.Intents;
29 
30 import java.io.FileDescriptor;
31 import java.io.FileNotFoundException;
32 import java.io.PrintWriter;
33 import 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  */
39 public 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 
ProfileProvider(ContactsProvider2 delegate)46     public ProfileProvider(ContactsProvider2 delegate) {
47         mDelegate = delegate;
48     }
49 
50     @Override
getDatabaseHelper(Context context)51     protected ProfileDatabaseHelper getDatabaseHelper(Context context) {
52         return ProfileDatabaseHelper.getInstance(context);
53     }
54 
55     @Override
getTransactionHolder()56     protected ThreadLocal<ContactsTransaction> getTransactionHolder() {
57         return mDelegate.getTransactionHolder();
58     }
59 
60     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)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
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)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
insertInTransaction(Uri uri, ContentValues values)75     protected Uri insertInTransaction(Uri uri, ContentValues values) {
76         useProfileDbForTransaction();
77         return mDelegate.insertInTransaction(uri, values);
78     }
79 
80     @Override
updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs)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
deleteInTransaction(Uri uri, String selection, String[] selectionArgs)88     protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
89         useProfileDbForTransaction();
90         return mDelegate.deleteInTransaction(uri, selection, selectionArgs);
91     }
92 
93     @Override
openAssetFile(Uri uri, String mode)94     public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
95         return mDelegate.openAssetFileLocal(uri, mode);
96     }
97 
useProfileDbForTransaction()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
notifyChange()105     protected void notifyChange() {
106         mDelegate.notifyChange();
107     }
108 
notifyChange(boolean syncToNetwork, boolean syncToMetadataNetWork)109     protected void notifyChange(boolean syncToNetwork, boolean syncToMetadataNetWork) {
110         mDelegate.notifyChange(syncToNetwork, syncToMetadataNetWork);
111     }
112 
getLocale()113     protected Locale getLocale() {
114         return mDelegate.getLocale();
115     }
116 
117     @Override
onBegin()118     public void onBegin() {
119         mDelegate.onBeginTransactionInternal(true);
120     }
121 
122     @Override
onCommit()123     public void onCommit() {
124         mDelegate.onCommitTransactionInternal(true);
125         sendProfileChangedBroadcast();
126     }
127 
128     @Override
onRollback()129     public void onRollback() {
130         mDelegate.onRollbackTransactionInternal(true);
131     }
132 
133     @Override
yield(ContactsTransaction transaction)134     protected boolean yield(ContactsTransaction transaction) {
135         return mDelegate.yield(transaction);
136     }
137 
138     @Override
getType(Uri uri)139     public String getType(Uri uri) {
140         return mDelegate.getType(uri);
141     }
142 
143     /** Use only for debug logging */
144     @Override
toString()145     public String toString() {
146         return "ProfileProvider";
147     }
148 
sendProfileChangedBroadcast()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
dump(FileDescriptor fd, PrintWriter pw, String[] args)155     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
156         dump(pw, "Profile");
157     }
158 }
159