TelephonyProvider.java revision 4167fcc61e42c74ea9554153299c3392cd9339bc
1/* //device/content/providers/telephony/TelephonyProvider.java
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18package com.android.providers.telephony;
19
20import android.content.ContentProvider;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.SharedPreferences;
25import android.content.UriMatcher;
26import android.content.res.Resources;
27import android.content.res.XmlResourceParser;
28import android.database.Cursor;
29import android.database.SQLException;
30import android.database.sqlite.SQLiteDatabase;
31import android.database.sqlite.SQLiteOpenHelper;
32import android.database.sqlite.SQLiteQueryBuilder;
33import android.net.Uri;
34import android.os.Environment;
35import android.os.FileUtils;
36import android.provider.Telephony;
37import android.telephony.TelephonyManager;
38import android.util.Log;
39import android.util.Xml;
40
41import com.android.internal.telephony.BaseCommands;
42import com.android.internal.telephony.Phone;
43import com.android.internal.telephony.PhoneConstants;
44import com.android.internal.util.XmlUtils;
45
46import org.xmlpull.v1.XmlPullParser;
47import org.xmlpull.v1.XmlPullParserException;
48
49import java.io.File;
50import java.io.FileNotFoundException;
51import java.io.FileReader;
52import java.io.IOException;
53
54
55public class TelephonyProvider extends ContentProvider
56{
57    private static final String DATABASE_NAME = "telephony.db";
58    private static final boolean DBG = true;
59
60    private static final int DATABASE_VERSION = 7 << 16;
61    private static final int URL_TELEPHONY = 1;
62    private static final int URL_CURRENT = 2;
63    private static final int URL_ID = 3;
64    private static final int URL_RESTOREAPN = 4;
65    private static final int URL_PREFERAPN = 5;
66    private static final int URL_PREFERAPN_NO_UPDATE = 6;
67
68    private static final String TAG = "TelephonyProvider";
69    private static final String CARRIERS_TABLE = "carriers";
70
71    private static final String PREF_FILE = "preferred-apn";
72    private static final String COLUMN_APN_ID = "apn_id";
73    private static final String APN_CONFIG_CHECKSUM = "apn_conf_checksum";
74
75    private static final String PARTNER_APNS_PATH = "etc/apns-conf.xml";
76
77    private static final UriMatcher s_urlMatcher = new UriMatcher(UriMatcher.NO_MATCH);
78
79    private static final ContentValues s_currentNullMap;
80    private static final ContentValues s_currentSetMap;
81
82    static {
83        s_urlMatcher.addURI("telephony", "carriers", URL_TELEPHONY);
84        s_urlMatcher.addURI("telephony", "carriers/current", URL_CURRENT);
85        s_urlMatcher.addURI("telephony", "carriers/#", URL_ID);
86        s_urlMatcher.addURI("telephony", "carriers/restore", URL_RESTOREAPN);
87        s_urlMatcher.addURI("telephony", "carriers/preferapn", URL_PREFERAPN);
88        s_urlMatcher.addURI("telephony", "carriers/preferapn_no_update", URL_PREFERAPN_NO_UPDATE);
89
90        s_currentNullMap = new ContentValues(1);
91        s_currentNullMap.put("current", (Long) null);
92
93        s_currentSetMap = new ContentValues(1);
94        s_currentSetMap.put("current", "1");
95    }
96
97    private static class DatabaseHelper extends SQLiteOpenHelper {
98        // Context to access resources with
99        private Context mContext;
100
101        /**
102         * DatabaseHelper helper class for loading apns into a database.
103         *
104         * @param context of the user.
105         */
106        public DatabaseHelper(Context context) {
107            super(context, DATABASE_NAME, null, getVersion(context));
108            mContext = context;
109        }
110
111        private static int getVersion(Context context) {
112            // Get the database version, combining a static schema version and the XML version
113            Resources r = context.getResources();
114            XmlResourceParser parser = r.getXml(com.android.internal.R.xml.apns);
115            try {
116                XmlUtils.beginDocument(parser, "apns");
117                int publicversion = Integer.parseInt(parser.getAttributeValue(null, "version"));
118                return DATABASE_VERSION | publicversion;
119            } catch (Exception e) {
120                Log.e(TAG, "Can't get version of APN database", e);
121                return DATABASE_VERSION;
122            } finally {
123                parser.close();
124            }
125        }
126
127        @Override
128        public void onCreate(SQLiteDatabase db) {
129            // Set up the database schema
130            db.execSQL("CREATE TABLE " + CARRIERS_TABLE +
131                "(_id INTEGER PRIMARY KEY," +
132                    "name TEXT," +
133                    "numeric TEXT," +
134                    "mcc TEXT," +
135                    "mnc TEXT," +
136                    "apn TEXT," +
137                    "user TEXT," +
138                    "server TEXT," +
139                    "password TEXT," +
140                    "proxy TEXT," +
141                    "port TEXT," +
142                    "mmsproxy TEXT," +
143                    "mmsport TEXT," +
144                    "mmsc TEXT," +
145                    "authtype INTEGER," +
146                    "type TEXT," +
147                    "current INTEGER," +
148                    "protocol TEXT," +
149                    "roaming_protocol TEXT," +
150                    "carrier_enabled BOOLEAN," +
151                    "bearer INTEGER);");
152
153            initDatabase(db);
154        }
155
156        private void initDatabase(SQLiteDatabase db) {
157            // Read internal APNS data
158            Resources r = mContext.getResources();
159            XmlResourceParser parser = r.getXml(com.android.internal.R.xml.apns);
160            int publicversion = -1;
161            try {
162                XmlUtils.beginDocument(parser, "apns");
163                publicversion = Integer.parseInt(parser.getAttributeValue(null, "version"));
164                loadApns(db, parser);
165            } catch (Exception e) {
166                Log.e(TAG, "Got exception while loading APN database.", e);
167            } finally {
168                parser.close();
169            }
170
171           // Read external APNS data (partner-provided)
172            XmlPullParser confparser = null;
173            // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
174            File confFile = new File(Environment.getRootDirectory(), PARTNER_APNS_PATH);
175            FileReader confreader = null;
176            try {
177                confreader = new FileReader(confFile);
178                confparser = Xml.newPullParser();
179                confparser.setInput(confreader);
180                XmlUtils.beginDocument(confparser, "apns");
181
182                // Sanity check. Force internal version and confidential versions to agree
183                int confversion = Integer.parseInt(confparser.getAttributeValue(null, "version"));
184                if (publicversion != confversion) {
185                    throw new IllegalStateException("Internal APNS file version doesn't match "
186                            + confFile.getAbsolutePath());
187                }
188
189                loadApns(db, confparser);
190            } catch (FileNotFoundException e) {
191                // It's ok if the file isn't found. It means there isn't a confidential file
192                // Log.e(TAG, "File not found: '" + confFile.getAbsolutePath() + "'");
193            } catch (Exception e) {
194                Log.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
195            } finally {
196                try { if (confreader != null) confreader.close(); } catch (IOException e) { }
197            }
198        }
199
200        @Override
201        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
202            if (oldVersion < (5 << 16 | 6)) {
203                // 5 << 16 is the Database version and 6 in the xml version.
204
205                // This change adds a new authtype column to the database.
206                // The auth type column can have 4 values: 0 (None), 1 (PAP), 2 (CHAP)
207                // 3 (PAP or CHAP). To avoid breaking compatibility, with already working
208                // APNs, the unset value (-1) will be used. If the value is -1.
209                // the authentication will default to 0 (if no user / password) is specified
210                // or to 3. Currently, there have been no reported problems with
211                // pre-configured APNs and hence it is set to -1 for them. Similarly,
212                // if the user, has added a new APN, we set the authentication type
213                // to -1.
214
215                db.execSQL("ALTER TABLE " + CARRIERS_TABLE +
216                        " ADD COLUMN authtype INTEGER DEFAULT -1;");
217
218                oldVersion = 5 << 16 | 6;
219            }
220            if (oldVersion < (6 << 16 | 6)) {
221                // Add protcol fields to the APN. The XML file does not change.
222                db.execSQL("ALTER TABLE " + CARRIERS_TABLE +
223                        " ADD COLUMN protocol TEXT DEFAULT IP;");
224                db.execSQL("ALTER TABLE " + CARRIERS_TABLE +
225                        " ADD COLUMN roaming_protocol TEXT DEFAULT IP;");
226                oldVersion = 6 << 16 | 6;
227            }
228            if (oldVersion < (7 << 16 | 6)) {
229                // Add protcol fields to the APN. The XML file does not change.
230                db.execSQL("ALTER TABLE " + CARRIERS_TABLE +
231                        " ADD COLUMN carrier_enabled BOOLEAN DEFAULT 1;");
232                db.execSQL("ALTER TABLE " + CARRIERS_TABLE +
233                        " ADD COLUMN bearer INTEGER DEFAULT 0;");
234                oldVersion = 7 << 16 | 6;
235            }
236        }
237
238        /**
239         * Gets the next row of apn values.
240         *
241         * @param parser the parser
242         * @return the row or null if it's not an apn
243         */
244        private ContentValues getRow(XmlPullParser parser) {
245            if (!"apn".equals(parser.getName())) {
246                return null;
247            }
248
249            ContentValues map = new ContentValues();
250
251            String mcc = parser.getAttributeValue(null, "mcc");
252            String mnc = parser.getAttributeValue(null, "mnc");
253            String numeric = mcc + mnc;
254
255            map.put(Telephony.Carriers.NUMERIC,numeric);
256            map.put(Telephony.Carriers.MCC, mcc);
257            map.put(Telephony.Carriers.MNC, mnc);
258            map.put(Telephony.Carriers.NAME, parser.getAttributeValue(null, "carrier"));
259            map.put(Telephony.Carriers.APN, parser.getAttributeValue(null, "apn"));
260            map.put(Telephony.Carriers.USER, parser.getAttributeValue(null, "user"));
261            map.put(Telephony.Carriers.SERVER, parser.getAttributeValue(null, "server"));
262            map.put(Telephony.Carriers.PASSWORD, parser.getAttributeValue(null, "password"));
263
264            // do not add NULL to the map so that insert() will set the default value
265            String proxy = parser.getAttributeValue(null, "proxy");
266            if (proxy != null) {
267                map.put(Telephony.Carriers.PROXY, proxy);
268            }
269            String port = parser.getAttributeValue(null, "port");
270            if (port != null) {
271                map.put(Telephony.Carriers.PORT, port);
272            }
273            String mmsproxy = parser.getAttributeValue(null, "mmsproxy");
274            if (mmsproxy != null) {
275                map.put(Telephony.Carriers.MMSPROXY, mmsproxy);
276            }
277            String mmsport = parser.getAttributeValue(null, "mmsport");
278            if (mmsport != null) {
279                map.put(Telephony.Carriers.MMSPORT, mmsport);
280            }
281            map.put(Telephony.Carriers.MMSC, parser.getAttributeValue(null, "mmsc"));
282            String type = parser.getAttributeValue(null, "type");
283            if (type != null) {
284                map.put(Telephony.Carriers.TYPE, type);
285            }
286
287            String auth = parser.getAttributeValue(null, "authtype");
288            if (auth != null) {
289                map.put(Telephony.Carriers.AUTH_TYPE, Integer.parseInt(auth));
290            }
291
292            String protocol = parser.getAttributeValue(null, "protocol");
293            if (protocol != null) {
294                map.put(Telephony.Carriers.PROTOCOL, protocol);
295            }
296
297            String roamingProtocol = parser.getAttributeValue(null, "roaming_protocol");
298            if (roamingProtocol != null) {
299                map.put(Telephony.Carriers.ROAMING_PROTOCOL, roamingProtocol);
300            }
301
302            String carrierEnabled = parser.getAttributeValue(null, "carrier_enabled");
303            if (carrierEnabled != null) {
304                map.put(Telephony.Carriers.CARRIER_ENABLED, Boolean.parseBoolean(carrierEnabled));
305            }
306
307            String bearer = parser.getAttributeValue(null, "bearer");
308            if (bearer != null) {
309                map.put(Telephony.Carriers.BEARER, Integer.parseInt(bearer));
310            }
311            return map;
312        }
313
314        /*
315         * Loads apns from xml file into the database
316         *
317         * @param db the sqlite database to write to
318         * @param parser the xml parser
319         *
320         */
321        private void loadApns(SQLiteDatabase db, XmlPullParser parser) {
322            if (parser != null) {
323                try {
324                    db.beginTransaction();
325                    while (true) {
326                        XmlUtils.nextElement(parser);
327                        ContentValues row = getRow(parser);
328                        if (row != null) {
329                            insertAddingDefaults(db, CARRIERS_TABLE, row);
330                        } else {
331                            break;  // do we really want to skip the rest of the file?
332                        }
333                    }
334                    db.setTransactionSuccessful();
335                } catch (XmlPullParserException e)  {
336                    Log.e(TAG, "Got execption while getting perferred time zone.", e);
337                } catch (IOException e) {
338                    Log.e(TAG, "Got execption while getting perferred time zone.", e);
339                } catch (SQLException e){
340                    Log.e(TAG, "Got SQLException", e);
341                } finally {
342                    db.endTransaction();
343                }
344            }
345        }
346
347        private void insertAddingDefaults(SQLiteDatabase db, String table, ContentValues row) {
348            // Initialize defaults if any
349            if (row.containsKey(Telephony.Carriers.AUTH_TYPE) == false) {
350                row.put(Telephony.Carriers.AUTH_TYPE, -1);
351            }
352            if (row.containsKey(Telephony.Carriers.PROTOCOL) == false) {
353                row.put(Telephony.Carriers.PROTOCOL, "IP");
354            }
355            if (row.containsKey(Telephony.Carriers.ROAMING_PROTOCOL) == false) {
356                row.put(Telephony.Carriers.ROAMING_PROTOCOL, "IP");
357            }
358            if (row.containsKey(Telephony.Carriers.CARRIER_ENABLED) == false) {
359                row.put(Telephony.Carriers.CARRIER_ENABLED, true);
360            }
361            if (row.containsKey(Telephony.Carriers.BEARER) == false) {
362                row.put(Telephony.Carriers.BEARER, 0);
363            }
364            db.insert(CARRIERS_TABLE, null, row);
365        }
366    }
367
368    @Override
369    public boolean onCreate() {
370        long oldCheckSum = getAPNConfigCheckSum();
371        File confFile = new File(Environment.getRootDirectory(), PARTNER_APNS_PATH);
372        long newCheckSum = -1L;
373
374        if (DBG) {
375            Log.w(TAG, "onCreate: confFile=" + confFile.getAbsolutePath() +
376                    " oldCheckSum=" + oldCheckSum);
377        }
378        mOpenHelper = new DatabaseHelper(getContext());
379
380        if (isLteOnCdma()) {
381            // Check to see if apns-conf.xml file changed. If so, generate db again.
382            //
383            // TODO: Generalize so we can handle apns-conf.xml updates
384            // and preserve any modifications the user might make. For
385            // now its safe on LteOnCdma devices because the user cannot
386            // make changes.
387            try {
388                newCheckSum = FileUtils.checksumCrc32(confFile);
389                if (DBG) Log.w(TAG, "onCreate: newCheckSum=" + newCheckSum);
390                if (oldCheckSum != newCheckSum) {
391                    Log.w(TAG, "Rebuilding Telephony.db");
392                    restoreDefaultAPN();
393                    setAPNConfigCheckSum(newCheckSum);
394                }
395            } catch (FileNotFoundException e) {
396                Log.e(TAG, "FileNotFoundException: '" + confFile.getAbsolutePath() + "'", e);
397            } catch (IOException e) {
398                Log.e(TAG, "IOException: '" + confFile.getAbsolutePath() + "'", e);
399            }
400        }
401        return true;
402    }
403
404    private boolean isLteOnCdma() {
405        return TelephonyManager.getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE;
406    }
407
408    private void setPreferredApnId(Long id) {
409        SharedPreferences sp = getContext().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
410        SharedPreferences.Editor editor = sp.edit();
411        editor.putLong(COLUMN_APN_ID, id != null ? id.longValue() : -1);
412        editor.apply();
413    }
414
415    private long getPreferredApnId() {
416        SharedPreferences sp = getContext().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
417        return sp.getLong(COLUMN_APN_ID, -1);
418    }
419
420    private long getAPNConfigCheckSum() {
421        SharedPreferences sp = getContext().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
422        return sp.getLong(APN_CONFIG_CHECKSUM, -1);
423    }
424
425    private void setAPNConfigCheckSum(long id) {
426        SharedPreferences sp = getContext().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
427        SharedPreferences.Editor editor = sp.edit();
428        editor.putLong(APN_CONFIG_CHECKSUM, id);
429        editor.apply();
430    }
431
432    @Override
433    public Cursor query(Uri url, String[] projectionIn, String selection,
434            String[] selectionArgs, String sort) {
435
436        checkPermission();
437
438        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
439        qb.setTables("carriers");
440
441        int match = s_urlMatcher.match(url);
442        switch (match) {
443            // do nothing
444            case URL_TELEPHONY: {
445                break;
446            }
447
448
449            case URL_CURRENT: {
450                qb.appendWhere("current IS NOT NULL");
451                // do not ignore the selection since MMS may use it.
452                //selection = null;
453                break;
454            }
455
456            case URL_ID: {
457                qb.appendWhere("_id = " + url.getPathSegments().get(1));
458                break;
459            }
460
461            case URL_PREFERAPN:
462            case URL_PREFERAPN_NO_UPDATE: {
463                qb.appendWhere("_id = " + getPreferredApnId());
464                break;
465            }
466
467            default: {
468                return null;
469            }
470        }
471
472        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
473        Cursor ret = qb.query(db, projectionIn, selection, selectionArgs, null, null, sort);
474        ret.setNotificationUri(getContext().getContentResolver(), url);
475        return ret;
476    }
477
478    @Override
479    public String getType(Uri url)
480    {
481        switch (s_urlMatcher.match(url)) {
482        case URL_TELEPHONY:
483            return "vnd.android.cursor.dir/telephony-carrier";
484
485        case URL_ID:
486            return "vnd.android.cursor.item/telephony-carrier";
487
488        case URL_PREFERAPN:
489        case URL_PREFERAPN_NO_UPDATE:
490            return "vnd.android.cursor.item/telephony-carrier";
491
492        default:
493            throw new IllegalArgumentException("Unknown URL " + url);
494        }
495    }
496
497    @Override
498    public Uri insert(Uri url, ContentValues initialValues)
499    {
500        Uri result = null;
501
502        checkPermission();
503
504        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
505        int match = s_urlMatcher.match(url);
506        boolean notify = false;
507        switch (match)
508        {
509            case URL_TELEPHONY:
510            {
511                ContentValues values;
512                if (initialValues != null) {
513                    values = new ContentValues(initialValues);
514                } else {
515                    values = new ContentValues();
516                }
517
518                // TODO Review this. This code should probably not bet here.
519                // It is valid for the database to return a null string.
520                if (!values.containsKey(Telephony.Carriers.NAME)) {
521                    values.put(Telephony.Carriers.NAME, "");
522                }
523                if (!values.containsKey(Telephony.Carriers.APN)) {
524                    values.put(Telephony.Carriers.APN, "");
525                }
526                if (!values.containsKey(Telephony.Carriers.PORT)) {
527                    values.put(Telephony.Carriers.PORT, "");
528                }
529                if (!values.containsKey(Telephony.Carriers.PROXY)) {
530                    values.put(Telephony.Carriers.PROXY, "");
531                }
532                if (!values.containsKey(Telephony.Carriers.USER)) {
533                    values.put(Telephony.Carriers.USER, "");
534                }
535                if (!values.containsKey(Telephony.Carriers.SERVER)) {
536                    values.put(Telephony.Carriers.SERVER, "");
537                }
538                if (!values.containsKey(Telephony.Carriers.PASSWORD)) {
539                    values.put(Telephony.Carriers.PASSWORD, "");
540                }
541                if (!values.containsKey(Telephony.Carriers.MMSPORT)) {
542                    values.put(Telephony.Carriers.MMSPORT, "");
543                }
544                if (!values.containsKey(Telephony.Carriers.MMSPROXY)) {
545                    values.put(Telephony.Carriers.MMSPROXY, "");
546                }
547                if (!values.containsKey(Telephony.Carriers.AUTH_TYPE)) {
548                    values.put(Telephony.Carriers.AUTH_TYPE, -1);
549                }
550                if (!values.containsKey(Telephony.Carriers.PROTOCOL)) {
551                    values.put(Telephony.Carriers.PROTOCOL, "IP");
552                }
553                if (!values.containsKey(Telephony.Carriers.ROAMING_PROTOCOL)) {
554                    values.put(Telephony.Carriers.ROAMING_PROTOCOL, "IP");
555                }
556                if (!values.containsKey(Telephony.Carriers.CARRIER_ENABLED)) {
557                    values.put(Telephony.Carriers.CARRIER_ENABLED, true);
558                }
559                if (!values.containsKey(Telephony.Carriers.BEARER)) {
560                    values.put(Telephony.Carriers.BEARER, 0);
561                }
562
563                long rowID = db.insert(CARRIERS_TABLE, null, values);
564                if (rowID > 0)
565                {
566                    result = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, rowID);
567                    notify = true;
568                }
569
570                if (false) Log.d(TAG, "inserted " + values.toString() + " rowID = " + rowID);
571                break;
572            }
573
574            case URL_CURRENT:
575            {
576                // null out the previous operator
577                db.update("carriers", s_currentNullMap, "current IS NOT NULL", null);
578
579                String numeric = initialValues.getAsString("numeric");
580                int updated = db.update("carriers", s_currentSetMap,
581                        "numeric = '" + numeric + "'", null);
582
583                if (updated > 0)
584                {
585                    if (false) {
586                        Log.d(TAG, "Setting numeric '" + numeric + "' to be the current operator");
587                    }
588                }
589                else
590                {
591                    Log.e(TAG, "Failed setting numeric '" + numeric + "' to the current operator");
592                }
593                break;
594            }
595
596            case URL_PREFERAPN:
597            case URL_PREFERAPN_NO_UPDATE:
598            {
599                if (initialValues != null) {
600                    if(initialValues.containsKey(COLUMN_APN_ID)) {
601                        setPreferredApnId(initialValues.getAsLong(COLUMN_APN_ID));
602                    }
603                }
604                break;
605            }
606        }
607
608        if (notify) {
609            getContext().getContentResolver().notifyChange(Telephony.Carriers.CONTENT_URI, null);
610        }
611
612        return result;
613    }
614
615    @Override
616    public int delete(Uri url, String where, String[] whereArgs)
617    {
618        int count = 0;
619
620        checkPermission();
621
622        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
623        int match = s_urlMatcher.match(url);
624        switch (match)
625        {
626            case URL_TELEPHONY:
627            {
628                count = db.delete(CARRIERS_TABLE, where, whereArgs);
629                break;
630            }
631
632            case URL_CURRENT:
633            {
634                count = db.delete(CARRIERS_TABLE, where, whereArgs);
635                break;
636            }
637
638            case URL_ID:
639            {
640                count = db.delete(CARRIERS_TABLE, Telephony.Carriers._ID + "=?",
641                        new String[] { url.getLastPathSegment() });
642                break;
643            }
644
645            case URL_RESTOREAPN: {
646                count = 1;
647                restoreDefaultAPN();
648                break;
649            }
650
651            case URL_PREFERAPN:
652            case URL_PREFERAPN_NO_UPDATE:
653            {
654                setPreferredApnId((long)-1);
655                if (match == URL_PREFERAPN) count = 1;
656                break;
657            }
658
659            default: {
660                throw new UnsupportedOperationException("Cannot delete that URL: " + url);
661            }
662        }
663
664        if (count > 0) {
665            getContext().getContentResolver().notifyChange(Telephony.Carriers.CONTENT_URI, null);
666        }
667
668        return count;
669    }
670
671    @Override
672    public int update(Uri url, ContentValues values, String where, String[] whereArgs)
673    {
674        int count = 0;
675
676        checkPermission();
677
678        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
679        int match = s_urlMatcher.match(url);
680        switch (match)
681        {
682            case URL_TELEPHONY:
683            {
684                count = db.update(CARRIERS_TABLE, values, where, whereArgs);
685                break;
686            }
687
688            case URL_CURRENT:
689            {
690                count = db.update(CARRIERS_TABLE, values, where, whereArgs);
691                break;
692            }
693
694            case URL_ID:
695            {
696                if (where != null || whereArgs != null) {
697                    throw new UnsupportedOperationException(
698                            "Cannot update URL " + url + " with a where clause");
699                }
700                count = db.update(CARRIERS_TABLE, values, Telephony.Carriers._ID + "=?",
701                        new String[] { url.getLastPathSegment() });
702                break;
703            }
704
705            case URL_PREFERAPN:
706            case URL_PREFERAPN_NO_UPDATE:
707            {
708                if (values != null) {
709                    if (values.containsKey(COLUMN_APN_ID)) {
710                        setPreferredApnId(values.getAsLong(COLUMN_APN_ID));
711                        if (match == URL_PREFERAPN) count = 1;
712                    }
713                }
714                break;
715            }
716
717            default: {
718                throw new UnsupportedOperationException("Cannot update that URL: " + url);
719            }
720        }
721
722        if (count > 0) {
723            getContext().getContentResolver().notifyChange(Telephony.Carriers.CONTENT_URI, null);
724        }
725
726        return count;
727    }
728
729    private void checkPermission() {
730        getContext().enforceCallingOrSelfPermission("android.permission.WRITE_APN_SETTINGS",
731                "No permission to write APN settings");
732    }
733
734    private DatabaseHelper mOpenHelper;
735
736    private void restoreDefaultAPN() {
737        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
738
739        db.delete(CARRIERS_TABLE, null, null);
740        setPreferredApnId((long)-1);
741        mOpenHelper.initDatabase(db);
742    }
743}
744