blob: aab529c15c2e8ca371460867487a20acd6aee715 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
19import android.content.ContentUris;
20import android.content.Context;
21import android.content.Intent;
22import android.database.Cursor;
23import android.net.Uri;
24import android.os.Bundle;
25import android.preference.Preference;
26import android.preference.PreferenceActivity;
27import android.preference.PreferenceCategory;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080028import android.preference.PreferenceGroup;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070029import android.preference.PreferenceScreen;
30import android.provider.Telephony;
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080031import android.text.TextUtils;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070032import android.view.Menu;
33import android.view.MenuItem;
34
35public class ApnSettings extends PreferenceActivity {
36
37 public static final String EXTRA_POSITION = "position";
38
39 private static final int ID_INDEX = 0;
40 private static final int NAME_INDEX = 1;
41 private static final int APN_INDEX = 2;
42
43 private static final int MENU_NEW = Menu.FIRST;
44
45 private Cursor mCursor;
46
47 @Override
48 protected void onCreate(Bundle icicle) {
49 super.onCreate(icicle);
50
51 addPreferencesFromResource(R.xml.apn_settings);
52 }
53
54 @Override
55 protected void onResume() {
56 super.onResume();
57
58 fillList();
59 }
60
61 private void fillList() {
62 mCursor = managedQuery(Telephony.Carriers.CONTENT_URI, new String[] {
63 "_id", "name", "apn"}, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
64
The Android Open Source Project1feaa852009-02-10 15:44:05 -080065 PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070066 apnList.removeAll();
67
68 mCursor.moveToFirst();
69 while (!mCursor.isAfterLast()) {
70 String name = mCursor.getString(NAME_INDEX);
71 String apn = mCursor.getString(APN_INDEX);
72
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080073 if (name != null && apn != null && TextUtils.getTrimmedLength(name) > 0
74 && TextUtils.getTrimmedLength(apn) > 0) {
75 Preference pref = new Preference((Context) this);
76 pref.setKey(mCursor.getString(ID_INDEX));
77 pref.setTitle(name);
78 pref.setSummary(apn);
79 pref.setPersistent(false);
80 apnList.addPreference(pref);
81 }
82
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070083 mCursor.moveToNext();
84 }
85 }
86
87 @Override
88 public boolean onCreateOptionsMenu(Menu menu) {
89 super.onCreateOptionsMenu(menu);
90 menu.add(0, MENU_NEW, 0,
91 getResources().getString(R.string.menu_new))
92 .setIcon(android.R.drawable.ic_menu_add);
93 return true;
94 }
95
96 @Override
97 public boolean onOptionsItemSelected(MenuItem item) {
98 switch (item.getItemId()) {
99 case MENU_NEW:
100 addNewApn();
101 return true;
102 }
103 return super.onOptionsItemSelected(item);
104 }
105
106 private void addNewApn() {
107 startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI));
108 }
109
110 @Override
111 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
112 int pos = Integer.parseInt(preference.getKey());
113 Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
114 startActivity(new Intent(Intent.ACTION_EDIT, url));
115 return true;
116 }
117}