summaryrefslogtreecommitdiffstats
path: root/phone/src/com/android/phone2/CdmaVoicePrivacyCheckBoxPreference.java
blob: 9714970dfdec41d42c5a009e7e8e823cb2c00620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.android.phone2;

import com.android.internal.telephony.Phone;

import android.content.Context;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.util.Log;

public class CdmaVoicePrivacyCheckBoxPreference extends CheckBoxPreference {
    private static final String LOG_TAG = "CdmaVoicePrivacyCheckBoxPreference";
    private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);

    Phone phone;
    private MyHandler mHandler = new MyHandler();

    public CdmaVoicePrivacyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        phone = PhoneApp.getPhone();
        phone.getEnhancedVoicePrivacy(mHandler.obtainMessage(MyHandler.MESSAGE_GET_VP));
    }

    public CdmaVoicePrivacyCheckBoxPreference(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
    }

    public CdmaVoicePrivacyCheckBoxPreference(Context context) {
        this(context, null);
    }


    @Override
    protected void onClick() {
        super.onClick();

        phone.enableEnhancedVoicePrivacy(isChecked(),
                mHandler.obtainMessage(MyHandler.MESSAGE_SET_VP));
    }



    private class MyHandler extends Handler {
        private static final int MESSAGE_GET_VP = 0;
        private static final int MESSAGE_SET_VP = 1;

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_GET_VP:
                    handleGetVPResponse(msg);
                    break;
                case MESSAGE_SET_VP:
                    handleSetVPResponse(msg);
                    break;
            }
        }

        private void handleGetVPResponse(Message msg) {
            AsyncResult ar = (AsyncResult) msg.obj;

            if (ar.exception != null) {
                if (DBG) Log.d(LOG_TAG, "handleGetVPResponse: ar.exception=" + ar.exception);
                setEnabled(false);
            } else {
                if (DBG) Log.d(LOG_TAG, "handleGetVPResponse: VP state successfully queried.");
                final int enable = ((int[]) ar.result)[0];
                setChecked(enable != 0);

                android.provider.Settings.Secure.putInt(getContext().getContentResolver(),
                        android.provider.Settings.Secure.ENHANCED_VOICE_PRIVACY_ENABLED, enable);
            }
        }

        private void handleSetVPResponse(Message msg) {
            AsyncResult ar = (AsyncResult) msg.obj;

            if (ar.exception != null) {
                if (DBG) Log.d(LOG_TAG, "handleSetVPResponse: ar.exception=" + ar.exception);
            }
            if (DBG) Log.d(LOG_TAG, "handleSetVPResponse: re get");

            phone.getEnhancedVoicePrivacy(obtainMessage(MESSAGE_GET_VP));
        }
    }
}