Home | History | Annotate | Download | only in inference
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.dialer.p13n.inference;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.support.annotation.MainThread;
     22 import android.support.annotation.NonNull;
     23 import android.support.annotation.Nullable;
     24 import com.android.dialer.common.Assert;
     25 import com.android.dialer.common.ConfigProviderBindings;
     26 import com.android.dialer.p13n.inference.protocol.P13nRanker;
     27 import com.android.dialer.p13n.inference.protocol.P13nRankerFactory;
     28 import java.util.List;
     29 
     30 /** Single entry point for all personalized ranking. */
     31 public final class P13nRanking {
     32 
     33   private static P13nRanker ranker;
     34 
     35   private P13nRanking() {}
     36 
     37   @MainThread
     38   @NonNull
     39   public static P13nRanker get(@NonNull Context context) {
     40     Assert.isNotNull(context);
     41     Assert.isMainThread();
     42 
     43     if (ranker != null) {
     44       return ranker;
     45     }
     46 
     47     if (!ConfigProviderBindings.get(context).getBoolean("p13n_ranker_should_enable", false)) {
     48       setToIdentityRanker();
     49       return ranker;
     50     }
     51 
     52     Context application = context.getApplicationContext();
     53     if (application instanceof P13nRankerFactory) {
     54       ranker = ((P13nRankerFactory) application).newP13nRanker();
     55     }
     56 
     57     if (ranker == null) {
     58       setToIdentityRanker();
     59     }
     60     return ranker;
     61   }
     62 
     63   private static void setToIdentityRanker() {
     64     ranker =
     65         new P13nRanker() {
     66           @Override
     67           public void refresh(@Nullable P13nRefreshCompleteListener listener) {}
     68 
     69           @Override
     70           public List<String> rankList(List<String> phoneNumbers) {
     71             return phoneNumbers;
     72           }
     73 
     74           @NonNull
     75           @Override
     76           public Cursor rankCursor(@NonNull Cursor phoneQueryResults, int queryLength) {
     77             return phoneQueryResults;
     78           }
     79 
     80           @Override
     81           public boolean shouldShowEmptyListForNullQuery() {
     82             return true;
     83           }
     84         };
     85   }
     86 
     87   public static void setForTesting(@NonNull P13nRanker ranker) {
     88     P13nRanking.ranker = ranker;
     89   }
     90 }
     91