1 /* 2 * Copyright (C) 2013 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.app.calllog.calllogcache; 18 19 import android.content.Context; 20 import android.support.annotation.VisibleForTesting; 21 import android.telecom.PhoneAccountHandle; 22 import android.text.TextUtils; 23 import android.util.ArrayMap; 24 import android.util.Pair; 25 import com.android.dialer.calllogutils.PhoneAccountUtils; 26 import com.android.dialer.phonenumberutil.PhoneNumberHelper; 27 import java.util.Map; 28 import java.util.concurrent.ConcurrentHashMap; 29 30 /** 31 * This is the CallLogCache for versions of dialer Lollipop Mr1 and above with support for multi-SIM 32 * devices. 33 * 34 * <p>This class should not be initialized directly and instead be acquired from {@link 35 * CallLogCache#getCallLogCache}. 36 */ 37 class CallLogCacheLollipopMr1 extends CallLogCache { 38 39 /* 40 * Maps from a phone-account/number pair to a boolean because multiple numbers could return true 41 * for the voicemail number if those numbers are not pre-normalized. Access must be synchronzied 42 * as it's used in the background thread in CallLogAdapter. {@see CallLogAdapter#loadData} 43 */ 44 @VisibleForTesting 45 final Map<Pair<PhoneAccountHandle, CharSequence>, Boolean> mVoicemailQueryCache = 46 new ConcurrentHashMap<>(); 47 48 private final Map<PhoneAccountHandle, String> mPhoneAccountLabelCache = new ArrayMap<>(); 49 private final Map<PhoneAccountHandle, Integer> mPhoneAccountColorCache = new ArrayMap<>(); 50 private final Map<PhoneAccountHandle, Boolean> mPhoneAccountCallWithNoteCache = new ArrayMap<>(); 51 52 /* package */ CallLogCacheLollipopMr1(Context context) { 53 super(context); 54 } 55 56 @Override 57 public void reset() { 58 mVoicemailQueryCache.clear(); 59 mPhoneAccountLabelCache.clear(); 60 mPhoneAccountColorCache.clear(); 61 mPhoneAccountCallWithNoteCache.clear(); 62 63 super.reset(); 64 } 65 66 @Override 67 public boolean isVoicemailNumber(PhoneAccountHandle accountHandle, CharSequence number) { 68 if (TextUtils.isEmpty(number)) { 69 return false; 70 } 71 72 Pair<PhoneAccountHandle, CharSequence> key = new Pair<>(accountHandle, number); 73 Boolean value = mVoicemailQueryCache.get(key); 74 if (value != null) { 75 return value; 76 } 77 boolean isVoicemail = 78 PhoneNumberHelper.isVoicemailNumber(mContext, accountHandle, number.toString()); 79 mVoicemailQueryCache.put(key, isVoicemail); 80 return isVoicemail; 81 } 82 83 @Override 84 public String getAccountLabel(PhoneAccountHandle accountHandle) { 85 if (mPhoneAccountLabelCache.containsKey(accountHandle)) { 86 return mPhoneAccountLabelCache.get(accountHandle); 87 } else { 88 String label = PhoneAccountUtils.getAccountLabel(mContext, accountHandle); 89 mPhoneAccountLabelCache.put(accountHandle, label); 90 return label; 91 } 92 } 93 94 @Override 95 public int getAccountColor(PhoneAccountHandle accountHandle) { 96 if (mPhoneAccountColorCache.containsKey(accountHandle)) { 97 return mPhoneAccountColorCache.get(accountHandle); 98 } else { 99 Integer color = PhoneAccountUtils.getAccountColor(mContext, accountHandle); 100 mPhoneAccountColorCache.put(accountHandle, color); 101 return color; 102 } 103 } 104 105 @Override 106 public boolean doesAccountSupportCallSubject(PhoneAccountHandle accountHandle) { 107 if (mPhoneAccountCallWithNoteCache.containsKey(accountHandle)) { 108 return mPhoneAccountCallWithNoteCache.get(accountHandle); 109 } else { 110 Boolean supportsCallWithNote = 111 PhoneAccountUtils.getAccountSupportsCallSubject(mContext, accountHandle); 112 mPhoneAccountCallWithNoteCache.put(accountHandle, supportsCallWithNote); 113 return supportsCallWithNote; 114 } 115 } 116 } 117