Enabled recent queries for search suggestions in calendar search

 - Also provided a way for users to clear search history from settings

Change-Id: I1ad366021a0cb32af0260f31bd3d42d36aeabfb1
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 0e38c33..80e51b0 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -64,10 +64,6 @@
             </intent-filter>
         </activity-alias>
 
-        <!-- Make all activities a searchable context -->
-        <meta-data android:name="android.app.default_searchable"
-            android:value="com.android.calendar.SearchActivity"/>
-
         <activity android:name="EditEventActivity" android:label="@string/event_edit_title"
             android:theme="@android:style/Theme"
             android:configChanges="orientation|keyboardHidden">
@@ -108,6 +104,11 @@
         <activity android:name="SelectCalendarsActivity" android:label="@string/calendars_title" />
         <activity android:name="CalendarPreferenceActivity" android:label="@string/preferences_title" />
 
+        <!-- Declarations for search -->
+        <!-- Make all activities a searchable context -->
+        <meta-data android:name="android.app.default_searchable"
+            android:value="com.android.calendar.SearchActivity"/>
+
         <activity android:name="SearchActivity" android:label="@string/search_title"
             android:launchMode="singleTop" android:theme="@style/CalendarTheme.WithActionBar">
             <intent-filter>
@@ -116,6 +117,9 @@
             <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
         </activity>
 
+        <provider android:name=".CalendarRecentSuggestionsProvider"
+            android:authorities="com.android.calendar.CalendarRecentSuggestionsProvider" />
+
         <!-- Declarations for alerts/reminders -->
         <activity android:name=".alerts.AlertActivity" android:launchMode="singleInstance"
              android:theme="@android:style/Theme.Light" android:excludeFromRecents="true" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9114a03..f219b43 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -372,6 +372,12 @@
     <string name="preferences_week_start_day_dialog">Week starts on</string>
     <!-- DO NOT TRANSLATE -->
     <string name="preferences_week_start_day_default">-1</string>
+    <!-- Title of the settings item to clear the recent search history [CHAR LIMIT=40] -->
+    <string name="preferences_clear_search_history_title">Clear search history</string>
+    <!-- Summary of the settings item to clear the recent search history [CHAR LIMIT=70]-->
+    <string name="preferences_clear_search_history_summary">Remove all the searches you have performed</string>
+    <!-- Message to show in a toast when the history is cleared [CHAR LIMIT=40] -->
+    <string name="search_history_cleared">Search history cleared</string>
     <!-- Settings check box label to enable or disable notifications -->
     <string name="preferences_alerts_title">Notifications</string>
     <!-- Settings dialog label that specifies when the phone should vibrate -->
diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml
index 6ff8a58..04bc69d 100644
--- a/res/xml/preferences.xml
+++ b/res/xml/preferences.xml
@@ -27,6 +27,10 @@
             android:entries="@array/preferences_week_start_day_labels"
             android:entryValues="@array/preferences_week_start_day_values"
             android:dialogTitle="@string/preferences_week_start_day_dialog" />
+        <PreferenceScreen
+            android:key="preferences_clear_search_history"
+            android:title="@string/preferences_clear_search_history_title"
+            android:summary="@string/preferences_clear_search_history_summary" />
     </PreferenceCategory>
 
     <PreferenceCategory android:title="@string/preferences_reminder_title">
diff --git a/res/xml/searchable.xml b/res/xml/searchable.xml
index 3056e49..814dae1 100644
--- a/res/xml/searchable.xml
+++ b/res/xml/searchable.xml
@@ -15,5 +15,7 @@
 -->
 
 <searchable xmlns:android="http://schemas.android.com/apk/res/android"
-    android:label="@string/app_label" >
+    android:label="@string/app_label"
+    android:searchSuggestAuthority="com.android.calendar.CalendarRecentSuggestionsProvider"
+    android:searchSuggestSelection=" ?" >
 </searchable>
