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 
17 package com.android.phone;
18 
19 import android.content.Context;
20 import android.os.AsyncResult;
21 import android.os.Handler;
22 import android.os.Message;
23 import android.preference.SwitchPreference;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 
27 import com.android.internal.telephony.Phone;
28 
29 public class CdmaVoicePrivacySwitchPreference extends SwitchPreference {
30     private static final String LOG_TAG = "CdmaVoicePrivacySwitchPreference";
31     private final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
32 
33     Phone phone;
34     private MyHandler mHandler = new MyHandler();
35 
CdmaVoicePrivacySwitchPreference(Context context, AttributeSet attrs, int defStyle)36     public CdmaVoicePrivacySwitchPreference(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 
CdmaVoicePrivacySwitchPreference(Context context, AttributeSet attrs)43     public CdmaVoicePrivacySwitchPreference(Context context, AttributeSet attrs) {
44         this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
45     }
46 
CdmaVoicePrivacySwitchPreference(Context context)47     public CdmaVoicePrivacySwitchPreference(Context context) {
48         this(context, null);
49     }
50 
51 
52     @Override
onClick()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
handleMessage(Message msg)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 
handleGetVPResponse(Message msg)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 
handleSetVPResponse(Message msg)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 
setPhone(Phone phone)106     public void setPhone(Phone phone) {
107         this.phone = phone;
108     }
109 }
110