| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.app.SearchManager; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.database.Cursor; |
| 22 |
import android.database.sqlite.SQLiteDatabase; |
| 23 |
import android.database.sqlite.SQLiteOpenHelper; |
| 24 |
import android.net.Uri; |
| 25 |
import android.text.TextUtils; |
| 26 |
import android.util.Log; |
| 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 |
public class SearchRecentSuggestionsProvider extends ContentProvider { |
| 75 |
|
| 76 |
private static final String TAG = "SuggestionsProvider"; |
| 77 |
|
| 78 |
|
| 79 |
private String mAuthority; |
| 80 |
private int mMode; |
| 81 |
private boolean mTwoLineDisplay; |
| 82 |
|
| 83 |
|
| 84 |
private SQLiteOpenHelper mOpenHelper; |
| 85 |
private static final String sDatabaseName = "suggestions.db"; |
| 86 |
private static final String sSuggestions = "suggestions"; |
| 87 |
private static final String ORDER_BY = "date DESC"; |
| 88 |
private static final String NULL_COLUMN = "query"; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private static final int DATABASE_VERSION = 2 * 256; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static final int DATABASE_MODE_QUERIES = 1; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static final int DATABASE_MODE_2LINES = 2; |
| 111 |
|
| 112 |
|
| 113 |
private static final int URI_MATCH_SUGGEST = 1; |
| 114 |
|
| 115 |
private Uri mSuggestionsUri; |
| 116 |
private UriMatcher mUriMatcher; |
| 117 |
|
| 118 |
private String mSuggestSuggestionClause; |
| 119 |
@UnsupportedAppUsage |
| 120 |
private String[] mSuggestionProjection; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private static class DatabaseHelper extends SQLiteOpenHelper { |
| 130 |
|
| 131 |
private int mNewVersion; |
| 132 |
|
| 133 |
public DatabaseHelper(Context context, int newVersion) { |
| 134 |
super(context, sDatabaseName, null, newVersion); |
| 135 |
mNewVersion = newVersion; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onCreate(SQLiteDatabase db) { |
| 140 |
StringBuilder builder = new StringBuilder(); |
| 141 |
builder.append("CREATE TABLE suggestions (" + |
| 142 |
"_id INTEGER PRIMARY KEY" + |
| 143 |
",display1 TEXT UNIQUE ON CONFLICT REPLACE"); |
| 144 |
if (0 != (mNewVersion & DATABASE_MODE_2LINES)) { |
| 145 |
builder.append(",display2 TEXT"); |
| 146 |
} |
| 147 |
builder.append(",query TEXT" + |
| 148 |
",date LONG" + |
| 149 |
");"); |
| 150 |
db.execSQL(builder.toString()); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 155 |
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " |
| 156 |
+ newVersion + ", which will destroy all old data"); |
| 157 |
db.execSQL("DROP TABLE IF EXISTS suggestions"); |
| 158 |
onCreate(db); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
protected void setupSuggestions(String authority, int mode) { |
| 176 |
if (TextUtils.isEmpty(authority) || |
| 177 |
((mode & DATABASE_MODE_QUERIES) == 0)) { |
| 178 |
throw new IllegalArgumentException(); |
| 179 |
} |
| 180 |
|
| 181 |
mTwoLineDisplay = (0 != (mode & DATABASE_MODE_2LINES)); |
| 182 |
|
| 183 |
|
| 184 |
mAuthority = new String(authority); |
| 185 |
mMode = mode; |
| 186 |
|
| 187 |
|
| 188 |
mSuggestionsUri = Uri.parse("content://" + mAuthority + "/suggestions"); |
| 189 |
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 190 |
mUriMatcher.addURI(mAuthority, SearchManager.SUGGEST_URI_PATH_QUERY, URI_MATCH_SUGGEST); |
| 191 |
|
| 192 |
if (mTwoLineDisplay) { |
| 193 |
mSuggestSuggestionClause = "display1 LIKE ? OR display2 LIKE ?"; |
| 194 |
|
| 195 |
mSuggestionProjection = new String [] { |
| 196 |
"0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT, |
| 197 |
"'android.resource://system/" |
| 198 |
+ com.android.internal.R.drawable.ic_menu_recent_history + "' AS " |
| 199 |
+ SearchManager.SUGGEST_COLUMN_ICON_1, |
| 200 |
"display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 201 |
"display2 AS " + SearchManager.SUGGEST_COLUMN_TEXT_2, |
| 202 |
"query AS " + SearchManager.SUGGEST_COLUMN_QUERY, |
| 203 |
"_id" |
| 204 |
}; |
| 205 |
} else { |
| 206 |
mSuggestSuggestionClause = "display1 LIKE ?"; |
| 207 |
|
| 208 |
mSuggestionProjection = new String [] { |
| 209 |
"0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT, |
| 210 |
"'android.resource://system/" |
| 211 |
+ com.android.internal.R.drawable.ic_menu_recent_history + "' AS " |
| 212 |
+ SearchManager.SUGGEST_COLUMN_ICON_1, |
| 213 |
"display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 214 |
"query AS " + SearchManager.SUGGEST_COLUMN_QUERY, |
| 215 |
"_id" |
| 216 |
}; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
@Override |
| 227 |
public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 228 |
SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 229 |
|
| 230 |
final int length = uri.getPathSegments().size(); |
| 231 |
if (length != 1) { |
| 232 |
throw new IllegalArgumentException("Unknown Uri"); |
| 233 |
} |
| 234 |
|
| 235 |
final String base = uri.getPathSegments().get(0); |
| 236 |
int count = 0; |
| 237 |
if (base.equals(sSuggestions)) { |
| 238 |
count = db.delete(sSuggestions, selection, selectionArgs); |
| 239 |
} else { |
| 240 |
throw new IllegalArgumentException("Unknown Uri"); |
| 241 |
} |
| 242 |
getContext().getContentResolver().notifyChange(uri, null); |
| 243 |
return count; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
@Override |
| 251 |
public String getType(Uri uri) { |
| 252 |
if (mUriMatcher.match(uri) == URI_MATCH_SUGGEST) { |
| 253 |
return SearchManager.SUGGEST_MIME_TYPE; |
| 254 |
} |
| 255 |
int length = uri.getPathSegments().size(); |
| 256 |
if (length >= 1) { |
| 257 |
String base = uri.getPathSegments().get(0); |
| 258 |
if (base.equals(sSuggestions)) { |
| 259 |
if (length == 1) { |
| 260 |
return "vnd.android.cursor.dir/suggestion"; |
| 261 |
} else if (length == 2) { |
| 262 |
return "vnd.android.cursor.item/suggestion"; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
throw new IllegalArgumentException("Unknown Uri"); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@Override |
| 274 |
public Uri insert(Uri uri, ContentValues values) { |
| 275 |
SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 276 |
|
| 277 |
int length = uri.getPathSegments().size(); |
| 278 |
if (length < 1) { |
| 279 |
throw new IllegalArgumentException("Unknown Uri"); |
| 280 |
} |
| 281 |
|
| 282 |
long rowID = -1; |
| 283 |
String base = uri.getPathSegments().get(0); |
| 284 |
Uri newUri = null; |
| 285 |
if (base.equals(sSuggestions)) { |
| 286 |
if (length == 1) { |
| 287 |
rowID = db.insert(sSuggestions, NULL_COLUMN, values); |
| 288 |
if (rowID > 0) { |
| 289 |
newUri = Uri.withAppendedPath(mSuggestionsUri, String.valueOf(rowID)); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
if (rowID < 0) { |
| 294 |
throw new IllegalArgumentException("Unknown Uri"); |
| 295 |
} |
| 296 |
getContext().getContentResolver().notifyChange(newUri, null); |
| 297 |
return newUri; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
@Override |
| 305 |
public boolean onCreate() { |
| 306 |
if (mAuthority == null || mMode == 0) { |
| 307 |
throw new IllegalArgumentException("Provider not configured"); |
| 308 |
} |
| 309 |
int mWorkingDbVersion = DATABASE_VERSION + mMode; |
| 310 |
mOpenHelper = new DatabaseHelper(getContext(), mWorkingDbVersion); |
| 311 |
|
| 312 |
return true; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
@Override |
| 321 |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 322 |
String sortOrder) { |
| 323 |
SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 324 |
|
| 325 |
|
| 326 |
if (mUriMatcher.match(uri) == URI_MATCH_SUGGEST) { |
| 327 |
String suggestSelection; |
| 328 |
String[] myArgs; |
| 329 |
if (TextUtils.isEmpty(selectionArgs[0])) { |
| 330 |
suggestSelection = null; |
| 331 |
myArgs = null; |
| 332 |
} else { |
| 333 |
String like = "%" + selectionArgs[0] + "%"; |
| 334 |
if (mTwoLineDisplay) { |
| 335 |
myArgs = new String [] { like, like }; |
| 336 |
} else { |
| 337 |
myArgs = new String [] { like }; |
| 338 |
} |
| 339 |
suggestSelection = mSuggestSuggestionClause; |
| 340 |
} |
| 341 |
|
| 342 |
Cursor c = db.query(sSuggestions, mSuggestionProjection, |
| 343 |
suggestSelection, myArgs, null, null, ORDER_BY, null); |
| 344 |
c.setNotificationUri(getContext().getContentResolver(), uri); |
| 345 |
return c; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
int length = uri.getPathSegments().size(); |
| 350 |
if (length != 1 && length != 2) { |
| 351 |
throw new IllegalArgumentException("Unknown Uri"); |
| 352 |
} |
| 353 |
|
| 354 |
String base = uri.getPathSegments().get(0); |
| 355 |
if (!base.equals(sSuggestions)) { |
| 356 |
throw new IllegalArgumentException("Unknown Uri"); |
| 357 |
} |
| 358 |
|
| 359 |
String[] useProjection = null; |
| 360 |
if (projection != null && projection.length > 0) { |
| 361 |
useProjection = new String[projection.length + 1]; |
| 362 |
System.arraycopy(projection, 0, useProjection, 0, projection.length); |
| 363 |
useProjection[projection.length] = "_id AS _id"; |
| 364 |
} |
| 365 |
|
| 366 |
StringBuilder whereClause = new StringBuilder(256); |
| 367 |
if (length == 2) { |
| 368 |
whereClause.append("(_id = ").append(uri.getPathSegments().get(1)).append(")"); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
if (selection != null && selection.length() > 0) { |
| 373 |
if (whereClause.length() > 0) { |
| 374 |
whereClause.append(" AND "); |
| 375 |
} |
| 376 |
|
| 377 |
whereClause.append('('); |
| 378 |
whereClause.append(selection); |
| 379 |
whereClause.append(')'); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
Cursor c = db.query(base, useProjection, whereClause.toString(), |
| 384 |
selectionArgs, null, null, sortOrder, |
| 385 |
null); |
| 386 |
c.setNotificationUri(getContext().getContentResolver(), uri); |
| 387 |
return c; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
@Override |
| 395 |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 396 |
throw new UnsupportedOperationException("Not implemented"); |
| 397 |
} |
| 398 |
|
| 399 |
} |