ProfileProvider.java revision 078f588cef389358adabc579de00747878f3c108
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 android.content.ContentValues;
19import android.content.Context;
20import android.content.res.AssetFileDescriptor;
21import android.database.Cursor;
22import android.database.sqlite.SQLiteDatabase;
23import android.net.Uri;
24
25import java.io.FileNotFoundException;
26import java.util.Locale;
27
28/**
29 * Simple content provider to handle directing profile-specific calls against a separate
30 * database from the rest of contacts.
31 */
32public class ProfileProvider extends AbstractContactsProvider {
33
34    private static final String READ_PERMISSION = "android.permission.READ_PROFILE";
35    private static final String WRITE_PERMISSION = "android.permission.WRITE_PROFILE";
36
37    // The Contacts provider handles most of the logic - this provider is only invoked when the
38    // URI belongs to a profile action, setting up the proper database.
39    private final ContactsProvider2 mDelegate;
40
41    public ProfileProvider(ContactsProvider2 delegate) {
42        mDelegate = delegate;
43    }
44
45    private void enforceReadPermission() {
46        mDelegate.getContext().enforceCallingOrSelfPermission(READ_PERMISSION, null);
47    }
48
49    private void enforceWritePermission() {
50        mDelegate.getContext().enforceCallingOrSelfPermission(WRITE_PERMISSION, null);
51    }
52
53    protected ProfileDatabaseHelper getDatabaseHelper(Context context) {
54        return ProfileDatabaseHelper.getInstance(context);
55    }
56
57    @Override
58    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
59            String sortOrder) {
60        enforceReadPermission();
61        mDelegate.substituteDb(getDatabaseHelper().getReadableDatabase());
62        return mDelegate.queryLocal(uri, projection, selection, selectionArgs, sortOrder, -1);
63    }
64
65    @Override
66    protected Uri insertInTransaction(Uri uri, ContentValues values) {
67        enforceWritePermission();
68        useProfileDbForTransaction();
69        return mDelegate.insertInTransaction(uri, values);
70    }
71
72    @Override
73    protected int updateInTransaction(Uri uri, ContentValues values, String selection,
74            String[] selectionArgs) {
75        enforceWritePermission();
76        useProfileDbForTransaction();
77        return mDelegate.updateInTransaction(uri, values, selection, selectionArgs);
78    }
79
80    @Override
81    protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
82        enforceWritePermission();
83        useProfileDbForTransaction();
84        return mDelegate.deleteInTransaction(uri, selection, selectionArgs);
85    }
86
87    @Override
88    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
89        if (mode != null && mode.contains("w")) {
90            enforceWritePermission();
91            mDelegate.substituteDb(getDatabaseHelper().getWritableDatabase());
92        } else {
93            enforceReadPermission();
94            mDelegate.substituteDb(getDatabaseHelper().getReadableDatabase());
95        }
96        return mDelegate.openAssetFileLocal(uri, mode);
97    }
98
99    private void useProfileDbForTransaction() {
100        ContactsTransaction transaction = getCurrentTransaction();
101        SQLiteDatabase db = getDatabaseHelper().getWritableDatabase();
102        transaction.startTransactionForDb(db, ContactsProvider2.PROFILE_DB_TAG, this);
103        mDelegate.substituteDb(db);
104    }
105
106    @Override
107    protected void notifyChange() {
108        mDelegate.notifyChange();
109    }
110
111    protected void notifyChange(boolean syncToNetwork) {
112        mDelegate.notifyChange(syncToNetwork);
113    }
114
115    protected Locale getLocale() {
116        return mDelegate.getLocale();
117    }
118
119    public void onBegin() {
120        mDelegate.onBegin();
121    }
122
123    public void onCommit() {
124        mDelegate.onCommit();
125    }
126
127    @Override
128    public void onRollback() {
129        mDelegate.onRollback();
130    }
131
132    @Override
133    protected boolean yield(ContactsTransaction transaction) {
134        return mDelegate.yield(transaction);
135    }
136
137    @Override
138    public String getType(Uri uri) {
139        return mDelegate.getType(uri);
140    }
141}
142