1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2011-2013, 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
19package com.android.internal.telephony;
20
21import android.os.ServiceManager;
22import android.telephony.Rlog;
23
24import com.android.internal.telephony.IIccPhoneBook;
25import com.android.internal.telephony.uicc.AdnRecord;
26
27import java.lang.ArrayIndexOutOfBoundsException;
28import java.lang.NullPointerException;
29import java.util.List;
30
31public class UiccPhoneBookController extends IIccPhoneBook.Stub {
32    private static final String TAG = "UiccPhoneBookController";
33    private Phone[] mPhone;
34
35    /* only one UiccPhoneBookController exists */
36    public UiccPhoneBookController(Phone[] phone) {
37        if (ServiceManager.getService("simphonebook") == null) {
38               ServiceManager.addService("simphonebook", this);
39        }
40        mPhone = phone;
41    }
42
43    @Override
44    public boolean
45    updateAdnRecordsInEfBySearch (int efid, String oldTag, String oldPhoneNumber,
46            String newTag, String newPhoneNumber, String pin2) throws android.os.RemoteException {
47        return updateAdnRecordsInEfBySearchForSubscriber(getDefaultSubscription(), efid, oldTag,
48                oldPhoneNumber, newTag, newPhoneNumber, pin2);
49    }
50
51    @Override
52    public boolean
53    updateAdnRecordsInEfBySearchForSubscriber(int subId, int efid, String oldTag,
54            String oldPhoneNumber, String newTag, String newPhoneNumber,
55            String pin2) throws android.os.RemoteException {
56        IccPhoneBookInterfaceManager iccPbkIntMgr =
57                             getIccPhoneBookInterfaceManager(subId);
58        if (iccPbkIntMgr != null) {
59            return iccPbkIntMgr.updateAdnRecordsInEfBySearch(efid, oldTag,
60                    oldPhoneNumber, newTag, newPhoneNumber, pin2);
61        } else {
62            Rlog.e(TAG,"updateAdnRecordsInEfBySearch iccPbkIntMgr is" +
63                      " null for Subscription:"+subId);
64            return false;
65        }
66    }
67
68    @Override
69    public boolean
70    updateAdnRecordsInEfByIndex(int efid, String newTag,
71            String newPhoneNumber, int index, String pin2) throws android.os.RemoteException {
72        return updateAdnRecordsInEfByIndexForSubscriber(getDefaultSubscription(), efid, newTag,
73                newPhoneNumber, index, pin2);
74    }
75
76    @Override
77    public boolean
78    updateAdnRecordsInEfByIndexForSubscriber(int subId, int efid, String newTag,
79            String newPhoneNumber, int index, String pin2) throws android.os.RemoteException {
80        IccPhoneBookInterfaceManager iccPbkIntMgr =
81                             getIccPhoneBookInterfaceManager(subId);
82        if (iccPbkIntMgr != null) {
83            return iccPbkIntMgr.updateAdnRecordsInEfByIndex(efid, newTag,
84                    newPhoneNumber, index, pin2);
85        } else {
86            Rlog.e(TAG,"updateAdnRecordsInEfByIndex iccPbkIntMgr is" +
87                      " null for Subscription:"+subId);
88            return false;
89        }
90    }
91
92    @Override
93    public int[] getAdnRecordsSize(int efid) throws android.os.RemoteException {
94        return getAdnRecordsSizeForSubscriber(getDefaultSubscription(), efid);
95    }
96
97    @Override
98    public int[]
99    getAdnRecordsSizeForSubscriber(int subId, int efid) throws android.os.RemoteException {
100        IccPhoneBookInterfaceManager iccPbkIntMgr =
101                             getIccPhoneBookInterfaceManager(subId);
102        if (iccPbkIntMgr != null) {
103            return iccPbkIntMgr.getAdnRecordsSize(efid);
104        } else {
105            Rlog.e(TAG,"getAdnRecordsSize iccPbkIntMgr is" +
106                      " null for Subscription:"+subId);
107            return null;
108        }
109    }
110
111    @Override
112    public List<AdnRecord> getAdnRecordsInEf(int efid) throws android.os.RemoteException {
113        return getAdnRecordsInEfForSubscriber(getDefaultSubscription(), efid);
114    }
115
116    @Override
117    public List<AdnRecord> getAdnRecordsInEfForSubscriber(int subId, int efid)
118           throws android.os.RemoteException {
119        IccPhoneBookInterfaceManager iccPbkIntMgr =
120                             getIccPhoneBookInterfaceManager(subId);
121        if (iccPbkIntMgr != null) {
122            return iccPbkIntMgr.getAdnRecordsInEf(efid);
123        } else {
124            Rlog.e(TAG,"getAdnRecordsInEf iccPbkIntMgr is" +
125                      "null for Subscription:"+subId);
126            return null;
127        }
128    }
129
130    /**
131     * get phone book interface manager object based on subscription.
132     **/
133    private IccPhoneBookInterfaceManager
134            getIccPhoneBookInterfaceManager(int subId) {
135
136        int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
137        try {
138            return mPhone[phoneId].getIccPhoneBookInterfaceManager();
139        } catch (NullPointerException e) {
140            Rlog.e(TAG, "Exception is :"+e.toString()+" For subscription :"+subId );
141            e.printStackTrace(); //To print stack trace
142            return null;
143        } catch (ArrayIndexOutOfBoundsException e) {
144            Rlog.e(TAG, "Exception is :"+e.toString()+" For subscription :"+subId );
145            e.printStackTrace();
146            return null;
147        }
148    }
149
150    private int getDefaultSubscription() {
151        return PhoneFactory.getDefaultSubscription();
152    }
153}
154