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