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.Activity;
     20 import android.os.AsyncResult;
     21 import android.os.Bundle;
     22 import android.os.Handler;
     23 import android.os.Message;
     24 import android.text.TextUtils;
     25 import android.text.method.DigitsKeyListener;
     26 import android.util.Log;
     27 import android.view.View;
     28 import android.widget.EditText;
     29 import android.widget.LinearLayout;
     30 import android.widget.TextView;
     31 
     32 import com.android.internal.telephony.CommandException;
     33 import com.android.internal.telephony.Phone;
     34 import com.android.internal.telephony.PhoneFactory;
     35 
     36 /**
     37  * UI to enable/disable FDN.
     38  */
     39 public class EnableFdnScreen extends Activity {
     40     private static final String LOG_TAG = PhoneApp.LOG_TAG;
     41     private static final boolean DBG = false;
     42 
     43     private static final int ENABLE_FDN_COMPLETE = 100;
     44 
     45     private LinearLayout mPinFieldContainer;
     46     private EditText mPin2Field;
     47     private TextView mStatusField;
     48     private boolean mEnable;
     49     private Phone mPhone;
     50 
     51     private Handler mHandler = new Handler() {
     52         public void handleMessage(Message msg) {
     53             switch (msg.what) {
     54                 case ENABLE_FDN_COMPLETE:
     55                     AsyncResult ar = (AsyncResult) msg.obj;
     56                     handleResult(ar);
     57                     break;
     58             }
     59 
     60             return;
     61         }
     62     };
     63 
     64     @Override
     65     protected void onCreate(Bundle icicle) {
     66         super.onCreate(icicle);
     67 
     68         setContentView(R.layout.enable_fdn_screen);
     69         setupView();
     70 
     71         mPhone = PhoneFactory.getDefaultPhone();
     72         mEnable = !mPhone.getIccCard().getIccFdnEnabled();
     73 
     74         int id = mEnable ? R.string.enable_fdn : R.string.disable_fdn;
     75         setTitle(getResources().getText(id));
     76     }
     77 
     78     @Override
     79     protected void onResume() {
     80         super.onResume();
     81         mPhone = PhoneFactory.getDefaultPhone();
     82     }
     83 
     84     private void setupView() {
     85         mPin2Field = (EditText) findViewById(R.id.pin);
     86         mPin2Field.setKeyListener(DigitsKeyListener.getInstance());
     87         mPin2Field.setMovementMethod(null);
     88         mPin2Field.setOnClickListener(mClicked);
     89 
     90         mPinFieldContainer = (LinearLayout) findViewById(R.id.pinc);
     91         mStatusField = (TextView) findViewById(R.id.status);
     92     }
     93 
     94     private void showStatus(CharSequence statusMsg) {
     95         if (statusMsg != null) {
     96             mStatusField.setText(statusMsg);
     97             mStatusField.setVisibility(View.VISIBLE);
     98             mPinFieldContainer.setVisibility(View.GONE);
     99         } else {
    100             mPinFieldContainer.setVisibility(View.VISIBLE);
    101             mStatusField.setVisibility(View.GONE);
    102         }
    103     }
    104 
    105     private String getPin2() {
    106         return mPin2Field.getText().toString();
    107     }
    108 
    109     private void enableFdn() {
    110         Message callback = Message.obtain(mHandler, ENABLE_FDN_COMPLETE);
    111         mPhone.getIccCard().setIccFdnEnabled(mEnable, getPin2(), callback);
    112         if (DBG) log("enableFdn: please wait...");
    113     }
    114 
    115     private void handleResult(AsyncResult ar) {
    116         if (ar.exception == null) {
    117             if (DBG) log("handleResult: success!");
    118             showStatus(getResources().getText(mEnable ?
    119                             R.string.enable_fdn_ok : R.string.disable_fdn_ok));
    120         } else if (ar.exception instanceof CommandException
    121                 /* && ((CommandException)ar.exception).getCommandError() ==
    122                     CommandException.Error.GENERIC_FAILURE */ ) {
    123             if (DBG) log("handleResult: failed!");
    124             showStatus(getResources().getText(
    125                     R.string.pin_failed));
    126         }
    127 
    128         mHandler.postDelayed(new Runnable() {
    129             public void run() {
    130                 finish();
    131             }
    132         }, 3000);
    133     }
    134 
    135     private View.OnClickListener mClicked = new View.OnClickListener() {
    136         public void onClick(View v) {
    137             if (TextUtils.isEmpty(mPin2Field.getText())) {
    138                 return;
    139             }
    140 
    141             showStatus(getResources().getText(
    142                     R.string.enable_in_progress));
    143 
    144             enableFdn();
    145         }
    146     };
    147 
    148     private void log(String msg) {
    149         Log.d(LOG_TAG, "[EnableSimPin] " + msg);
    150     }
    151 }
    152