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.tests; 17 18 import static org.hamcrest.Matchers.equalTo; 19 import static org.junit.Assume.assumeThat; 20 import static org.junit.Assume.assumeTrue; 21 22 import android.content.ContentProviderOperation; 23 import android.content.ContentProviderResult; 24 import android.content.ContentResolver; 25 import android.content.ContentValues; 26 import android.content.Context; 27 import android.content.OperationApplicationException; 28 import android.database.Cursor; 29 import android.net.Uri; 30 import android.os.RemoteException; 31 import android.support.annotation.NonNull; 32 import android.support.test.InstrumentationRegistry; 33 import android.telephony.TelephonyManager; 34 35 import com.android.contacts.database.SimContactDao; 36 import com.android.contacts.database.SimContactDaoImpl; 37 import com.android.contacts.model.SimCard; 38 import com.android.contacts.model.SimContact; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 public class SimContactsTestHelper { 44 45 private final Context mContext; 46 private final TelephonyManager mTelephonyManager; 47 private final ContentResolver mResolver; 48 private final SimContactDao mSimDao; 49 SimContactsTestHelper()50 public SimContactsTestHelper() { 51 this(InstrumentationRegistry.getTargetContext()); 52 } 53 SimContactsTestHelper(Context context)54 public SimContactsTestHelper(Context context) { 55 mContext = context; 56 mResolver = context.getContentResolver(); 57 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 58 mSimDao = SimContactDao.create(context); 59 } 60 getSimContactCount()61 public int getSimContactCount() { 62 Cursor cursor = mContext.getContentResolver().query(SimContactDaoImpl.ICC_CONTENT_URI, 63 null, null, null, null); 64 try { 65 return cursor.getCount(); 66 } finally { 67 cursor.close(); 68 } 69 } 70 addSimContact(String name, String number)71 public Uri addSimContact(String name, String number) { 72 ContentValues values = new ContentValues(); 73 // Oddly even though it's called name when querying we have to use "tag" for it to work 74 // when inserting. 75 if (name != null) { 76 values.put("tag", name); 77 } 78 if (number != null) { 79 values.put(SimContactDaoImpl.NUMBER, number); 80 } 81 return mResolver.insert(SimContactDaoImpl.ICC_CONTENT_URI, values); 82 } 83 deleteAllSimContacts()84 public ContentProviderResult[] deleteAllSimContacts() 85 throws RemoteException, OperationApplicationException { 86 final List<SimCard> sims = mSimDao.getSimCards(); 87 if (sims.isEmpty()) { 88 throw new IllegalStateException("Expected SIM card"); 89 } 90 final List<SimContact> contacts = mSimDao.loadContactsForSim(sims.get(0)); 91 ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 92 for (SimContact contact : contacts) { 93 ops.add(ContentProviderOperation 94 .newDelete(SimContactDaoImpl.ICC_CONTENT_URI) 95 .withSelection(getWriteSelection(contact), null) 96 .build()); 97 } 98 return mResolver.applyBatch(SimContactDaoImpl.ICC_CONTENT_URI.getAuthority(), ops); 99 } 100 restore(ArrayList<ContentProviderOperation> restoreOps)101 public ContentProviderResult[] restore(ArrayList<ContentProviderOperation> restoreOps) 102 throws RemoteException, OperationApplicationException { 103 if (restoreOps == null) return null; 104 105 // Remove SIM contacts because we assume that caller wants the data to be in the exact 106 // state as when the restore ops were captured. 107 deleteAllSimContacts(); 108 return mResolver.applyBatch(SimContactDaoImpl.ICC_CONTENT_URI.getAuthority(), restoreOps); 109 } 110 captureRestoreSnapshot()111 public ArrayList<ContentProviderOperation> captureRestoreSnapshot() { 112 final List<SimCard> sims = mSimDao.getSimCards(); 113 if (sims.isEmpty()) { 114 throw new IllegalStateException("Expected SIM card"); 115 } 116 final ArrayList<SimContact> contacts = mSimDao.loadContactsForSim(sims.get(0)); 117 118 final ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 119 for (SimContact contact : contacts) { 120 final String[] emails = contact.getEmails(); 121 if (emails != null && emails.length > 0) { 122 throw new IllegalStateException("Cannot restore emails." + 123 " Please manually remove SIM contacts with emails."); 124 } 125 ops.add(ContentProviderOperation 126 .newInsert(SimContactDaoImpl.ICC_CONTENT_URI) 127 .withValue("tag", contact.getName()) 128 .withValue("number", contact.getPhone()) 129 .build()); 130 } 131 return ops; 132 } 133 getWriteSelection(SimContact simContact)134 public String getWriteSelection(SimContact simContact) { 135 return "tag='" + simContact.getName() + "' AND " + SimContactDaoImpl.NUMBER + "='" + 136 simContact.getPhone() + "'"; 137 } 138 deleteSimContact(@onNull String name, @NonNull String number)139 public int deleteSimContact(@NonNull String name, @NonNull String number) { 140 // IccProvider doesn't use the selection args. 141 final String selection = "tag='" + name + "' AND " + 142 SimContactDaoImpl.NUMBER + "='" + number + "'"; 143 return mResolver.delete(SimContactDaoImpl.ICC_CONTENT_URI, selection, null); 144 } 145 isSimReady()146 public boolean isSimReady() { 147 return mTelephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY; 148 } 149 doesSimHaveContacts()150 public boolean doesSimHaveContacts() { 151 return isSimReady() && getSimContactCount() > 0; 152 } 153 isSimWritable()154 public boolean isSimWritable() { 155 if (!isSimReady()) return false; 156 final String name = "writabeProbe" + System.nanoTime(); 157 final Uri uri = addSimContact(name, "15095550101"); 158 return uri != null && deleteSimContact(name, "15095550101") == 1; 159 } 160 assumeSimReady()161 public void assumeSimReady() { 162 assumeTrue(isSimReady()); 163 } 164 assumeHasSimContacts()165 public void assumeHasSimContacts() { 166 assumeTrue(doesSimHaveContacts()); 167 } 168 assumeSimCardAbsent()169 public void assumeSimCardAbsent() { 170 assumeThat(mTelephonyManager.getSimState(), equalTo(TelephonyManager.SIM_STATE_ABSENT)); 171 } 172 173 // The emulator reports SIM_STATE_READY but writes are ignored. This verifies that the 174 // device will actually persist writes to the SIM card. assumeSimWritable()175 public void assumeSimWritable() { 176 assumeSimReady(); 177 assumeTrue(isSimWritable()); 178 } 179 } 180