1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony;
18
19import android.os.AsyncResult;
20import android.os.Handler;
21import android.os.Message;
22import android.os.Registrant;
23import android.os.RegistrantList;
24import android.util.Log;
25
26import java.util.ArrayList;
27
28/**
29 * {@hide}
30 */
31public abstract class IccRecords extends Handler implements IccConstants {
32
33    protected static final boolean DBG = true;
34    // ***** Instance Variables
35
36    protected PhoneBase phone;
37    protected RegistrantList recordsLoadedRegistrants = new RegistrantList();
38
39    protected int recordsToLoad;  // number of pending load requests
40
41    protected AdnRecordCache adnCache;
42
43    // ***** Cached SIM State; cleared on channel close
44
45    protected boolean recordsRequested = false; // true if we've made requests for the sim records
46
47    public String iccid;
48    protected String msisdn = null;  // My mobile number
49    protected String msisdnTag = null;
50    protected String voiceMailNum = null;
51    protected String voiceMailTag = null;
52    protected String newVoiceMailNum = null;
53    protected String newVoiceMailTag = null;
54    protected boolean isVoiceMailFixed = false;
55    protected int countVoiceMessages = 0;
56
57    protected int mncLength = UNINITIALIZED;
58    protected int mailboxIndex = 0; // 0 is no mailbox dailing number associated
59
60    protected String spn;
61    protected int spnDisplayCondition;
62
63    // ***** Constants
64
65    // Markers for mncLength
66    protected static final int UNINITIALIZED = -1;
67    protected static final int UNKNOWN = 0;
68
69    // Bitmasks for SPN display rules.
70    protected static final int SPN_RULE_SHOW_SPN  = 0x01;
71    protected static final int SPN_RULE_SHOW_PLMN = 0x02;
72
73    // ***** Event Constants
74    protected static final int EVENT_SET_MSISDN_DONE = 30;
75
76    // ***** Constructor
77
78    public IccRecords(PhoneBase p) {
79        this.phone = p;
80    }
81
82    protected abstract void onRadioOffOrNotAvailable();
83
84    //***** Public Methods
85    public AdnRecordCache getAdnCache() {
86        return adnCache;
87    }
88
89    public void registerForRecordsLoaded(Handler h, int what, Object obj) {
90        Registrant r = new Registrant(h, what, obj);
91        recordsLoadedRegistrants.add(r);
92
93        if (recordsToLoad == 0 && recordsRequested == true) {
94            r.notifyRegistrant(new AsyncResult(null, null, null));
95        }
96    }
97
98    public void unregisterForRecordsLoaded(Handler h) {
99        recordsLoadedRegistrants.remove(h);
100    }
101
102    public String getMsisdnNumber() {
103        return msisdn;
104    }
105
106    /**
107     * Set subscriber number to SIM record
108     *
109     * The subscriber number is stored in EF_MSISDN (TS 51.011)
110     *
111     * When the operation is complete, onComplete will be sent to its handler
112     *
113     * @param alphaTag alpha-tagging of the dailing nubmer (up to 10 characters)
114     * @param number dailing nubmer (up to 20 digits)
115     *        if the number starts with '+', then set to international TOA
116     * @param onComplete
117     *        onComplete.obj will be an AsyncResult
118     *        ((AsyncResult)onComplete.obj).exception == null on success
119     *        ((AsyncResult)onComplete.obj).exception != null on fail
120     */
121    public void setMsisdnNumber(String alphaTag, String number,
122            Message onComplete) {
123
124        msisdn = number;
125        msisdnTag = alphaTag;
126
127        if(DBG) log("Set MSISDN: " + msisdnTag +" " + msisdn);
128
129
130        AdnRecord adn = new AdnRecord(msisdnTag, msisdn);
131
132        new AdnRecordLoader(phone).updateEF(adn, EF_MSISDN, EF_EXT1, 1, null,
133                obtainMessage(EVENT_SET_MSISDN_DONE, onComplete));
134    }
135
136    public String getMsisdnAlphaTag() {
137        return msisdnTag;
138    }
139
140    public String getVoiceMailNumber() {
141        return voiceMailNum;
142    }
143
144    /**
145     * Return Service Provider Name stored in SIM (EF_SPN=0x6F46) or in RUIM (EF_RUIM_SPN=0x6F41)
146     * @return null if SIM is not yet ready or no RUIM entry
147     */
148    public String getServiceProviderName() {
149        return spn;
150    }
151
152    /**
153     * Set voice mail number to SIM record
154     *
155     * The voice mail number can be stored either in EF_MBDN (TS 51.011) or
156     * EF_MAILBOX_CPHS (CPHS 4.2)
157     *
158     * If EF_MBDN is available, store the voice mail number to EF_MBDN
159     *
160     * If EF_MAILBOX_CPHS is enabled, store the voice mail number to EF_CHPS
161     *
162     * So the voice mail number will be stored in both EFs if both are available
163     *
164     * Return error only if both EF_MBDN and EF_MAILBOX_CPHS fail.
165     *
166     * When the operation is complete, onComplete will be sent to its handler
167     *
168     * @param alphaTag alpha-tagging of the dailing nubmer (upto 10 characters)
169     * @param voiceNumber dailing nubmer (upto 20 digits)
170     *        if the number is start with '+', then set to international TOA
171     * @param onComplete
172     *        onComplete.obj will be an AsyncResult
173     *        ((AsyncResult)onComplete.obj).exception == null on success
174     *        ((AsyncResult)onComplete.obj).exception != null on fail
175     */
176    public abstract void setVoiceMailNumber(String alphaTag, String voiceNumber,
177            Message onComplete);
178
179    public String getVoiceMailAlphaTag() {
180        return voiceMailTag;
181    }
182
183    /**
184     * Sets the SIM voice message waiting indicator records
185     * @param line GSM Subscriber Profile Number, one-based. Only '1' is supported
186     * @param countWaiting The number of messages waiting, if known. Use
187     *                     -1 to indicate that an unknown number of
188     *                      messages are waiting
189     */
190    public abstract void setVoiceMessageWaiting(int line, int countWaiting);
191
192    /** @return  true if there are messages waiting, false otherwise. */
193    public boolean getVoiceMessageWaiting() {
194        return countVoiceMessages != 0;
195    }
196
197    /**
198     * Returns number of voice messages waiting, if available
199     * If not available (eg, on an older CPHS SIM) -1 is returned if
200     * getVoiceMessageWaiting() is true
201     */
202    public int getVoiceMessageCount() {
203        return countVoiceMessages;
204    }
205
206    /**
207     * Called by STK Service when REFRESH is received.
208     * @param fileChanged indicates whether any files changed
209     * @param fileList if non-null, a list of EF files that changed
210     */
211    public abstract void onRefresh(boolean fileChanged, int[] fileList);
212
213
214    public boolean getRecordsLoaded() {
215        if (recordsToLoad == 0 && recordsRequested == true) {
216            return true;
217        } else {
218            return false;
219        }
220    }
221
222    //***** Overridden from Handler
223    public abstract void handleMessage(Message msg);
224
225    protected abstract void onRecordLoaded();
226
227    protected abstract void onAllRecordsLoaded();
228
229    /**
230     * Returns the SpnDisplayRule based on settings on the SIM and the
231     * specified plmn (currently-registered PLMN).  See TS 22.101 Annex A
232     * and TS 51.011 10.3.11 for details.
233     *
234     * If the SPN is not found on the SIM, the rule is always PLMN_ONLY.
235     */
236    protected abstract int getDisplayRule(String plmn);
237
238    protected abstract void log(String s);
239}
240