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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
package com.android.systemui.cm;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import com.android.internal.util.cm.SpamFilter;
import com.android.internal.util.cm.SpamFilter.SpamContract.PackageTable;
import com.android.internal.util.cm.SpamFilter.SpamContract.NotificationTable;
public class SpamMessageProvider extends ContentProvider {
public static final String AUTHORITY = SpamFilter.AUTHORITY;
private static final String UPDATE_COUNT_QUERY =
"UPDATE " + NotificationTable.TABLE_NAME +
" SET " + NotificationTable.LAST_BLOCKED + "=%d," +
NotificationTable.COUNT + "=" + NotificationTable.COUNT + "+1 " +
" WHERE " + NotificationTable.ID + "='%s'";
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PACKAGES = 0;
private static final int MESSAGES = 1;
private static final int PACKAGE_FOR_ID = 2;
private static final int MESSAGE_UPDATE_COUNT = 3;
private static final int MESSAGE_FOR_ID = 4;
static {
sURIMatcher.addURI(AUTHORITY, "packages", PACKAGES);
sURIMatcher.addURI(AUTHORITY, "messages", MESSAGES);
sURIMatcher.addURI(AUTHORITY, "message/#", MESSAGE_FOR_ID);
sURIMatcher.addURI(AUTHORITY, "message/inc_count/#", MESSAGE_UPDATE_COUNT);
}
private SpamOpenHelper mDbHelper;
@Override
public boolean onCreate() {
mDbHelper = new SpamOpenHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
int match = sURIMatcher.match(uri);
switch (match) {
case PACKAGE_FOR_ID:
return mDbHelper.getReadableDatabase().query(PackageTable.TABLE_NAME,
new String[]{NotificationTable.ID}, PackageTable.PACKAGE_NAME + "=?",
new String[]{uri.getLastPathSegment()}, null, null, null);
case PACKAGES:
return mDbHelper.getReadableDatabase().query(PackageTable.TABLE_NAME,
projection, selection, selectionArgs, null, null, sortOrder);
case MESSAGES:
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(NotificationTable.TABLE_NAME + " LEFT OUTER JOIN " + PackageTable.TABLE_NAME +
" ON (" + NotificationTable.TABLE_NAME + "." + NotificationTable.PACKAGE_ID + " = "
+ PackageTable.TABLE_NAME + "." + PackageTable.ID + ")");
SQLiteDatabase db = mDbHelper.getReadableDatabase();
return qb.query(db, projection, selection, selectionArgs,
null, null, null);
case MESSAGE_FOR_ID:
qb = new SQLiteQueryBuilder();
qb.setTables(NotificationTable.TABLE_NAME + " LEFT OUTER JOIN " + PackageTable.TABLE_NAME +
" ON (" + NotificationTable.TABLE_NAME + "." + NotificationTable.PACKAGE_ID + " = "
+ PackageTable.TABLE_NAME + "." + PackageTable.ID + ")");
qb.appendWhere(NotificationTable.TABLE_NAME + "." + NotificationTable.ID + "="
+ uri.getLastPathSegment());
return qb.query(mDbHelper.getReadableDatabase(),
null, null, null, null,
null, null);
default:
return null;
}
}
private long getPackageId(String pkg) {
long rowId = -1;
Cursor idCursor = mDbHelper.getReadableDatabase().query(PackageTable.TABLE_NAME,
new String[]{NotificationTable.ID}, PackageTable.PACKAGE_NAME + "=?",
new String[]{pkg}, null, null, null);
if (idCursor != null) {
if (idCursor.moveToFirst()) {
rowId = idCursor.getLong(0);
}
idCursor.close();
}
return rowId;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if (values == null) {
return null;
}
int match = sURIMatcher.match(uri);
switch (match) {
case MESSAGES:
String msgText = values.getAsString(NotificationTable.MESSAGE_TEXT);
String packageName = values.getAsString(PackageTable.PACKAGE_NAME);
if (TextUtils.isEmpty(msgText) || TextUtils.isEmpty(packageName)) {
return null;
}
values.clear();
values.put(PackageTable.PACKAGE_NAME, packageName);
long packageId = getPackageId(packageName);
if (packageId == -1) {
SQLiteDatabase writableDb = mDbHelper.getWritableDatabase();
packageId = writableDb.insert(
PackageTable.TABLE_NAME, null, values);
}
if (packageId != -1) {
values.clear();
values.put(NotificationTable.MESSAGE_TEXT, msgText);
values.put(NotificationTable.NORMALIZED_TEXT,
SpamFilter.getNormalizedContent(msgText));
values.put(NotificationTable.PACKAGE_ID, packageId);
values.put(NotificationTable.LAST_BLOCKED, System.currentTimeMillis());
long id = mDbHelper.getReadableDatabase().insert(NotificationTable.TABLE_NAME,
null, values);
if (id != -1) {
notifyChange(String.valueOf(id));
}
}
return null;
default:
return null;
}
}
private void notifyChange(String id) {
Uri uri = Uri.withAppendedPath(SpamFilter.NOTIFICATION_URI,
id);
getContext().getContentResolver().notifyChange(uri, null);
}
private void removePackageIfNecessary(int packageId) {
long numEntries = DatabaseUtils.queryNumEntries(mDbHelper.getReadableDatabase(),
NotificationTable.TABLE_NAME, NotificationTable.PACKAGE_ID + "=?",
new String[]{String.valueOf(packageId)});
if (numEntries == 0) {
SQLiteDatabase writableDb = mDbHelper.getWritableDatabase();
writableDb.delete(PackageTable.TABLE_NAME, PackageTable.ID + "=?",
new String[]{String.valueOf(packageId)});
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int match = sURIMatcher.match(uri);
switch (match) {
case MESSAGE_FOR_ID:
int packageId = -1;
Cursor idCursor = mDbHelper.getReadableDatabase().query(NotificationTable.TABLE_NAME,
new String[]{NotificationTable.PACKAGE_ID}, NotificationTable.ID + "=?",
new String[]{uri.getLastPathSegment()}, null, null, null);
if (idCursor != null) {
if (idCursor.moveToFirst()) {
packageId = idCursor.getInt(0);
}
idCursor.close();
}
SQLiteDatabase writableDb = mDbHelper.getWritableDatabase();
String id = uri.getLastPathSegment();
int result = writableDb.delete(NotificationTable.TABLE_NAME,
NotificationTable.ID + "=?", new String[]{id});
removePackageIfNecessary(packageId);
if (result > 0) {
notifyChange(id);
}
return result;
default:
return 0;
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int match = sURIMatcher.match(uri);
switch (match) {
case MESSAGE_UPDATE_COUNT:
String id = uri.getLastPathSegment();
String formattedQuery = String.format(UPDATE_COUNT_QUERY,
System.currentTimeMillis(), id);
SQLiteDatabase writableDb = mDbHelper.getWritableDatabase();
writableDb.execSQL(formattedQuery);
notifyChange(id);
return 0;
default:
return 0;
}
}
}
|