forked from darchstar/android_packages_apps_Phone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMSISDNEditPreference.java
130 lines (103 loc) · 4.26 KB
/
MSISDNEditPreference.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.android.phone;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Message;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.util.Log;
public class MSISDNEditPreference extends EditTextPreference {
private static final String LOG_TAG = "MSISDNListPreference";
public static final String PHONE_NUMBER = "phone_number";
private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
private MyHandler mHandler = new MyHandler();
private Phone mPhone;
private Context mContext;
private TimeConsumingPreferenceListener tcpListener;
public MSISDNEditPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mPhone = PhoneFactory.getDefaultPhone();
mContext = context;
}
public MSISDNEditPreference(Context context) {
this(context, null);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
String alphaTag = mPhone.getLine1AlphaTag();
if (alphaTag == null || "".equals(alphaTag)) {
// No tag, set it.
alphaTag = "Voice Line 1";
}
mPhone.setLine1Number(alphaTag, getText(),
mHandler.obtainMessage(MyHandler.MESSAGE_SET_MSISDN));
if (tcpListener != null) {
tcpListener.onStarted(this, false);
}
// Save the number into the system property
SharedPreferences prefs = mContext.getSharedPreferences(MSISDNEditPreference.class.getPackage().getName() + "_preferences", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
String phoneNum = getText().trim();
String savedNum = prefs.getString(PHONE_NUMBER, null);
// If there is no string, treat it as null
if (phoneNum.length() == 0) {
phoneNum = null;
}
if (phoneNum == null && savedNum == null) {
Log.d(LOG_TAG, "No phone number set yet");
} else {
if (phoneNum != null && phoneNum.equals(savedNum) == false) {
/* Save phone number only if there is some number set and
it is not equal to the already saved one */
if (DBG)
Log.d(LOG_TAG, "Saving phone number: " + phoneNum);
editor.putString(PHONE_NUMBER, phoneNum);
editor.commit();
} else if (phoneNum == null && savedNum != null) {
/* Remove saved number only if there is some saved and
there is no number set */
if (DBG)
Log.d(LOG_TAG, "Removing phone number");
editor.remove(PHONE_NUMBER);
editor.commit();
} else if (DBG) {
Log.d(LOG_TAG, "No change");
}
}
}
}
void init(TimeConsumingPreferenceListener listener, boolean skipReading) {
tcpListener = listener;
if (!skipReading) {
setText(mPhone.getLine1Number());
}
}
private class MyHandler extends Handler {
private static final int MESSAGE_SET_MSISDN = 0;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_SET_MSISDN:
handleSetMSISDNResponse(msg);
break;
}
}
private void handleSetMSISDNResponse(Message msg) {
AsyncResult ar = (AsyncResult) msg.obj;
if (ar.exception != null) {
if (DBG)
Log.d(LOG_TAG, "handleSetMSISDNResponse: ar.exception=" + ar.exception);
// setEnabled(false);
}
if (DBG)
Log.d(LOG_TAG, "handleSetMSISDNResponse: re get");
tcpListener.onFinished(MSISDNEditPreference.this, false);
}
}
}