\ No newline at end of file
diff --git a/src/com/android/calendar/CalendarPreferenceActivity.java b/src/com/android/calendar/CalendarPreferenceActivity.java
index 5b7843f..f4b8cbd 100644
--- a/src/com/android/calendar/CalendarPreferenceActivity.java
+++ b/src/com/android/calendar/CalendarPreferenceActivity.java
@@ -24,10 +24,13 @@
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
 import android.preference.ListPreference;
+import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceManager;
 import android.preference.PreferenceScreen;
 import android.preference.RingtonePreference;
+import android.provider.SearchRecentSuggestions;
+import android.widget.Toast;
 
 public class CalendarPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
     private static final String BUILD_VERSION = "build_version";
@@ -40,6 +43,8 @@
     public static final String KEY_HIDE_DECLINED = "preferences_hide_declined";
     public static final String KEY_WEEK_START_DAY = "preferences_week_start_day";
 
+    public static final String KEY_CLEAR_SEARCH_HISTORY = "preferences_clear_search_history";
+
     public static final String KEY_ALERTS = "preferences_alerts";
     public static final String KEY_ALERTS_VIBRATE = "preferences_alerts_vibrate";
     public static final String KEY_ALERTS_VIBRATE_WHEN = "preferences_alerts_vibrateWhen";
@@ -179,4 +184,22 @@
             mPopup.setEnabled(false);
         }
     }
+
+
+    @Override @SuppressWarnings("deprecation")
+    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
+        String key = preference.getKey();
+        if (key.equals(KEY_CLEAR_SEARCH_HISTORY)) {
+            SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
+                    CalendarRecentSuggestionsProvider.AUTHORITY,
+                    CalendarRecentSuggestionsProvider.MODE);
+            suggestions.clearHistory();
+            Toast.makeText(this, R.string.search_history_cleared,
+                    Toast.LENGTH_SHORT).show();
+            return true;
+        }
+        return false;
+    }
+
+
 }
diff --git a/src/com/android/calendar/CalendarRecentSuggestionsProvider.java b/src/com/android/calendar/CalendarRecentSuggestionsProvider.java
new file mode 100644
index 0000000..deb66d8
--- /dev/null
+++ b/src/com/android/calendar/CalendarRecentSuggestionsProvider.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2010 The Android Open Source 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.android.calendar;
+
+import android.content.SearchRecentSuggestionsProvider;
+
+public class CalendarRecentSuggestionsProvider extends SearchRecentSuggestionsProvider {
+
+    public final static String AUTHORITY = "com.android.calendar.CalendarRecentSuggestionsProvider";
+
+    public final static int MODE = DATABASE_MODE_QUERIES;
+
+    public CalendarRecentSuggestionsProvider() {
+        setupSuggestions(AUTHORITY, MODE);
+    }
+
+}
diff --git a/src/com/android/calendar/SearchActivity.java b/src/com/android/calendar/SearchActivity.java
index 0fdfca2..1fa4a18 100644
--- a/src/com/android/calendar/SearchActivity.java
+++ b/src/com/android/calendar/SearchActivity.java
@@ -44,6 +44,7 @@
 import android.os.Handler;
 import android.provider.Calendar;
 import android.provider.Calendar.Events;
+import android.provider.SearchRecentSuggestions;
 import android.text.format.Time;
 import android.util.Log;
 import android.view.KeyEvent;
@@ -182,6 +183,13 @@
     }
 
     private void search(String searchQuery, Time goToTime) {
+        // save query in recent queries
+        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
+                CalendarRecentSuggestionsProvider.AUTHORITY,
+                CalendarRecentSuggestionsProvider.MODE);
+        suggestions.saveRecentQuery(searchQuery, null);
+
+
         EventInfo searchEventInfo = new EventInfo();
         searchEventInfo.eventType = EventType.SEARCH;
         searchEventInfo.query = searchQuery;