1 /*
2  * Copyright (C) 2020 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 android.provider.cts.simphonebook;
18 
19 import android.Manifest;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.database.Cursor;
23 import android.os.Bundle;
24 import android.provider.SimPhonebookContract;
25 import android.provider.SimPhonebookContract.ElementaryFiles;
26 import android.telephony.SubscriptionInfo;
27 import android.util.Log;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import com.android.compatibility.common.util.SystemUtil;
32 
33 import org.junit.rules.ExternalResource;
34 
35 import java.util.List;
36 import java.util.Objects;
37 
38 /** Removes all records from all the removalbe SIM cards after tests have run. */
39 class SimsCleanupRule extends ExternalResource {
40     private static final String TAG = SimsCleanupRule.class.getSimpleName();
41     private final ContentResolver mResolver;
42     private final int mEfType;
43     private final Bundle mExtras = new Bundle();
44 
SimsCleanupRule(int efType)45     SimsCleanupRule(int efType) {
46         Context context = ApplicationProvider.getApplicationContext();
47         mResolver = context.getContentResolver();
48         mEfType = efType;
49     }
50 
setPin2(String pin2)51     void setPin2(String pin2) {
52         mExtras.putString(SimPhonebookContract.SimRecords.QUERY_ARG_PIN2, pin2);
53     }
54 
55     @Override
after()56     protected void after() {
57         RemovableSims removableSims = new RemovableSims(
58                 ApplicationProvider.getApplicationContext());
59         List<SubscriptionInfo> infos = removableSims.getSubscriptionInfoForRemovableSims();
60         for (SubscriptionInfo info : infos) {
61             if (mEfType == ElementaryFiles.EF_FDN) {
62                 clearFdn(info.getSubscriptionId());
63             } else {
64                 clearEf(info.getSubscriptionId());
65             }
66         }
67     }
68 
clearFdn(int subscriptionId)69     private void clearFdn(int subscriptionId) {
70         SystemUtil.runWithShellPermissionIdentity(() -> clearEf(subscriptionId),
71                 Manifest.permission.MODIFY_PHONE_STATE);
72     }
73 
clearEf(int subscriptionId)74     private void clearEf(int subscriptionId) {
75         try (Cursor cursor = Objects.requireNonNull(mResolver.query(
76                 SimPhonebookContract.SimRecords.getContentUri(subscriptionId, mEfType),
77                 new String[]{SimPhonebookContract.SimRecords.RECORD_NUMBER}, null, null))) {
78             while (cursor.moveToNext()) {
79                 mResolver.delete(
80                         SimPhonebookContract.SimRecords.getItemUri(subscriptionId, mEfType,
81                                 cursor.getInt(0)), mExtras);
82             }
83         } catch (Exception e) {
84             // Swallow this so that the exception in the rule doesn't overwrite the failure in the
85             // test.
86             Log.e(TAG, "Failure cleaning up SIM", e);
87         }
88     }
89 }
90