aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/providers/contacts/VoicemailStatusTable.java
blob: f3008c0ed17d4a2b0efb1923e280717af941a256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.providers.contacts;

import static com.android.providers.contacts.util.DbQueryUtils.concatenateClauses;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.VoicemailContract.Status;
import android.util.ArraySet;

import com.android.common.content.ProjectionMap;
import com.android.providers.contacts.VoicemailContentProvider.UriData;

import java.util.Set;

/**
 * Implementation of {@link VoicemailTable.Delegate} for the voicemail status table.
 *
 * Public methods of this class are thread-safe as it is used in a content provider, which should
 * be thread-safe.
 */
public class VoicemailStatusTable implements VoicemailTable.Delegate {

    private static final ProjectionMap sStatusProjectionMap = new ProjectionMap.Builder()
            .add(Status._ID)
            .add(Status.PHONE_ACCOUNT_COMPONENT_NAME)
            .add(Status.PHONE_ACCOUNT_ID)
            .add(Status.CONFIGURATION_STATE)
            .add(Status.DATA_CHANNEL_STATE)
            .add(Status.NOTIFICATION_CHANNEL_STATE)
            .add(Status.SETTINGS_URI)
            .add(Status.SOURCE_PACKAGE)
            .add(Status.VOICEMAIL_ACCESS_URI)
            .add(Status.QUOTA_OCCUPIED)
            .add(Status.QUOTA_TOTAL)
            .add(Status.SOURCE_TYPE)
            .build();

    private static final Object DATABASE_LOCK = new Object();

    private final String mTableName;
    private final Context mContext;
    private final CallLogDatabaseHelper mDbHelper;
    private final VoicemailTable.DelegateHelper mDelegateHelper;

    public VoicemailStatusTable(String tableName, Context context, CallLogDatabaseHelper dbHelper,
            VoicemailTable.DelegateHelper delegateHelper) {
        mTableName = tableName;
        mContext = context;
        mDbHelper = dbHelper;
        mDelegateHelper = delegateHelper;
    }

    @Override
    public Uri insert(UriData uriData, ContentValues values) {
        synchronized (DATABASE_LOCK) {
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            // Try to update before insert.
            String combinedClause = uriData.getWhereClause();
            int rowsChanged = getDatabaseModifier(db)
                    .update(uriData.getUri(), mTableName, values, combinedClause, null);
            if (rowsChanged != 0) {
                final String[] selection = new String[] {Status._ID};
                Cursor c = db.query(mTableName, selection, combinedClause, null, null, null, null);
                c.moveToFirst();
                int rowId = c.getInt(0);
                c.close();
                return ContentUris.withAppendedId(uriData.getUri(), rowId);
            }
            ContentValues copiedValues = new ContentValues(values);
            mDelegateHelper.checkAndAddSourcePackageIntoValues(uriData, copiedValues);
            long rowId = getDatabaseModifier(db).insert(mTableName, null, copiedValues);
            if (rowId > 0) {
                return ContentUris.withAppendedId(uriData.getUri(), rowId);
            } else {
                return null;
            }
        }
    }

    @Override
    public int delete(UriData uriData, String selection, String[] selectionArgs) {
        synchronized (DATABASE_LOCK) {
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            String combinedClause = concatenateClauses(selection, uriData.getWhereClause());
            return getDatabaseModifier(db).delete(mTableName, combinedClause,
                    selectionArgs);
        }
    }

    @Override
    public Cursor query(UriData uriData, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        synchronized (DATABASE_LOCK) {
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            qb.setTables(mTableName);
            qb.setProjectionMap(sStatusProjectionMap);
            qb.setStrict(true);

            String combinedClause = concatenateClauses(selection, uriData.getWhereClause());
            SQLiteDatabase db = mDbHelper.getReadableDatabase();
            Cursor c = qb
                    .query(db, projection, combinedClause, selectionArgs, null, null, sortOrder);
            if (c != null) {
                c.setNotificationUri(mContext.getContentResolver(), Status.CONTENT_URI);
            }
            return c;
        }
    }

    @Override
    public int update(UriData uriData, ContentValues values, String selection,
            String[] selectionArgs) {
        synchronized (DATABASE_LOCK) {
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            String combinedClause = concatenateClauses(selection, uriData.getWhereClause());
            return getDatabaseModifier(db)
                    .update(uriData.getUri(), mTableName, values, combinedClause, selectionArgs);
        }
    }

    @Override
    public String getType(UriData uriData) {
        if (uriData.hasId()) {
            return Status.ITEM_TYPE;
        } else {
            return Status.DIR_TYPE;
        }
    }

    @Override
    public ParcelFileDescriptor openFile(UriData uriData, String mode) {
        throw new UnsupportedOperationException("File operation is not supported for status table");
    }

    private DatabaseModifier getDatabaseModifier(SQLiteDatabase db) {
        return new DbModifierWithNotification(mTableName, db, mContext);
    }

    @Override
    public ArraySet<String> getSourcePackages() {
        return mDbHelper.selectDistinctColumn(mTableName, Status.SOURCE_PACKAGE);
    }
}