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 FDN.
37 */
38public class EnableFdnScreen extends Activity {
39    private static final String LOG_TAG = PhoneGlobals.LOG_TAG;
40    private static final boolean DBG = false;
41
42    private static final int ENABLE_FDN_COMPLETE = 100;
43
44    private LinearLayout mPinFieldContainer;
45    private EditText mPin2Field;
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_FDN_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_fdn_screen);
68        setupView();
69
70        mPhone = PhoneGlobals.getPhone();
71        mEnable = !mPhone.getIccCard().getIccFdnEnabled();
72
73        int id = mEnable ? R.string.enable_fdn : R.string.disable_fdn;
74        setTitle(getResources().getText(id));
75    }
76
77    @Override
78    protected void onResume() {
79        super.onResume();
80        mPhone = PhoneGlobals.getPhone();
81    }
82
83    private void setupView() {
84        mPin2Field = (EditText) findViewById(R.id.pin);
85        mPin2Field.setKeyListener(DigitsKeyListener.getInstance());
86        mPin2Field.setMovementMethod(null);
87        mPin2Field.setOnClickListener(mClicked);
88
89        mPinFieldContainer = (LinearLayout) findViewById(R.id.pinc);
90        mStatusField = (TextView) findViewById(R.id.status);
91    }
92
93    private void showStatus(CharSequence statusMsg) {
94        if (statusMsg != null) {
95            mStatusField.setText(statusMsg);
96            mStatusField.setVisibility(View.VISIBLE);
97            mPinFieldContainer.setVisibility(View.GONE);
98        } else {
99            mPinFieldContainer.setVisibility(View.VISIBLE);
100            mStatusField.setVisibility(View.GONE);
101        }
102    }
103
104    private String getPin2() {
105        return mPin2Field.getText().toString();
106    }
107
108    private void enableFdn() {
109        Message callback = Message.obtain(mHandler, ENABLE_FDN_COMPLETE);
110        mPhone.getIccCard().setIccFdnEnabled(mEnable, getPin2(), callback);
111        if (DBG) log("enableFdn: please wait...");
112    }
113
114    private void handleResult(AsyncResult ar) {
115        if (ar.exception == null) {
116            if (DBG) log("handleResult: success!");
117            showStatus(getResources().getText(mEnable ?
118                            R.string.enable_fdn_ok : R.string.disable_fdn_ok));
119        } else if (ar.exception instanceof CommandException
120                /* && ((CommandException)ar.exception).getCommandError() ==
121                    CommandException.Error.GENERIC_FAILURE */ ) {
122            if (DBG) log("handleResult: failed!");
123            showStatus(getResources().getText(
124                    R.string.pin_failed));
125        }
126
127        mHandler.postDelayed(new Runnable() {
128            public void run() {
129                finish();
130            }
131        }, 3000);
132    }
133
134    private View.OnClickListener mClicked = new View.OnClickListener() {
135        public void onClick(View v) {
136            if (TextUtils.isEmpty(mPin2Field.getText())) {
137                return;
138            }
139
140            showStatus(getResources().getText(
141                    R.string.enable_in_progress));
142
143            enableFdn();
144        }
145    };
146
147    private void log(String msg) {
148        Log.d(LOG_TAG, "[EnableSimPin] " + msg);
149    }
150}
151