• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings;
18 
19 import android.app.Dialog;
20 import android.app.ProgressDialog;
21 import android.content.BroadcastReceiver;
22 import android.content.ContentResolver;
23 import android.content.ContentUris;
24 import android.content.ContentValues;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.database.Cursor;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.HandlerThread;
33 import android.os.Looper;
34 import android.os.Message;
35 import android.preference.Preference;
36 import android.preference.PreferenceActivity;
37 import android.preference.PreferenceGroup;
38 import android.preference.PreferenceScreen;
39 import android.provider.Telephony;
40 import android.util.Log;
41 import android.view.Menu;
42 import android.view.MenuItem;
43 import android.widget.Toast;
44 
45 import com.android.internal.telephony.Phone;
46 import com.android.internal.telephony.PhoneConstants;
47 import com.android.internal.telephony.TelephonyIntents;
48 import com.android.internal.telephony.TelephonyProperties;
49 
50 import java.util.ArrayList;
51 
52 public class ApnSettings extends PreferenceActivity implements
53         Preference.OnPreferenceChangeListener {
54     static final String TAG = "ApnSettings";
55 
56     public static final String EXTRA_POSITION = "position";
57     public static final String RESTORE_CARRIERS_URI =
58         "content://telephony/carriers/restore";
59     public static final String PREFERRED_APN_URI =
60         "content://telephony/carriers/preferapn";
61 
62     public static final String APN_ID = "apn_id";
63 
64     private static final int ID_INDEX = 0;
65     private static final int NAME_INDEX = 1;
66     private static final int APN_INDEX = 2;
67     private static final int TYPES_INDEX = 3;
68 
69     private static final int MENU_NEW = Menu.FIRST;
70     private static final int MENU_RESTORE = Menu.FIRST + 1;
71 
72     private static final int EVENT_RESTORE_DEFAULTAPN_START = 1;
73     private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2;
74 
75     private static final int DIALOG_RESTORE_DEFAULTAPN = 1001;
76 
77     private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI);
78     private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI);
79 
80     private static boolean mRestoreDefaultApnMode;
81 
82     private RestoreApnUiHandler mRestoreApnUiHandler;
83     private RestoreApnProcessHandler mRestoreApnProcessHandler;
84     private HandlerThread mRestoreDefaultApnThread;
85 
86     private String mSelectedKey;
87 
88     private IntentFilter mMobileStateFilter;
89 
90     private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() {
91         @Override
92         public void onReceive(Context context, Intent intent) {
93             if (intent.getAction().equals(
94                     TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
95                 PhoneConstants.DataState state = getMobileDataState(intent);
96                 switch (state) {
97                 case CONNECTED:
98                     if (!mRestoreDefaultApnMode) {
99                         fillList();
100                     } else {
101                         showDialog(DIALOG_RESTORE_DEFAULTAPN);
102                     }
103                     break;
104                 }
105             }
106         }
107     };
108 
getMobileDataState(Intent intent)109     private static PhoneConstants.DataState getMobileDataState(Intent intent) {
110         String str = intent.getStringExtra(PhoneConstants.STATE_KEY);
111         if (str != null) {
112             return Enum.valueOf(PhoneConstants.DataState.class, str);
113         } else {
114             return PhoneConstants.DataState.DISCONNECTED;
115         }
116     }
117 
118     @Override
onCreate(Bundle icicle)119     protected void onCreate(Bundle icicle) {
120         super.onCreate(icicle);
121 
122         addPreferencesFromResource(R.xml.apn_settings);
123         getListView().setItemsCanFocus(true);
124 
125         mMobileStateFilter = new IntentFilter(
126                 TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
127     }
128 
129     @Override
onResume()130     protected void onResume() {
131         super.onResume();
132 
133         registerReceiver(mMobileStateReceiver, mMobileStateFilter);
134 
135         if (!mRestoreDefaultApnMode) {
136             fillList();
137         } else {
138             showDialog(DIALOG_RESTORE_DEFAULTAPN);
139         }
140     }
141 
142     @Override
onPause()143     protected void onPause() {
144         super.onPause();
145 
146         unregisterReceiver(mMobileStateReceiver);
147     }
148 
149     @Override
onDestroy()150     protected void onDestroy() {
151         super.onDestroy();
152 
153         if (mRestoreDefaultApnThread != null) {
154             mRestoreDefaultApnThread.quit();
155         }
156     }
157 
fillList()158     private void fillList() {
159         String where = "numeric=\""
160             + android.os.SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "")
161             + "\"";
162 
163         Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] {
164                 "_id", "name", "apn", "type"}, where, null,
165                 Telephony.Carriers.DEFAULT_SORT_ORDER);
166 
167         if (cursor != null) {
168             PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
169             apnList.removeAll();
170 
171             ArrayList<Preference> mmsApnList = new ArrayList<Preference>();
172 
173             mSelectedKey = getSelectedApnKey();
174             cursor.moveToFirst();
175             while (!cursor.isAfterLast()) {
176                 String name = cursor.getString(NAME_INDEX);
177                 String apn = cursor.getString(APN_INDEX);
178                 String key = cursor.getString(ID_INDEX);
179                 String type = cursor.getString(TYPES_INDEX);
180 
181                 ApnPreference pref = new ApnPreference(this);
182 
183                 pref.setKey(key);
184                 pref.setTitle(name);
185                 pref.setSummary(apn);
186                 pref.setPersistent(false);
187                 pref.setOnPreferenceChangeListener(this);
188 
189                 boolean selectable = ((type == null) || !type.equals("mms"));
190                 pref.setSelectable(selectable);
191                 if (selectable) {
192                     if ((mSelectedKey != null) && mSelectedKey.equals(key)) {
193                         pref.setChecked();
194                     }
195                     apnList.addPreference(pref);
196                 } else {
197                     mmsApnList.add(pref);
198                 }
199                 cursor.moveToNext();
200             }
201             cursor.close();
202 
203             for (Preference preference : mmsApnList) {
204                 apnList.addPreference(preference);
205             }
206         }
207     }
208 
209     @Override
onCreateOptionsMenu(Menu menu)210     public boolean onCreateOptionsMenu(Menu menu) {
211         super.onCreateOptionsMenu(menu);
212         menu.add(0, MENU_NEW, 0,
213                 getResources().getString(R.string.menu_new))
214                 .setIcon(android.R.drawable.ic_menu_add)
215                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
216         menu.add(0, MENU_RESTORE, 0,
217                 getResources().getString(R.string.menu_restore))
218                 .setIcon(android.R.drawable.ic_menu_upload);
219         return true;
220     }
221 
222     @Override
onOptionsItemSelected(MenuItem item)223     public boolean onOptionsItemSelected(MenuItem item) {
224         switch (item.getItemId()) {
225         case MENU_NEW:
226             addNewApn();
227             return true;
228 
229         case MENU_RESTORE:
230             restoreDefaultApn();
231             return true;
232         }
233         return super.onOptionsItemSelected(item);
234     }
235 
addNewApn()236     private void addNewApn() {
237         startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI));
238     }
239 
240     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)241     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
242         int pos = Integer.parseInt(preference.getKey());
243         Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
244         startActivity(new Intent(Intent.ACTION_EDIT, url));
245         return true;
246     }
247 
onPreferenceChange(Preference preference, Object newValue)248     public boolean onPreferenceChange(Preference preference, Object newValue) {
249         Log.d(TAG, "onPreferenceChange(): Preference - " + preference
250                 + ", newValue - " + newValue + ", newValue type - "
251                 + newValue.getClass());
252         if (newValue instanceof String) {
253             setSelectedApnKey((String) newValue);
254         }
255 
256         return true;
257     }
258 
setSelectedApnKey(String key)259     private void setSelectedApnKey(String key) {
260         mSelectedKey = key;
261         ContentResolver resolver = getContentResolver();
262 
263         ContentValues values = new ContentValues();
264         values.put(APN_ID, mSelectedKey);
265         resolver.update(PREFERAPN_URI, values, null, null);
266     }
267 
getSelectedApnKey()268     private String getSelectedApnKey() {
269         String key = null;
270 
271         Cursor cursor = getContentResolver().query(PREFERAPN_URI, new String[] {"_id"},
272                 null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
273         if (cursor.getCount() > 0) {
274             cursor.moveToFirst();
275             key = cursor.getString(ID_INDEX);
276         }
277         cursor.close();
278         return key;
279     }
280 
restoreDefaultApn()281     private boolean restoreDefaultApn() {
282         showDialog(DIALOG_RESTORE_DEFAULTAPN);
283         mRestoreDefaultApnMode = true;
284 
285         if (mRestoreApnUiHandler == null) {
286             mRestoreApnUiHandler = new RestoreApnUiHandler();
287         }
288 
289         if (mRestoreApnProcessHandler == null ||
290             mRestoreDefaultApnThread == null) {
291             mRestoreDefaultApnThread = new HandlerThread(
292                     "Restore default APN Handler: Process Thread");
293             mRestoreDefaultApnThread.start();
294             mRestoreApnProcessHandler = new RestoreApnProcessHandler(
295                     mRestoreDefaultApnThread.getLooper(), mRestoreApnUiHandler);
296         }
297 
298         mRestoreApnProcessHandler
299                 .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START);
300         return true;
301     }
302 
303     private class RestoreApnUiHandler extends Handler {
304         @Override
handleMessage(Message msg)305         public void handleMessage(Message msg) {
306             switch (msg.what) {
307                 case EVENT_RESTORE_DEFAULTAPN_COMPLETE:
308                     fillList();
309                     getPreferenceScreen().setEnabled(true);
310                     mRestoreDefaultApnMode = false;
311                     dismissDialog(DIALOG_RESTORE_DEFAULTAPN);
312                     Toast.makeText(
313                         ApnSettings.this,
314                         getResources().getString(
315                                 R.string.restore_default_apn_completed),
316                         Toast.LENGTH_LONG).show();
317                     break;
318             }
319         }
320     }
321 
322     private class RestoreApnProcessHandler extends Handler {
323         private Handler mRestoreApnUiHandler;
324 
RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler)325         public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) {
326             super(looper);
327             this.mRestoreApnUiHandler = restoreApnUiHandler;
328         }
329 
330         @Override
handleMessage(Message msg)331         public void handleMessage(Message msg) {
332             switch (msg.what) {
333                 case EVENT_RESTORE_DEFAULTAPN_START:
334                     ContentResolver resolver = getContentResolver();
335                     resolver.delete(DEFAULTAPN_URI, null, null);
336                     mRestoreApnUiHandler
337                         .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE);
338                     break;
339             }
340         }
341     }
342 
343     @Override
onCreateDialog(int id)344     protected Dialog onCreateDialog(int id) {
345         if (id == DIALOG_RESTORE_DEFAULTAPN) {
346             ProgressDialog dialog = new ProgressDialog(this);
347             dialog.setMessage(getResources().getString(R.string.restore_default_apn));
348             dialog.setCancelable(false);
349             return dialog;
350         }
351         return null;
352     }
353 
354     @Override
onPrepareDialog(int id, Dialog dialog)355     protected void onPrepareDialog(int id, Dialog dialog) {
356         if (id == DIALOG_RESTORE_DEFAULTAPN) {
357             getPreferenceScreen().setEnabled(false);
358         }
359     }
360 }
361