2 * Copyright (C) 2013 The CyanogenMod project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.content.Context;
20 import android.os.SystemProperties;
21 import android.preference.EditTextPreference;
22 import android.provider.Settings;
23 import android.text.InputFilter;
24 import android.text.Spanned;
25 import android.util.AttributeSet;
26 import android.util.Log;
28 import cyanogenmod.providers.CMSettings;
30 public class HostnamePreference extends EditTextPreference {
32 private static final String TAG = "HostnamePreference";
34 private static final String PROP_HOSTNAME = "net.hostname";
36 private final String DEFAULT_HOSTNAME;
38 InputFilter mHostnameInputFilter = new InputFilter() {
40 public CharSequence filter(CharSequence source, int start, int end,
41 Spanned dest, int dstart, int dend) {
43 if (source.length() == 0)
46 // remove any character that is not alphanumeric, period, or hyphen
47 return source.subSequence(start, end).toString().replaceAll("[^-.a-zA-Z0-9]", "");
51 public HostnamePreference(Context context, AttributeSet attrs, int defStyle) {
52 super(context, attrs, defStyle);
54 // determine the default hostname
55 String id = Settings.Secure.getString(getContext().getContentResolver(),
56 Settings.Secure.ANDROID_ID);
57 if (id != null && id.length() > 0) {
58 DEFAULT_HOSTNAME = "android-".concat(id);
60 DEFAULT_HOSTNAME = "";
63 setSummary(getText());
64 getEditText().setFilters(new InputFilter[] { mHostnameInputFilter });
65 getEditText().setHint(DEFAULT_HOSTNAME);
68 public HostnamePreference(Context context, AttributeSet attrs) {
69 this(context, attrs, com.android.internal.R.attr.editTextPreferenceStyle);
72 public HostnamePreference(Context context) {
77 protected void onDialogClosed(boolean positiveResult) {
79 String hostname = getEditText().getText().toString();
81 // remove any preceding or succeeding periods or hyphens
82 hostname = hostname.replaceAll("(?:\\.|-)+$", "");
83 hostname = hostname.replaceAll("^(?:\\.|-)+", "");
85 if (hostname.length() == 0) {
86 if (DEFAULT_HOSTNAME.length() != 0) {
87 // if no hostname is given, use the default
88 hostname = DEFAULT_HOSTNAME;
90 // if no other name can be determined
91 // fall back on the current hostname
100 public void setText(String text) {
102 Log.e(TAG, "tried to set null hostname, request ignored");
104 } else if (text.length() == 0) {
105 Log.w(TAG, "setting empty hostname");
107 Log.i(TAG, "hostname has been set: " + text);
109 SystemProperties.set(PROP_HOSTNAME, text);
110 persistHostname(text);
115 public String getText() {
116 return SystemProperties.get(PROP_HOSTNAME);
120 public void onSetInitialValue(boolean restoreValue, Object defaultValue) {
121 String hostname = getText();
122 persistHostname(hostname);
125 public void persistHostname(String hostname) {
126 CMSettings.Secure.putString(getContext().getContentResolver(),
127 CMSettings.Secure.DEVICE_HOSTNAME, hostname);