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