1/*
2 * Copyright (C) 2006 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.RemoteException;
22import android.os.ServiceManager;
23import android.telephony.SubscriptionManager;
24import android.telephony.Rlog;
25import android.telephony.TelephonyManager;
26
27import java.lang.NullPointerException;
28import java.lang.ArrayIndexOutOfBoundsException;
29
30import com.android.internal.telephony.IPhoneSubInfo;
31import com.android.internal.telephony.Phone;
32import com.android.internal.telephony.PhoneSubInfoProxy;
33
34public class PhoneSubInfoController extends IPhoneSubInfo.Stub {
35    private static final String TAG = "PhoneSubInfoController";
36    private Phone[] mPhone;
37
38    public PhoneSubInfoController(Phone[] phone) {
39        mPhone = phone;
40        if (ServiceManager.getService("iphonesubinfo") == null) {
41            ServiceManager.addService("iphonesubinfo", this);
42        }
43    }
44
45
46    public String getDeviceId() {
47        return getDeviceIdForPhone(SubscriptionManager.getPhoneId(getDefaultSubscription()));
48    }
49
50    public String getDeviceIdForPhone(int phoneId) {
51        Phone phone = getPhone(phoneId);
52        if (phone != null) {
53            return phone.getDeviceId();
54        } else {
55            Rlog.e(TAG,"getDeviceIdForPhone phone " + phoneId + " is null");
56            return null;
57        }
58    }
59
60    public String getNaiForSubscriber(int subId) {
61        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
62        if (phoneSubInfoProxy != null) {
63            return phoneSubInfoProxy.getNai();
64        } else {
65            Rlog.e(TAG,"getNai phoneSubInfoProxy is null" +
66                      " for Subscription:" + subId);
67            return null;
68        }
69    }
70
71    public String getImeiForSubscriber(int subId) {
72        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
73        if (phoneSubInfoProxy != null) {
74            return phoneSubInfoProxy.getImei();
75        } else {
76            Rlog.e(TAG,"getDeviceId phoneSubInfoProxy is null" +
77                    " for Subscription:" + subId);
78            return null;
79        }
80    }
81
82    public String getDeviceSvn() {
83        return getDeviceSvnUsingSubId(getDefaultSubscription());
84    }
85
86    public String getDeviceSvnUsingSubId(int subId) {
87        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
88        if (phoneSubInfoProxy != null) {
89            return phoneSubInfoProxy.getDeviceSvn();
90        } else {
91            Rlog.e(TAG,"getDeviceSvn phoneSubInfoProxy is null");
92            return null;
93        }
94    }
95
96    public String getSubscriberId() {
97        return getSubscriberIdForSubscriber(getDefaultSubscription());
98    }
99
100    public String getSubscriberIdForSubscriber(int subId) {
101        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
102        if (phoneSubInfoProxy != null) {
103            return phoneSubInfoProxy.getSubscriberId();
104        } else {
105            Rlog.e(TAG,"getSubscriberId phoneSubInfoProxy is" +
106                      " null for Subscription:" + subId);
107            return null;
108        }
109    }
110
111    /**
112     * Retrieves the serial number of the ICC, if applicable.
113     */
114    public String getIccSerialNumber() {
115        return getIccSerialNumberForSubscriber(getDefaultSubscription());
116    }
117
118    public String getIccSerialNumberForSubscriber(int subId) {
119        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
120        if (phoneSubInfoProxy != null) {
121            return phoneSubInfoProxy.getIccSerialNumber();
122        } else {
123            Rlog.e(TAG,"getIccSerialNumber phoneSubInfoProxy is" +
124                      " null for Subscription:" + subId);
125            return null;
126        }
127    }
128
129    public String getLine1Number() {
130        return getLine1NumberForSubscriber(getDefaultSubscription());
131    }
132
133    public String getLine1NumberForSubscriber(int subId) {
134        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
135        if (phoneSubInfoProxy != null) {
136            return phoneSubInfoProxy.getLine1Number();
137        } else {
138            Rlog.e(TAG,"getLine1Number phoneSubInfoProxy is" +
139                      " null for Subscription:" + subId);
140            return null;
141        }
142    }
143
144    public String getLine1AlphaTag() {
145        return getLine1AlphaTagForSubscriber(getDefaultSubscription());
146    }
147
148    public String getLine1AlphaTagForSubscriber(int subId) {
149        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
150        if (phoneSubInfoProxy != null) {
151            return phoneSubInfoProxy.getLine1AlphaTag();
152        } else {
153            Rlog.e(TAG,"getLine1AlphaTag phoneSubInfoProxy is" +
154                      " null for Subscription:" + subId);
155            return null;
156        }
157    }
158
159    public String getMsisdn() {
160        return getMsisdnForSubscriber(getDefaultSubscription());
161    }
162
163    public String getMsisdnForSubscriber(int subId) {
164        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
165        if (phoneSubInfoProxy != null) {
166            return phoneSubInfoProxy.getMsisdn();
167        } else {
168            Rlog.e(TAG,"getMsisdn phoneSubInfoProxy is" +
169                      " null for Subscription:" + subId);
170            return null;
171        }
172    }
173
174    public String getVoiceMailNumber() {
175        return getVoiceMailNumberForSubscriber(getDefaultSubscription());
176    }
177
178    public String getVoiceMailNumberForSubscriber(int subId) {
179        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
180        if (phoneSubInfoProxy != null) {
181            return phoneSubInfoProxy.getVoiceMailNumber();
182        } else {
183            Rlog.e(TAG,"getVoiceMailNumber phoneSubInfoProxy is" +
184                      " null for Subscription:" + subId);
185            return null;
186        }
187    }
188
189    public String getCompleteVoiceMailNumber() {
190        return getCompleteVoiceMailNumberForSubscriber(getDefaultSubscription());
191    }
192
193    public String getCompleteVoiceMailNumberForSubscriber(int subId) {
194        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
195        if (phoneSubInfoProxy != null) {
196            return phoneSubInfoProxy.getCompleteVoiceMailNumber();
197        } else {
198            Rlog.e(TAG,"getCompleteVoiceMailNumber phoneSubInfoProxy" +
199                      " is null for Subscription:" + subId);
200            return null;
201        }
202    }
203
204    public String getVoiceMailAlphaTag() {
205        return getVoiceMailAlphaTagForSubscriber(getDefaultSubscription());
206    }
207
208    public String getVoiceMailAlphaTagForSubscriber(int subId) {
209        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
210        if (phoneSubInfoProxy != null) {
211            return phoneSubInfoProxy.getVoiceMailAlphaTag();
212        } else {
213            Rlog.e(TAG,"getVoiceMailAlphaTag phoneSubInfoProxy is" +
214                      " null for Subscription:" + subId);
215            return null;
216        }
217    }
218
219    /**
220     * get Phone sub info proxy object based on subId.
221     **/
222    private PhoneSubInfoProxy getPhoneSubInfoProxy(int subId) {
223
224        int phoneId = SubscriptionManager.getPhoneId(subId);
225
226        try {
227            return getPhone(phoneId).getPhoneSubInfoProxy();
228        } catch (NullPointerException e) {
229            Rlog.e(TAG, "Exception is :" + e.toString() + " For subId :" + subId);
230            e.printStackTrace();
231            return null;
232        }
233    }
234
235    private PhoneProxy getPhone(int phoneId) {
236        if (phoneId < 0 || phoneId >= TelephonyManager.getDefault().getPhoneCount()) {
237            phoneId = 0;
238        }
239        return (PhoneProxy) mPhone[phoneId];
240    }
241
242    private int getDefaultSubscription() {
243        return  PhoneFactory.getDefaultSubscription();
244    }
245
246
247    public String getIsimImpi() {
248        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(getDefaultSubscription());
249        return phoneSubInfoProxy.getIsimImpi();
250    }
251
252    public String getIsimDomain() {
253        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(getDefaultSubscription());
254        return phoneSubInfoProxy.getIsimDomain();
255    }
256
257    public String[] getIsimImpu() {
258        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(getDefaultSubscription());
259        return phoneSubInfoProxy.getIsimImpu();
260    }
261
262    public String getIsimIst() throws RemoteException {
263        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(getDefaultSubscription());
264        return phoneSubInfoProxy.getIsimIst();
265    }
266
267    public String[] getIsimPcscf() throws RemoteException {
268        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(getDefaultSubscription());
269        return phoneSubInfoProxy.getIsimPcscf();
270    }
271
272    public String getIsimChallengeResponse(String nonce) throws RemoteException {
273        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(getDefaultSubscription());
274        return phoneSubInfoProxy.getIsimChallengeResponse(nonce);
275    }
276
277    public String getIccSimChallengeResponse(int subId, int appType, String data)
278            throws RemoteException {
279        PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
280        return phoneSubInfoProxy.getIccSimChallengeResponse(subId, appType, data);
281    }
282
283     public String getGroupIdLevel1() {
284         return getGroupIdLevel1ForSubscriber(getDefaultSubscription());
285     }
286
287     public String getGroupIdLevel1ForSubscriber(int subId) {
288         PhoneSubInfoProxy phoneSubInfoProxy = getPhoneSubInfoProxy(subId);
289         if (phoneSubInfoProxy != null) {
290             return phoneSubInfoProxy.getGroupIdLevel1();
291         } else {
292             Rlog.e(TAG,"getGroupIdLevel1 phoneSubInfoProxy is" +
293                       " null for Subscription:" + subId);
294             return null;
295         }
296     }
297}
298