104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville/*
204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * Copyright (C) 2006 The Android Open Source Project
304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville *
404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * Licensed under the Apache License, Version 2.0 (the "License");
504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * you may not use this file except in compliance with the License.
604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * You may obtain a copy of the License at
704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville *
804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville *      http://www.apache.org/licenses/LICENSE-2.0
904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville *
1004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * Unless required by applicable law or agreed to in writing, software
1104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * distributed under the License is distributed on an "AS IS" BASIS,
1204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * See the License for the specific language governing permissions and
1404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * limitations under the License.
1504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville */
1604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
1704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savillepackage com.android.internal.telephony;
1804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
1904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport android.content.pm.PackageManager;
2004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport android.os.AsyncResult;
2104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport android.os.Handler;
2204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport android.os.Looper;
2304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport android.os.Message;
2404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport android.os.ServiceManager;
2504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
2604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savilleimport java.util.List;
27d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marquesimport java.util.concurrent.atomic.AtomicBoolean;
2804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
2904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville/**
3004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * SimPhoneBookInterfaceManager to provide an inter-process communication to
3104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville * access ADN-like SIM records.
3204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville */
3304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Savillepublic abstract class IccPhoneBookInterfaceManager extends IIccPhoneBook.Stub {
3404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected static final boolean DBG = true;
3504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
3604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected PhoneBase phone;
3704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected AdnRecordCache adnCache;
38a1303e0193aeb32131572fb0dfe8e595a908b4e0Romain Guy    protected final Object mLock = new Object();
3904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected int recordSize[];
4004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected boolean success;
4104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected List<AdnRecord> records;
4204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
4304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected static final boolean ALLOW_SIM_OP_IN_UI_THREAD = false;
4404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
4504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected static final int EVENT_GET_SIZE_DONE = 1;
4604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected static final int EVENT_LOAD_DONE = 2;
4704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected static final int EVENT_UPDATE_DONE = 3;
4804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
4904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected Handler mBaseHandler = new Handler() {
5004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        @Override
5104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        public void handleMessage(Message msg) {
5204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            AsyncResult ar;
5304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
5404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            switch (msg.what) {
5504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                case EVENT_GET_SIZE_DONE:
5604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    ar = (AsyncResult) msg.obj;
5704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    synchronized (mLock) {
5804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        if (ar.exception == null) {
5904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            recordSize = (int[])ar.result;
6004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            // recordSize[0]  is the record length
6104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            // recordSize[1]  is the total length of the EF file
6204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            // recordSize[2]  is the number of records in the EF file
6304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            logd("GET_RECORD_SIZE Size " + recordSize[0] +
6404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                                    " total " + recordSize[1] +
6504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                                    " #record " + recordSize[2]);
6604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        }
67d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques                        notifyPending(ar);
6804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    }
6904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    break;
7004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                case EVENT_UPDATE_DONE:
7104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    ar = (AsyncResult) msg.obj;
7204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    synchronized (mLock) {
7304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        success = (ar.exception == null);
74d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques                        notifyPending(ar);
7504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    }
7604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    break;
7704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                case EVENT_LOAD_DONE:
7804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    ar = (AsyncResult)msg.obj;
7904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    synchronized (mLock) {
8004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        if (ar.exception == null) {
81a1303e0193aeb32131572fb0dfe8e595a908b4e0Romain Guy                            records = (List<AdnRecord>) ar.result;
8204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        } else {
8304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            if(DBG) logd("Cannot load ADN records");
8404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            if (records != null) {
8504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                                records.clear();
8604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                            }
8704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        }
88d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques                        notifyPending(ar);
8904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    }
9004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    break;
9104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            }
9204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
93d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques
94d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques        private void notifyPending(AsyncResult ar) {
95d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            if (ar.userObj == null) {
96d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques                return;
97d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            }
98d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            AtomicBoolean status = (AtomicBoolean) ar.userObj;
99d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            status.set(true);
100d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            mLock.notifyAll();
101d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques        }
10204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    };
10304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
10404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    public IccPhoneBookInterfaceManager(PhoneBase phone) {
10504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        this.phone = phone;
10604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
10704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
10804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    public void dispose() {
10904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
11004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
11104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected void publish() {
11204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        //NOTE service "simphonebook" added by IccSmsInterfaceManagerProxy
11304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        ServiceManager.addService("simphonebook", this);
11404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
11504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
11604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected abstract void logd(String msg);
11704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
11804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected abstract void loge(String msg);
11904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
12004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    /**
12104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * Replace oldAdn with newAdn in ADN-like record in EF
12204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
12304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * getAdnRecordsInEf must be called at least once before this function,
12434efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh     * otherwise an error will be returned. Currently the email field
12534efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh     * if set in the ADN record is ignored.
12604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * throws SecurityException if no WRITE_CONTACTS permission
12704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
12804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param efid must be one among EF_ADN, EF_FDN, and EF_SDN
12904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param oldTag adn tag to be replaced
13004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param oldPhoneNumber adn number to be replaced
13104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *        Set both oldTag and oldPhoneNubmer to "" means to replace an
13204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *        empty record, aka, insert new record
13304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param newTag adn tag to be stored
13404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param newPhoneNumber adn number ot be stored
13504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *        Set both newTag and newPhoneNubmer to "" means to replace the old
13604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *        record with empty one, aka, delete old record
13704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param pin2 required to update EF_FDN, otherwise must be null
13804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @return true for success
13904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     */
14004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    public boolean
14104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    updateAdnRecordsInEfBySearch (int efid,
14204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            String oldTag, String oldPhoneNumber,
14304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            String newTag, String newPhoneNumber, String pin2) {
14404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
14504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
14604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (phone.getContext().checkCallingOrSelfPermission(
14704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                android.Manifest.permission.WRITE_CONTACTS)
14804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            != PackageManager.PERMISSION_GRANTED) {
14904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            throw new SecurityException(
15004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    "Requires android.permission.WRITE_CONTACTS permission");
15104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
15204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
15304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
15404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (DBG) logd("updateAdnRecordsInEfBySearch: efid=" + efid +
15504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                " ("+ oldTag + "," + oldPhoneNumber + ")"+ "==>" +
15604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                " ("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);
15774ba31ba266231b76e2ab4ab46e0a9af6a6bcbb3happydroid
15874ba31ba266231b76e2ab4ab46e0a9af6a6bcbb3happydroid        efid = updateEfForIccType(efid);
15974ba31ba266231b76e2ab4ab46e0a9af6a6bcbb3happydroid
16004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        synchronized(mLock) {
16104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            checkThread();
16204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            success = false;
163d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            AtomicBoolean status = new AtomicBoolean(false);
164d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            Message response = mBaseHandler.obtainMessage(EVENT_UPDATE_DONE, status);
16504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            AdnRecord oldAdn = new AdnRecord(oldTag, oldPhoneNumber);
16604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            AdnRecord newAdn = new AdnRecord(newTag, newPhoneNumber);
16704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            adnCache.updateAdnBySearch(efid, oldAdn, newAdn, pin2, response);
168d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            waitForResult(status);
16904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
17004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        return success;
17104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
17204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
17304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    /**
17404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * Update an ADN-like EF record by record index
17504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
17604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * This is useful for iteration the whole ADN file, such as write the whole
17734efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh     * phone book or erase/format the whole phonebook. Currently the email field
17834efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh     * if set in the ADN record is ignored.
17904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * throws SecurityException if no WRITE_CONTACTS permission
18004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
18104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param efid must be one among EF_ADN, EF_FDN, and EF_SDN
18204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param newTag adn tag to be stored
18304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param newPhoneNumber adn number to be stored
18404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *        Set both newTag and newPhoneNubmer to "" means to replace the old
18504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *        record with empty one, aka, delete old record
18604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param index is 1-based adn record index to be updated
18704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param pin2 required to update EF_FDN, otherwise must be null
18804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @return true for success
18904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     */
19004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    public boolean
19104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    updateAdnRecordsInEfByIndex(int efid, String newTag,
19204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            String newPhoneNumber, int index, String pin2) {
19304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
19404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (phone.getContext().checkCallingOrSelfPermission(
19504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                android.Manifest.permission.WRITE_CONTACTS)
19604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                != PackageManager.PERMISSION_GRANTED) {
19704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            throw new SecurityException(
19804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    "Requires android.permission.WRITE_CONTACTS permission");
19904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
20004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
20104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (DBG) logd("updateAdnRecordsInEfByIndex: efid=" + efid +
20204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                " Index=" + index + " ==> " +
20304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                "("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);
20404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        synchronized(mLock) {
20504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            checkThread();
20604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            success = false;
207d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            AtomicBoolean status = new AtomicBoolean(false);
208d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            Message response = mBaseHandler.obtainMessage(EVENT_UPDATE_DONE, status);
20904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            AdnRecord newAdn = new AdnRecord(newTag, newPhoneNumber);
21004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            adnCache.updateAdnByIndex(efid, newAdn, index, pin2, response);
211d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            waitForResult(status);
21204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
21304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        return success;
21404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
21504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
21604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    /**
21704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * Get the capacity of records in efid
21804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
21904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param efid the EF id of a ADN-like ICC
22004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @return  int[3] array
22104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *            recordSizes[0]  is the single record length
22204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *            recordSizes[1]  is the total length of the EF file
22304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *            recordSizes[2]  is the number of records in the EF file
22404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     */
22504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    public abstract int[] getAdnRecordsSize(int efid);
22604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
22704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    /**
22804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * Loads the AdnRecords in efid and returns them as a
22904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * List of AdnRecords
23004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
23104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * throws SecurityException if no READ_CONTACTS permission
23204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     *
23304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @param efid the EF id of a ADN-like ICC
23404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     * @return List of AdnRecord
23504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville     */
23604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    public List<AdnRecord> getAdnRecordsInEf(int efid) {
23704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
23804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (phone.getContext().checkCallingOrSelfPermission(
23904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                android.Manifest.permission.READ_CONTACTS)
24004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                != PackageManager.PERMISSION_GRANTED) {
24104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            throw new SecurityException(
24204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                    "Requires android.permission.READ_CONTACTS permission");
24304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
24404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
24534efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh        efid = updateEfForIccType(efid);
24604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (DBG) logd("getAdnRecordsInEF: efid=" + efid);
24704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
24804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        synchronized(mLock) {
24904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            checkThread();
250d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            AtomicBoolean status = new AtomicBoolean(false);
251d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            Message response = mBaseHandler.obtainMessage(EVENT_LOAD_DONE, status);
25234efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh            adnCache.requestLoadAllAdnLike(efid, adnCache.extensionEfForEf(efid), response);
253d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            waitForResult(status);
25404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
255d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques        return records;
25604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
25704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
25804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    protected void checkThread() {
25904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        if (!ALLOW_SIM_OP_IN_UI_THREAD) {
26004e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            // Make sure this isn't the UI thread, since it will block
26104e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            if (mBaseHandler.getLooper().equals(Looper.myLooper())) {
26204e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                loge("query() called on the main UI thread!");
26304e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                throw new IllegalStateException(
26404e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville                        "You cannot call query on this provder from the main UI thread.");
26504e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville            }
26604e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville        }
26704e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville    }
26834efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh
269d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques    protected void waitForResult(AtomicBoolean status) {
270d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques        while (!status.get()) {
271d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            try {
272d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques                mLock.wait();
273d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            } catch (InterruptedException e) {
274d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques                logd("interrupted while trying to update by search");
275d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques            }
276d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques        }
277d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques    }
278d7784628368404ff53caf080640cd9ff32775fd3David Sobreira Marques
27934efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh    private int updateEfForIccType(int efid) {
28034efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh        // Check if we are trying to read ADN records
28134efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh        if (efid == IccConstants.EF_ADN) {
28234efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh            if (phone.getIccCard().isApplicationOnIcc(IccCardApplication.AppType.APPTYPE_USIM)) {
28334efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh                return IccConstants.EF_PBR;
28434efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh            }
28534efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh        }
28634efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh        return efid;
28734efc39f256d5833687c7bd7d83258d6394c9307Jaikumar Ganesh    }
28804e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville}
28904e71b3db84fd5f7fc4eefb49a33154ea91ec9fcWink Saville
290