Example #1
0
    @Override
    public Cursor loadCursor() {
      final ContentResolver cr = getContext().getContentResolver();

      final String selection;
      final String[] selectionArgs;

      // Null represents the root filter
      if (mRequest.getFilter() == null) {
        selection =
            DBUtils.concatenateWhere(HomeItems.DATASET_ID + " = ?", HomeItems.FILTER + " IS NULL");
        selectionArgs = new String[] {mRequest.getDatasetId()};
      } else {
        selection =
            DBUtils.concatenateWhere(HomeItems.DATASET_ID + " = ?", HomeItems.FILTER + " = ?");
        selectionArgs = new String[] {mRequest.getDatasetId(), mRequest.getFilter()};
      }

      // XXX: You can use CONTENT_FAKE_URI for development to pull items from fake_home_items.json.
      final Cursor c = cr.query(HomeItems.CONTENT_URI, null, selection, selectionArgs, null);

      // SQLiteBridgeContentProvider may return a null Cursor if the database hasn't been created
      // yet.
      if (c != null) {
        final Uri notificationUri = getDatasetNotificationUri(mRequest.getDatasetId());
        c.setNotificationUri(cr, notificationUri);
      }

      return c;
    }
  private Cursor filterAllSites(
      ContentResolver cr,
      String[] projection,
      CharSequence constraint,
      int limit,
      CharSequence urlFilter) {
    String selection = "";
    String[] selectionArgs = null;

    // The combined history/bookmarks selection queries for sites with a url or title containing
    // the constraint string(s), treating space-separated words as separate constraints
    String[] constraintWords = constraint.toString().split(" ");
    for (int i = 0; i < constraintWords.length; i++) {
      selection =
          DBUtils.concatenateWhere(
              selection, "(" + Combined.URL + " LIKE ? OR " + Combined.TITLE + " LIKE ?)");
      String constraintWord = "%" + constraintWords[i] + "%";
      selectionArgs =
          DBUtils.appendSelectionArgs(selectionArgs, new String[] {constraintWord, constraintWord});
    }

    if (urlFilter != null) {
      selection = DBUtils.concatenateWhere(selection, "(" + Combined.URL + " NOT LIKE ?)");
      selectionArgs =
          DBUtils.appendSelectionArgs(selectionArgs, new String[] {urlFilter.toString()});
    }

    // Our version of frecency is computed by scaling the number of visits by a multiplier
    // that approximates Gaussian decay, based on how long ago the entry was last visited.
    // Since we're limited by the math we can do with sqlite, we're calculating this
    // approximation using the Cauchy distribution: multiplier = 15^2 / (age^2 + 15^2).
    // Using 15 as our scale parameter, we get a constant 15^2 = 225. Following this math,
    // frecencyScore = numVisits * max(1, 100 * 225 / (age*age + 225)). (See bug 704977)
    // We also give bookmarks an extra bonus boost by adding 100 points to their frecency score.
    final String age =
        "(" + Combined.DATE_LAST_VISITED + " - " + System.currentTimeMillis() + ") / 86400000";
    final String sortOrder =
        "(CASE WHEN "
            + Combined.BOOKMARK_ID
            + " > -1 THEN 100 ELSE 0 END) + "
            + Combined.VISITS
            + " * MAX(1, 100 * 225 / ("
            + age
            + "*"
            + age
            + " + 225)) DESC";

    Cursor c =
        cr.query(combinedUriWithLimit(limit), projection, selection, selectionArgs, sortOrder);

    return new LocalDBCursor(c);
  }