summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/HostnamePreference.java
diff options
context:
space:
mode:
authorAlex Avance <aravance@gmail.com>2011-01-13 17:46:46 -0600
committerBrinly Taylor <uberlaggydarwin@gmail.com>2015-10-27 12:57:05 +1030
commit3c2b9a6f3a7e438e8905428e84d8352b77e2c4f0 (patch)
treea7f7edb99f9774ce4877840b15e5196e31f0e750 /src/com/android/settings/HostnamePreference.java
parentf8da1c898785decdd9d1d434a8b7aadf56527b93 (diff)
downloadpackages_apps_Settings-3c2b9a6f3a7e438e8905428e84d8352b77e2c4f0.tar.gz
packages_apps_Settings-3c2b9a6f3a7e438e8905428e84d8352b77e2c4f0.tar.bz2
packages_apps_Settings-3c2b9a6f3a7e438e8905428e84d8352b77e2c4f0.zip
Add an option to change the device hostname (2/2).
This adds an option to modify the device hostname used in ip resolution. This is useful when connecting to the android device in a dynamic dhcp environment. Change-Id: Ibc145b74036617248d4f33c6866cc9c8a8cc8974
Diffstat (limited to 'src/com/android/settings/HostnamePreference.java')
-rw-r--r--src/com/android/settings/HostnamePreference.java127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/com/android/settings/HostnamePreference.java b/src/com/android/settings/HostnamePreference.java
new file mode 100644
index 000000000..3f2bb2ed5
--- /dev/null
+++ b/src/com/android/settings/HostnamePreference.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2013 The CyanogenMod project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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);
+ }
+}