1c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/*
2c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Copyright (C) 2006 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;
18c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
19c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.content.pm.PackageManager;
20c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.AsyncResult;
21c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.Handler;
22c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.Looper;
23c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.Message;
24c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.ServiceManager;
25c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
26e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenkaimport com.android.internal.telephony.IccCardApplicationStatus.AppType;
27e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka
28c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.util.List;
29c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.util.concurrent.atomic.AtomicBoolean;
30c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
31c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/**
32c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * SimPhoneBookInterfaceManager to provide an inter-process communication to
33c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * access ADN-like SIM records.
34c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
35c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savillepublic abstract class IccPhoneBookInterfaceManager extends IIccPhoneBook.Stub {
36c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final boolean DBG = true;
37c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
38c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected PhoneBase phone;
39c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected AdnRecordCache adnCache;
40c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected final Object mLock = new Object();
41c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected int recordSize[];
42c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected boolean success;
43c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected List<AdnRecord> records;
44c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
45c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final boolean ALLOW_SIM_OP_IN_UI_THREAD = false;
46c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
47c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final int EVENT_GET_SIZE_DONE = 1;
48c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final int EVENT_LOAD_DONE = 2;
49c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final int EVENT_UPDATE_DONE = 3;
50c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
51c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected Handler mBaseHandler = new Handler() {
52c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        @Override
53c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        public void handleMessage(Message msg) {
54c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AsyncResult ar;
55c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
56c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            switch (msg.what) {
57c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                case EVENT_GET_SIZE_DONE:
58c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    ar = (AsyncResult) msg.obj;
59c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    synchronized (mLock) {
60c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        if (ar.exception == null) {
61c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            recordSize = (int[])ar.result;
62c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            // recordSize[0]  is the record length
63c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            // recordSize[1]  is the total length of the EF file
64c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            // recordSize[2]  is the number of records in the EF file
65c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            logd("GET_RECORD_SIZE Size " + recordSize[0] +
66c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                                    " total " + recordSize[1] +
67c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                                    " #record " + recordSize[2]);
68c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        }
69c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        notifyPending(ar);
70c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
71c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    break;
72c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                case EVENT_UPDATE_DONE:
73c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    ar = (AsyncResult) msg.obj;
74c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    synchronized (mLock) {
75c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        success = (ar.exception == null);
76c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        notifyPending(ar);
77c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
78c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    break;
79c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                case EVENT_LOAD_DONE:
80c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    ar = (AsyncResult)msg.obj;
81c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    synchronized (mLock) {
82c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        if (ar.exception == null) {
83c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            records = (List<AdnRecord>) ar.result;
84c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        } else {
85c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            if(DBG) logd("Cannot load ADN records");
86c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            if (records != null) {
87c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                                records.clear();
88c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            }
89c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        }
90c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        notifyPending(ar);
91c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
92c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    break;
93c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
94c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
95c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
96c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        private void notifyPending(AsyncResult ar) {
97c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            if (ar.userObj == null) {
98c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                return;
99c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
100c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = (AtomicBoolean) ar.userObj;
101c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            status.set(true);
102c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            mLock.notifyAll();
103c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
104c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    };
105c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
106c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public IccPhoneBookInterfaceManager(PhoneBase phone) {
107c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        this.phone = phone;
108bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        IccRecords r = phone.mIccRecords.get();
109bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        if (r != null) {
110bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka            adnCache = r.getAdnCache();
111bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        }
112c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
113c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
114c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public void dispose() {
115c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
116c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
117bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka    public void updateIccRecords(IccRecords iccRecords) {
118bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        if (iccRecords != null) {
119bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka            adnCache = iccRecords.getAdnCache();
120bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        } else {
121bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka            adnCache = null;
122bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        }
123bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka    }
124bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka
125c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected void publish() {
126c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        //NOTE service "simphonebook" added by IccSmsInterfaceManagerProxy
127c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        ServiceManager.addService("simphonebook", this);
128c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
129c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
130c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected abstract void logd(String msg);
131c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
132c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected abstract void loge(String msg);
133c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
134c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
135c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Replace oldAdn with newAdn in ADN-like record in EF
136c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
137c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * getAdnRecordsInEf must be called at least once before this function,
138c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * otherwise an error will be returned. Currently the email field
139c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * if set in the ADN record is ignored.
140c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * throws SecurityException if no WRITE_CONTACTS permission
141c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
142c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid must be one among EF_ADN, EF_FDN, and EF_SDN
143c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param oldTag adn tag to be replaced
144c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param oldPhoneNumber adn number to be replaced
145c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        Set both oldTag and oldPhoneNubmer to "" means to replace an
146c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        empty record, aka, insert new record
147c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newTag adn tag to be stored
148c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newPhoneNumber adn number ot be stored
149c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        Set both newTag and newPhoneNubmer to "" means to replace the old
150c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        record with empty one, aka, delete old record
151c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param pin2 required to update EF_FDN, otherwise must be null
152c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return true for success
153c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
154c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public boolean
155c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    updateAdnRecordsInEfBySearch (int efid,
156c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            String oldTag, String oldPhoneNumber,
157c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            String newTag, String newPhoneNumber, String pin2) {
158c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
159c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
160c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (phone.getContext().checkCallingOrSelfPermission(
161c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                android.Manifest.permission.WRITE_CONTACTS)
162c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            != PackageManager.PERMISSION_GRANTED) {
163c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            throw new SecurityException(
164c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    "Requires android.permission.WRITE_CONTACTS permission");
165c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
166c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
167c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
168c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) logd("updateAdnRecordsInEfBySearch: efid=" + efid +
169c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                " ("+ oldTag + "," + oldPhoneNumber + ")"+ "==>" +
170c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                " ("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);
171c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
172c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        efid = updateEfForIccType(efid);
173c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
174c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        synchronized(mLock) {
175c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            checkThread();
176c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            success = false;
177c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = new AtomicBoolean(false);
178c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Message response = mBaseHandler.obtainMessage(EVENT_UPDATE_DONE, status);
179c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AdnRecord oldAdn = new AdnRecord(oldTag, oldPhoneNumber);
180c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AdnRecord newAdn = new AdnRecord(newTag, newPhoneNumber);
181c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            adnCache.updateAdnBySearch(efid, oldAdn, newAdn, pin2, response);
182c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            waitForResult(status);
183c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
184c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return success;
185c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
186c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
187c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
188c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Update an ADN-like EF record by record index
189c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
190c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * This is useful for iteration the whole ADN file, such as write the whole
191c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * phone book or erase/format the whole phonebook. Currently the email field
192c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * if set in the ADN record is ignored.
193c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * throws SecurityException if no WRITE_CONTACTS permission
194c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
195c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid must be one among EF_ADN, EF_FDN, and EF_SDN
196c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newTag adn tag to be stored
197c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newPhoneNumber adn number to be stored
198c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        Set both newTag and newPhoneNubmer to "" means to replace the old
199c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        record with empty one, aka, delete old record
200c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param index is 1-based adn record index to be updated
201c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param pin2 required to update EF_FDN, otherwise must be null
202c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return true for success
203c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
204c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public boolean
205c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    updateAdnRecordsInEfByIndex(int efid, String newTag,
206c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            String newPhoneNumber, int index, String pin2) {
207c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
208c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (phone.getContext().checkCallingOrSelfPermission(
209c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                android.Manifest.permission.WRITE_CONTACTS)
210c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                != PackageManager.PERMISSION_GRANTED) {
211c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            throw new SecurityException(
212c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    "Requires android.permission.WRITE_CONTACTS permission");
213c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
214c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
215c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) logd("updateAdnRecordsInEfByIndex: efid=" + efid +
216c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                " Index=" + index + " ==> " +
217c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                "("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);
218c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        synchronized(mLock) {
219c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            checkThread();
220c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            success = false;
221c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = new AtomicBoolean(false);
222c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Message response = mBaseHandler.obtainMessage(EVENT_UPDATE_DONE, status);
223c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AdnRecord newAdn = new AdnRecord(newTag, newPhoneNumber);
224c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            adnCache.updateAdnByIndex(efid, newAdn, index, pin2, response);
225c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            waitForResult(status);
226c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
227c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return success;
228c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
229c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
230c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
231c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Get the capacity of records in efid
232c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
233c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid the EF id of a ADN-like ICC
234c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return  int[3] array
235c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *            recordSizes[0]  is the single record length
236c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *            recordSizes[1]  is the total length of the EF file
237c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *            recordSizes[2]  is the number of records in the EF file
238c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
239c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public abstract int[] getAdnRecordsSize(int efid);
240c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
241c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
242c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Loads the AdnRecords in efid and returns them as a
243c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * List of AdnRecords
244c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
245c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * throws SecurityException if no READ_CONTACTS permission
246c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
247c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid the EF id of a ADN-like ICC
248c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return List of AdnRecord
249c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
250c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public List<AdnRecord> getAdnRecordsInEf(int efid) {
251c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
252c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (phone.getContext().checkCallingOrSelfPermission(
253c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                android.Manifest.permission.READ_CONTACTS)
254c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                != PackageManager.PERMISSION_GRANTED) {
255c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            throw new SecurityException(
256c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    "Requires android.permission.READ_CONTACTS permission");
257c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
258c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
259c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        efid = updateEfForIccType(efid);
260c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) logd("getAdnRecordsInEF: efid=" + efid);
261c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
262c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        synchronized(mLock) {
263c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            checkThread();
264c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = new AtomicBoolean(false);
265c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Message response = mBaseHandler.obtainMessage(EVENT_LOAD_DONE, status);
266c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            adnCache.requestLoadAllAdnLike(efid, adnCache.extensionEfForEf(efid), response);
267c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            waitForResult(status);
268c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
269c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return records;
270c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
271c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
272c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected void checkThread() {
273c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (!ALLOW_SIM_OP_IN_UI_THREAD) {
274c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            // Make sure this isn't the UI thread, since it will block
275c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            if (mBaseHandler.getLooper().equals(Looper.myLooper())) {
276c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                loge("query() called on the main UI thread!");
277c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                throw new IllegalStateException(
278c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        "You cannot call query on this provder from the main UI thread.");
279c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
280c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
281c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
282c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
283c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected void waitForResult(AtomicBoolean status) {
284c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        while (!status.get()) {
285c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            try {
286c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                mLock.wait();
287c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            } catch (InterruptedException e) {
288c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                logd("interrupted while trying to update by search");
289c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
290c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
291c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
292c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
293c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private int updateEfForIccType(int efid) {
294c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        // Check if we are trying to read ADN records
295c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (efid == IccConstants.EF_ADN) {
296ba06d1becedde6ee94229154b2e493d6d4a3ad53Alex Yakavenka            if (phone.getCurrentUiccAppType() == AppType.APPTYPE_USIM) {
297c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                return IccConstants.EF_PBR;
298c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
299c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
300c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return efid;
301c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
302c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville}
303c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
304