blob: 092fa64afbc52f5cc26f6cc5e14452d406ba1407 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
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
17package com.android.phone;
18
19import android.app.Activity;
Steve Statia7bee4652024-02-27 17:48:05 +000020import android.content.Context;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.os.AsyncResult;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.Message;
Steve Statia7bee4652024-02-27 17:48:05 +000025import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.text.TextUtils;
27import android.text.method.DigitsKeyListener;
28import android.util.Log;
29import android.view.View;
30import android.widget.EditText;
31import android.widget.LinearLayout;
32import android.widget.TextView;
33
34import com.android.internal.telephony.CommandException;
35import com.android.internal.telephony.Phone;
36
37/**
38 * UI to enable/disable the ICC PIN.
39 */
40public 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
Steve Statia7bee4652024-02-27 17:48:05 +000046 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047 private LinearLayout mPinFieldContainer;
48 private EditText mPinField;
49 private TextView mStatusField;
50 private boolean mEnable;
51 private Phone mPhone;
Steve Statia7bee4652024-02-27 17:48:05 +000052 private boolean mDisallowedConfig = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053
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
68 protected void onCreate(Bundle icicle) {
69 super.onCreate(icicle);
70
Steve Statia7bee4652024-02-27 17:48:05 +000071 mUserManager = this.getSystemService(UserManager.class);
72 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
73 mDisallowedConfig = true;
74 }
75
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 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
86 private void setupView() {
87 mPinField = (EditText) findViewById(R.id.pin);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 mPinFieldContainer = (LinearLayout) findViewById(R.id.pinc);
89 mStatusField = (TextView) findViewById(R.id.status);
Steve Statia7bee4652024-02-27 17:48:05 +000090
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 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 }
103
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
115 private String getPin() {
116 return mPinField.getText().toString();
117 }
118
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
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
159 private void log(String msg) {
160 Log.d(LOG_TAG, "[EnableIccPin] " + msg);
161 }
162}