diff options
author | LuK1337 <priv.luk@gmail.com> | 2018-08-02 17:29:18 +0200 |
---|---|---|
committer | LuK1337 <priv.luk@gmail.com> | 2018-08-02 17:30:35 +0200 |
commit | 59ec383e3a6a6f4a3a4f47029559cfc1be40518c (patch) | |
tree | 208c07382a34d6b2968eb819486194e6423f9723 /sdk/src/java/org | |
parent | c15ceb2352db32dbb5cc0fa96e07edafc20a8531 (diff) | |
download | lineage-sdk-59ec383e3a6a6f4a3a4f47029559cfc1be40518c.tar.gz lineage-sdk-59ec383e3a6a6f4a3a4f47029559cfc1be40518c.tar.bz2 lineage-sdk-59ec383e3a6a6f4a3a4f47029559cfc1be40518c.zip |
Import preference class for custom hostname feature
Change-Id: I85383db3583eba75f675ad5e4a4be1379c012ef6
Diffstat (limited to 'sdk/src/java/org')
-rw-r--r-- | sdk/src/java/org/lineageos/internal/preference/deviceinfo/HostnamePreference.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/sdk/src/java/org/lineageos/internal/preference/deviceinfo/HostnamePreference.java b/sdk/src/java/org/lineageos/internal/preference/deviceinfo/HostnamePreference.java new file mode 100644 index 00000000..56600b2b --- /dev/null +++ b/sdk/src/java/org/lineageos/internal/preference/deviceinfo/HostnamePreference.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2013 The CyanogenMod project + * Copyright (C) 2018 The LineageOS 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 org.lineageos.internal.preference; + +import android.content.Context; +import android.os.SystemProperties; +import android.support.v7.preference.EditTextPreference; +import android.text.TextUtils; +import android.util.AttributeSet; +import android.util.Log; + +import lineageos.providers.LineageSettings; + +public class HostnamePreference extends EditTextPreference { + + private static final String TAG = "HostnamePreference"; + + private static final String PROP_HOSTNAME = "net.hostname"; + + public HostnamePreference(Context context, AttributeSet attrs) { + super(context, attrs); + setSummary(getText()); + } + + @Override + public void setText(String text) { + if (text == null) { + Log.e(TAG, "tried to set null hostname, request ignored"); + return; + } + // Remove any character that is not alphanumeric, period, or hyphen + text = text.replaceAll("[^-.a-zA-Z0-9]", ""); + if (TextUtils.isEmpty(text)) { + 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) { + persistHostname(getText()); + } + + public void persistHostname(String hostname) { + LineageSettings.Secure.putString(getContext().getContentResolver(), + LineageSettings.Secure.DEVICE_HOSTNAME, hostname); + } + +} |