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

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

DO NOT MERGE Add back Yenta experiment (1/2)

This reverts commit b7257d86.

Test: enable yenta experiement and do a few searches
Bug: 33387897
Change-Id: I34bec0c7820e8d32c53bb8ac42b24787e4dc03b5
parent f5faebf8
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.net.Uri;

import com.android.contacts.logging.Logger;
import com.android.contacts.util.DeviceLocalAccountTypeFactory;
import com.android.contactsbind.search.AutocompleteHelper;

/**
 * Creates default bindings for overlays.
@@ -30,6 +31,10 @@ public class ObjectFactory {
        return null;
    }

    public static AutocompleteHelper getAutocompleteHelper(Context context) {
        return null;
    }

    public static DeviceLocalAccountTypeFactory getDeviceLocalAccountTypeFactory(Context context) {
        return new DeviceLocalAccountTypeFactory.Default(context);
    }
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ public final class AutocompleteHelper {
        void onAutocompletesAvailable(Cursor cursor);
    }

    private AutocompleteHelper() {
    public AutocompleteHelper() {
    }

    public void setListener(Listener listener) {
+10 −0
Original line number Diff line number Diff line
@@ -49,6 +49,16 @@ public final class Experiments {
    public static final String PULL_TO_REFRESH_CANCEL_REFRESH_MILLIS =
            "PullToRefresh__cancel_refresh_millis";

    /**
     * Search study boolean indicating whether to inject yenta search results before CP2 results.
     */
    public static final String SEARCH_YENTA = "Search__yenta";

    /**
     * The time to wait for Yenta search results before giving up.
     */
    public static final String SEARCH_YENTA_TIMEOUT_MILLIS = "Search__yenta_timeout";

    private Experiments() {
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -31,9 +31,11 @@ import android.provider.ContactsContract.SearchSnippets;
import android.text.TextUtils;
import android.view.View;

import com.android.contacts.Experiments;
import com.android.contacts.compat.ContactsCompat;
import com.android.contacts.model.account.AccountWithDataSet;
import com.android.contacts.preference.ContactsPreferences;
import com.android.contactsbind.experiments.Flags;

import java.util.ArrayList;
import java.util.List;
@@ -114,6 +116,13 @@ public class DefaultContactListAdapter extends ContactListAdapter {
                loader.setUri(builder.build());
                loader.setProjection(getProjection(true));
                sortOrder = STREQUENT_SORT;
                if (Flags.getInstance().getBoolean(Experiments.SEARCH_YENTA)
                        && loader instanceof FavoritesAndContactsLoader
                        && directoryId == Directory.DEFAULT) {
                    final FavoritesAndContactsLoader favoritesAndContactsLoader =
                            (FavoritesAndContactsLoader) loader;
                    favoritesAndContactsLoader.setAutocompleteQuery(query);
                }
            }
        } else {
            final ContactListFilter filter = getFilter();
+63 −3
Original line number Diff line number Diff line
@@ -22,24 +22,38 @@ import android.database.MergeCursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;

import com.android.contacts.Experiments;
import com.android.contactsbind.ObjectFactory;
import com.android.contactsbind.experiments.Flags;
import com.android.contactsbind.search.AutocompleteHelper;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * A loader for use in the default contact list, which will also query for favorite contacts
 * if configured to do so.
 */
public class FavoritesAndContactsLoader extends CursorLoader {
public class FavoritesAndContactsLoader extends CursorLoader implements
        AutocompleteHelper.Listener {

    private boolean mLoadFavorites;

    private String[] mProjection;

    private String mAutocompleteQuery;
    private CountDownLatch mAutocompleteLatch = new CountDownLatch(1);
    private Cursor mAutocompleteCursor;
    private int mAutocompleteTimeout;

    public FavoritesAndContactsLoader(Context context) {
        super(context);
        mAutocompleteTimeout = Flags.getInstance().getInteger(
                Experiments.SEARCH_YENTA_TIMEOUT_MILLIS);
    }

    /** Whether to load favorites and merge results in before any other results. */
@@ -47,6 +61,10 @@ public class FavoritesAndContactsLoader extends CursorLoader {
        mLoadFavorites = flag;
    }

    public void setAutocompleteQuery(String autocompleteQuery) {
        mAutocompleteQuery = autocompleteQuery;
    }

    public void setProjection(String[] projection) {
        super.setProjection(projection);
        mProjection = projection;
@@ -58,8 +76,36 @@ public class FavoritesAndContactsLoader extends CursorLoader {
        if (mLoadFavorites) {
            cursors.add(loadFavoritesContacts());
        }
        final Cursor contactsCursor = loadContacts();

        if (mAutocompleteQuery != null) {
            final AutocompleteHelper autocompleteHelper =
                    ObjectFactory.getAutocompleteHelper(getContext());
            if (autocompleteHelper != null) {
                autocompleteHelper.setListener(this);
                autocompleteHelper.setProjection(mProjection);
                autocompleteHelper.setQuery(mAutocompleteQuery);
                try {
                    if (!mAutocompleteLatch.await(mAutocompleteTimeout, TimeUnit.MILLISECONDS)) {
                        logw("Timeout expired before receiving autocompletions");
                    }
                } catch (InterruptedException e) {
                    logw("Interrupted while waiting for autocompletions");
                }
                if (mAutocompleteCursor != null) {
                    cursors.add(mAutocompleteCursor);
                    // TODO: exclude these results from the main loader results, see b/30742359
                }
            }
        }

        // 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() {
@@ -93,4 +139,18 @@ public class FavoritesAndContactsLoader extends CursorLoader {
                Contacts.CONTENT_URI, mProjection, selection.toString(), new String[]{"1"},
                getSortOrder());
    }

    @Override
    public void onAutocompletesAvailable(Cursor cursor) {
        if (cursor != null && cursor.getCount() > 0) {
            mAutocompleteCursor = cursor;
            mAutocompleteLatch.countDown();
        }
    }

    private static void logw(String message) {
        if (Log.isLoggable(AutocompleteHelper.TAG, Log.WARN)) {
            Log.w(AutocompleteHelper.TAG, message);
        }
    }
}