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