OSDN Git Service

Fix dependencies of packages that target earlier releases
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / provider / SearchHistory.java
1 /*
2 * Copyright (C) 2014 The CyanogenMod 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 package com.cyanogenmod.eleven.provider;
17
18 import android.content.ContentValues;
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22
23 import java.util.ArrayList;
24
25 public class SearchHistory {
26     /* Maximum # of items in the db */
27     private static final int MAX_ITEMS_IN_DB = 25;
28
29     private static SearchHistory sInstance = null;
30
31     private MusicDB mMusicDatabase = null;
32
33     public SearchHistory(final Context context) {
34         mMusicDatabase = MusicDB.getInstance(context);
35     }
36
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);");
41     }
42
43     public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
44         // No upgrade path needed yet
45     }
46
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);
50         onCreate(db);
51     }
52
53     /**
54      * @param context The {@link android.content.Context} to use
55      * @return A new instance of this class.
56      */
57     public static final synchronized SearchHistory getInstance(final Context context) {
58         if (sInstance == null) {
59             sInstance = new SearchHistory(context.getApplicationContext());
60         }
61         return sInstance;
62     }
63
64     /**
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
68      */
69     public void addSearchString(final String searchString) {
70         if (searchString == null) {
71             return;
72         }
73
74         String trimmedString = searchString.trim();
75
76         if (trimmedString.isEmpty()) {
77             return;
78         }
79
80         final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
81         database.beginTransaction();
82
83         try {
84             // delete existing searches with the same search string
85             database.delete(SearchHistoryColumns.NAME,
86                     SearchHistoryColumns.SEARCHSTRING + " = ? COLLATE NOCASE",
87                     new String[] { trimmedString });
88
89             // add the entry
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);
94
95             // if our db is too large, delete the extra items
96             Cursor oldest = null;
97             try {
98                 database.query(SearchHistoryColumns.NAME,
99                         new String[]{SearchHistoryColumns.TIMESEARCHED}, null, null, null, null,
100                         SearchHistoryColumns.TIMESEARCHED + " ASC");
101
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);
105
106                     database.delete(SearchHistoryColumns.NAME,
107                             SearchHistoryColumns.TIMESEARCHED + " < ?",
108                             new String[] { String.valueOf(timeOfRecordToKeep) });
109
110                 }
111             } finally {
112                 if (oldest != null) {
113                     oldest.close();
114                     oldest = null;
115                 }
116             }
117         } finally {
118             database.setTransactionSuccessful();
119             database.endTransaction();
120         }
121     }
122
123     /**
124      * Gets a cursor to the list of recently searched strings
125      * @param limit number of items to limit to
126      * @return cursor
127      */
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);
133     }
134
135     /**
136      * Gets the recently searched strings
137      * @return list of esarched strings
138      */
139     public ArrayList<String> getRecentSearches() {
140         Cursor searches = queryRecentSearches(String.valueOf(MAX_ITEMS_IN_DB));
141
142         ArrayList<String> results = new ArrayList<String>(MAX_ITEMS_IN_DB);
143
144         try {
145             if (searches != null && searches.moveToFirst()) {
146                 int colIdx = searches.getColumnIndex(SearchHistoryColumns.SEARCHSTRING);
147
148                 do {
149                     results.add(searches.getString(colIdx));
150                 } while (searches.moveToNext());
151             }
152         } finally {
153             if (searches != null) {
154                 searches.close();
155                 searches = null;
156             }
157         }
158
159         return results;
160     }
161
162     public interface SearchHistoryColumns {
163         /* Table name */
164         public static final String NAME = "searchhistory";
165
166         /* What was searched */
167         public static final String SEARCHSTRING = "searchstring";
168
169         /* Time of search */
170         public static final String TIMESEARCHED = "timesearched";
171     }
172 }