Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2006 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.phone;
     18 
     19 import android.app.ProgressDialog;
     20 import android.content.Context;
     21 import android.os.AsyncResult;
     22 import android.os.Handler;
     23 import android.os.Message;
     24 import android.preference.Preference;
     25 import android.preference.PreferenceCategory;
     26 import android.preference.TwoStatePreference;
     27 import android.telephony.ServiceState;
     28 import android.telephony.SubscriptionManager;
     29 import android.telephony.TelephonyManager;
     30 import android.util.AttributeSet;
     31 import android.util.Log;
     32 
     33 import com.android.internal.telephony.CommandException;
     34 import com.android.internal.telephony.Phone;
     35 import com.android.internal.telephony.PhoneFactory;
     36 
     37 /**
     38  * "Networks" settings UI for the Phone app.
     39  */
     40 public class NetworkOperators extends PreferenceCategory
     41         implements Preference.OnPreferenceChangeListener {
     42 
     43     private static final String LOG_TAG = "NetworkOperators";
     44     private static final boolean DBG = true;
     45 
     46     private static final int EVENT_AUTO_SELECT_DONE = 100;
     47     private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 200;
     48 
     49     //String keys for preference lookup
     50     public static final String BUTTON_NETWORK_SELECT_KEY = "button_network_select_key";
     51     public static final String BUTTON_AUTO_SELECT_KEY = "button_auto_select_key";
     52     public static final String CATEGORY_NETWORK_OPERATORS_KEY = "network_operators_category_key";
     53 
     54     int mPhoneId = SubscriptionManager.INVALID_PHONE_INDEX;
     55 
     56     //preference objects
     57     private NetworkSelectListPreference mNetworkSelect;
     58     private TwoStatePreference mAutoSelect;
     59 
     60     private int mSubId;
     61     private ProgressDialog mProgressDialog;
     62 
     63     public NetworkOperators(Context context, AttributeSet attrs) {
     64         super(context, attrs);
     65     }
     66 
     67     public NetworkOperators(Context context) {
     68         super(context);
     69     }
     70 
     71     /**
     72      * Initialize NetworkOperators instance.
     73      */
     74     public void initialize() {
     75         mNetworkSelect =
     76                 (NetworkSelectListPreference) findPreference(BUTTON_NETWORK_SELECT_KEY);
     77         mAutoSelect =
     78                 (TwoStatePreference) findPreference(BUTTON_AUTO_SELECT_KEY);
     79         mProgressDialog = new ProgressDialog(getContext());
     80     }
     81 
     82     /**
     83      * Update NetworkOperators instance if like subId or queryService are updated.
     84      *
     85      * @param subId Corresponding subscription ID of this network.
     86      * @param queryService The service to do network queries.
     87      */
     88     protected void update(final int subId, INetworkQueryService queryService) {
     89         mSubId = subId;
     90         mPhoneId = SubscriptionManager.getPhoneId(mSubId);
     91 
     92         if (mAutoSelect != null) {
     93             mAutoSelect.setOnPreferenceChangeListener(this);
     94         }
     95 
     96         if (mNetworkSelect != null) {
     97             mNetworkSelect.initialize(mSubId, queryService, this, mProgressDialog);
     98         }
     99 
    100         getNetworkSelectionMode();
    101     }
    102 
    103     /**
    104      * Implemented to support onPreferenceChangeListener to look for preference
    105      * changes specifically on auto select button.
    106      *
    107      * @param preference is the preference to be changed, should be auto select button.
    108      * @param newValue should be the value of whether autoSelect is checked.
    109      */
    110     @Override
    111     public boolean onPreferenceChange(Preference preference, Object newValue) {
    112         if (preference == mAutoSelect) {
    113             boolean autoSelect = (Boolean) newValue;
    114             selectNetworkAutomatic(autoSelect);
    115             return true;
    116         }
    117         return false;
    118     }
    119 
    120     private final Handler mHandler = new Handler() {
    121         @Override
    122         public void handleMessage(Message msg) {
    123             AsyncResult ar;
    124             switch (msg.what) {
    125                 case EVENT_AUTO_SELECT_DONE:
    126                     mAutoSelect.setEnabled(true);
    127                     dismissProgressBar();
    128 
    129                     ar = (AsyncResult) msg.obj;
    130                     if (ar.exception != null) {
    131                         if (DBG) logd("automatic network selection: failed!");
    132                         displayNetworkSelectionFailed(ar.exception);
    133                     } else {
    134                         if (DBG) logd("automatic network selection: succeeded!");
    135                         displayNetworkSelectionSucceeded();
    136                     }
    137 
    138                     break;
    139                 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
    140                     ar = (AsyncResult) msg.obj;
    141                     if (ar.exception != null) {
    142                         if (DBG) logd("get network selection mode: failed!");
    143                     } else if (ar.result != null) {
    144                         try {
    145                             int[] modes = (int[]) ar.result;
    146                             boolean autoSelect = (modes[0] == 0);
    147                             if (DBG) {
    148                                 logd("get network selection mode: "
    149                                         + (autoSelect ? "auto" : "manual") + " selection");
    150                             }
    151                             if (mAutoSelect != null) {
    152                                 mAutoSelect.setChecked(autoSelect);
    153                             }
    154                             if (mNetworkSelect != null) {
    155                                 mNetworkSelect.setEnabled(!autoSelect);
    156                             }
    157                         } catch (Exception e) {
    158                             if (DBG) loge("get network selection mode: unable to parse result.");
    159                         }
    160                     }
    161             }
    162 
    163             return;
    164         }
    165     };
    166 
    167     // Used by both mAutoSelect and mNetworkSelect buttons.
    168     protected void displayNetworkSelectionFailed(Throwable ex) {
    169         String status;
    170 
    171         if ((ex != null && ex instanceof CommandException)
    172                 && ((CommandException) ex).getCommandError()
    173                         == CommandException.Error.ILLEGAL_SIM_OR_ME) {
    174             status = getContext().getResources().getString(R.string.not_allowed);
    175         } else {
    176             status = getContext().getResources().getString(R.string.connect_later);
    177         }
    178 
    179         final PhoneGlobals app = PhoneGlobals.getInstance();
    180         app.notificationMgr.postTransientNotification(
    181                 NotificationMgr.NETWORK_SELECTION_NOTIFICATION, status);
    182 
    183         TelephonyManager tm = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
    184         Phone phone = PhoneFactory.getPhone(mPhoneId);
    185         if (phone != null) {
    186             ServiceState ss = tm.getServiceStateForSubscriber(phone.getSubId());
    187             if (ss != null) {
    188                 app.notificationMgr.updateNetworkSelection(ss.getState(), phone.getSubId());
    189             }
    190         }
    191     }
    192 
    193     // Used by both mAutoSelect and mNetworkSelect buttons.
    194     protected void displayNetworkSelectionSucceeded() {
    195         String status = getContext().getResources().getString(R.string.registration_done);
    196 
    197         final PhoneGlobals app = PhoneGlobals.getInstance();
    198         app.notificationMgr.postTransientNotification(
    199                 NotificationMgr.NETWORK_SELECTION_NOTIFICATION, status);
    200     }
    201 
    202     private void selectNetworkAutomatic(boolean autoSelect) {
    203         if (mNetworkSelect != null) {
    204             mNetworkSelect.setEnabled(!autoSelect);
    205         }
    206         if (autoSelect) {
    207             if (DBG) logd("select network automatically...");
    208             showAutoSelectProgressBar();
    209             mAutoSelect.setEnabled(false);
    210             Message msg = mHandler.obtainMessage(EVENT_AUTO_SELECT_DONE);
    211             Phone phone = PhoneFactory.getPhone(mPhoneId);
    212             if (phone != null) {
    213                 phone.setNetworkSelectionModeAutomatic(msg);
    214             }
    215         } else if (mNetworkSelect != null) {
    216             mNetworkSelect.onClick();
    217         }
    218     }
    219 
    220     protected void getNetworkSelectionMode() {
    221         if (DBG) logd("getting network selection mode...");
    222         Message msg = mHandler.obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE);
    223         Phone phone = PhoneFactory.getPhone(mPhoneId);
    224         if (phone != null) {
    225             phone.getNetworkSelectionMode(msg);
    226         }
    227     }
    228 
    229     private void dismissProgressBar() {
    230         if (mProgressDialog != null && mProgressDialog.isShowing()) {
    231             mProgressDialog.dismiss();
    232         }
    233     }
    234 
    235     private void showAutoSelectProgressBar() {
    236         mProgressDialog.setMessage(
    237                 getContext().getResources().getString(R.string.register_automatically));
    238         mProgressDialog.setCanceledOnTouchOutside(false);
    239         mProgressDialog.setCancelable(false);
    240         mProgressDialog.setIndeterminate(true);
    241         mProgressDialog.show();
    242     }
    243 
    244     protected boolean preferenceTreeClick(Preference preference) {
    245         return (preference == mAutoSelect || preference == mNetworkSelect);
    246     }
    247 
    248     private void logd(String msg) {
    249         Log.d(LOG_TAG, "[NetworksList] " + msg);
    250     }
    251 
    252     private void loge(String msg) {
    253         Log.e(LOG_TAG, "[NetworksList] " + msg);
    254     }
    255 }
    256