Adds client id URL filtering to the provider

 Bug: 3270662
 Moves the client id URL filtering into the provider so that it is
 applied to all history queries, updates, and inserts. We do this
 because we do not want the client id in the history.

Change-Id: Ifb77debcb1c2102bd72701910bfbf07ed23c45ef
diff --git a/src/com/android/browser/provider/BrowserProvider2.java b/src/com/android/browser/provider/BrowserProvider2.java
index 8d9f1fe..0f51695 100644
--- a/src/com/android/browser/provider/BrowserProvider2.java
+++ b/src/com/android/browser/provider/BrowserProvider2.java
@@ -697,6 +697,7 @@
                 // fall through
             }
             case HISTORY: {
+                filterSearchClient(selectionArgs);
                 if (sortOrder == null) {
                     sortOrder = DEFAULT_SORT_HISTORY;
                 }
@@ -803,6 +804,7 @@
                 // fall through
             }
             case HISTORY: {
+                filterSearchClient(selectionArgs);
                 return db.delete(TABLE_HISTORY, selection, selectionArgs);
             }
 
@@ -877,6 +879,9 @@
                 if (!values.containsKey(History.DATE_CREATED)) {
                     values.put(History.DATE_CREATED, System.currentTimeMillis());
                 }
+                String url = values.getAsString(History.URL);
+                url = filterSearchClient(url);
+                values.put(History.URL, url);
 
                 // Extract out the image values so they can be inserted into the images table
                 ContentValues imageValues = extractImageValues(values,
@@ -917,6 +922,32 @@
         }
     }
 
+    private void filterSearchClient(String[] selectionArgs) {
+        if (selectionArgs != null) {
+            for (int i = 0; i < selectionArgs.length; i++) {
+                selectionArgs[i] = filterSearchClient(selectionArgs[i]);
+            }
+        }
+    }
+
+    // Filters out the client=ms- param for search urls
+    private String filterSearchClient(String url) {
+        // remove "client" before updating it to the history so that it wont
+        // show up in the auto-complete list.
+        int index = url.indexOf("client=ms-");
+        if (index > 0 && url.contains(".google.")) {
+            int end = url.indexOf('&', index);
+            if (end > 0) {
+                url = url.substring(0, index)
+                        .concat(url.substring(end + 1));
+            } else {
+                // the url.charAt(index-1) should be either '?' or '&'
+                url = url.substring(0, index-1);
+            }
+        }
+        return url;
+    }
+
     /**
      * Searches are unique, so perform an UPSERT manually since SQLite doesn't support them.
      */
@@ -1086,6 +1117,7 @@
     int updateHistoryInTransaction(ContentValues values, String selection, String[] selectionArgs) {
         int count = 0;
         final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+        filterSearchClient(selectionArgs);
         Cursor cursor = query(History.CONTENT_URI,
                 new String[] { History._ID, History.URL },
                 selection, selectionArgs, null);
@@ -1095,7 +1127,8 @@
             boolean updatingUrl = values.containsKey(History.URL);
             String url = null;
             if (updatingUrl) {
-                url = values.getAsString(History.URL);
+                url = filterSearchClient(values.getAsString(History.URL));
+                values.put(History.URL, url);
             }
             ContentValues imageValues = extractImageValues(values, url);