ApnSettings.java revision a578a6c4ad2924236a8882e3eb794474d57ca450
17898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project/*
27898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
37898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
47898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
57898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * you may not use this file except in compliance with the License.
67898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * You may obtain a copy of the License at
77898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
87898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
97898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project *
107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * See the License for the specific language governing permissions and
147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project * limitations under the License.
157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project */
167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectpackage com.android.settings;
187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
197898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.content.ContentUris;
207898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.content.Context;
217898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.content.Intent;
227898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.database.Cursor;
237898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.net.Uri;
247898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.os.Bundle;
257898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.preference.Preference;
267898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.preference.PreferenceActivity;
277898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.preference.PreferenceCategory;
287898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.preference.PreferenceGroup;
297898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.preference.PreferenceScreen;
307898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.provider.Telephony;
317898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.text.TextUtils;
327898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.view.Menu;
337898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectimport android.view.MenuItem;
347898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
357898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Projectpublic class ApnSettings extends PreferenceActivity {
367898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
377898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    public static final String EXTRA_POSITION = "position";
387898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
397898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private static final int ID_INDEX = 0;
407898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private static final int NAME_INDEX = 1;
417898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private static final int APN_INDEX = 2;
427898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
437898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private static final int MENU_NEW = Menu.FIRST;
447898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
457898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private Cursor mCursor;
467898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
477898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    @Override
487898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    protected void onCreate(Bundle icicle) {
497898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        super.onCreate(icicle);
507898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
517898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        addPreferencesFromResource(R.xml.apn_settings);
527898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
537898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
547898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    @Override
557898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    protected void onResume() {
567898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        super.onResume();
577898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
587898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        fillList();
597898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
607898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
617898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private void fillList() {
627898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        mCursor = managedQuery(Telephony.Carriers.CONTENT_URI, new String[] {
637898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                "_id", "name", "apn"}, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
647898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
657898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
667898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        apnList.removeAll();
677898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
687898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        mCursor.moveToFirst();
697898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        while (!mCursor.isAfterLast()) {
707898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            String name = mCursor.getString(NAME_INDEX);
717898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            String apn = mCursor.getString(APN_INDEX);
727898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
737898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            if (name != null && apn != null && TextUtils.getTrimmedLength(name) > 0
747898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                    && TextUtils.getTrimmedLength(apn) > 0) {
757898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                Preference pref = new Preference((Context) this);
767898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                pref.setKey(mCursor.getString(ID_INDEX));
777898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                pref.setTitle(name);
787898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                pref.setSummary(apn);
797898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                pref.setPersistent(false);
807898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                apnList.addPreference(pref);
817898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            }
827898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
837898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            mCursor.moveToNext();
847898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        }
857898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
867898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
877898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    @Override
887898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    public boolean onCreateOptionsMenu(Menu menu) {
897898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        super.onCreateOptionsMenu(menu);
907898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        menu.add(0, MENU_NEW, 0,
917898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                getResources().getString(R.string.menu_new))
927898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project                .setIcon(android.R.drawable.ic_menu_add);
937898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        return true;
947898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
957898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
967898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    @Override
977898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
987898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        switch (item.getItemId()) {
997898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        case MENU_NEW:
1007898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            addNewApn();
1017898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project            return true;
1027898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        }
1037898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        return super.onOptionsItemSelected(item);
1047898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
1057898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1067898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    private void addNewApn() {
1077898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI));
1087898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
1097898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project
1107898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    @Override
1117898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
1127898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        int pos = Integer.parseInt(preference.getKey());
1137898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
1147898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        startActivity(new Intent(Intent.ACTION_EDIT, url));
1157898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project        return true;
1167898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project    }
1177898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project}
1187898d76cc005bbe1c5893a9f57439561e0771ccThe Android Open Source Project