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
26d720945f2be5ea5fe0faf67e67d9ea0e184eba67Alex Yakavenkaimport com.android.internal.telephony.uicc.AdnRecord;
27d720945f2be5ea5fe0faf67e67d9ea0e184eba67Alex Yakavenkaimport com.android.internal.telephony.uicc.AdnRecordCache;
28d720945f2be5ea5fe0faf67e67d9ea0e184eba67Alex Yakavenkaimport com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
29d720945f2be5ea5fe0faf67e67d9ea0e184eba67Alex Yakavenkaimport com.android.internal.telephony.uicc.IccConstants;
30d720945f2be5ea5fe0faf67e67d9ea0e184eba67Alex Yakavenkaimport com.android.internal.telephony.uicc.IccRecords;
31a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport com.android.internal.telephony.uicc.UiccCard;
32a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport com.android.internal.telephony.uicc.UiccCardApplication;
33e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka
34c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.util.List;
35c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.util.concurrent.atomic.AtomicBoolean;
36c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
37c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/**
38c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * SimPhoneBookInterfaceManager to provide an inter-process communication to
39c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * access ADN-like SIM records.
40c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
41a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savillepublic abstract class IccPhoneBookInterfaceManager {
42c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final boolean DBG = true;
43c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
4422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected PhoneBase mPhone;
45a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private   UiccCardApplication mCurrentApp = null;
4622d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected AdnRecordCache mAdnCache;
47c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected final Object mLock = new Object();
4822d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected int mRecordSize[];
4922d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected boolean mSuccess;
50a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private   boolean mIs3gCard = false;  // flag to determine if card is 3G or 2G
5122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected List<AdnRecord> mRecords;
52c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
53a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
54c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final boolean ALLOW_SIM_OP_IN_UI_THREAD = false;
55c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
56c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final int EVENT_GET_SIZE_DONE = 1;
57c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final int EVENT_LOAD_DONE = 2;
58c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected static final int EVENT_UPDATE_DONE = 3;
59c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
60c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected Handler mBaseHandler = new Handler() {
61c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        @Override
62c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        public void handleMessage(Message msg) {
63c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AsyncResult ar;
64c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
65c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            switch (msg.what) {
66c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                case EVENT_GET_SIZE_DONE:
67c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    ar = (AsyncResult) msg.obj;
68c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    synchronized (mLock) {
69c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        if (ar.exception == null) {
7022d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                            mRecordSize = (int[])ar.result;
71c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            // recordSize[0]  is the record length
72c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            // recordSize[1]  is the total length of the EF file
73c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            // recordSize[2]  is the number of records in the EF file
7422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                            logd("GET_RECORD_SIZE Size " + mRecordSize[0] +
7522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                                    " total " + mRecordSize[1] +
7622d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                                    " #record " + mRecordSize[2]);
77c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        }
78c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        notifyPending(ar);
79c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
80c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    break;
81c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                case EVENT_UPDATE_DONE:
82c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    ar = (AsyncResult) msg.obj;
83c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    synchronized (mLock) {
8422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                        mSuccess = (ar.exception == null);
85c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        notifyPending(ar);
86c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
87c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    break;
88c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                case EVENT_LOAD_DONE:
89c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    ar = (AsyncResult)msg.obj;
90c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    synchronized (mLock) {
91c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        if (ar.exception == null) {
9222d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                            mRecords = (List<AdnRecord>) ar.result;
93c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        } else {
94c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            if(DBG) logd("Cannot load ADN records");
9522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                            if (mRecords != null) {
9622d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                                mRecords.clear();
97c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                            }
98c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        }
99c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        notifyPending(ar);
100c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
101c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    break;
102c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
103c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
104c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
105c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        private void notifyPending(AsyncResult ar) {
106145c68db12c0fba4c6e1ff67329f02f29fc683e2Amit Mahajan            if (ar.userObj != null) {
107145c68db12c0fba4c6e1ff67329f02f29fc683e2Amit Mahajan                AtomicBoolean status = (AtomicBoolean) ar.userObj;
108145c68db12c0fba4c6e1ff67329f02f29fc683e2Amit Mahajan                status.set(true);
109c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
110c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            mLock.notifyAll();
111c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
112c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    };
113c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
114c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public IccPhoneBookInterfaceManager(PhoneBase phone) {
11522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        this.mPhone = phone;
116bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        IccRecords r = phone.mIccRecords.get();
117bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        if (r != null) {
11822d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mAdnCache = r.getAdnCache();
119bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        }
120c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
121c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
122c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public void dispose() {
123c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
124c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
125bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka    public void updateIccRecords(IccRecords iccRecords) {
126bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        if (iccRecords != null) {
12722d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mAdnCache = iccRecords.getAdnCache();
128bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        } else {
12922d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mAdnCache = null;
130bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka        }
131bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka    }
132bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenka
133c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected abstract void logd(String msg);
134c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
135c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected abstract void loge(String msg);
136c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
137c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
138c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Replace oldAdn with newAdn in ADN-like record in EF
139c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
140c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * getAdnRecordsInEf must be called at least once before this function,
141c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * otherwise an error will be returned. Currently the email field
142c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * if set in the ADN record is ignored.
143c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * throws SecurityException if no WRITE_CONTACTS permission
144c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
145c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid must be one among EF_ADN, EF_FDN, and EF_SDN
146c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param oldTag adn tag to be replaced
147c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param oldPhoneNumber adn number to be replaced
148c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        Set both oldTag and oldPhoneNubmer to "" means to replace an
149c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        empty record, aka, insert new record
150c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newTag adn tag to be stored
151c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newPhoneNumber adn number ot be stored
152c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        Set both newTag and newPhoneNubmer to "" means to replace the old
153c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        record with empty one, aka, delete old record
154c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param pin2 required to update EF_FDN, otherwise must be null
155c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return true for success
156c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
157c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public boolean
158c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    updateAdnRecordsInEfBySearch (int efid,
159c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            String oldTag, String oldPhoneNumber,
160c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            String newTag, String newPhoneNumber, String pin2) {
161c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
162c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
16322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (mPhone.getContext().checkCallingOrSelfPermission(
164c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                android.Manifest.permission.WRITE_CONTACTS)
165c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            != PackageManager.PERMISSION_GRANTED) {
166c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            throw new SecurityException(
167c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    "Requires android.permission.WRITE_CONTACTS permission");
168c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
169c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
170c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
171c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) logd("updateAdnRecordsInEfBySearch: efid=" + efid +
172c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                " ("+ oldTag + "," + oldPhoneNumber + ")"+ "==>" +
173c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                " ("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);
174c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
175c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        efid = updateEfForIccType(efid);
176c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
177c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        synchronized(mLock) {
178c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            checkThread();
17922d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mSuccess = false;
180c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = new AtomicBoolean(false);
181c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Message response = mBaseHandler.obtainMessage(EVENT_UPDATE_DONE, status);
182c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AdnRecord oldAdn = new AdnRecord(oldTag, oldPhoneNumber);
183c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AdnRecord newAdn = new AdnRecord(newTag, newPhoneNumber);
184f16c95f5e7e34e7e3a51a012d172902365a3682eAlex Yakavenka            if (mAdnCache != null) {
185f16c95f5e7e34e7e3a51a012d172902365a3682eAlex Yakavenka                mAdnCache.updateAdnBySearch(efid, oldAdn, newAdn, pin2, response);
18698437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka                waitForResult(status);
18798437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka            } else {
18898437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka                loge("Failure while trying to update by search due to uninitialised adncache");
18998437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka            }
190c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
19122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        return mSuccess;
192c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
193c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
194c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
195c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Update an ADN-like EF record by record index
196c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
197c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * This is useful for iteration the whole ADN file, such as write the whole
198c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * phone book or erase/format the whole phonebook. Currently the email field
199c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * if set in the ADN record is ignored.
200c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * throws SecurityException if no WRITE_CONTACTS permission
201c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
202c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid must be one among EF_ADN, EF_FDN, and EF_SDN
203c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newTag adn tag to be stored
204c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param newPhoneNumber adn number to be stored
205c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        Set both newTag and newPhoneNubmer to "" means to replace the old
206c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *        record with empty one, aka, delete old record
207c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param index is 1-based adn record index to be updated
208c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param pin2 required to update EF_FDN, otherwise must be null
209c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return true for success
210c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
211c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public boolean
212c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    updateAdnRecordsInEfByIndex(int efid, String newTag,
213c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            String newPhoneNumber, int index, String pin2) {
214c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
21522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (mPhone.getContext().checkCallingOrSelfPermission(
216c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                android.Manifest.permission.WRITE_CONTACTS)
217c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                != PackageManager.PERMISSION_GRANTED) {
218c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            throw new SecurityException(
219c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    "Requires android.permission.WRITE_CONTACTS permission");
220c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
221c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
222c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) logd("updateAdnRecordsInEfByIndex: efid=" + efid +
223c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                " Index=" + index + " ==> " +
224c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                "("+ newTag + "," + newPhoneNumber + ")"+ " pin2=" + pin2);
225c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        synchronized(mLock) {
226c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            checkThread();
22722d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mSuccess = false;
228c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = new AtomicBoolean(false);
229c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Message response = mBaseHandler.obtainMessage(EVENT_UPDATE_DONE, status);
230c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AdnRecord newAdn = new AdnRecord(newTag, newPhoneNumber);
231f16c95f5e7e34e7e3a51a012d172902365a3682eAlex Yakavenka            if (mAdnCache != null) {
232f16c95f5e7e34e7e3a51a012d172902365a3682eAlex Yakavenka                mAdnCache.updateAdnByIndex(efid, newAdn, index, pin2, response);
23398437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka                waitForResult(status);
23498437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka            } else {
23598437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka                loge("Failure while trying to update by index due to uninitialised adncache");
23698437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka            }
237c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
23822d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        return mSuccess;
239c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
240c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
241c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
242c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Get the capacity of records in efid
243c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
244c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid the EF id of a ADN-like ICC
245c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return  int[3] array
246c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *            recordSizes[0]  is the single record length
247c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *            recordSizes[1]  is the total length of the EF file
248c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *            recordSizes[2]  is the number of records in the EF file
249c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
250c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public abstract int[] getAdnRecordsSize(int efid);
251c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
252c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
253c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * Loads the AdnRecords in efid and returns them as a
254c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * List of AdnRecords
255c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
256c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * throws SecurityException if no READ_CONTACTS permission
257c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
258c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @param efid the EF id of a ADN-like ICC
259c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     * @return List of AdnRecord
260c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
261c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public List<AdnRecord> getAdnRecordsInEf(int efid) {
262c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
26322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (mPhone.getContext().checkCallingOrSelfPermission(
264c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                android.Manifest.permission.READ_CONTACTS)
265c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                != PackageManager.PERMISSION_GRANTED) {
266c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            throw new SecurityException(
267c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    "Requires android.permission.READ_CONTACTS permission");
268c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
269c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
270c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        efid = updateEfForIccType(efid);
271c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (DBG) logd("getAdnRecordsInEF: efid=" + efid);
272c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
273c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        synchronized(mLock) {
274c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            checkThread();
275c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            AtomicBoolean status = new AtomicBoolean(false);
276c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            Message response = mBaseHandler.obtainMessage(EVENT_LOAD_DONE, status);
277f16c95f5e7e34e7e3a51a012d172902365a3682eAlex Yakavenka            if (mAdnCache != null) {
278f16c95f5e7e34e7e3a51a012d172902365a3682eAlex Yakavenka                mAdnCache.requestLoadAllAdnLike(efid, mAdnCache.extensionEfForEf(efid), response);
27998437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka                waitForResult(status);
28098437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka            } else {
28198437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka                loge("Failure while trying to load from SIM due to uninitialised adncache");
28298437235fc30d809992c32410f02d22cc89fac8cAlex Yakavenka            }
283c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
28422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        return mRecords;
285c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
286c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
287c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected void checkThread() {
288c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (!ALLOW_SIM_OP_IN_UI_THREAD) {
289c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            // Make sure this isn't the UI thread, since it will block
290c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            if (mBaseHandler.getLooper().equals(Looper.myLooper())) {
291c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                loge("query() called on the main UI thread!");
292c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                throw new IllegalStateException(
293c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                        "You cannot call query on this provder from the main UI thread.");
294c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
295c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
296c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
297c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
298c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    protected void waitForResult(AtomicBoolean status) {
299c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        while (!status.get()) {
300c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            try {
301c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                mLock.wait();
302c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            } catch (InterruptedException e) {
303c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                logd("interrupted while trying to update by search");
304c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
305c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
306c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
307c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
308c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    private int updateEfForIccType(int efid) {
309c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        // Check if we are trying to read ADN records
310c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        if (efid == IccConstants.EF_ADN) {
31122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            if (mPhone.getCurrentUiccAppType() == AppType.APPTYPE_USIM) {
312c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                return IccConstants.EF_PBR;
313c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
314c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
315c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return efid;
316c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
317c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville}
318c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
319