• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 the ICC PIN.
38  */
39 public class EnableIccPinScreen extends Activity {
40     private static final String LOG_TAG = PhoneApp.LOG_TAG;
41 
42     private static final int ENABLE_ICC_PIN_COMPLETE = 100;
43     private static final boolean DBG = false;
44 
45     private LinearLayout mPinFieldContainer;
46     private EditText mPinField;
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_ICC_PIN_COMPLETE:
55                     AsyncResult ar = (AsyncResult) msg.obj;
56                     handleResult(ar);
57                     break;
58             }
59 
60             return;
61         }
62     };
63 
64     @Override
onCreate(Bundle icicle)65     protected void onCreate(Bundle icicle) {
66         super.onCreate(icicle);
67 
68         setContentView(R.layout.enable_sim_pin_screen);
69         setupView();
70 
71         mPhone = PhoneFactory.getDefaultPhone();
72         mEnable = !mPhone.getIccCard().getIccLockEnabled();
73 
74         int id = mEnable ? R.string.enable_sim_pin : R.string.disable_sim_pin;
75         setTitle(getResources().getText(id));
76     }
77 
setupView()78     private void setupView() {
79         mPinField = (EditText) findViewById(R.id.pin);
80         mPinField.setKeyListener(DigitsKeyListener.getInstance());
81         mPinField.setMovementMethod(null);
82         mPinField.setOnClickListener(mClicked);
83 
84         mPinFieldContainer = (LinearLayout) findViewById(R.id.pinc);
85         mStatusField = (TextView) findViewById(R.id.status);
86     }
87 
showStatus(CharSequence statusMsg)88     private void showStatus(CharSequence statusMsg) {
89         if (statusMsg != null) {
90             mStatusField.setText(statusMsg);
91             mStatusField.setVisibility(View.VISIBLE);
92             mPinFieldContainer.setVisibility(View.GONE);
93         } else {
94             mPinFieldContainer.setVisibility(View.VISIBLE);
95             mStatusField.setVisibility(View.GONE);
96         }
97     }
98 
getPin()99     private String getPin() {
100         return mPinField.getText().toString();
101     }
102 
enableIccPin()103     private void enableIccPin() {
104         Message callback = Message.obtain(mHandler, ENABLE_ICC_PIN_COMPLETE);
105         if (DBG) log("enableIccPin:");
106         mPhone.getIccCard().setIccLockEnabled(mEnable, getPin(), callback);
107         if (DBG) log("enableIccPin: please wait...");
108     }
109 
handleResult(AsyncResult ar)110     private void handleResult(AsyncResult ar) {
111         if (ar.exception == null) {
112             if (DBG) log("handleResult: success!");
113             showStatus(getResources().getText(
114                     mEnable ? R.string.enable_pin_ok : R.string.disable_pin_ok));
115         } else if (ar.exception instanceof CommandException
116                 /* && ((CommandException)ar.exception).getCommandError() ==
117                     CommandException.Error.GENERIC_FAILURE */ ) {
118             if (DBG) log("handleResult: failed!");
119             showStatus(getResources().getText(
120                     R.string.pin_failed));
121         }
122 
123         mHandler.postDelayed(new Runnable() {
124             public void run() {
125                 finish();
126             }
127         }, 3000);
128     }
129 
130     private View.OnClickListener mClicked = new View.OnClickListener() {
131         public void onClick(View v) {
132             if (TextUtils.isEmpty(mPinField.getText())) {
133                 return;
134             }
135 
136             showStatus(getResources().getText(
137                     R.string.enable_in_progress));
138 
139             enableIccPin();
140         }
141     };
142 
log(String msg)143     private void log(String msg) {
144         Log.d(LOG_TAG, "[EnableIccPin] " + msg);
145     }
146 }
147