blob: d6ed88d28fd18ed3087742fc03c37c7dbf9907d7 [file] [log] [blame]
Marcus Hagerott819214d2016-09-29 14:58:27 -07001/*
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 Mai69c182a2016-12-05 13:07:03 -080016package com.android.contacts.database;
Marcus Hagerott819214d2016-09-29 14:58:27 -070017
Marcus Hagerott819214d2016-09-29 14:58:27 -070018import android.content.ContentProviderResult;
Marcus Hagerott819214d2016-09-29 14:58:27 -070019import android.content.Context;
20import android.content.OperationApplicationException;
Marcus Hagerott819214d2016-09-29 14:58:27 -070021import android.os.RemoteException;
Marcus Hagerotta7d3c002016-12-05 15:56:10 -080022import android.support.annotation.VisibleForTesting;
Marcus Hagerott819214d2016-09-29 14:58:27 -070023
Gary Mai69c182a2016-12-05 13:07:03 -080024import com.android.contacts.model.SimCard;
25import com.android.contacts.model.SimContact;
26import com.android.contacts.model.account.AccountWithDataSet;
Gary Mai0a49afa2016-12-05 15:53:58 -080027
Marcus Hagerotta7d3c002016-12-05 15:56:10 -080028import com.google.common.base.Function;
Marcus Hagerott819214d2016-09-29 14:58:27 -070029
30import java.util.ArrayList;
John Shao3ed3af22016-10-18 18:31:29 -070031import java.util.Collections;
Marcus Hagerott819214d2016-09-29 14:58:27 -070032import java.util.List;
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -070033import java.util.Map;
34import java.util.Set;
Marcus Hagerott819214d2016-09-29 14:58:27 -070035
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 Hagerott2bb49842016-11-15 18:26:20 -080040public abstract class SimContactDao {
Marcus Hagerott7333c372016-11-07 09:40:20 -080041
Marcus Hagerott66e8b222016-10-23 15:41:55 -070042 // 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 Hagerotta7d3c002016-12-05 15:56:10 -080046 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 Hagerott66e8b222016-10-23 15:41:55 -070088 }
89
Marcus Hagerott2bb49842016-11-15 18:26:20 -080090 public abstract boolean canReadSimContacts();
Marcus Hagerott66e8b222016-10-23 15:41:55 -070091
Marcus Hagerott2bb49842016-11-15 18:26:20 -080092 public abstract List<SimCard> getSimCards();
Marcus Hagerott66e8b222016-10-23 15:41:55 -070093
Marcus Hagerott2bb49842016-11-15 18:26:20 -080094 public abstract ArrayList<SimContact> loadContactsForSim(SimCard sim);
Marcus Hagerott66e8b222016-10-23 15:41:55 -070095
Marcus Hagerott2bb49842016-11-15 18:26:20 -080096 public abstract ContentProviderResult[] importContacts(List<SimContact> contacts,
97 AccountWithDataSet targetAccount)
98 throws RemoteException, OperationApplicationException;
Marcus Hagerott66e8b222016-10-23 15:41:55 -070099
Marcus Hagerott2bb49842016-11-15 18:26:20 -0800100 public abstract void persistSimStates(List<SimCard> simCards);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700101
Marcus Hagerott2bb49842016-11-15 18:26:20 -0800102 public abstract SimCard getSimBySubscriptionId(int subscriptionId);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700103
Marcus Hagerott2bb49842016-11-15 18:26:20 -0800104 public abstract Map<AccountWithDataSet, Set<SimContact>> findAccountsOfExistingSimContacts(
105 List<SimContact> contacts);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700106
Marcus Hagerott2bb49842016-11-15 18:26:20 -0800107 public void persistSimState(SimCard sim) {
108 persistSimStates(Collections.singletonList(sim));
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700109 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700110}