Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b064f94b authored by Walter Jang's avatar Walter Jang
Browse files

Fallback to CP2 when we don't get anything from yenta

Test: see I553a9b3f7056aa24943db222f8450d5e5a6a4932
Bug: 30436991
Change-Id: Ia9fa55e274cc8e95b0e4d8d88f7a0859cc6d4231
parent c8285ce6
Loading
Loading
Loading
Loading
+25 −12
Original line number Diff line number Diff line
@@ -47,6 +47,9 @@ public class FavoritesAndContactsLoader extends CursorLoader implements Autocomp
    private CountDownLatch mAutocompleteLatch = new CountDownLatch(1);
    private Cursor mAutocompleteCursor;
    private int mAutocompleteTimeout;
    // If we didn't get anything back from autocomplete and we've fallen back to CP2,
    // we can't wait for the Experiments.SEARCH_YENTA_TIMEOUT_MILLIS everytime the query changes.
    private boolean mAutocompleteFallback;

    public FavoritesAndContactsLoader(Context context) {
        super(context);
@@ -71,14 +74,27 @@ public class FavoritesAndContactsLoader extends CursorLoader implements Autocomp
    @Override
    public Cursor loadInBackground() {
        List<Cursor> cursors = Lists.newArrayList();

        // Load favorites
        if (mLoadFavorites) {
            cursors.add(loadFavoritesContacts());
        }

        if (mAutocompleteQuery != null) {
        // Load contacts
        final Cursor contactsCursor;
        if (mAutocompleteQuery == null || mAutocompleteFallback) {
            // Query CP2 normally
            contactsCursor = loadContacts();
            cursors.add(contactsCursor);
        } else {
            final AutocompleteHelper autocompleteHelper =
                    ObjectFactory.getAutocompleteHelper(getContext());
            if (autocompleteHelper != null) {
            if (autocompleteHelper == null) {
                // Fallback to CP2, the flag is on but we couldn't instantiate autocomplete
                contactsCursor = loadContacts();
                cursors.add(contactsCursor);
                mAutocompleteFallback = true;
            } else {
                autocompleteHelper.setListener(this);
                autocompleteHelper.setProjection(mProjection);
                autocompleteHelper.setQuery(mAutocompleteQuery);
@@ -89,21 +105,18 @@ public class FavoritesAndContactsLoader extends CursorLoader implements Autocomp
                } catch (InterruptedException e) {
                    logw("Interrupted while waiting for autocompletions");
                }
                if (mAutocompleteCursor != null) {
                if (mAutocompleteCursor != null && mAutocompleteCursor.getCount() > 0) {
                    contactsCursor = null;
                    cursors.add(mAutocompleteCursor);
                    // TODO: exclude these results from the main loader results, see b/30742359
                } else {
                    // Fallback to CP2, we didn't get anything back from autocomplete
                    contactsCursor = loadContacts();
                    cursors.add(loadContacts());
                    mAutocompleteFallback = true;
                }
            }
        }

        // TODO: if the autocomplete experiment in on, only show those results even if they're empty
        final Cursor contactsCursor = mAutocompleteQuery == null ? loadContacts() : null;
        if (mAutocompleteQuery == null) {
            cursors.add(contactsCursor);
        }
        // Guard against passing an empty array to the MergeCursor constructor
        if (cursors.isEmpty()) cursors.add(null);

        return new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) {
            @Override
            public Bundle getExtras() {