Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 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 | */ |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | package com.android.contacts.database; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 17 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 18 | import android.content.ContentProviderResult; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.OperationApplicationException; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 22 | import android.support.annotation.VisibleForTesting; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 23 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 24 | import com.android.contacts.model.SimCard; |
| 25 | import com.android.contacts.model.SimContact; |
| 26 | import com.android.contacts.model.account.AccountWithDataSet; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 27 | |
Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 28 | import com.google.common.base.Function; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.ArrayList; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 31 | import java.util.Collections; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 32 | import java.util.List; |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 33 | import java.util.Map; |
| 34 | import java.util.Set; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 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 | */ |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 40 | public abstract class SimContactDao { |
Marcus Hagerott | 7333c37 | 2016-11-07 09:40:20 -0800 | [diff] [blame] | 41 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 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 | |
Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 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 |
| 85 | public static synchronized void setFactoryForTest( |
| 86 | Function<? super Context, SimContactDao> factory) { |
| 87 | sInstanceFactory = factory; |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 90 | public abstract boolean canReadSimContacts(); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 91 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 92 | public abstract List<SimCard> getSimCards(); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 93 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 94 | public abstract ArrayList<SimContact> loadContactsForSim(SimCard sim); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 95 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 96 | public abstract ContentProviderResult[] importContacts(List<SimContact> contacts, |
| 97 | AccountWithDataSet targetAccount) |
| 98 | throws RemoteException, OperationApplicationException; |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 99 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 100 | public abstract void persistSimStates(List<SimCard> simCards); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 101 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 102 | public abstract SimCard getSimBySubscriptionId(int subscriptionId); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 103 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 104 | public abstract Map<AccountWithDataSet, Set<SimContact>> findAccountsOfExistingSimContacts( |
| 105 | List<SimContact> contacts); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 106 | |
Marcus Hagerott | 2bb4984 | 2016-11-15 18:26:20 -0800 | [diff] [blame] | 107 | public void persistSimState(SimCard sim) { |
| 108 | persistSimStates(Collections.singletonList(sim)); |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 109 | } |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 110 | } |