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