content: /*
  * Copyright (C) 2014 The CyanogenMod Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  
  package com.wm.remusic.provider;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  
  import java.util.ArrayList;
  
  public class SearchHistory {
  
      private static final int MAX_ITEMS_IN_DB = 25;
  
      private static SearchHistory sInstance = null;
  
      private MusicDB mMusicDatabase = null;
  
      public SearchHistory(final Context context) {
          mMusicDatabase = MusicDB.getInstance(context);
      }
  
      public static final synchronized SearchHistory getInstance(final Context context) {
          if (sInstance == null) {
              sInstance = new SearchHistory(context.getApplicationContext());
          }
          return sInstance;
      }
  
      public void onCreate(final SQLiteDatabase db) {
          db.execSQL("CREATE TABLE IF NOT EXISTS " + SearchHistoryColumns.NAME + " ("
                  + SearchHistoryColumns.SEARCHSTRING + " TEXT NOT NULL,"
                  + SearchHistoryColumns.TIMESEARCHED + " LONG NOT NULL);");
      }
  
      public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
      }
  
      public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + SearchHistoryColumns.NAME);
          onCreate(db);
      }
  
      public void addSearchString(final String searchString) {
          if (searchString == null) {
              return;
          }
  
          String trimmedString = searchString.trim();
  
          if (trimmedString.isEmpty()) {
              return;
          }
  
          final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
          database.beginTransaction();
  
          try {
  
              database.delete(SearchHistoryColumns.NAME,
                      SearchHistoryColumns.SEARCHSTRING + " = ? COLLATE NOCASE",
                      new String[]{trimmedString});
  
              final ContentValues values = new ContentValues(2);
              values.put(SearchHistoryColumns.SEARCHSTRING, trimmedString);
              values.put(SearchHistoryColumns.TIMESEARCHED, System.currentTimeMillis());
              database.insert(SearchHistoryColumns.NAME, null, values);
  
              Cursor oldest = null;
              try {
                  database.query(SearchHistoryColumns.NAME,
                          new String[]{SearchHistoryColumns.TIMESEARCHED}, null, null, null, null,
                          SearchHistoryColumns.TIMESEARCHED + " ASC");
  
                  if (oldest != null && oldest.getCount() > MAX_ITEMS_IN_DB) {
                      oldest.moveToPosition(oldest.getCount() - MAX_ITEMS_IN_DB);
                      long timeOfRecordToKeep = oldest.getLong(0);
  
                      database.delete(SearchHistoryColumns.NAME,
                              SearchHistoryColumns.TIMESEARCHED + " < ?",
                              new String[]{String.valueOf(timeOfRecordToKeep)});
  
                  }
              } finally {
                  if (oldest != null) {
                      oldest.close();
                      oldest = null;
                  }
              }
          } finally {
              database.setTransactionSuccessful();
              database.endTransaction();
          }
      }
  
      public void deleteRecentSearches(String name) {
          final SQLiteDatabase database = mMusicDatabase.getReadableDatabase();
          database.delete(SearchHistoryColumns.NAME, SearchHistoryColumns.SEARCHSTRING + " = ?", new String[]{name});
      }
  
  
      public Cursor queryRecentSearches(final String limit) {
          final SQLiteDatabase database = mMusicDatabase.getReadableDatabase();
          return database.query(SearchHistoryColumns.NAME,
                  new String[]{SearchHistoryColumns.SEARCHSTRING}, null, null, null, null,
                  SearchHistoryColumns.TIMESEARCHED + " DESC", limit);
      }
  
      public ArrayList<String> getRecentSearches() {
          Cursor searches = queryRecentSearches(String.valueOf(MAX_ITEMS_IN_DB));
  
          ArrayList<String> results = new ArrayList<String>(MAX_ITEMS_IN_DB);
  
          try {
              if (searches != null && searches.moveToFirst()) {
                  int colIdx = searches.getColumnIndex(SearchHistoryColumns.SEARCHSTRING);
  
                  do {
                      results.add(searches.getString(colIdx));
                  } while (searches.moveToNext());
              }
          } finally {
              if (searches != null) {
                  searches.close();
                  searches = null;
              }
          }
  
          return results;
      }
  
      public interface SearchHistoryColumns {
          /* Table name */
          String NAME = "searchhistory";
  
          /* What was searched */
          String SEARCHSTRING = "searchstring";
  
          /* Time of search */
          String TIMESEARCHED = "timesearched";
      }
  }
  