2 * Copyright (C) 2014 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.cyanogenmod.eleven.provider;
18 import android.content.ContentValues;
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
23 import java.util.ArrayList;
25 public class SearchHistory {
26 /* Maximum # of items in the db */
27 private static final int MAX_ITEMS_IN_DB = 25;
29 private static SearchHistory sInstance = null;
31 private MusicDB mMusicDatabase = null;
33 public SearchHistory(final Context context) {
34 mMusicDatabase = MusicDB.getInstance(context);
37 public void onCreate(final SQLiteDatabase db) {
38 db.execSQL("CREATE TABLE IF NOT EXISTS " + SearchHistoryColumns.NAME + " ("
39 + SearchHistoryColumns.SEARCHSTRING + " STRING NOT NULL,"
40 + SearchHistoryColumns.TIMESEARCHED + " LONG NOT NULL);");
43 public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
44 // No upgrade path needed yet
47 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
48 // If we ever have downgrade, drop the table to be safe
49 db.execSQL("DROP TABLE IF EXISTS " + SearchHistoryColumns.NAME);
54 * @param context The {@link android.content.Context} to use
55 * @return A new instance of this class.
57 public static final synchronized SearchHistory getInstance(final Context context) {
58 if (sInstance == null) {
59 sInstance = new SearchHistory(context.getApplicationContext());
65 * Used to save a search request into the db. This function will remove any old
66 * searches for that string before inserting it into the db
67 * @param searchString The string that has been searched
69 public void addSearchString(final String searchString) {
70 if (searchString == null) {
74 String trimmedString = searchString.trim();
76 if (trimmedString.isEmpty()) {
80 final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
81 database.beginTransaction();
84 // delete existing searches with the same search string
85 database.delete(SearchHistoryColumns.NAME,
86 SearchHistoryColumns.SEARCHSTRING + " = ? COLLATE NOCASE",
87 new String[] { trimmedString });
90 final ContentValues values = new ContentValues(2);
91 values.put(SearchHistoryColumns.SEARCHSTRING, trimmedString);
92 values.put(SearchHistoryColumns.TIMESEARCHED, System.currentTimeMillis());
93 database.insert(SearchHistoryColumns.NAME, null, values);
95 // if our db is too large, delete the extra items
98 database.query(SearchHistoryColumns.NAME,
99 new String[]{SearchHistoryColumns.TIMESEARCHED}, null, null, null, null,
100 SearchHistoryColumns.TIMESEARCHED + " ASC");
102 if (oldest != null && oldest.getCount() > MAX_ITEMS_IN_DB) {
103 oldest.moveToPosition(oldest.getCount() - MAX_ITEMS_IN_DB);
104 long timeOfRecordToKeep = oldest.getLong(0);
106 database.delete(SearchHistoryColumns.NAME,
107 SearchHistoryColumns.TIMESEARCHED + " < ?",
108 new String[] { String.valueOf(timeOfRecordToKeep) });
112 if (oldest != null) {
118 database.setTransactionSuccessful();
119 database.endTransaction();
124 * Gets a cursor to the list of recently searched strings
125 * @param limit number of items to limit to
128 public Cursor queryRecentSearches(final String limit) {
129 final SQLiteDatabase database = mMusicDatabase.getReadableDatabase();
130 return database.query(SearchHistoryColumns.NAME,
131 new String[]{SearchHistoryColumns.SEARCHSTRING}, null, null, null, null,
132 SearchHistoryColumns.TIMESEARCHED + " DESC", limit);
136 * Gets the recently searched strings
137 * @return list of esarched strings
139 public ArrayList<String> getRecentSearches() {
140 Cursor searches = queryRecentSearches(String.valueOf(MAX_ITEMS_IN_DB));
142 ArrayList<String> results = new ArrayList<String>(MAX_ITEMS_IN_DB);
145 if (searches != null && searches.moveToFirst()) {
146 int colIdx = searches.getColumnIndex(SearchHistoryColumns.SEARCHSTRING);
149 results.add(searches.getString(colIdx));
150 } while (searches.moveToNext());
153 if (searches != null) {
162 public interface SearchHistoryColumns {
164 public static final String NAME = "searchhistory";
166 /* What was searched */
167 public static final String SEARCHSTRING = "searchstring";
170 public static final String TIMESEARCHED = "timesearched";