1/*
2 * Copyright (C) 2009 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 com.android.internal.telephony.Phone;
20
21import android.content.Context;
22import android.os.AsyncResult;
23import android.os.Handler;
24import android.os.Message;
25import android.preference.CheckBoxPreference;
26import android.util.AttributeSet;
27import android.util.Log;
28
29public class CdmaVoicePrivacyCheckBoxPreference extends CheckBoxPreference {
30    private static final String LOG_TAG = "CdmaVoicePrivacyCheckBoxPreference";
31    private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
32
33    Phone phone;
34    private MyHandler mHandler = new MyHandler();
35
36    public CdmaVoicePrivacyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
37        super(context, attrs, defStyle);
38
39        phone = PhoneGlobals.getPhone();
40        phone.getEnhancedVoicePrivacy(mHandler.obtainMessage(MyHandler.MESSAGE_GET_VP));
41    }
42
43    public CdmaVoicePrivacyCheckBoxPreference(Context context, AttributeSet attrs) {
44        this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
45    }
46
47    public CdmaVoicePrivacyCheckBoxPreference(Context context) {
48        this(context, null);
49    }
50
51
52    @Override
53    protected void onClick() {
54        super.onClick();
55
56        phone.enableEnhancedVoicePrivacy(isChecked(),
57                mHandler.obtainMessage(MyHandler.MESSAGE_SET_VP));
58    }
59
60
61
62    private class MyHandler extends Handler {
63        static final int MESSAGE_GET_VP = 0;
64        static final int MESSAGE_SET_VP = 1;
65
66        @Override
67        public void handleMessage(Message msg) {
68            switch (msg.what) {
69                case MESSAGE_GET_VP:
70                    handleGetVPResponse(msg);
71                    break;
72                case MESSAGE_SET_VP:
73                    handleSetVPResponse(msg);
74                    break;
75            }
76        }
77
78        private void handleGetVPResponse(Message msg) {
79            AsyncResult ar = (AsyncResult) msg.obj;
80
81            if (ar.exception != null) {
82                if (DBG) Log.d(LOG_TAG, "handleGetVPResponse: ar.exception=" + ar.exception);
83                setEnabled(false);
84            } else {
85                if (DBG) Log.d(LOG_TAG, "handleGetVPResponse: VP state successfully queried.");
86                final int enable = ((int[]) ar.result)[0];
87                setChecked(enable != 0);
88
89                android.provider.Settings.Secure.putInt(getContext().getContentResolver(),
90                        android.provider.Settings.Secure.ENHANCED_VOICE_PRIVACY_ENABLED, enable);
91            }
92        }
93
94        private void handleSetVPResponse(Message msg) {
95            AsyncResult ar = (AsyncResult) msg.obj;
96
97            if (ar.exception != null) {
98                if (DBG) Log.d(LOG_TAG, "handleSetVPResponse: ar.exception=" + ar.exception);
99            }
100            if (DBG) Log.d(LOG_TAG, "handleSetVPResponse: re get");
101
102            phone.getEnhancedVoicePrivacy(obtainMessage(MESSAGE_GET_VP));
103        }
104    }
105}
106