1 /* 2 * Copyright (C) 2015 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 17 package com.android.dialer.blocking; 18 19 import android.content.ContentProvider; 20 import android.content.ContentUris; 21 import android.content.ContentValues; 22 import android.content.UriMatcher; 23 import android.database.Cursor; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.database.sqlite.SQLiteQueryBuilder; 26 import android.net.Uri; 27 import android.support.annotation.VisibleForTesting; 28 import android.text.TextUtils; 29 import com.android.dialer.common.LogUtil; 30 import com.android.dialer.database.Database; 31 import com.android.dialer.database.DialerDatabaseHelper; 32 import com.android.dialer.database.FilteredNumberContract; 33 import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns; 34 import com.android.dialer.location.GeoUtil; 35 36 /** Filtered number content provider. */ 37 public class FilteredNumberProvider extends ContentProvider { 38 39 private static final int FILTERED_NUMBERS_TABLE = 1; 40 private static final int FILTERED_NUMBERS_TABLE_ID = 2; 41 private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 42 private DialerDatabaseHelper dialerDatabaseHelper; 43 44 @Override 45 public boolean onCreate() { 46 dialerDatabaseHelper = Database.get(getContext()).getDatabaseHelper(getContext()); 47 if (dialerDatabaseHelper == null) { 48 return false; 49 } 50 uriMatcher.addURI( 51 FilteredNumberContract.AUTHORITY, 52 FilteredNumberContract.FilteredNumber.FILTERED_NUMBERS_TABLE, 53 FILTERED_NUMBERS_TABLE); 54 uriMatcher.addURI( 55 FilteredNumberContract.AUTHORITY, 56 FilteredNumberContract.FilteredNumber.FILTERED_NUMBERS_TABLE + "/#", 57 FILTERED_NUMBERS_TABLE_ID); 58 return true; 59 } 60 61 @Override 62 public Cursor query( 63 Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 64 final SQLiteDatabase db = dialerDatabaseHelper.getReadableDatabase(); 65 SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 66 qb.setTables(DialerDatabaseHelper.Tables.FILTERED_NUMBER_TABLE); 67 final int match = uriMatcher.match(uri); 68 switch (match) { 69 case FILTERED_NUMBERS_TABLE: 70 break; 71 case FILTERED_NUMBERS_TABLE_ID: 72 qb.appendWhere(FilteredNumberColumns._ID + "=" + ContentUris.parseId(uri)); 73 break; 74 default: 75 throw new IllegalArgumentException("Unknown uri: " + uri); 76 } 77 final Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null); 78 if (c != null) { 79 c.setNotificationUri( 80 getContext().getContentResolver(), FilteredNumberContract.FilteredNumber.CONTENT_URI); 81 } else { 82 LogUtil.d("FilteredNumberProvider.query", "CURSOR WAS NULL"); 83 } 84 return c; 85 } 86 87 @Override 88 public String getType(Uri uri) { 89 return FilteredNumberContract.FilteredNumber.CONTENT_ITEM_TYPE; 90 } 91 92 @Override 93 public Uri insert(Uri uri, ContentValues values) { 94 SQLiteDatabase db = dialerDatabaseHelper.getWritableDatabase(); 95 setDefaultValues(values); 96 long id = db.insert(DialerDatabaseHelper.Tables.FILTERED_NUMBER_TABLE, null, values); 97 if (id < 0) { 98 return null; 99 } 100 notifyChange(uri); 101 return ContentUris.withAppendedId(uri, id); 102 } 103 104 @VisibleForTesting 105 protected long getCurrentTimeMs() { 106 return System.currentTimeMillis(); 107 } 108 109 private void setDefaultValues(ContentValues values) { 110 if (values.getAsString(FilteredNumberColumns.COUNTRY_ISO) == null) { 111 values.put(FilteredNumberColumns.COUNTRY_ISO, GeoUtil.getCurrentCountryIso(getContext())); 112 } 113 if (values.getAsInteger(FilteredNumberColumns.TIMES_FILTERED) == null) { 114 values.put(FilteredNumberContract.FilteredNumberColumns.TIMES_FILTERED, 0); 115 } 116 if (values.getAsLong(FilteredNumberColumns.CREATION_TIME) == null) { 117 values.put(FilteredNumberColumns.CREATION_TIME, getCurrentTimeMs()); 118 } 119 } 120 121 @Override 122 public int delete(Uri uri, String selection, String[] selectionArgs) { 123 SQLiteDatabase db = dialerDatabaseHelper.getWritableDatabase(); 124 final int match = uriMatcher.match(uri); 125 switch (match) { 126 case FILTERED_NUMBERS_TABLE: 127 break; 128 case FILTERED_NUMBERS_TABLE_ID: 129 selection = getSelectionWithId(selection, ContentUris.parseId(uri)); 130 break; 131 default: 132 throw new IllegalArgumentException("Unknown uri: " + uri); 133 } 134 int rows = 135 db.delete(DialerDatabaseHelper.Tables.FILTERED_NUMBER_TABLE, selection, selectionArgs); 136 if (rows > 0) { 137 notifyChange(uri); 138 } 139 return rows; 140 } 141 142 @Override 143 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 144 SQLiteDatabase db = dialerDatabaseHelper.getWritableDatabase(); 145 final int match = uriMatcher.match(uri); 146 switch (match) { 147 case FILTERED_NUMBERS_TABLE: 148 break; 149 case FILTERED_NUMBERS_TABLE_ID: 150 selection = getSelectionWithId(selection, ContentUris.parseId(uri)); 151 break; 152 default: 153 throw new IllegalArgumentException("Unknown uri: " + uri); 154 } 155 int rows = 156 db.update( 157 DialerDatabaseHelper.Tables.FILTERED_NUMBER_TABLE, values, selection, selectionArgs); 158 if (rows > 0) { 159 notifyChange(uri); 160 } 161 return rows; 162 } 163 164 private String getSelectionWithId(String selection, long id) { 165 if (TextUtils.isEmpty(selection)) { 166 return FilteredNumberContract.FilteredNumberColumns._ID + "=" + id; 167 } else { 168 return selection + "AND " + FilteredNumberContract.FilteredNumberColumns._ID + "=" + id; 169 } 170 } 171 172 private void notifyChange(Uri uri) { 173 getContext().getContentResolver().notifyChange(uri, null); 174 } 175 } 176