UiccController.java revision c38bb60d867c5d61d90b7179a9ed2b2d1848124f
1c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/*
2c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Copyright (C) 2011 The Android Open Source Project
3c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
4c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Licensed under the Apache License, Version 2.0 (the "License");
5c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * you may not use this file except in compliance with the License.
6c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * You may obtain a copy of the License at
7c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
8c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *      http://www.apache.org/licenses/LICENSE-2.0
9c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
10c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Unless required by applicable law or agreed to in writing, software
11c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * distributed under the License is distributed on an "AS IS" BASIS,
12c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * See the License for the specific language governing permissions and
14c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * limitations under the License.
15c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
16c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
17c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savillepackage com.android.internal.telephony.uicc;
18c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
19c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport com.android.internal.telephony.IccCard;
20c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport com.android.internal.telephony.PhoneBase;
21c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport com.android.internal.telephony.cdma.CDMALTEPhone;
22c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport com.android.internal.telephony.cdma.CDMAPhone;
23c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport com.android.internal.telephony.gsm.GSMPhone;
24c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
25c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.util.Log;
26c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
27c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/* This class is responsible for keeping all knowledge about
28c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * ICCs in the system. It is also used as API to get appropriate
29c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * applications to pass them to phone and service trackers.
30c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
31c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savillepublic class UiccController {
32c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private static final boolean DBG = true;
33c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private static final String LOG_TAG = "RIL_UiccController";
34c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
35c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private static UiccController mInstance;
36c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
37c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private PhoneBase mCurrentPhone;
38c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private boolean mIsCurrentCard3gpp;
39c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private IccCard mIccCard;
40c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
41c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public static synchronized UiccController getInstance(PhoneBase phone) {
42c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (mInstance == null) {
43c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            mInstance = new UiccController(phone);
44c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else {
45c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            mInstance.setNewPhone(phone);
46c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
47c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return mInstance;
48c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
49c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
50c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public IccCard getIccCard() {
51c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return mIccCard;
52c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
53c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
54c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private UiccController(PhoneBase phone) {
55c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) log("Creating UiccController");
56c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        setNewPhone(phone);
57c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
58c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
59c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private void setNewPhone(PhoneBase phone) {
60c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        mCurrentPhone = phone;
61c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (phone instanceof GSMPhone) {
62c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            if (DBG) log("New phone is GSMPhone");
63c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            updateCurrentCard(IccCard.CARD_IS_3GPP);
64c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else if (phone instanceof CDMALTEPhone){
65c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            if (DBG) log("New phone type is CDMALTEPhone");
66c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            updateCurrentCard(IccCard.CARD_IS_3GPP);
67c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else if (phone instanceof CDMAPhone){
68c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            if (DBG) log("New phone type is CDMAPhone");
69c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            updateCurrentCard(IccCard.CARD_IS_NOT_3GPP);
70c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else {
71c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Log.e(LOG_TAG, "Unhandled phone type. Critical error!");
72c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
73c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
74c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
75c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private void updateCurrentCard(boolean isNewCard3gpp) {
76c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (mIsCurrentCard3gpp == isNewCard3gpp && mIccCard != null) {
77c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            return;
78c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
79c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
80c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (mIccCard != null) {
81c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            mIccCard.dispose();
82c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            mIccCard = null;
83c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
84c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
85c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        mIsCurrentCard3gpp = isNewCard3gpp;
86c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        mIccCard = new IccCard(mCurrentPhone, mCurrentPhone.getPhoneName(),
87c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                isNewCard3gpp, DBG);
88c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
89c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
90c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private void log(String string) {
91c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        Log.d(LOG_TAG, string);
92c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
93c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville}