1afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/*
2afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
4afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * you may not use this file except in compliance with the License.
6afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * You may obtain a copy of the License at
7afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
8afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
10afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * See the License for the specific language governing permissions and
14afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * limitations under the License.
15afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
16afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
17afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectpackage com.android.settings;
18afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
19afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.Dialog;
20afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.ProgressDialog;
21682968870616d9e9540980921a5302836c45da33John Huangimport android.content.BroadcastReceiver;
22afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.ContentResolver;
23afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.ContentUris;
24682968870616d9e9540980921a5302836c45da33John Huangimport android.content.ContentValues;
25afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Context;
26afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Intent;
27682968870616d9e9540980921a5302836c45da33John Huangimport android.content.IntentFilter;
28afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.database.Cursor;
29afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.net.Uri;
30afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Bundle;
31afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Handler;
32afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.HandlerThread;
33afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Looper;
34afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Message;
35afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.preference.Preference;
36afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.preference.PreferenceActivity;
37afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.preference.PreferenceGroup;
38afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.preference.PreferenceScreen;
39afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.provider.Telephony;
40682968870616d9e9540980921a5302836c45da33John Huangimport android.util.Log;
41afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.Menu;
42afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.MenuItem;
43afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.Toast;
44afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
45682968870616d9e9540980921a5302836c45da33John Huangimport com.android.internal.telephony.Phone;
46682968870616d9e9540980921a5302836c45da33John Huangimport com.android.internal.telephony.TelephonyIntents;
47682968870616d9e9540980921a5302836c45da33John Huangimport com.android.internal.telephony.TelephonyProperties;
48682968870616d9e9540980921a5302836c45da33John Huang
49682968870616d9e9540980921a5302836c45da33John Huangimport java.util.ArrayList;
50682968870616d9e9540980921a5302836c45da33John Huang
51682968870616d9e9540980921a5302836c45da33John Huangpublic class ApnSettings extends PreferenceActivity implements
52682968870616d9e9540980921a5302836c45da33John Huang        Preference.OnPreferenceChangeListener {
53682968870616d9e9540980921a5302836c45da33John Huang    static final String TAG = "ApnSettings";
54afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
55afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public static final String EXTRA_POSITION = "position";
56afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public static final String RESTORE_CARRIERS_URI =
57afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        "content://telephony/carriers/restore";
58682968870616d9e9540980921a5302836c45da33John Huang    public static final String PREFERRED_APN_URI =
59682968870616d9e9540980921a5302836c45da33John Huang        "content://telephony/carriers/preferapn";
60682968870616d9e9540980921a5302836c45da33John Huang
61682968870616d9e9540980921a5302836c45da33John Huang    public static final String APN_ID = "apn_id";
62afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
63afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int ID_INDEX = 0;
64afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int NAME_INDEX = 1;
65afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int APN_INDEX = 2;
66682968870616d9e9540980921a5302836c45da33John Huang    private static final int TYPES_INDEX = 3;
67afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
68afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int MENU_NEW = Menu.FIRST;
69afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int MENU_RESTORE = Menu.FIRST + 1;
70afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
71afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int EVENT_RESTORE_DEFAULTAPN_START = 1;
72afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int EVENT_RESTORE_DEFAULTAPN_COMPLETE = 2;
73afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
74afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int DIALOG_RESTORE_DEFAULTAPN = 1001;
75afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
76afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final Uri DEFAULTAPN_URI = Uri.parse(RESTORE_CARRIERS_URI);
77682968870616d9e9540980921a5302836c45da33John Huang    private static final Uri PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI);
78afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
79afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static boolean mRestoreDefaultApnMode;
80afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
81afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private RestoreApnUiHandler mRestoreApnUiHandler;
82afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private RestoreApnProcessHandler mRestoreApnProcessHandler;
83afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
84682968870616d9e9540980921a5302836c45da33John Huang    private String mSelectedKey;
85682968870616d9e9540980921a5302836c45da33John Huang
86682968870616d9e9540980921a5302836c45da33John Huang    private IntentFilter mMobileStateFilter;
87682968870616d9e9540980921a5302836c45da33John Huang
88682968870616d9e9540980921a5302836c45da33John Huang    private final BroadcastReceiver mMobileStateReceiver = new BroadcastReceiver() {
89682968870616d9e9540980921a5302836c45da33John Huang        @Override
90682968870616d9e9540980921a5302836c45da33John Huang        public void onReceive(Context context, Intent intent) {
91682968870616d9e9540980921a5302836c45da33John Huang            if (intent.getAction().equals(
92682968870616d9e9540980921a5302836c45da33John Huang                    TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
93682968870616d9e9540980921a5302836c45da33John Huang                Phone.DataState state = getMobileDataState(intent);
94682968870616d9e9540980921a5302836c45da33John Huang                switch (state) {
95682968870616d9e9540980921a5302836c45da33John Huang                case CONNECTED:
96682968870616d9e9540980921a5302836c45da33John Huang                    if (!mRestoreDefaultApnMode) {
97682968870616d9e9540980921a5302836c45da33John Huang                        fillList();
98682968870616d9e9540980921a5302836c45da33John Huang                    } else {
99682968870616d9e9540980921a5302836c45da33John Huang                        showDialog(DIALOG_RESTORE_DEFAULTAPN);
100682968870616d9e9540980921a5302836c45da33John Huang                    }
101682968870616d9e9540980921a5302836c45da33John Huang                    break;
102682968870616d9e9540980921a5302836c45da33John Huang                }
103682968870616d9e9540980921a5302836c45da33John Huang            }
104682968870616d9e9540980921a5302836c45da33John Huang        }
105682968870616d9e9540980921a5302836c45da33John Huang    };
106682968870616d9e9540980921a5302836c45da33John Huang
107682968870616d9e9540980921a5302836c45da33John Huang    private static Phone.DataState getMobileDataState(Intent intent) {
108682968870616d9e9540980921a5302836c45da33John Huang        String str = intent.getStringExtra(Phone.STATE_KEY);
109682968870616d9e9540980921a5302836c45da33John Huang        if (str != null) {
110682968870616d9e9540980921a5302836c45da33John Huang            return Enum.valueOf(Phone.DataState.class, str);
111682968870616d9e9540980921a5302836c45da33John Huang        } else {
112682968870616d9e9540980921a5302836c45da33John Huang            return Phone.DataState.DISCONNECTED;
113682968870616d9e9540980921a5302836c45da33John Huang        }
114682968870616d9e9540980921a5302836c45da33John Huang    }
115afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
116afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
117afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    protected void onCreate(Bundle icicle) {
118afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onCreate(icicle);
119afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
120afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        addPreferencesFromResource(R.xml.apn_settings);
121682968870616d9e9540980921a5302836c45da33John Huang        getListView().setItemsCanFocus(true);
122682968870616d9e9540980921a5302836c45da33John Huang
123682968870616d9e9540980921a5302836c45da33John Huang        mMobileStateFilter = new IntentFilter(
124682968870616d9e9540980921a5302836c45da33John Huang                TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
125afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
126afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
127afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
128afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    protected void onResume() {
129afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onResume();
130afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
131682968870616d9e9540980921a5302836c45da33John Huang        registerReceiver(mMobileStateReceiver, mMobileStateFilter);
132682968870616d9e9540980921a5302836c45da33John Huang
133afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (!mRestoreDefaultApnMode) {
134afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            fillList();
135afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        } else {
136afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            showDialog(DIALOG_RESTORE_DEFAULTAPN);
137afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
138afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
139afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
140682968870616d9e9540980921a5302836c45da33John Huang    @Override
141682968870616d9e9540980921a5302836c45da33John Huang    protected void onPause() {
142682968870616d9e9540980921a5302836c45da33John Huang        super.onPause();
143682968870616d9e9540980921a5302836c45da33John Huang
144682968870616d9e9540980921a5302836c45da33John Huang        unregisterReceiver(mMobileStateReceiver);
145682968870616d9e9540980921a5302836c45da33John Huang    }
146682968870616d9e9540980921a5302836c45da33John Huang
147afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private void fillList() {
148682968870616d9e9540980921a5302836c45da33John Huang        String where = "numeric=\""
149c38866843e966622346dc8f7b028749cac9c2198Wink Saville            + android.os.SystemProperties.get(TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC, "")
150682968870616d9e9540980921a5302836c45da33John Huang            + "\"";
151682968870616d9e9540980921a5302836c45da33John Huang
152682968870616d9e9540980921a5302836c45da33John Huang        Cursor cursor = managedQuery(Telephony.Carriers.CONTENT_URI, new String[] {
153682968870616d9e9540980921a5302836c45da33John Huang                "_id", "name", "apn", "type"}, where,
154682968870616d9e9540980921a5302836c45da33John Huang                Telephony.Carriers.DEFAULT_SORT_ORDER);
155afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
156afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
157afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        apnList.removeAll();
158afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
159682968870616d9e9540980921a5302836c45da33John Huang        ArrayList<Preference> mmsApnList = new ArrayList<Preference>();
160682968870616d9e9540980921a5302836c45da33John Huang
161682968870616d9e9540980921a5302836c45da33John Huang        mSelectedKey = getSelectedApnKey();
162682968870616d9e9540980921a5302836c45da33John Huang        cursor.moveToFirst();
163682968870616d9e9540980921a5302836c45da33John Huang        while (!cursor.isAfterLast()) {
164682968870616d9e9540980921a5302836c45da33John Huang            String name = cursor.getString(NAME_INDEX);
165682968870616d9e9540980921a5302836c45da33John Huang            String apn = cursor.getString(APN_INDEX);
166682968870616d9e9540980921a5302836c45da33John Huang            String key = cursor.getString(ID_INDEX);
167682968870616d9e9540980921a5302836c45da33John Huang            String type = cursor.getString(TYPES_INDEX);
168682968870616d9e9540980921a5302836c45da33John Huang
169682968870616d9e9540980921a5302836c45da33John Huang            ApnPreference pref = new ApnPreference(this);
170682968870616d9e9540980921a5302836c45da33John Huang
171682968870616d9e9540980921a5302836c45da33John Huang            pref.setKey(key);
172682968870616d9e9540980921a5302836c45da33John Huang            pref.setTitle(name);
173682968870616d9e9540980921a5302836c45da33John Huang            pref.setSummary(apn);
174682968870616d9e9540980921a5302836c45da33John Huang            pref.setPersistent(false);
175682968870616d9e9540980921a5302836c45da33John Huang            pref.setOnPreferenceChangeListener(this);
176682968870616d9e9540980921a5302836c45da33John Huang
177682968870616d9e9540980921a5302836c45da33John Huang            boolean selectable = ((type == null) || !type.equals("mms"));
178682968870616d9e9540980921a5302836c45da33John Huang            pref.setSelectable(selectable);
179682968870616d9e9540980921a5302836c45da33John Huang            if (selectable) {
180682968870616d9e9540980921a5302836c45da33John Huang                if ((mSelectedKey != null) && mSelectedKey.equals(key)) {
1818762093e0094e09493fa6cbac8e8fbf265f18165Michael Chan                    pref.setChecked();
182682968870616d9e9540980921a5302836c45da33John Huang                }
183afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                apnList.addPreference(pref);
184682968870616d9e9540980921a5302836c45da33John Huang            } else {
185682968870616d9e9540980921a5302836c45da33John Huang                mmsApnList.add(pref);
186afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
187682968870616d9e9540980921a5302836c45da33John Huang            cursor.moveToNext();
188682968870616d9e9540980921a5302836c45da33John Huang        }
189682968870616d9e9540980921a5302836c45da33John Huang        cursor.close();
190afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
191682968870616d9e9540980921a5302836c45da33John Huang        for (Preference preference : mmsApnList) {
192682968870616d9e9540980921a5302836c45da33John Huang            apnList.addPreference(preference);
193afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
194afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
195afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
196afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
197afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public boolean onCreateOptionsMenu(Menu menu) {
198afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onCreateOptionsMenu(menu);
199afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        menu.add(0, MENU_NEW, 0,
200afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                getResources().getString(R.string.menu_new))
201afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                .setIcon(android.R.drawable.ic_menu_add);
202afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        menu.add(0, MENU_RESTORE, 0,
203afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                getResources().getString(R.string.menu_restore))
204afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                .setIcon(android.R.drawable.ic_menu_upload);
205afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return true;
206afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
207afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
208afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
209afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
210afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        switch (item.getItemId()) {
211afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        case MENU_NEW:
212afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            addNewApn();
213afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return true;
214afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
215afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        case MENU_RESTORE:
216afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            restoreDefaultApn();
217afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return true;
218afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
219afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return super.onOptionsItemSelected(item);
220afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
221afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
222afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private void addNewApn() {
223afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        startActivity(new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI));
224afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
225afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
226afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
227afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
228afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        int pos = Integer.parseInt(preference.getKey());
229afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        Uri url = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, pos);
230afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        startActivity(new Intent(Intent.ACTION_EDIT, url));
231afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return true;
232afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
233afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
234682968870616d9e9540980921a5302836c45da33John Huang    public boolean onPreferenceChange(Preference preference, Object newValue) {
235682968870616d9e9540980921a5302836c45da33John Huang        Log.d(TAG, "onPreferenceChange(): Preference - " + preference
236682968870616d9e9540980921a5302836c45da33John Huang                + ", newValue - " + newValue + ", newValue type - "
237682968870616d9e9540980921a5302836c45da33John Huang                + newValue.getClass());
238682968870616d9e9540980921a5302836c45da33John Huang        if (newValue instanceof String) {
239682968870616d9e9540980921a5302836c45da33John Huang            setSelectedApnKey((String) newValue);
240682968870616d9e9540980921a5302836c45da33John Huang        }
241682968870616d9e9540980921a5302836c45da33John Huang
242682968870616d9e9540980921a5302836c45da33John Huang        return true;
243682968870616d9e9540980921a5302836c45da33John Huang    }
244682968870616d9e9540980921a5302836c45da33John Huang
245682968870616d9e9540980921a5302836c45da33John Huang    private void setSelectedApnKey(String key) {
246682968870616d9e9540980921a5302836c45da33John Huang        mSelectedKey = key;
247682968870616d9e9540980921a5302836c45da33John Huang        ContentResolver resolver = getContentResolver();
248682968870616d9e9540980921a5302836c45da33John Huang
249682968870616d9e9540980921a5302836c45da33John Huang        ContentValues values = new ContentValues();
250682968870616d9e9540980921a5302836c45da33John Huang        values.put(APN_ID, mSelectedKey);
251682968870616d9e9540980921a5302836c45da33John Huang        resolver.update(PREFERAPN_URI, values, null, null);
252682968870616d9e9540980921a5302836c45da33John Huang    }
253682968870616d9e9540980921a5302836c45da33John Huang
254682968870616d9e9540980921a5302836c45da33John Huang    private String getSelectedApnKey() {
255682968870616d9e9540980921a5302836c45da33John Huang        String key = null;
256682968870616d9e9540980921a5302836c45da33John Huang
257682968870616d9e9540980921a5302836c45da33John Huang        Cursor cursor = managedQuery(PREFERAPN_URI, new String[] {"_id"},
258682968870616d9e9540980921a5302836c45da33John Huang                null, Telephony.Carriers.DEFAULT_SORT_ORDER);
259682968870616d9e9540980921a5302836c45da33John Huang        if (cursor.getCount() > 0) {
260682968870616d9e9540980921a5302836c45da33John Huang            cursor.moveToFirst();
261682968870616d9e9540980921a5302836c45da33John Huang            key = cursor.getString(ID_INDEX);
262682968870616d9e9540980921a5302836c45da33John Huang        }
263682968870616d9e9540980921a5302836c45da33John Huang        cursor.close();
264682968870616d9e9540980921a5302836c45da33John Huang        return key;
265682968870616d9e9540980921a5302836c45da33John Huang    }
266682968870616d9e9540980921a5302836c45da33John Huang
267afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private boolean restoreDefaultApn() {
268afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        showDialog(DIALOG_RESTORE_DEFAULTAPN);
269afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mRestoreDefaultApnMode = true;
270afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
271afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (mRestoreApnUiHandler == null) {
272afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            mRestoreApnUiHandler = new RestoreApnUiHandler();
273afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
274afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
275afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (mRestoreApnProcessHandler == null) {
276afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            HandlerThread restoreDefaultApnThread = new HandlerThread(
277afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    "Restore default APN Handler: Process Thread");
278afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            restoreDefaultApnThread.start();
279afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            mRestoreApnProcessHandler = new RestoreApnProcessHandler(
280afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    restoreDefaultApnThread.getLooper(), mRestoreApnUiHandler);
281afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
282afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
283afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mRestoreApnProcessHandler
284afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_START);
285afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return true;
286afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
287afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
288afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private class RestoreApnUiHandler extends Handler {
289afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        @Override
290afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public void handleMessage(Message msg) {
291afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            switch (msg.what) {
292afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                case EVENT_RESTORE_DEFAULTAPN_COMPLETE:
293afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    fillList();
294afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    getPreferenceScreen().setEnabled(true);
295afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    mRestoreDefaultApnMode = false;
296afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    dismissDialog(DIALOG_RESTORE_DEFAULTAPN);
297afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    Toast.makeText(
298afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                        ApnSettings.this,
299afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                        getResources().getString(
300afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                                R.string.restore_default_apn_completed),
301afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                        Toast.LENGTH_LONG).show();
302afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    break;
303afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
304afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
305afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
306afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
307afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private class RestoreApnProcessHandler extends Handler {
308afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        private Handler mRestoreApnUiHandler;
309afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
310afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public RestoreApnProcessHandler(Looper looper, Handler restoreApnUiHandler) {
311afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            super(looper);
312afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            this.mRestoreApnUiHandler = restoreApnUiHandler;
313afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
314afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
315afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        @Override
316afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public void handleMessage(Message msg) {
317afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            switch (msg.what) {
318afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                case EVENT_RESTORE_DEFAULTAPN_START:
319afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    ContentResolver resolver = getContentResolver();
320afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    resolver.delete(DEFAULTAPN_URI, null, null);
321afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    mRestoreApnUiHandler
322afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                        .sendEmptyMessage(EVENT_RESTORE_DEFAULTAPN_COMPLETE);
323afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    break;
324afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
325afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
326afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
327afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
328afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
329afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    protected Dialog onCreateDialog(int id) {
330afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (id == DIALOG_RESTORE_DEFAULTAPN) {
331afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            ProgressDialog dialog = new ProgressDialog(this);
332afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            dialog.setMessage(getResources().getString(R.string.restore_default_apn));
333afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            dialog.setCancelable(false);
334afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return dialog;
335afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
336afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return null;
337afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
338afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
339afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
340afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    protected void onPrepareDialog(int id, Dialog dialog) {
341afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (id == DIALOG_RESTORE_DEFAULTAPN) {
342afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            getPreferenceScreen().setEnabled(false);
343afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
344afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
345afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project}
346