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
|
package com.android.settings;
import android.content.Context;
import android.os.SystemProperties;
import android.preference.EditTextPreference;
import android.provider.Settings;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.util.Log;
public class HostnamePreference extends EditTextPreference {
private static final String TAG = "HostnamePreference";
private static final String PROP_HOSTNAME = "net.hostname";
private final String DEFAULT_HOSTNAME;
InputFilter mHostnameInputFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (source.length() == 0)
return null;
// remove any character that is not alphanumeric, period, or hyphen
return source.subSequence(start, end).toString().replaceAll("[^-.a-zA-Z0-9]", "");
}
};
public HostnamePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// determine the default hostname
String id = Settings.Secure.getString(getContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
if (id != null && id.length() > 0) {
DEFAULT_HOSTNAME = "android-".concat(id);
} else {
DEFAULT_HOSTNAME = "";
}
setSummary(getText());
getEditText().setFilters(new InputFilter[] { mHostnameInputFilter });
getEditText().setHint(DEFAULT_HOSTNAME);
}
public HostnamePreference(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextPreferenceStyle);
}
public HostnamePreference(Context context) {
this(context, null);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String hostname = getEditText().getText().toString();
// remove any preceding or succeeding periods or hyphens
hostname = hostname.replaceAll("(?:\\.|-)+$", "");
hostname = hostname.replaceAll("^(?:\\.|-)+", "");
if (hostname.length() == 0) {
if (DEFAULT_HOSTNAME.length() != 0) {
// if no hostname is given, use the default
hostname = DEFAULT_HOSTNAME;
} else {
// if no other name can be determined
// fall back on the current hostname
hostname = getText();
}
}
setText(hostname);
}
}
@Override
public void setText(String text) {
if (text == null) {
Log.e(TAG, "tried to set null hostname, request ignored");
return;
} else if (text.length() == 0) {
Log.w(TAG, "setting empty hostname");
} else {
Log.i(TAG, "hostname has been set: " + text);
}
SystemProperties.set(PROP_HOSTNAME, text);
persistHostname(text);
setSummary(text);
}
@Override
public String getText() {
return SystemProperties.get(PROP_HOSTNAME);
}
@Override
public void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String hostname = getText();
persistHostname(hostname);
}
public void persistHostname(String hostname) {
Settings.Secure.putString(getContext().getContentResolver(),
Settings.Secure.DEVICE_HOSTNAME, hostname);
}
}
|