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 package com.android.contacts.database; 17 18 import android.content.ContentProviderResult; 19 import android.content.Context; 20 import android.content.OperationApplicationException; 21 import android.os.RemoteException; 22 import androidx.annotation.VisibleForTesting; 23 24 import com.android.contacts.model.SimCard; 25 import com.android.contacts.model.SimContact; 26 import com.android.contacts.model.account.AccountWithDataSet; 27 28 import com.google.common.base.Function; 29 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Set; 35 36 /** 37 * Provides data access methods for loading contacts from a SIM card and and migrating these 38 * SIM contacts to a CP2 account. 39 */ 40 public abstract class SimContactDao { 41 42 // Set to true for manual testing on an emulator or phone without a SIM card 43 // DO NOT SUBMIT if set to true 44 private static final boolean USE_FAKE_INSTANCE = false; 45 46 public static final Function<Context, SimContactDao> DEFAULT_FACTORY = 47 new Function<Context, SimContactDao>() { 48 @Override 49 public SimContactDao apply(Context context) { 50 return USE_FAKE_INSTANCE ? 51 createDebugInstance(context) : 52 new SimContactDaoImpl(context); 53 } 54 }; 55 private static Function<? super Context, SimContactDao> sInstanceFactory = DEFAULT_FACTORY; 56 createDebugInstance(Context context)57 private static SimContactDao createDebugInstance(Context context) { 58 return new SimContactDaoImpl.DebugImpl(context) 59 .addSimCard(new SimCard("fake-sim-id1", 1, "Fake Carrier", 60 "Card 1", "15095550101", "us").withContacts( 61 new SimContact(1, "Sim One", "15095550111", null), 62 new SimContact(2, "Sim Two", "15095550112", null), 63 new SimContact(3, "Sim Three", "15095550113", null), 64 new SimContact(4, "Sim Four", "15095550114", null), 65 new SimContact(5, "411 & more", "411", null) 66 )) 67 .addSimCard(new SimCard("fake-sim-id2", 2, "Carrier Two", 68 "Card 2", "15095550102", "us").withContacts( 69 new SimContact(1, "John Sim", "15095550121", null), 70 new SimContact(2, "Bob Sim", "15095550122", null), 71 new SimContact(3, "Mary Sim", "15095550123", null), 72 new SimContact(4, "Alice Sim", "15095550124", null), 73 new SimContact(5, "Sim Duplicate", "15095550121", null) 74 )); 75 } 76 create(Context context)77 public static synchronized SimContactDao create(Context context) { 78 return sInstanceFactory.apply(context); 79 } 80 81 /** 82 * Sets the factory function used by {@link SimContactDao#create} 83 */ 84 @VisibleForTesting setFactoryForTest( Function<? super Context, SimContactDao> factory)85 public static synchronized void setFactoryForTest( 86 Function<? super Context, SimContactDao> factory) { 87 sInstanceFactory = factory; 88 } 89 canReadSimContacts()90 public abstract boolean canReadSimContacts(); 91 getSimCards()92 public abstract List<SimCard> getSimCards(); 93 loadContactsForSim(SimCard sim)94 public abstract ArrayList<SimContact> loadContactsForSim(SimCard sim); 95 importContacts(List<SimContact> contacts, AccountWithDataSet targetAccount)96 public abstract ContentProviderResult[] importContacts(List<SimContact> contacts, 97 AccountWithDataSet targetAccount) 98 throws RemoteException, OperationApplicationException; 99 persistSimStates(List<SimCard> simCards)100 public abstract void persistSimStates(List<SimCard> simCards); 101 getSimBySubscriptionId(int subscriptionId)102 public abstract SimCard getSimBySubscriptionId(int subscriptionId); 103 findAccountsOfExistingSimContacts( List<SimContact> contacts)104 public abstract Map<AccountWithDataSet, Set<SimContact>> findAccountsOfExistingSimContacts( 105 List<SimContact> contacts); 106 persistSimState(SimCard sim)107 public void persistSimState(SimCard sim) { 108 persistSimStates(Collections.singletonList(sim)); 109 } 110 } 111