1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2011-2013, 2021 The Linux Foundation. All rights reserved.
4  * Not a Contribution.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package com.android.internal.telephony;
20 
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.os.Build;
23 import android.os.TelephonyServiceManager.ServiceRegisterer;
24 import android.telephony.TelephonyFrameworkInitializer;
25 import android.content.ContentValues;
26 
27 import com.android.internal.telephony.uicc.AdnCapacity;
28 import com.android.internal.telephony.uicc.AdnRecord;
29 import com.android.telephony.Rlog;
30 
31 import java.util.List;
32 
33 public class UiccPhoneBookController extends IIccPhoneBook.Stub {
34     private static final String TAG = "UiccPhoneBookController";
35 
36     /* only one UiccPhoneBookController exists */
37     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
UiccPhoneBookController()38     public UiccPhoneBookController() {
39         ServiceRegisterer iccPhoneBookServiceRegisterer = TelephonyFrameworkInitializer
40                 .getTelephonyServiceManager()
41                 .getIccPhoneBookServiceRegisterer();
42         if (iccPhoneBookServiceRegisterer.get() == null) {
43             iccPhoneBookServiceRegisterer.register(this);
44         }
45     }
46 
47     @Override
48     public boolean
updateAdnRecordsInEfBySearch(int efid, String oldTag, String oldPhoneNumber, String newTag, String newPhoneNumber, String pin2)49     updateAdnRecordsInEfBySearch (int efid, String oldTag, String oldPhoneNumber,
50             String newTag, String newPhoneNumber, String pin2) throws android.os.RemoteException {
51         ContentValues values = new ContentValues();
52         values.put(IccProvider.STR_TAG, oldTag);
53         values.put(IccProvider.STR_NUMBER, oldPhoneNumber);
54         values.put(IccProvider.STR_NEW_TAG, newTag);
55         values.put(IccProvider.STR_NEW_NUMBER, newPhoneNumber);
56         return updateAdnRecordsInEfBySearchForSubscriber(getDefaultSubscription(),
57                 efid, values, pin2);
58     }
59 
60     @Override
61     public boolean
updateAdnRecordsInEfByIndexForSubscriber(int subId, int efid, ContentValues values, int index, String pin2)62     updateAdnRecordsInEfByIndexForSubscriber(int subId, int efid, ContentValues values,
63             int index, String pin2) throws android.os.RemoteException {
64         IccPhoneBookInterfaceManager iccPbkIntMgr =
65                              getIccPhoneBookInterfaceManager(subId);
66         if (iccPbkIntMgr != null) {
67             return iccPbkIntMgr.updateAdnRecordsInEfByIndex(efid, values,
68                     index, pin2);
69         } else {
70             Rlog.e(TAG,"updateAdnRecordsInEfByIndex iccPbkIntMgr is" +
71                       " null for Subscription:"+subId);
72             return false;
73         }
74     }
75 
76     @Override
getAdnRecordsSize(int efid)77     public int[] getAdnRecordsSize(int efid) throws android.os.RemoteException {
78         return getAdnRecordsSizeForSubscriber(getDefaultSubscription(), efid);
79     }
80 
81     @Override
82     public int[]
getAdnRecordsSizeForSubscriber(int subId, int efid)83     getAdnRecordsSizeForSubscriber(int subId, int efid) throws android.os.RemoteException {
84         IccPhoneBookInterfaceManager iccPbkIntMgr =
85                              getIccPhoneBookInterfaceManager(subId);
86         if (iccPbkIntMgr != null) {
87             return iccPbkIntMgr.getAdnRecordsSize(efid);
88         } else {
89             Rlog.e(TAG,"getAdnRecordsSize iccPbkIntMgr is" +
90                       " null for Subscription:"+subId);
91             return null;
92         }
93     }
94 
95     @Override
getAdnRecordsInEf(int efid)96     public List<AdnRecord> getAdnRecordsInEf(int efid) throws android.os.RemoteException {
97         return getAdnRecordsInEfForSubscriber(getDefaultSubscription(), efid);
98     }
99 
100     @Override
getAdnRecordsInEfForSubscriber(int subId, int efid)101     public List<AdnRecord> getAdnRecordsInEfForSubscriber(int subId, int efid)
102            throws android.os.RemoteException {
103         IccPhoneBookInterfaceManager iccPbkIntMgr =
104                              getIccPhoneBookInterfaceManager(subId);
105         if (iccPbkIntMgr != null) {
106             return iccPbkIntMgr.getAdnRecordsInEf(efid);
107         } else {
108             Rlog.e(TAG,"getAdnRecordsInEf iccPbkIntMgr is" +
109                       "null for Subscription:"+subId);
110             return null;
111         }
112     }
113 
114     @Override
getAdnRecordsCapacityForSubscriber(int subId)115     public AdnCapacity getAdnRecordsCapacityForSubscriber(int subId)
116            throws android.os.RemoteException {
117         IccPhoneBookInterfaceManager iccPbkIntMgr = getIccPhoneBookInterfaceManager(subId);
118         if (iccPbkIntMgr != null) {
119             return iccPbkIntMgr.getAdnRecordsCapacity();
120         } else {
121             Rlog.e(TAG, "getAdnRecordsCapacity iccPbkIntMgr is null for Subscription:" + subId);
122             return null;
123         }
124     }
125 
126     @Override
127     public boolean
updateAdnRecordsInEfBySearchForSubscriber(int subId, int efid, ContentValues values, String pin2)128     updateAdnRecordsInEfBySearchForSubscriber(int subId, int efid,
129             ContentValues values, String pin2)
130             throws android.os.RemoteException {
131         IccPhoneBookInterfaceManager iccPbkIntMgr = getIccPhoneBookInterfaceManager(subId);
132         if (iccPbkIntMgr != null) {
133             return iccPbkIntMgr.updateAdnRecordsInEfBySearchForSubscriber(
134                 efid, values, pin2);
135         } else {
136             Rlog.e(TAG,"updateAdnRecordsInEfBySearchForSubscriber " +
137                 "iccPbkIntMgr is null for Subscription:"+subId);
138             return false;
139         }
140     }
141 
142     /**
143      * get phone book interface manager object based on subscription.
144      **/
145     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
146     private IccPhoneBookInterfaceManager
getIccPhoneBookInterfaceManager(int subId)147             getIccPhoneBookInterfaceManager(int subId) {
148 
149         int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
150         try {
151             return PhoneFactory.getPhone(phoneId).getIccPhoneBookInterfaceManager();
152         } catch (NullPointerException e) {
153             Rlog.e(TAG, "Exception is :"+e.toString()+" For subscription :"+subId );
154             e.printStackTrace(); //To print stack trace
155             return null;
156         } catch (ArrayIndexOutOfBoundsException e) {
157             Rlog.e(TAG, "Exception is :"+e.toString()+" For subscription :"+subId );
158             e.printStackTrace();
159             return null;
160         }
161     }
162 
163     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
getDefaultSubscription()164     private int getDefaultSubscription() {
165         return PhoneFactory.getDefaultSubscription();
166     }
167 }
168