1 /* 2 * Copyright (C) 2017 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.preferredsim.suggestion; 18 19 import android.content.Context; 20 import android.support.annotation.NonNull; 21 import android.support.annotation.Nullable; 22 import android.support.annotation.WorkerThread; 23 import android.telecom.PhoneAccountHandle; 24 import com.android.dialer.common.Assert; 25 import com.android.dialer.common.LogUtil; 26 import com.google.common.base.Optional; 27 28 /** Provides hints to the user when selecting a SIM to make a call. */ 29 @SuppressWarnings("Guava") 30 public interface SuggestionProvider { 31 32 String EXTRA_SIM_SUGGESTION_REASON = "sim_suggestion_reason"; 33 34 /** The reason the suggestion is made. */ 35 enum Reason { 36 UNKNOWN, 37 // The SIM has the same carrier as the callee. 38 INTRA_CARRIER, 39 // The user has selected the SIM for the callee multiple times. 40 FREQUENT, 41 // The user has select the SIM for this category of calls (contacts from certain accounts, 42 // etc.). 43 USER_SET, 44 // The user has selected the SIM for all contacts on the account. 45 ACCOUNT, 46 // Unspecified reason. 47 OTHER, 48 } 49 50 /** The suggestion. */ 51 class Suggestion { 52 @NonNull public final PhoneAccountHandle phoneAccountHandle; 53 @NonNull public final Reason reason; 54 public final boolean shouldAutoSelect; 55 Suggestion( @onNull PhoneAccountHandle phoneAccountHandle, @NonNull Reason reason, boolean shouldAutoSelect)56 public Suggestion( 57 @NonNull PhoneAccountHandle phoneAccountHandle, 58 @NonNull Reason reason, 59 boolean shouldAutoSelect) { 60 this.phoneAccountHandle = Assert.isNotNull(phoneAccountHandle); 61 this.reason = Assert.isNotNull(reason); 62 this.shouldAutoSelect = shouldAutoSelect; 63 } 64 } 65 66 @WorkerThread 67 @NonNull getSuggestion(@onNull Context context, @NonNull String number)68 Optional<Suggestion> getSuggestion(@NonNull Context context, @NonNull String number); 69 70 @WorkerThread reportUserSelection( @onNull Context context, @NonNull String number, @NonNull PhoneAccountHandle phoneAccountHandle, boolean rememberSelection)71 void reportUserSelection( 72 @NonNull Context context, 73 @NonNull String number, 74 @NonNull PhoneAccountHandle phoneAccountHandle, 75 boolean rememberSelection); 76 77 @WorkerThread reportIncorrectSuggestion( @onNull Context context, @NonNull String number, @NonNull PhoneAccountHandle newAccount)78 void reportIncorrectSuggestion( 79 @NonNull Context context, @NonNull String number, @NonNull PhoneAccountHandle newAccount); 80 81 /** 82 * Return the hint for {@code phoneAccountHandle}. Absent if no hint is available for the account. 83 */ getHint( Context context, PhoneAccountHandle phoneAccountHandle, @Nullable Suggestion suggestion)84 static Optional<String> getHint( 85 Context context, PhoneAccountHandle phoneAccountHandle, @Nullable Suggestion suggestion) { 86 if (suggestion == null) { 87 return Optional.absent(); 88 } 89 if (!phoneAccountHandle.equals(suggestion.phoneAccountHandle)) { 90 return Optional.absent(); 91 } 92 switch (suggestion.reason) { 93 case INTRA_CARRIER: 94 return Optional.of( 95 context.getString(R.string.pre_call_select_phone_account_hint_intra_carrier)); 96 case FREQUENT: 97 return Optional.of(context.getString(R.string.pre_call_select_phone_account_hint_frequent)); 98 default: 99 LogUtil.w("CallingAccountSelector.getHint", "unhandled reason " + suggestion.reason); 100 return Optional.absent(); 101 } 102 } 103 } 104